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 | ||
36 | public SequenceDefinition(SequenceDeclaration declaration, | |
2feb8414 | 37 | IDefinitionScope definitionScope, String fieldName) throws CTFReaderException { |
866e5b51 | 38 | super(definitionScope, fieldName); |
2feb8414 | 39 | Definition lenDef = null; |
ce2388e0 | 40 | |
866e5b51 | 41 | this.declaration = declaration; |
866e5b51 FC |
42 | |
43 | if (definitionScope != null) { | |
2feb8414 AM |
44 | lenDef = definitionScope.lookupDefinition(declaration.getLengthName()); |
45 | } | |
46 | ||
ce2388e0 | 47 | if (lenDef == null) { |
2feb8414 AM |
48 | throw new CTFReaderException("Sequence length field not found"); //$NON-NLS-1$ |
49 | } | |
ce2388e0 | 50 | |
2feb8414 AM |
51 | if (!(lenDef instanceof IntegerDefinition)) { |
52 | throw new CTFReaderException("Sequence length field not integer"); //$NON-NLS-1$ | |
53 | } | |
54 | ||
55 | lengthDefinition = (IntegerDefinition) lenDef; | |
56 | ||
57 | if (this.lengthDefinition.getDeclaration().isSigned()) { | |
58 | throw new CTFReaderException("Sequence length must not be signed"); //$NON-NLS-1$ | |
866e5b51 | 59 | } |
866e5b51 FC |
60 | } |
61 | ||
62 | // ------------------------------------------------------------------------ | |
63 | // Getters/Setters/Predicates | |
64 | // ------------------------------------------------------------------------ | |
65 | ||
66 | public SequenceDeclaration getDeclaration() { | |
67 | return declaration; | |
68 | } | |
69 | ||
70 | public int getLength() { | |
71 | return currentLength; | |
72 | } | |
73 | ||
74 | public Definition getElem(int i) { | |
75 | if (i > definitions.length) { | |
76 | return null; | |
77 | } | |
78 | ||
79 | return definitions[i]; | |
80 | } | |
81 | ||
82 | public boolean isString() { | |
83 | IntegerDeclaration elemInt; | |
84 | ||
85 | if (declaration.getElementType() instanceof IntegerDeclaration) { | |
86 | elemInt = (IntegerDeclaration) declaration.getElementType(); | |
87 | if (elemInt.isCharacter()) { | |
88 | return true; | |
89 | } | |
90 | } | |
91 | return false; | |
92 | } | |
93 | ||
94 | // ------------------------------------------------------------------------ | |
95 | // Operations | |
96 | // ------------------------------------------------------------------------ | |
97 | ||
98 | @Override | |
99 | public void read(BitBuffer input) { | |
100 | currentLength = (int) lengthDefinition.getValue(); | |
101 | ||
102 | if ((definitions == null) || (definitions.length < currentLength)) { | |
103 | Definition newDefinitions[] = new Definition[currentLength]; | |
104 | ||
105 | int i = 0; | |
106 | ||
107 | if (definitions != null) { | |
108 | for (; i < definitions.length; i++) { | |
109 | newDefinitions[i] = definitions[i]; | |
110 | } | |
111 | } | |
112 | ||
113 | for (; i < currentLength; i++) { | |
114 | newDefinitions[i] = declaration.getElementType().createDefinition( | |
115 | definitionScope, fieldName + "[" + i + "]"); //$NON-NLS-1$ //$NON-NLS-2$ | |
116 | } | |
117 | ||
118 | definitions = newDefinitions; | |
119 | } | |
120 | ||
121 | for (int i = 0; i < currentLength; i++) { | |
122 | definitions[i].read(input); | |
123 | } | |
124 | } | |
125 | ||
126 | @Override | |
127 | public String toString() { | |
128 | StringBuilder b = new StringBuilder(); | |
129 | ||
130 | if (this.isString()) { | |
131 | for (int i = 0; i < currentLength; i++) { | |
132 | IntegerDefinition character = (IntegerDefinition) definitions[i]; | |
133 | ||
134 | if (character.getValue() == 0) { | |
135 | break; | |
136 | } | |
137 | ||
138 | b.append(character.toString()); | |
139 | } | |
140 | } else { | |
141 | b.append('['); | |
142 | if (currentLength > 0) { | |
143 | for (int i = 0; i < (currentLength - 1); i++) { | |
144 | b.append(' '); | |
145 | b.append(definitions[i].toString()); | |
146 | b.append(','); | |
147 | } | |
148 | b.append(' '); | |
149 | b.append(definitions[currentLength - 1].toString()); | |
150 | } | |
151 | b.append(" ]"); //$NON-NLS-1$ | |
152 | ||
153 | } | |
154 | ||
155 | return b.toString(); | |
156 | } | |
157 | } |