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 | ||
a4fa4e36 MK |
15 | import org.eclipse.linuxtools.ctf.core.event.io.BitBuffer; |
16 | import org.eclipse.linuxtools.ctf.core.event.scope.IDefinitionScope; | |
17 | import org.eclipse.linuxtools.ctf.core.trace.CTFReaderException; | |
18 | ||
866e5b51 | 19 | /** |
d37aaa7f | 20 | * A CTF string declaration. |
a511da0d | 21 | * |
d37aaa7f FC |
22 | * Strings are an array of bytes of variable size and are terminated by a '\0' |
23 | * "NULL" character. Their encoding is described in the TSDL meta-data. In | |
24 | * absence of encoding attribute information, the default encoding is UTF-8. | |
25 | * | |
26 | * @version 1.0 | |
27 | * @author Matthew Khouzam | |
28 | * @author Simon Marchi | |
866e5b51 | 29 | */ |
a4fa4e36 | 30 | public class StringDeclaration extends Declaration { |
866e5b51 FC |
31 | |
32 | // ------------------------------------------------------------------------ | |
33 | // Attributes | |
34 | // ------------------------------------------------------------------------ | |
35 | ||
a4fa4e36 | 36 | private final Encoding fEncoding; |
866e5b51 FC |
37 | |
38 | // ------------------------------------------------------------------------ | |
39 | // Constructors | |
40 | // ------------------------------------------------------------------------ | |
41 | ||
9ac2eb62 MK |
42 | /** |
43 | * Generate a UTF8 string declaration | |
44 | */ | |
866e5b51 | 45 | public StringDeclaration() { |
a4fa4e36 | 46 | fEncoding = Encoding.UTF8; |
866e5b51 FC |
47 | } |
48 | ||
9ac2eb62 | 49 | /** |
a511da0d | 50 | * Generate an encoded string declaration |
9ac2eb62 MK |
51 | * @param encoding the encoding, utf8 or ascii |
52 | */ | |
866e5b51 | 53 | public StringDeclaration(Encoding encoding) { |
a4fa4e36 | 54 | fEncoding = encoding; |
866e5b51 FC |
55 | } |
56 | ||
57 | // ------------------------------------------------------------------------ | |
58 | // Getters/Setters/Predicates | |
59 | // ------------------------------------------------------------------------ | |
60 | ||
9ac2eb62 MK |
61 | /** |
62 | * | |
63 | * @return the character encoding. | |
64 | */ | |
65 | public Encoding getEncoding() { | |
a4fa4e36 | 66 | return fEncoding; |
866e5b51 FC |
67 | } |
68 | ||
fd74e6c1 MK |
69 | @Override |
70 | public long getAlignment() { | |
818bd3de EB |
71 | // See ctf 4.2.5: Strings are always aligned on byte size. |
72 | return 8; | |
fd74e6c1 | 73 | } |
a4fa4e36 MK |
74 | |
75 | /** | |
76 | * @since 3.0 | |
77 | */ | |
78 | @Override | |
79 | public int getMaximumSize() { | |
80 | return Integer.MAX_VALUE; | |
81 | } | |
866e5b51 FC |
82 | // ------------------------------------------------------------------------ |
83 | // Operations | |
84 | // ------------------------------------------------------------------------ | |
85 | ||
a4fa4e36 MK |
86 | /** |
87 | * @since 3.0 | |
88 | */ | |
866e5b51 FC |
89 | @Override |
90 | public StringDefinition createDefinition(IDefinitionScope definitionScope, | |
a4fa4e36 MK |
91 | String fieldName, BitBuffer input) throws CTFReaderException { |
92 | String value = read(input); | |
93 | return new StringDefinition(this, definitionScope, fieldName, value); | |
866e5b51 FC |
94 | } |
95 | ||
a4fa4e36 MK |
96 | private String read(BitBuffer input) throws CTFReaderException { |
97 | /* Offset the buffer position wrt the current alignment */ | |
98 | alignRead(input); | |
99 | ||
100 | StringBuilder sb = new StringBuilder(); | |
101 | char c = (char) input.get(8, false); | |
102 | while (c != 0) { | |
103 | sb.append(c); | |
104 | c = (char) input.get(8, false); | |
105 | } | |
106 | return sb.toString(); | |
107 | } | |
866e5b51 FC |
108 | @Override |
109 | public String toString() { | |
110 | /* Only used for debugging */ | |
111 | return "[declaration] string[" + Integer.toHexString(hashCode()) + ']'; //$NON-NLS-1$ | |
112 | } | |
113 | ||
114 | } |