| 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.ctf.core.trace.CTFReaderException; |
| 16 | import org.eclipse.linuxtools.internal.ctf.core.event.io.BitBuffer; |
| 17 | |
| 18 | /** |
| 19 | * <b><u>SequenceDefinition</u></b> |
| 20 | */ |
| 21 | public class SequenceDefinition extends Definition { |
| 22 | |
| 23 | // ------------------------------------------------------------------------ |
| 24 | // Attributes |
| 25 | // ------------------------------------------------------------------------ |
| 26 | |
| 27 | private final SequenceDeclaration declaration; |
| 28 | private final IntegerDefinition lengthDefinition; |
| 29 | private Definition definitions[]; |
| 30 | private int currentLength; |
| 31 | |
| 32 | // ------------------------------------------------------------------------ |
| 33 | // Constructors |
| 34 | // ------------------------------------------------------------------------ |
| 35 | |
| 36 | /** |
| 37 | * Constructor |
| 38 | * |
| 39 | * @param declaration |
| 40 | * the parent declaration |
| 41 | * @param definitionScope |
| 42 | * the parent scope |
| 43 | * @param fieldName |
| 44 | * the field name |
| 45 | * @throws CTFReaderException |
| 46 | * If the sequence field was malformatted |
| 47 | */ |
| 48 | public SequenceDefinition(SequenceDeclaration declaration, |
| 49 | IDefinitionScope definitionScope, String fieldName) |
| 50 | throws CTFReaderException { |
| 51 | super(definitionScope, fieldName); |
| 52 | Definition lenDef = null; |
| 53 | |
| 54 | this.declaration = declaration; |
| 55 | |
| 56 | if (definitionScope != null) { |
| 57 | lenDef = definitionScope.lookupDefinition(declaration |
| 58 | .getLengthName()); |
| 59 | } |
| 60 | |
| 61 | if (lenDef == null) { |
| 62 | throw new CTFReaderException("Sequence length field not found"); //$NON-NLS-1$ |
| 63 | } |
| 64 | |
| 65 | if (!(lenDef instanceof IntegerDefinition)) { |
| 66 | throw new CTFReaderException("Sequence length field not integer"); //$NON-NLS-1$ |
| 67 | } |
| 68 | |
| 69 | lengthDefinition = (IntegerDefinition) lenDef; |
| 70 | |
| 71 | if (this.lengthDefinition.getDeclaration().isSigned()) { |
| 72 | throw new CTFReaderException("Sequence length must not be signed"); //$NON-NLS-1$ |
| 73 | } |
| 74 | } |
| 75 | |
| 76 | // ------------------------------------------------------------------------ |
| 77 | // Getters/Setters/Predicates |
| 78 | // ------------------------------------------------------------------------ |
| 79 | |
| 80 | @Override |
| 81 | public SequenceDeclaration getDeclaration() { |
| 82 | return declaration; |
| 83 | } |
| 84 | |
| 85 | /** |
| 86 | * The length of the sequence in number of elements so a sequence of 5 |
| 87 | * GIANT_rediculous_long_ints is the same as a sequence of 5 bits. (5) |
| 88 | * |
| 89 | * @return the length of the sequence |
| 90 | */ |
| 91 | public int getLength() { |
| 92 | return currentLength; |
| 93 | } |
| 94 | |
| 95 | /** |
| 96 | * Get the element at i |
| 97 | * |
| 98 | * @param i |
| 99 | * the index (cannot be negative) |
| 100 | * @return The element at I, if I > length, null, if I < 0, the method |
| 101 | * throws an out of bounds exception |
| 102 | */ |
| 103 | public Definition getElem(int i) { |
| 104 | if (i > definitions.length) { |
| 105 | return null; |
| 106 | } |
| 107 | |
| 108 | return definitions[i]; |
| 109 | } |
| 110 | |
| 111 | /** |
| 112 | * Is the sequence a null terminated string? |
| 113 | * @return true == is a string, false == is not a string |
| 114 | */ |
| 115 | public boolean isString() { |
| 116 | IntegerDeclaration elemInt; |
| 117 | |
| 118 | if (declaration.getElementType() instanceof IntegerDeclaration) { |
| 119 | elemInt = (IntegerDeclaration) declaration.getElementType(); |
| 120 | if (elemInt.isCharacter()) { |
| 121 | return true; |
| 122 | } |
| 123 | } |
| 124 | return false; |
| 125 | } |
| 126 | |
| 127 | // ------------------------------------------------------------------------ |
| 128 | // Operations |
| 129 | // ------------------------------------------------------------------------ |
| 130 | |
| 131 | @Override |
| 132 | public void read(BitBuffer input) { |
| 133 | currentLength = (int) lengthDefinition.getValue(); |
| 134 | |
| 135 | if ((definitions == null) || (definitions.length < currentLength)) { |
| 136 | Definition newDefinitions[] = new Definition[currentLength]; |
| 137 | |
| 138 | int i = 0; |
| 139 | |
| 140 | if (definitions != null) { |
| 141 | for (; i < definitions.length; i++) { |
| 142 | newDefinitions[i] = definitions[i]; |
| 143 | } |
| 144 | } |
| 145 | |
| 146 | for (; i < currentLength; i++) { |
| 147 | newDefinitions[i] = declaration.getElementType() |
| 148 | .createDefinition(definitionScope, |
| 149 | fieldName + "[" + i + "]"); //$NON-NLS-1$ //$NON-NLS-2$ |
| 150 | } |
| 151 | |
| 152 | definitions = newDefinitions; |
| 153 | } |
| 154 | |
| 155 | for (int i = 0; i < currentLength; i++) { |
| 156 | definitions[i].read(input); |
| 157 | } |
| 158 | } |
| 159 | |
| 160 | @Override |
| 161 | public String toString() { |
| 162 | StringBuilder b = new StringBuilder(); |
| 163 | |
| 164 | if (this.isString()) { |
| 165 | for (int i = 0; i < currentLength; i++) { |
| 166 | IntegerDefinition character = (IntegerDefinition) definitions[i]; |
| 167 | |
| 168 | if (character.getValue() == 0) { |
| 169 | break; |
| 170 | } |
| 171 | |
| 172 | b.append(character.toString()); |
| 173 | } |
| 174 | } else { |
| 175 | b.append('['); |
| 176 | if (currentLength > 0) { |
| 177 | for (int i = 0; i < (currentLength - 1); i++) { |
| 178 | b.append(' '); |
| 179 | b.append(definitions[i].toString()); |
| 180 | b.append(','); |
| 181 | } |
| 182 | b.append(' '); |
| 183 | b.append(definitions[currentLength - 1].toString()); |
| 184 | } |
| 185 | b.append(" ]"); //$NON-NLS-1$ |
| 186 | |
| 187 | } |
| 188 | |
| 189 | return b.toString(); |
| 190 | } |
| 191 | } |