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 | ||
2feb8414 | 15 | import org.eclipse.linuxtools.ctf.core.trace.CTFReaderException; |
ce2388e0 | 16 | import org.eclipse.linuxtools.internal.ctf.core.event.io.BitBuffer; |
866e5b51 FC |
17 | |
18 | /** | |
19 | * <b><u>SequenceDefinition</u></b> | |
20 | */ | |
21 | public class SequenceDefinition extends Definition { | |
22 | ||
23 | // ------------------------------------------------------------------------ | |
24 | // Attributes | |
25 | // ------------------------------------------------------------------------ | |
26 | ||
27 | private final SequenceDeclaration declaration; | |
2feb8414 | 28 | private final IntegerDefinition lengthDefinition; |
866e5b51 FC |
29 | private Definition definitions[]; |
30 | private int currentLength; | |
31 | ||
32 | // ------------------------------------------------------------------------ | |
33 | // Constructors | |
34 | // ------------------------------------------------------------------------ | |
35 | ||
9ac2eb62 MK |
36 | /** |
37 | * Constructor | |
38 | * | |
39 | * @param declaration | |
40 | * the parent declaration | |
41 | * @param definitionScope | |
42 | * the parent scope | |
43 | * @param fieldName | |
44 | * the field name | |
be6df2d8 AM |
45 | * @throws CTFReaderException |
46 | * If the sequence field was malformatted | |
9ac2eb62 | 47 | */ |
866e5b51 | 48 | public SequenceDefinition(SequenceDeclaration declaration, |
9ac2eb62 MK |
49 | IDefinitionScope definitionScope, String fieldName) |
50 | throws CTFReaderException { | |
866e5b51 | 51 | super(definitionScope, fieldName); |
2feb8414 | 52 | Definition lenDef = null; |
ce2388e0 | 53 | |
866e5b51 | 54 | this.declaration = declaration; |
866e5b51 FC |
55 | |
56 | if (definitionScope != null) { | |
9ac2eb62 MK |
57 | lenDef = definitionScope.lookupDefinition(declaration |
58 | .getLengthName()); | |
2feb8414 AM |
59 | } |
60 | ||
9ac2eb62 MK |
61 | if (lenDef == null) { |
62 | throw new CTFReaderException("Sequence length field not found"); //$NON-NLS-1$ | |
63 | } | |
ce2388e0 | 64 | |
9ac2eb62 MK |
65 | if (!(lenDef instanceof IntegerDefinition)) { |
66 | throw new CTFReaderException("Sequence length field not integer"); //$NON-NLS-1$ | |
67 | } | |
2feb8414 | 68 | |
9ac2eb62 | 69 | lengthDefinition = (IntegerDefinition) lenDef; |
2feb8414 AM |
70 | |
71 | if (this.lengthDefinition.getDeclaration().isSigned()) { | |
72 | throw new CTFReaderException("Sequence length must not be signed"); //$NON-NLS-1$ | |
866e5b51 | 73 | } |
866e5b51 FC |
74 | } |
75 | ||
76 | // ------------------------------------------------------------------------ | |
77 | // Getters/Setters/Predicates | |
78 | // ------------------------------------------------------------------------ | |
79 | ||
9ac2eb62 | 80 | @Override |
866e5b51 FC |
81 | public SequenceDeclaration getDeclaration() { |
82 | return declaration; | |
83 | } | |
84 | ||
9ac2eb62 MK |
85 | /** |
86 | * The length of the sequence in number of elements so a sequence of 5 | |
87 | * GIANT_rediculous_long_ints is the same as a sequence of 5 bits. (5) | |
88 | * | |
89 | * @return the length of the sequence | |
90 | */ | |
866e5b51 FC |
91 | public int getLength() { |
92 | return currentLength; | |
93 | } | |
94 | ||
9ac2eb62 MK |
95 | /** |
96 | * Get the element at i | |
97 | * | |
98 | * @param i | |
99 | * the index (cannot be negative) | |
100 | * @return The element at I, if I > length, null, if I < 0, the method | |
101 | * throws an out of bounds exception | |
102 | */ | |
866e5b51 FC |
103 | public Definition getElem(int i) { |
104 | if (i > definitions.length) { | |
105 | return null; | |
106 | } | |
107 | ||
108 | return definitions[i]; | |
109 | } | |
110 | ||
9ac2eb62 MK |
111 | /** |
112 | * Is the sequence a null terminated string? | |
113 | * @return true == is a string, false == is not a string | |
114 | */ | |
866e5b51 FC |
115 | public boolean isString() { |
116 | IntegerDeclaration elemInt; | |
117 | ||
118 | if (declaration.getElementType() instanceof IntegerDeclaration) { | |
119 | elemInt = (IntegerDeclaration) declaration.getElementType(); | |
120 | if (elemInt.isCharacter()) { | |
121 | return true; | |
122 | } | |
123 | } | |
124 | return false; | |
125 | } | |
126 | ||
127 | // ------------------------------------------------------------------------ | |
128 | // Operations | |
129 | // ------------------------------------------------------------------------ | |
130 | ||
131 | @Override | |
132 | public void read(BitBuffer input) { | |
133 | currentLength = (int) lengthDefinition.getValue(); | |
134 | ||
135 | if ((definitions == null) || (definitions.length < currentLength)) { | |
136 | Definition newDefinitions[] = new Definition[currentLength]; | |
137 | ||
138 | int i = 0; | |
139 | ||
140 | if (definitions != null) { | |
141 | for (; i < definitions.length; i++) { | |
142 | newDefinitions[i] = definitions[i]; | |
143 | } | |
144 | } | |
145 | ||
146 | for (; i < currentLength; i++) { | |
9ac2eb62 MK |
147 | newDefinitions[i] = declaration.getElementType() |
148 | .createDefinition(definitionScope, | |
149 | fieldName + "[" + i + "]"); //$NON-NLS-1$ //$NON-NLS-2$ | |
866e5b51 FC |
150 | } |
151 | ||
152 | definitions = newDefinitions; | |
153 | } | |
154 | ||
155 | for (int i = 0; i < currentLength; i++) { | |
156 | definitions[i].read(input); | |
157 | } | |
158 | } | |
159 | ||
160 | @Override | |
161 | public String toString() { | |
162 | StringBuilder b = new StringBuilder(); | |
163 | ||
164 | if (this.isString()) { | |
165 | for (int i = 0; i < currentLength; i++) { | |
166 | IntegerDefinition character = (IntegerDefinition) definitions[i]; | |
167 | ||
168 | if (character.getValue() == 0) { | |
169 | break; | |
170 | } | |
171 | ||
172 | b.append(character.toString()); | |
173 | } | |
174 | } else { | |
175 | b.append('['); | |
176 | if (currentLength > 0) { | |
177 | for (int i = 0; i < (currentLength - 1); i++) { | |
178 | b.append(' '); | |
179 | b.append(definitions[i].toString()); | |
180 | b.append(','); | |
181 | } | |
182 | b.append(' '); | |
183 | b.append(definitions[currentLength - 1].toString()); | |
184 | } | |
185 | b.append(" ]"); //$NON-NLS-1$ | |
186 | ||
187 | } | |
188 | ||
189 | return b.toString(); | |
190 | } | |
191 | } |