ctf: Fix API inconsistencies
[deliverable/tracecompass.git] / org.eclipse.linuxtools.ctf.core / src / org / eclipse / linuxtools / ctf / core / event / types / SequenceDefinition.java
CommitLineData
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
13package org.eclipse.linuxtools.ctf.core.event.types;
14
486efb2e 15import org.eclipse.linuxtools.ctf.core.event.io.BitBuffer;
2feb8414 16import 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 */
28public 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
139 public void read(BitBuffer input) {
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) {
148 for (; i < definitions.length; i++) {
149 newDefinitions[i] = definitions[i];
150 }
151 }
152
153 for (; i < currentLength; i++) {
9ac2eb62
MK
154 newDefinitions[i] = declaration.getElementType()
155 .createDefinition(definitionScope,
156 fieldName + "[" + i + "]"); //$NON-NLS-1$ //$NON-NLS-2$
866e5b51
FC
157 }
158
159 definitions = newDefinitions;
160 }
161
162 for (int i = 0; i < currentLength; i++) {
163 definitions[i].read(input);
164 }
165 }
166
167 @Override
168 public String toString() {
169 StringBuilder b = new StringBuilder();
170
171 if (this.isString()) {
172 for (int i = 0; i < currentLength; i++) {
173 IntegerDefinition character = (IntegerDefinition) definitions[i];
174
175 if (character.getValue() == 0) {
176 break;
177 }
178
179 b.append(character.toString());
180 }
181 } else {
182 b.append('[');
183 if (currentLength > 0) {
184 for (int i = 0; i < (currentLength - 1); i++) {
185 b.append(' ');
186 b.append(definitions[i].toString());
187 b.append(',');
188 }
189 b.append(' ');
190 b.append(definitions[currentLength - 1].toString());
191 }
192 b.append(" ]"); //$NON-NLS-1$
193
194 }
195
196 return b.toString();
197 }
198}
This page took 0.037707 seconds and 5 git commands to generate.