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 * <b><u>SequenceDefinition</u></b>
21 public class SequenceDefinition
extends Definition
{
23 // ------------------------------------------------------------------------
25 // ------------------------------------------------------------------------
27 private final SequenceDeclaration declaration
;
28 private final IntegerDefinition lengthDefinition
;
29 private Definition definitions
[];
30 private int currentLength
;
32 // ------------------------------------------------------------------------
34 // ------------------------------------------------------------------------
36 public SequenceDefinition(SequenceDeclaration declaration
,
37 IDefinitionScope definitionScope
, String fieldName
) throws CTFReaderException
{
38 super(definitionScope
, fieldName
);
39 Definition lenDef
= null;
41 this.declaration
= declaration
;
43 if (definitionScope
!= null) {
44 lenDef
= definitionScope
.lookupDefinition(declaration
.getLengthName());
48 throw new CTFReaderException("Sequence length field not found"); //$NON-NLS-1$
51 if (!(lenDef
instanceof IntegerDefinition
)) {
52 throw new CTFReaderException("Sequence length field not integer"); //$NON-NLS-1$
55 lengthDefinition
= (IntegerDefinition
) lenDef
;
57 if (this.lengthDefinition
.getDeclaration().isSigned()) {
58 throw new CTFReaderException("Sequence length must not be signed"); //$NON-NLS-1$
62 // ------------------------------------------------------------------------
63 // Getters/Setters/Predicates
64 // ------------------------------------------------------------------------
66 public SequenceDeclaration
getDeclaration() {
70 public int getLength() {
74 public Definition
getElem(int i
) {
75 if (i
> definitions
.length
) {
79 return definitions
[i
];
82 public boolean isString() {
83 IntegerDeclaration elemInt
;
85 if (declaration
.getElementType() instanceof IntegerDeclaration
) {
86 elemInt
= (IntegerDeclaration
) declaration
.getElementType();
87 if (elemInt
.isCharacter()) {
94 // ------------------------------------------------------------------------
96 // ------------------------------------------------------------------------
99 public void read(BitBuffer input
) {
100 currentLength
= (int) lengthDefinition
.getValue();
102 if ((definitions
== null) || (definitions
.length
< currentLength
)) {
103 Definition newDefinitions
[] = new Definition
[currentLength
];
107 if (definitions
!= null) {
108 for (; i
< definitions
.length
; i
++) {
109 newDefinitions
[i
] = definitions
[i
];
113 for (; i
< currentLength
; i
++) {
114 newDefinitions
[i
] = declaration
.getElementType().createDefinition(
115 definitionScope
, fieldName
+ "[" + i
+ "]"); //$NON-NLS-1$ //$NON-NLS-2$
118 definitions
= newDefinitions
;
121 for (int i
= 0; i
< currentLength
; i
++) {
122 definitions
[i
].read(input
);
127 public String
toString() {
128 StringBuilder b
= new StringBuilder();
130 if (this.isString()) {
131 for (int i
= 0; i
< currentLength
; i
++) {
132 IntegerDefinition character
= (IntegerDefinition
) definitions
[i
];
134 if (character
.getValue() == 0) {
138 b
.append(character
.toString());
142 if (currentLength
> 0) {
143 for (int i
= 0; i
< (currentLength
- 1); i
++) {
145 b
.append(definitions
[i
].toString());
149 b
.append(definitions
[currentLength
- 1].toString());
151 b
.append(" ]"); //$NON-NLS-1$