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 * A CTF string definition (similar to a C null-terminated byte array).
20 * Strings are an array of bytes of variable size and are terminated by a '\0'
21 * "NULL" character. Their encoding is described in the TSDL meta-data. In
22 * absence of encoding attribute information, the default encoding is UTF-8.
25 * @author Matthew Khouzam
26 * @author Simon Marchi
28 public class StringDefinition
extends Definition
{
30 // ------------------------------------------------------------------------
32 // ------------------------------------------------------------------------
34 private StringDeclaration declaration
;
36 private StringBuilder string
;
38 // ------------------------------------------------------------------------
40 // ------------------------------------------------------------------------
44 * @param declaration the parent declaration
45 * @param definitionScope the parent scope
46 * @param fieldName the field name
48 public StringDefinition(StringDeclaration declaration
,
49 IDefinitionScope definitionScope
, String fieldName
) {
50 super(definitionScope
, fieldName
);
52 this.declaration
= declaration
;
54 string
= new StringBuilder();
57 // ------------------------------------------------------------------------
58 // Getters/Setters/Predicates
59 // ------------------------------------------------------------------------
62 public StringDeclaration
getDeclaration() {
67 * Sets the string declaration
68 * @param declaration the declaration
70 public void setDeclaration(StringDeclaration declaration
) {
71 this.declaration
= declaration
;
76 * @return the stringbuilder
78 public StringBuilder
getString() {
83 * Sets a stringbuilder for the definition
84 * @param string the stringbuilder
86 public void setString(StringBuilder string
) {
91 * Gets the string (value)
94 public String
getValue() {
95 return string
.toString();
98 // ------------------------------------------------------------------------
100 // ------------------------------------------------------------------------
103 public void read(BitBuffer input
) {
104 /* Offset the buffer position wrt the current alignment */
105 int align
= (int) declaration
.getAlignment();
106 int pos
= input
.position() + ((align
- (input
.position() % align
)) % align
);
111 char c
= (char) input
.getInt(8, false);
114 c
= (char) input
.getInt(8, false);
119 public String
toString() {
120 return '\"' + getValue() + '\"';