Merge master in TmfTraceModel
[deliverable/tracecompass.git] / org.eclipse.linuxtools.ctf.core / src / org / eclipse / linuxtools / ctf / core / event / types / ArrayDefinition.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.internal.ctf.core.event.io.BitBuffer;
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
33 public ArrayDefinition(ArrayDeclaration declaration,
34 IDefinitionScope definitionScope, String fieldName) {
35 super(definitionScope, fieldName);
36
37 this.declaration = declaration;
38
39 definitions = new Definition[declaration.getLength()];
40
41 for (int i = 0; i < declaration.getLength(); i++) {
42 definitions[i] = declaration.getElementType().createDefinition(
43 definitionScope, fieldName + "[" + i + "]"); //$NON-NLS-1$ //$NON-NLS-2$
44 }
45 }
46
47 // ------------------------------------------------------------------------
48 // Getters/Setters/Predicates
49 // ------------------------------------------------------------------------
50
51 /**
52 * @return the definitions
53 */
54 public Definition[] getDefinitions() {
55 return definitions;
56 }
57
58 /**
59 * @param definitions
60 * the definitions to set
61 */
62 public void setDefinitions(Definition[] definitions) {
63 this.definitions = definitions;
64 }
65
66 public Definition getElem(int i) {
67 if (i > definitions.length) {
68 return null;
69 }
70
71 return definitions[i];
72 }
73
74 public ArrayDeclaration getDeclaration() {
75 return declaration;
76 }
77
78 /**
79 * Sometimes, strings are encoded as an array of 1-byte integers (each one
80 * being an UTF-8 byte).
81 *
82 * @return true if this array is in fact an UTF-8 string. false if it's a
83 * "normal" array of generic Definition's.
84 */
85 public boolean isString() {
86 IntegerDeclaration elemInt;
87
88 if (declaration.getElementType() instanceof IntegerDeclaration) {
89 /*
90 * If the first byte is a "character", we'll consider the whole
91 * array a character string.
92 */
93 elemInt = (IntegerDeclaration) declaration.getElementType();
94 if (elemInt.isCharacter()) {
95 return true;
96 }
97 }
98 return false;
99 }
100
101 // ------------------------------------------------------------------------
102 // Operations
103 // ------------------------------------------------------------------------
104
105 @Override
106 public void read(BitBuffer input) {
107 for (Definition definition : definitions) {
108 definition.read(input);
109 }
110 }
111
112 @Override
113 public String toString() {
114 StringBuilder b = new StringBuilder();
115
116 if (this.isString()) {
117 for (Definition def : definitions) {
118 IntegerDefinition character = (IntegerDefinition) def;
119
120 if (character.getValue() == 0) {
121 break;
122 }
123
124 b.append(character.toString());
125 }
126 } else if (definitions == null) {
127 b.append("[ ]"); //$NON-NLS-1$
128 } else {
129 b.append('[');
130 for (int i = 0; i < (definitions.length - 1); i++) {
131 b.append(' ');
132 b.append(definitions[i].toString());
133 b.append(',');
134 }
135 b.append(' ');
136 b.append(definitions[definitions.length - 1].toString());
137 b.append(" ]"); //$NON-NLS-1$
138 }
139
140 return b.toString();
141 }
142 }
This page took 0.03534 seconds and 6 git commands to generate.