Commit | Line | Data |
---|---|---|
866e5b51 | 1 | /******************************************************************************* |
ed902a2b | 2 | * Copyright (c) 2011, 2014 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 | ||
f357bcd4 | 13 | package org.eclipse.tracecompass.ctf.core.event.types; |
866e5b51 | 14 | |
d890ec37 MK |
15 | import org.eclipse.jdt.annotation.NonNullByDefault; |
16 | import org.eclipse.jdt.annotation.Nullable; | |
680f9173 | 17 | import org.eclipse.tracecompass.ctf.core.CTFException; |
f357bcd4 AM |
18 | import org.eclipse.tracecompass.ctf.core.event.io.BitBuffer; |
19 | import org.eclipse.tracecompass.ctf.core.event.scope.IDefinitionScope; | |
a4fa4e36 | 20 | |
866e5b51 | 21 | /** |
d37aaa7f | 22 | * A CTF string declaration. |
a511da0d | 23 | * |
d37aaa7f FC |
24 | * Strings are an array of bytes of variable size and are terminated by a '\0' |
25 | * "NULL" character. Their encoding is described in the TSDL meta-data. In | |
26 | * absence of encoding attribute information, the default encoding is UTF-8. | |
27 | * | |
28 | * @version 1.0 | |
29 | * @author Matthew Khouzam | |
30 | * @author Simon Marchi | |
866e5b51 | 31 | */ |
d890ec37 | 32 | @NonNullByDefault |
266c4d51 | 33 | public final class StringDeclaration extends Declaration { |
866e5b51 | 34 | |
f068c622 MK |
35 | private static final StringDeclaration STRING_DEC_UTF8 = new StringDeclaration(Encoding.UTF8); |
36 | private static final StringDeclaration STRING_DEC_ASCII = new StringDeclaration(Encoding.ASCII); | |
37 | private static final StringDeclaration STRING_DEC_NO_ENC = new StringDeclaration(Encoding.NONE); | |
38 | ||
866e5b51 FC |
39 | // ------------------------------------------------------------------------ |
40 | // Attributes | |
41 | // ------------------------------------------------------------------------ | |
42 | ||
f068c622 | 43 | private static final int BITS_PER_BYTE = Byte.SIZE; |
a4fa4e36 | 44 | private final Encoding fEncoding; |
866e5b51 FC |
45 | |
46 | // ------------------------------------------------------------------------ | |
47 | // Constructors | |
48 | // ------------------------------------------------------------------------ | |
49 | ||
9ac2eb62 | 50 | /** |
a511da0d | 51 | * Generate an encoded string declaration |
e00e6663 MK |
52 | * |
53 | * @param encoding | |
54 | * the encoding, utf8 or ascii | |
9ac2eb62 | 55 | */ |
d890ec37 | 56 | private StringDeclaration(Encoding encoding) { |
a4fa4e36 | 57 | fEncoding = encoding; |
866e5b51 FC |
58 | } |
59 | ||
d890ec37 MK |
60 | /** |
61 | * Create a StringDeclaration with the default UTF-8 encoding | |
62 | * | |
63 | * @return a {@link StringDeclaration} with UTF-8 encoding | |
64 | */ | |
65 | public static StringDeclaration getStringDeclaration() { | |
66 | return STRING_DEC_UTF8; | |
67 | } | |
68 | ||
f068c622 MK |
69 | /** |
70 | * Create a StringDeclaration | |
71 | * | |
72 | * @param encoding | |
d890ec37 MK |
73 | * the {@link Encoding} can be Encoding.UTF8, Encoding.ASCII or |
74 | * other | |
f068c622 MK |
75 | * @return a {@link StringDeclaration} |
76 | * @throws IllegalArgumentException | |
77 | * if the encoding is not recognized. | |
78 | */ | |
79 | public static StringDeclaration getStringDeclaration(Encoding encoding) { | |
80 | switch (encoding) { | |
81 | case ASCII: | |
82 | return STRING_DEC_ASCII; | |
83 | case NONE: | |
84 | return STRING_DEC_NO_ENC; | |
85 | case UTF8: | |
86 | return STRING_DEC_UTF8; | |
87 | default: | |
88 | throw new IllegalArgumentException("Unrecognized encoding: " + encoding); //$NON-NLS-1$ | |
89 | } | |
90 | } | |
91 | ||
866e5b51 FC |
92 | // ------------------------------------------------------------------------ |
93 | // Getters/Setters/Predicates | |
94 | // ------------------------------------------------------------------------ | |
95 | ||
9ac2eb62 MK |
96 | /** |
97 | * | |
98 | * @return the character encoding. | |
99 | */ | |
100 | public Encoding getEncoding() { | |
a4fa4e36 | 101 | return fEncoding; |
866e5b51 FC |
102 | } |
103 | ||
fd74e6c1 MK |
104 | @Override |
105 | public long getAlignment() { | |
818bd3de | 106 | // See ctf 4.2.5: Strings are always aligned on byte size. |
f068c622 | 107 | return BITS_PER_BYTE; |
fd74e6c1 | 108 | } |
a4fa4e36 | 109 | |
a4fa4e36 MK |
110 | @Override |
111 | public int getMaximumSize() { | |
5b53901a AM |
112 | /* |
113 | * Every definition can have a different size, so we do not scope this. | |
114 | * Minimum size is one byte (8 bits) though. | |
115 | */ | |
116 | return 8; | |
a4fa4e36 | 117 | } |
e00e6663 | 118 | |
866e5b51 FC |
119 | // ------------------------------------------------------------------------ |
120 | // Operations | |
121 | // ------------------------------------------------------------------------ | |
122 | ||
123 | @Override | |
d890ec37 | 124 | public StringDefinition createDefinition(@Nullable IDefinitionScope definitionScope, |
680f9173 | 125 | String fieldName, BitBuffer input) throws CTFException { |
a4fa4e36 MK |
126 | String value = read(input); |
127 | return new StringDefinition(this, definitionScope, fieldName, value); | |
866e5b51 FC |
128 | } |
129 | ||
680f9173 | 130 | private String read(BitBuffer input) throws CTFException { |
a4fa4e36 MK |
131 | /* Offset the buffer position wrt the current alignment */ |
132 | alignRead(input); | |
133 | ||
134 | StringBuilder sb = new StringBuilder(); | |
f068c622 | 135 | char c = (char) input.get(BITS_PER_BYTE, false); |
a4fa4e36 MK |
136 | while (c != 0) { |
137 | sb.append(c); | |
f068c622 | 138 | c = (char) input.get(BITS_PER_BYTE, false); |
a4fa4e36 | 139 | } |
0e4f957e | 140 | return sb.toString(); |
a4fa4e36 | 141 | } |
e00e6663 | 142 | |
866e5b51 FC |
143 | @Override |
144 | public String toString() { | |
145 | /* Only used for debugging */ | |
146 | return "[declaration] string[" + Integer.toHexString(hashCode()) + ']'; //$NON-NLS-1$ | |
147 | } | |
148 | ||
e00e6663 MK |
149 | @Override |
150 | public int hashCode() { | |
151 | final int prime = 31; | |
152 | int result = prime; | |
d890ec37 MK |
153 | switch (fEncoding) { |
154 | case ASCII: | |
155 | result += 1; | |
156 | break; | |
157 | case NONE: | |
158 | result += 2; | |
159 | break; | |
160 | case UTF8: | |
161 | result += 3; | |
162 | break; | |
163 | default: | |
164 | break; | |
e00e6663 MK |
165 | } |
166 | return result; | |
167 | } | |
168 | ||
169 | @Override | |
d890ec37 | 170 | public boolean equals(@Nullable Object obj) { |
e00e6663 MK |
171 | if (this == obj) { |
172 | return true; | |
173 | } | |
174 | if (obj == null) { | |
175 | return false; | |
176 | } | |
177 | if (getClass() != obj.getClass()) { | |
178 | return false; | |
179 | } | |
180 | StringDeclaration other = (StringDeclaration) obj; | |
181 | if (fEncoding != other.fEncoding) { | |
182 | return false; | |
183 | } | |
184 | return true; | |
185 | } | |
186 | ||
66aa25f0 | 187 | @Override |
d890ec37 | 188 | public boolean isBinaryEquivalent(@Nullable IDeclaration other) { |
66aa25f0 MK |
189 | return equals(other); |
190 | } | |
191 | ||
866e5b51 | 192 | } |