Merge branch 'master' into lttng_2_0_control_dev
[deliverable/tracecompass.git] / org.eclipse.linuxtools.ctf.core / src / org / eclipse / linuxtools / ctf / core / event / types / SequenceDefinition.java
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
15 import org.eclipse.linuxtools.ctf.core.event.io.BitBuffer;
16
17 /**
18 * <b><u>SequenceDefinition</u></b>
19 */
20 public class SequenceDefinition extends Definition {
21
22 // ------------------------------------------------------------------------
23 // Attributes
24 // ------------------------------------------------------------------------
25
26 private final SequenceDeclaration declaration;
27 private IntegerDefinition lengthDefinition;
28 private Definition definitions[];
29 private int currentLength;
30
31 // ------------------------------------------------------------------------
32 // Constructors
33 // ------------------------------------------------------------------------
34
35 public SequenceDefinition(SequenceDeclaration declaration,
36 IDefinitionScope definitionScope, String fieldName) {
37 super(definitionScope, fieldName);
38
39 this.declaration = declaration;
40 // this.definitionScope = definitionScope;
41
42 if (definitionScope != null) {
43 Definition lenDef = definitionScope.lookupDefinition(declaration.getLengthName());
44 lengthDefinition = (IntegerDefinition) lenDef;
45 }
46 /*
47 * if (lenDef == null) { throw new
48 * Exception("Sequence length field not found"); }
49 *
50 * if (!(lenDef instanceof IntegerDefinition)) { throw new
51 * Exception("Sequence length field not integer"); }
52 */
53 /*
54 * if (this.lengthDefinition.declaration.signed) { throw new
55 * Exception("Sequence length must not be signed"); }
56 */
57 }
58
59 // ------------------------------------------------------------------------
60 // Getters/Setters/Predicates
61 // ------------------------------------------------------------------------
62
63 public SequenceDeclaration getDeclaration() {
64 return declaration;
65 }
66
67 public int getLength() {
68 return currentLength;
69 }
70
71 public Definition getElem(int i) {
72 if (i > definitions.length) {
73 return null;
74 }
75
76 return definitions[i];
77 }
78
79 public boolean isString() {
80 IntegerDeclaration elemInt;
81
82 if (declaration.getElementType() instanceof IntegerDeclaration) {
83 elemInt = (IntegerDeclaration) declaration.getElementType();
84 if (elemInt.isCharacter()) {
85 return true;
86 }
87 }
88 return false;
89 }
90
91 // ------------------------------------------------------------------------
92 // Operations
93 // ------------------------------------------------------------------------
94
95 @Override
96 public void read(BitBuffer input) {
97 currentLength = (int) lengthDefinition.getValue();
98
99 if ((definitions == null) || (definitions.length < currentLength)) {
100 Definition newDefinitions[] = new Definition[currentLength];
101
102 int i = 0;
103
104 if (definitions != null) {
105 for (; i < definitions.length; i++) {
106 newDefinitions[i] = definitions[i];
107 }
108 }
109
110 for (; i < currentLength; i++) {
111 newDefinitions[i] = declaration.getElementType().createDefinition(
112 definitionScope, fieldName + "[" + i + "]"); //$NON-NLS-1$ //$NON-NLS-2$
113 }
114
115 definitions = newDefinitions;
116 }
117
118 for (int i = 0; i < currentLength; i++) {
119 definitions[i].read(input);
120 }
121 }
122
123 @Override
124 public String toString() {
125 StringBuilder b = new StringBuilder();
126
127 if (this.isString()) {
128 for (int i = 0; i < currentLength; i++) {
129 IntegerDefinition character = (IntegerDefinition) definitions[i];
130
131 if (character.getValue() == 0) {
132 break;
133 }
134
135 b.append(character.toString());
136 }
137 } else {
138 b.append('[');
139 if (currentLength > 0) {
140 for (int i = 0; i < (currentLength - 1); i++) {
141 b.append(' ');
142 b.append(definitions[i].toString());
143 b.append(',');
144 }
145 b.append(' ');
146 b.append(definitions[currentLength - 1].toString());
147 }
148 b.append(" ]"); //$NON-NLS-1$
149
150 }
151
152 return b.toString();
153 }
154 }
This page took 0.035741 seconds and 6 git commands to generate.