Commit | Line | Data |
---|---|---|
866e5b51 FC |
1 | /******************************************************************************* |
2 | * Copyright (c) 2011-2012 Ericsson, Ecole Polytechnique de Montreal and others | |
3 | * | |
4 | * All rights reserved. This program and the accompanying materials are made | |
5 | * available under the terms of the Eclipse Public License v1.0 which | |
6 | * accompanies this distribution, and is available at | |
7 | * http://www.eclipse.org/legal/epl-v10.html | |
8 | * | |
9 | * Contributors: Matthew Khouzam - Initial API and implementation | |
10 | * Contributors: Simon Marchi - Initial API and implementation | |
11 | *******************************************************************************/ | |
12 | ||
13 | package org.eclipse.linuxtools.ctf.core.event.types; | |
14 | ||
486efb2e | 15 | import org.eclipse.linuxtools.ctf.core.event.io.BitBuffer; |
2feb8414 | 16 | import org.eclipse.linuxtools.ctf.core.trace.CTFReaderException; |
866e5b51 FC |
17 | |
18 | /** | |
d37aaa7f | 19 | * A CTF sequence definition (a fixed-size array). |
486efb2e | 20 | * |
d37aaa7f FC |
21 | * An array where the size is fixed but declared in the trace, unlike array |
22 | * where it is declared with a literal | |
23 | * | |
24 | * @version 1.0 | |
25 | * @author Matthew Khouzam | |
26 | * @author Simon Marchi | |
866e5b51 FC |
27 | */ |
28 | public class SequenceDefinition extends Definition { | |
29 | ||
30 | // ------------------------------------------------------------------------ | |
31 | // Attributes | |
32 | // ------------------------------------------------------------------------ | |
33 | ||
34 | private final SequenceDeclaration declaration; | |
2feb8414 | 35 | private final IntegerDefinition lengthDefinition; |
866e5b51 FC |
36 | private Definition definitions[]; |
37 | private int currentLength; | |
38 | ||
39 | // ------------------------------------------------------------------------ | |
40 | // Constructors | |
41 | // ------------------------------------------------------------------------ | |
42 | ||
9ac2eb62 MK |
43 | /** |
44 | * Constructor | |
45 | * | |
46 | * @param declaration | |
47 | * the parent declaration | |
48 | * @param definitionScope | |
49 | * the parent scope | |
50 | * @param fieldName | |
51 | * the field name | |
be6df2d8 AM |
52 | * @throws CTFReaderException |
53 | * If the sequence field was malformatted | |
9ac2eb62 | 54 | */ |
866e5b51 | 55 | public SequenceDefinition(SequenceDeclaration declaration, |
9ac2eb62 MK |
56 | IDefinitionScope definitionScope, String fieldName) |
57 | throws CTFReaderException { | |
866e5b51 | 58 | super(definitionScope, fieldName); |
2feb8414 | 59 | Definition lenDef = null; |
ce2388e0 | 60 | |
866e5b51 | 61 | this.declaration = declaration; |
866e5b51 FC |
62 | |
63 | if (definitionScope != null) { | |
9ac2eb62 MK |
64 | lenDef = definitionScope.lookupDefinition(declaration |
65 | .getLengthName()); | |
2feb8414 AM |
66 | } |
67 | ||
9ac2eb62 MK |
68 | if (lenDef == null) { |
69 | throw new CTFReaderException("Sequence length field not found"); //$NON-NLS-1$ | |
70 | } | |
ce2388e0 | 71 | |
9ac2eb62 MK |
72 | if (!(lenDef instanceof IntegerDefinition)) { |
73 | throw new CTFReaderException("Sequence length field not integer"); //$NON-NLS-1$ | |
74 | } | |
2feb8414 | 75 | |
9ac2eb62 | 76 | lengthDefinition = (IntegerDefinition) lenDef; |
2feb8414 AM |
77 | |
78 | if (this.lengthDefinition.getDeclaration().isSigned()) { | |
79 | throw new CTFReaderException("Sequence length must not be signed"); //$NON-NLS-1$ | |
866e5b51 | 80 | } |
866e5b51 FC |
81 | } |
82 | ||
83 | // ------------------------------------------------------------------------ | |
84 | // Getters/Setters/Predicates | |
85 | // ------------------------------------------------------------------------ | |
86 | ||
9ac2eb62 | 87 | @Override |
866e5b51 FC |
88 | public SequenceDeclaration getDeclaration() { |
89 | return declaration; | |
90 | } | |
91 | ||
9ac2eb62 MK |
92 | /** |
93 | * The length of the sequence in number of elements so a sequence of 5 | |
94 | * GIANT_rediculous_long_ints is the same as a sequence of 5 bits. (5) | |
95 | * | |
96 | * @return the length of the sequence | |
97 | */ | |
866e5b51 FC |
98 | public int getLength() { |
99 | return currentLength; | |
100 | } | |
101 | ||
9ac2eb62 MK |
102 | /** |
103 | * Get the element at i | |
104 | * | |
105 | * @param i | |
106 | * the index (cannot be negative) | |
107 | * @return The element at I, if I > length, null, if I < 0, the method | |
108 | * throws an out of bounds exception | |
109 | */ | |
866e5b51 FC |
110 | public Definition getElem(int i) { |
111 | if (i > definitions.length) { | |
112 | return null; | |
113 | } | |
114 | ||
115 | return definitions[i]; | |
116 | } | |
117 | ||
9ac2eb62 MK |
118 | /** |
119 | * Is the sequence a null terminated string? | |
120 | * @return true == is a string, false == is not a string | |
121 | */ | |
866e5b51 FC |
122 | public boolean isString() { |
123 | IntegerDeclaration elemInt; | |
124 | ||
125 | if (declaration.getElementType() instanceof IntegerDeclaration) { | |
126 | elemInt = (IntegerDeclaration) declaration.getElementType(); | |
127 | if (elemInt.isCharacter()) { | |
128 | return true; | |
129 | } | |
130 | } | |
131 | return false; | |
132 | } | |
133 | ||
134 | // ------------------------------------------------------------------------ | |
135 | // Operations | |
136 | // ------------------------------------------------------------------------ | |
137 | ||
138 | @Override | |
db8e8f7d | 139 | public void read(BitBuffer input) throws CTFReaderException { |
866e5b51 FC |
140 | currentLength = (int) lengthDefinition.getValue(); |
141 | ||
142 | if ((definitions == null) || (definitions.length < currentLength)) { | |
143 | Definition newDefinitions[] = new Definition[currentLength]; | |
144 | ||
145 | int i = 0; | |
146 | ||
147 | if (definitions != null) { | |
0594c61c | 148 | System.arraycopy(definitions, 0, newDefinitions, 0, definitions.length); |
866e5b51 FC |
149 | } |
150 | ||
151 | for (; i < currentLength; i++) { | |
9ac2eb62 | 152 | newDefinitions[i] = declaration.getElementType() |
0594c61c AM |
153 | .createDefinition(getDefinitionScope(), |
154 | getFieldName() + "[" + i + "]"); //$NON-NLS-1$ //$NON-NLS-2$ | |
866e5b51 FC |
155 | } |
156 | ||
157 | definitions = newDefinitions; | |
158 | } | |
159 | ||
160 | for (int i = 0; i < currentLength; i++) { | |
161 | definitions[i].read(input); | |
162 | } | |
163 | } | |
164 | ||
165 | @Override | |
166 | public String toString() { | |
167 | StringBuilder b = new StringBuilder(); | |
168 | ||
169 | if (this.isString()) { | |
170 | for (int i = 0; i < currentLength; i++) { | |
171 | IntegerDefinition character = (IntegerDefinition) definitions[i]; | |
172 | ||
173 | if (character.getValue() == 0) { | |
174 | break; | |
175 | } | |
176 | ||
177 | b.append(character.toString()); | |
178 | } | |
179 | } else { | |
180 | b.append('['); | |
181 | if (currentLength > 0) { | |
182 | for (int i = 0; i < (currentLength - 1); i++) { | |
183 | b.append(' '); | |
184 | b.append(definitions[i].toString()); | |
185 | b.append(','); | |
186 | } | |
187 | b.append(' '); | |
188 | b.append(definitions[currentLength - 1].toString()); | |
189 | } | |
190 | b.append(" ]"); //$NON-NLS-1$ | |
191 | ||
192 | } | |
193 | ||
194 | return b.toString(); | |
195 | } | |
196 | } |