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