1 /*******************************************************************************
2 * Copyright (c) 2011-2012 Ericsson, Ecole Polytechnique de Montreal and others
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
9 * Contributors: Matthew Khouzam - Initial API and implementation
10 * Contributors: Simon Marchi - Initial API and implementation
11 *******************************************************************************/
13 package org
.eclipse
.linuxtools
.ctf
.core
.event
.types
;
15 import org
.eclipse
.linuxtools
.ctf
.core
.event
.io
.BitBuffer
;
16 import org
.eclipse
.linuxtools
.ctf
.core
.trace
.CTFReaderException
;
19 * A CTF sequence definition (a fixed-size array).
21 * An array where the size is fixed but declared in the trace, unlike array
22 * where it is declared with a literal
25 * @author Matthew Khouzam
26 * @author Simon Marchi
28 public class SequenceDefinition
extends Definition
{
30 // ------------------------------------------------------------------------
32 // ------------------------------------------------------------------------
34 private final SequenceDeclaration declaration
;
35 private final IntegerDefinition lengthDefinition
;
36 private Definition definitions
[];
37 private int currentLength
;
39 // ------------------------------------------------------------------------
41 // ------------------------------------------------------------------------
47 * the parent declaration
48 * @param definitionScope
52 * @throws CTFReaderException
53 * If the sequence field was malformatted
55 public SequenceDefinition(SequenceDeclaration declaration
,
56 IDefinitionScope definitionScope
, String fieldName
)
57 throws CTFReaderException
{
58 super(definitionScope
, fieldName
);
59 Definition lenDef
= null;
61 this.declaration
= declaration
;
63 if (definitionScope
!= null) {
64 lenDef
= definitionScope
.lookupDefinition(declaration
69 throw new CTFReaderException("Sequence length field not found"); //$NON-NLS-1$
72 if (!(lenDef
instanceof IntegerDefinition
)) {
73 throw new CTFReaderException("Sequence length field not integer"); //$NON-NLS-1$
76 lengthDefinition
= (IntegerDefinition
) lenDef
;
78 if (this.lengthDefinition
.getDeclaration().isSigned()) {
79 throw new CTFReaderException("Sequence length must not be signed"); //$NON-NLS-1$
83 // ------------------------------------------------------------------------
84 // Getters/Setters/Predicates
85 // ------------------------------------------------------------------------
88 public SequenceDeclaration
getDeclaration() {
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)
96 * @return the length of the sequence
98 public int getLength() {
103 * Get the element at 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
110 public Definition
getElem(int i
) {
111 if (i
> definitions
.length
) {
115 return definitions
[i
];
119 * Is the sequence a null terminated string?
120 * @return true == is a string, false == is not a string
122 public boolean isString() {
123 IntegerDeclaration elemInt
;
125 if (declaration
.getElementType() instanceof IntegerDeclaration
) {
126 elemInt
= (IntegerDeclaration
) declaration
.getElementType();
127 if (elemInt
.isCharacter()) {
134 // ------------------------------------------------------------------------
136 // ------------------------------------------------------------------------
139 public void read(BitBuffer input
) throws CTFReaderException
{
140 currentLength
= (int) lengthDefinition
.getValue();
142 if ((definitions
== null) || (definitions
.length
< currentLength
)) {
143 Definition newDefinitions
[] = new Definition
[currentLength
];
147 if (definitions
!= null) {
148 System
.arraycopy(definitions
, 0, newDefinitions
, 0, definitions
.length
);
151 for (; i
< currentLength
; i
++) {
152 newDefinitions
[i
] = declaration
.getElementType()
153 .createDefinition(getDefinitionScope(),
154 getFieldName() + "[" + i
+ "]"); //$NON-NLS-1$ //$NON-NLS-2$
157 definitions
= newDefinitions
;
160 for (int i
= 0; i
< currentLength
; i
++) {
161 definitions
[i
].read(input
);
166 public String
toString() {
167 StringBuilder b
= new StringBuilder();
169 if (this.isString()) {
170 for (int i
= 0; i
< currentLength
; i
++) {
171 IntegerDefinition character
= (IntegerDefinition
) definitions
[i
];
173 if (character
.getValue() == 0) {
177 b
.append(character
.toString());
181 if (currentLength
> 0) {
182 for (int i
= 0; i
< (currentLength
- 1); i
++) {
184 b
.append(definitions
[i
].toString());
188 b
.append(definitions
[currentLength
- 1].toString());
190 b
.append(" ]"); //$NON-NLS-1$