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
;
18 * <b><u>SequenceDefinition</u></b>
20 public class SequenceDefinition
extends Definition
{
22 // ------------------------------------------------------------------------
24 // ------------------------------------------------------------------------
26 private final SequenceDeclaration declaration
;
27 private IntegerDefinition lengthDefinition
;
28 private Definition definitions
[];
29 private int currentLength
;
31 // ------------------------------------------------------------------------
33 // ------------------------------------------------------------------------
35 public SequenceDefinition(SequenceDeclaration declaration
,
36 IDefinitionScope definitionScope
, String fieldName
) {
37 super(definitionScope
, fieldName
);
39 this.declaration
= declaration
;
40 // this.definitionScope = definitionScope;
42 if (definitionScope
!= null) {
43 Definition lenDef
= definitionScope
.lookupDefinition(declaration
.getLengthName());
44 lengthDefinition
= (IntegerDefinition
) lenDef
;
47 * if (lenDef == null) { throw new
48 * Exception("Sequence length field not found"); }
50 * if (!(lenDef instanceof IntegerDefinition)) { throw new
51 * Exception("Sequence length field not integer"); }
54 * if (this.lengthDefinition.declaration.signed) { throw new
55 * Exception("Sequence length must not be signed"); }
59 // ------------------------------------------------------------------------
60 // Getters/Setters/Predicates
61 // ------------------------------------------------------------------------
63 public SequenceDeclaration
getDeclaration() {
67 public int getLength() {
71 public Definition
getElem(int i
) {
72 if (i
> definitions
.length
) {
76 return definitions
[i
];
79 public boolean isString() {
80 IntegerDeclaration elemInt
;
82 if (declaration
.getElementType() instanceof IntegerDeclaration
) {
83 elemInt
= (IntegerDeclaration
) declaration
.getElementType();
84 if (elemInt
.isCharacter()) {
91 // ------------------------------------------------------------------------
93 // ------------------------------------------------------------------------
96 public void read(BitBuffer input
) {
97 currentLength
= (int) lengthDefinition
.getValue();
99 if ((definitions
== null) || (definitions
.length
< currentLength
)) {
100 Definition newDefinitions
[] = new Definition
[currentLength
];
104 if (definitions
!= null) {
105 for (; i
< definitions
.length
; i
++) {
106 newDefinitions
[i
] = definitions
[i
];
110 for (; i
< currentLength
; i
++) {
111 newDefinitions
[i
] = declaration
.getElementType().createDefinition(
112 definitionScope
, fieldName
+ "[" + i
+ "]"); //$NON-NLS-1$ //$NON-NLS-2$
115 definitions
= newDefinitions
;
118 for (int i
= 0; i
< currentLength
; i
++) {
119 definitions
[i
].read(input
);
124 public String
toString() {
125 StringBuilder b
= new StringBuilder();
127 if (this.isString()) {
128 for (int i
= 0; i
< currentLength
; i
++) {
129 IntegerDefinition character
= (IntegerDefinition
) definitions
[i
];
131 if (character
.getValue() == 0) {
135 b
.append(character
.toString());
139 if (currentLength
> 0) {
140 for (int i
= 0; i
< (currentLength
- 1); i
++) {
142 b
.append(definitions
[i
].toString());
146 b
.append(definitions
[currentLength
- 1].toString());
148 b
.append(" ]"); //$NON-NLS-1$