Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 29d946e83e6b912a80adc5ae373fc3757b8e08db (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
/*******************************************************************************
 * Copyright (c) 2011-2012 Ericsson, Ecole Polytechnique de Montreal and others
 *
 * All rights reserved. This program and the accompanying materials are made
 * available under the terms of the Eclipse Public License v1.0 which
 * accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors: Matthew Khouzam - Initial API and implementation
 * Contributors: Simon Marchi - Initial API and implementation
 *******************************************************************************/

package org.eclipse.linuxtools.ctf.core.event.types;

import org.eclipse.linuxtools.ctf.core.trace.CTFReaderException;
import org.eclipse.linuxtools.internal.ctf.core.event.io.BitBuffer;

/**
 * <b><u>SequenceDefinition</u></b>
 */
public class SequenceDefinition extends Definition {

    // ------------------------------------------------------------------------
    // Attributes
    // ------------------------------------------------------------------------

    private final SequenceDeclaration declaration;
    private final IntegerDefinition lengthDefinition;
    private Definition definitions[];
    private int currentLength;

    // ------------------------------------------------------------------------
    // Constructors
    // ------------------------------------------------------------------------

    public SequenceDefinition(SequenceDeclaration declaration,
            IDefinitionScope definitionScope, String fieldName) throws CTFReaderException {
        super(definitionScope, fieldName);
        Definition lenDef = null;
        
        this.declaration = declaration;

        if (definitionScope != null) {
            lenDef = definitionScope.lookupDefinition(declaration.getLengthName());
        }

         if (lenDef == null) { 
             throw new CTFReaderException("Sequence length field not found"); //$NON-NLS-1$
         }
        
         if (!(lenDef instanceof IntegerDefinition)) {
             throw new CTFReaderException("Sequence length field not integer"); //$NON-NLS-1$
         }

         lengthDefinition = (IntegerDefinition) lenDef;

        if (this.lengthDefinition.getDeclaration().isSigned()) {
            throw new CTFReaderException("Sequence length must not be signed"); //$NON-NLS-1$
        }
    }

    // ------------------------------------------------------------------------
    // Getters/Setters/Predicates
    // ------------------------------------------------------------------------

    public SequenceDeclaration getDeclaration() {
        return declaration;
    }

    public int getLength() {
        return currentLength;
    }

    public Definition getElem(int i) {
        if (i > definitions.length) {
            return null;
        }

        return definitions[i];
    }

    public boolean isString() {
        IntegerDeclaration elemInt;

        if (declaration.getElementType() instanceof IntegerDeclaration) {
            elemInt = (IntegerDeclaration) declaration.getElementType();
            if (elemInt.isCharacter()) {
                return true;
            }
        }
        return false;
    }

    // ------------------------------------------------------------------------
    // Operations
    // ------------------------------------------------------------------------

    @Override
    public void read(BitBuffer input) {
        currentLength = (int) lengthDefinition.getValue();

        if ((definitions == null) || (definitions.length < currentLength)) {
            Definition newDefinitions[] = new Definition[currentLength];

            int i = 0;

            if (definitions != null) {
                for (; i < definitions.length; i++) {
                    newDefinitions[i] = definitions[i];
                }
            }

            for (; i < currentLength; i++) {
                newDefinitions[i] = declaration.getElementType().createDefinition(
                        definitionScope, fieldName + "[" + i + "]"); //$NON-NLS-1$ //$NON-NLS-2$
            }

            definitions = newDefinitions;
        }

        for (int i = 0; i < currentLength; i++) {
            definitions[i].read(input);
        }
    }

    @Override
    public String toString() {
        StringBuilder b = new StringBuilder();

        if (this.isString()) {
            for (int i = 0; i < currentLength; i++) {
                IntegerDefinition character = (IntegerDefinition) definitions[i];

                if (character.getValue() == 0) {
                    break;
                }

                b.append(character.toString());
            }
        } else {
            b.append('[');
            if (currentLength > 0) {
                for (int i = 0; i < (currentLength - 1); i++) {
                    b.append(' ');
                    b.append(definitions[i].toString());
                    b.append(',');
                }
                b.append(' ');
                b.append(definitions[currentLength - 1].toString());
            }
            b.append(" ]"); //$NON-NLS-1$

        }

        return b.toString();
    }
}

Back to the top