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 | ||
ce2388e0 | 15 | import org.eclipse.linuxtools.internal.ctf.core.event.io.BitBuffer; |
866e5b51 FC |
16 | |
17 | /** | |
18 | * <b><u>ArrayDefinition</u></b> | |
19 | */ | |
20 | public class ArrayDefinition extends Definition { | |
21 | ||
22 | // ------------------------------------------------------------------------ | |
23 | // Attributes | |
24 | // ------------------------------------------------------------------------ | |
25 | ||
26 | private final ArrayDeclaration declaration; | |
27 | private Definition definitions[]; | |
28 | ||
29 | // ------------------------------------------------------------------------ | |
30 | // Constructors | |
31 | // ------------------------------------------------------------------------ | |
32 | ||
9ac2eb62 MK |
33 | /** |
34 | * Constructor | |
35 | * @param declaration the parent declaration | |
36 | * @param definitionScope the parent scope | |
37 | * @param fieldName the field name | |
38 | */ | |
866e5b51 FC |
39 | public ArrayDefinition(ArrayDeclaration declaration, |
40 | IDefinitionScope definitionScope, String fieldName) { | |
41 | super(definitionScope, fieldName); | |
42 | ||
43 | this.declaration = declaration; | |
44 | ||
45 | definitions = new Definition[declaration.getLength()]; | |
46 | ||
47 | for (int i = 0; i < declaration.getLength(); i++) { | |
48 | definitions[i] = declaration.getElementType().createDefinition( | |
49 | definitionScope, fieldName + "[" + i + "]"); //$NON-NLS-1$ //$NON-NLS-2$ | |
50 | } | |
51 | } | |
52 | ||
53 | // ------------------------------------------------------------------------ | |
54 | // Getters/Setters/Predicates | |
55 | // ------------------------------------------------------------------------ | |
56 | ||
57 | /** | |
58 | * @return the definitions | |
59 | */ | |
60 | public Definition[] getDefinitions() { | |
61 | return definitions; | |
62 | } | |
63 | ||
64 | /** | |
65 | * @param definitions | |
66 | * the definitions to set | |
67 | */ | |
68 | public void setDefinitions(Definition[] definitions) { | |
69 | this.definitions = definitions; | |
70 | } | |
71 | ||
9ac2eb62 MK |
72 | /** |
73 | * Get the element at i | |
74 | * @param i the index (cannot be negative) | |
75 | * @return The element at I, if I > length, null, if I < 0, the method throws an out of bounds exception | |
76 | */ | |
866e5b51 FC |
77 | public Definition getElem(int i) { |
78 | if (i > definitions.length) { | |
79 | return null; | |
80 | } | |
81 | ||
82 | return definitions[i]; | |
83 | } | |
84 | ||
9ac2eb62 | 85 | @Override |
866e5b51 FC |
86 | public ArrayDeclaration getDeclaration() { |
87 | return declaration; | |
88 | } | |
89 | ||
90 | /** | |
91 | * Sometimes, strings are encoded as an array of 1-byte integers (each one | |
92 | * being an UTF-8 byte). | |
93 | * | |
94 | * @return true if this array is in fact an UTF-8 string. false if it's a | |
95 | * "normal" array of generic Definition's. | |
96 | */ | |
97 | public boolean isString() { | |
98 | IntegerDeclaration elemInt; | |
99 | ||
100 | if (declaration.getElementType() instanceof IntegerDeclaration) { | |
101 | /* | |
102 | * If the first byte is a "character", we'll consider the whole | |
103 | * array a character string. | |
104 | */ | |
105 | elemInt = (IntegerDeclaration) declaration.getElementType(); | |
106 | if (elemInt.isCharacter()) { | |
107 | return true; | |
108 | } | |
109 | } | |
110 | return false; | |
111 | } | |
112 | ||
113 | // ------------------------------------------------------------------------ | |
114 | // Operations | |
115 | // ------------------------------------------------------------------------ | |
116 | ||
117 | @Override | |
118 | public void read(BitBuffer input) { | |
119 | for (Definition definition : definitions) { | |
120 | definition.read(input); | |
121 | } | |
122 | } | |
123 | ||
124 | @Override | |
125 | public String toString() { | |
126 | StringBuilder b = new StringBuilder(); | |
127 | ||
128 | if (this.isString()) { | |
129 | for (Definition def : definitions) { | |
130 | IntegerDefinition character = (IntegerDefinition) def; | |
131 | ||
132 | if (character.getValue() == 0) { | |
133 | break; | |
134 | } | |
135 | ||
136 | b.append(character.toString()); | |
137 | } | |
138 | } else if (definitions == null) { | |
139 | b.append("[ ]"); //$NON-NLS-1$ | |
140 | } else { | |
141 | b.append('['); | |
142 | for (int i = 0; i < (definitions.length - 1); i++) { | |
143 | b.append(' '); | |
144 | b.append(definitions[i].toString()); | |
145 | b.append(','); | |
146 | } | |
147 | b.append(' '); | |
148 | b.append(definitions[definitions.length - 1].toString()); | |
149 | b.append(" ]"); //$NON-NLS-1$ | |
150 | } | |
151 | ||
152 | return b.toString(); | |
153 | } | |
154 | } |