Commit | Line | Data |
---|---|---|
866e5b51 FC |
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 | ||
486efb2e | 15 | import org.eclipse.linuxtools.ctf.core.event.io.BitBuffer; |
866e5b51 FC |
16 | |
17 | /** | |
d37aaa7f | 18 | * A CTF string definition (similar to a C null-terminated byte array). |
486efb2e | 19 | * |
d37aaa7f FC |
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. | |
23 | * | |
24 | * @version 1.0 | |
25 | * @author Matthew Khouzam | |
26 | * @author Simon Marchi | |
866e5b51 FC |
27 | */ |
28 | public class StringDefinition extends Definition { | |
29 | ||
30 | // ------------------------------------------------------------------------ | |
31 | // Attributes | |
32 | // ------------------------------------------------------------------------ | |
33 | ||
34 | private StringDeclaration declaration; | |
35 | ||
36 | private StringBuilder string; | |
37 | ||
38 | // ------------------------------------------------------------------------ | |
39 | // Constructors | |
40 | // ------------------------------------------------------------------------ | |
41 | ||
9ac2eb62 MK |
42 | /** |
43 | * Constructor | |
44 | * @param declaration the parent declaration | |
45 | * @param definitionScope the parent scope | |
46 | * @param fieldName the field name | |
47 | */ | |
866e5b51 FC |
48 | public StringDefinition(StringDeclaration declaration, |
49 | IDefinitionScope definitionScope, String fieldName) { | |
50 | super(definitionScope, fieldName); | |
51 | ||
52 | this.declaration = declaration; | |
53 | ||
54 | string = new StringBuilder(); | |
55 | } | |
56 | ||
57 | // ------------------------------------------------------------------------ | |
58 | // Getters/Setters/Predicates | |
59 | // ------------------------------------------------------------------------ | |
60 | ||
9ac2eb62 | 61 | @Override |
866e5b51 FC |
62 | public StringDeclaration getDeclaration() { |
63 | return declaration; | |
64 | } | |
65 | ||
9ac2eb62 MK |
66 | /** |
67 | * Sets the string declaration | |
68 | * @param declaration the declaration | |
69 | */ | |
866e5b51 FC |
70 | public void setDeclaration(StringDeclaration declaration) { |
71 | this.declaration = declaration; | |
72 | } | |
73 | ||
9ac2eb62 MK |
74 | /** |
75 | * Gets the string | |
76 | * @return the stringbuilder | |
77 | */ | |
866e5b51 FC |
78 | public StringBuilder getString() { |
79 | return string; | |
80 | } | |
81 | ||
9ac2eb62 MK |
82 | /** |
83 | * Sets a stringbuilder for the definition | |
84 | * @param string the stringbuilder | |
85 | */ | |
866e5b51 FC |
86 | public void setString(StringBuilder string) { |
87 | this.string = string; | |
88 | } | |
89 | ||
9ac2eb62 MK |
90 | /** |
91 | * Gets the string (value) | |
92 | * @return the string | |
93 | */ | |
866e5b51 FC |
94 | public String getValue() { |
95 | return string.toString(); | |
96 | } | |
97 | ||
98 | // ------------------------------------------------------------------------ | |
99 | // Operations | |
100 | // ------------------------------------------------------------------------ | |
101 | ||
102 | @Override | |
103 | public void read(BitBuffer input) { | |
9722444c AM |
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); | |
107 | input.position(pos); | |
108 | ||
866e5b51 FC |
109 | string.setLength(0); |
110 | ||
111 | char c = (char) input.getInt(8, false); | |
112 | while (c != 0) { | |
113 | string.append(c); | |
114 | c = (char) input.getInt(8, false); | |
115 | } | |
116 | } | |
117 | ||
118 | @Override | |
119 | public String toString() { | |
120 | return '\"' + getValue() + '\"'; | |
121 | } | |
122 | ||
123 | } |