Commit | Line | Data |
---|---|---|
866e5b51 | 1 | /******************************************************************************* |
60ae41e1 | 2 | * Copyright (c) 2011, 2013 Ericsson, Ecole Polytechnique de Montreal and others |
866e5b51 FC |
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; |
db8e8f7d | 16 | import org.eclipse.linuxtools.ctf.core.trace.CTFReaderException; |
866e5b51 FC |
17 | |
18 | /** | |
d37aaa7f | 19 | * A CTF string definition (similar to a C null-terminated byte array). |
486efb2e | 20 | * |
d37aaa7f FC |
21 | * Strings are an array of bytes of variable size and are terminated by a '\0' |
22 | * "NULL" character. Their encoding is described in the TSDL meta-data. In | |
23 | * absence of encoding attribute information, the default encoding is UTF-8. | |
24 | * | |
25 | * @version 1.0 | |
26 | * @author Matthew Khouzam | |
27 | * @author Simon Marchi | |
866e5b51 FC |
28 | */ |
29 | public class StringDefinition extends Definition { | |
30 | ||
31 | // ------------------------------------------------------------------------ | |
32 | // Attributes | |
33 | // ------------------------------------------------------------------------ | |
34 | ||
4f4b6212 | 35 | private StringDeclaration fDeclaration; |
866e5b51 | 36 | |
4f4b6212 | 37 | private String fString; |
866e5b51 FC |
38 | |
39 | // ------------------------------------------------------------------------ | |
40 | // Constructors | |
41 | // ------------------------------------------------------------------------ | |
42 | ||
9ac2eb62 MK |
43 | /** |
44 | * Constructor | |
d6205f97 MK |
45 | * |
46 | * @param declaration | |
47 | * the parent declaration | |
48 | * @param definitionScope | |
49 | * the parent scope | |
50 | * @param fieldName | |
51 | * the field name | |
9ac2eb62 | 52 | */ |
866e5b51 FC |
53 | public StringDefinition(StringDeclaration declaration, |
54 | IDefinitionScope definitionScope, String fieldName) { | |
55 | super(definitionScope, fieldName); | |
56 | ||
4f4b6212 | 57 | fDeclaration = declaration; |
866e5b51 | 58 | |
4f4b6212 | 59 | fString = ""; //$NON-NLS-1$ |
866e5b51 FC |
60 | } |
61 | ||
62 | // ------------------------------------------------------------------------ | |
63 | // Getters/Setters/Predicates | |
64 | // ------------------------------------------------------------------------ | |
65 | ||
9ac2eb62 | 66 | @Override |
866e5b51 | 67 | public StringDeclaration getDeclaration() { |
4f4b6212 | 68 | return fDeclaration; |
866e5b51 FC |
69 | } |
70 | ||
9ac2eb62 MK |
71 | /** |
72 | * Sets the string declaration | |
d6205f97 MK |
73 | * |
74 | * @param declaration | |
75 | * the declaration | |
9ac2eb62 | 76 | */ |
866e5b51 | 77 | public void setDeclaration(StringDeclaration declaration) { |
4f4b6212 | 78 | fDeclaration = declaration; |
866e5b51 FC |
79 | } |
80 | ||
9ac2eb62 | 81 | /** |
4f4b6212 | 82 | * Gets the string (value) |
d6205f97 | 83 | * |
4f4b6212 | 84 | * @return the string |
9ac2eb62 | 85 | */ |
4f4b6212 EB |
86 | public String getValue() { |
87 | return fString; | |
866e5b51 FC |
88 | } |
89 | ||
9ac2eb62 | 90 | /** |
4f4b6212 | 91 | * Sets the string (value) |
d6205f97 | 92 | * |
4f4b6212 | 93 | * @param str the string |
c4767854 | 94 | * @since 3.0 |
9ac2eb62 | 95 | */ |
4f4b6212 EB |
96 | public void setValue(String str) { |
97 | fString = str; | |
866e5b51 FC |
98 | } |
99 | ||
100 | // ------------------------------------------------------------------------ | |
101 | // Operations | |
102 | // ------------------------------------------------------------------------ | |
103 | ||
104 | @Override | |
db8e8f7d | 105 | public void read(BitBuffer input) throws CTFReaderException { |
9722444c | 106 | /* Offset the buffer position wrt the current alignment */ |
4f4b6212 EB |
107 | alignRead(input, fDeclaration); |
108 | ||
109 | StringBuilder sb = new StringBuilder(); | |
2b50c5ac | 110 | char c = (char) input.get(8, false); |
866e5b51 | 111 | while (c != 0) { |
4f4b6212 | 112 | sb.append(c); |
2b50c5ac | 113 | c = (char) input.get(8, false); |
866e5b51 | 114 | } |
4f4b6212 | 115 | fString = sb.toString(); |
866e5b51 FC |
116 | } |
117 | ||
118 | @Override | |
119 | public String toString() { | |
120 | return '\"' + getValue() + '\"'; | |
121 | } | |
122 | ||
123 | } |