ctf: Move plugins to the Trace Compass namespace
[deliverable/tracecompass.git] / org.eclipse.tracecompass.ctf.core / src / org / eclipse / linuxtools / ctf / core / event / types / StringDeclaration.java
1 /*******************************************************************************
2 * Copyright (c) 2011, 2013 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
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
19 /**
20 * A CTF string declaration.
21 *
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
29 */
30 public class StringDeclaration extends Declaration {
31
32 // ------------------------------------------------------------------------
33 // Attributes
34 // ------------------------------------------------------------------------
35
36 private final Encoding fEncoding;
37
38 // ------------------------------------------------------------------------
39 // Constructors
40 // ------------------------------------------------------------------------
41
42 /**
43 * Generate a UTF8 string declaration
44 */
45 public StringDeclaration() {
46 fEncoding = Encoding.UTF8;
47 }
48
49 /**
50 * Generate an encoded string declaration
51 * @param encoding the encoding, utf8 or ascii
52 */
53 public StringDeclaration(Encoding encoding) {
54 fEncoding = encoding;
55 }
56
57 // ------------------------------------------------------------------------
58 // Getters/Setters/Predicates
59 // ------------------------------------------------------------------------
60
61 /**
62 *
63 * @return the character encoding.
64 */
65 public Encoding getEncoding() {
66 return fEncoding;
67 }
68
69 @Override
70 public long getAlignment() {
71 // See ctf 4.2.5: Strings are always aligned on byte size.
72 return 8;
73 }
74
75 /**
76 * @since 3.0
77 */
78 @Override
79 public int getMaximumSize() {
80 return Integer.MAX_VALUE;
81 }
82 // ------------------------------------------------------------------------
83 // Operations
84 // ------------------------------------------------------------------------
85
86 /**
87 * @since 3.0
88 */
89 @Override
90 public StringDefinition createDefinition(IDefinitionScope definitionScope,
91 String fieldName, BitBuffer input) throws CTFReaderException {
92 String value = read(input);
93 return new StringDefinition(this, definitionScope, fieldName, value);
94 }
95
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 }
108 @Override
109 public String toString() {
110 /* Only used for debugging */
111 return "[declaration] string[" + Integer.toHexString(hashCode()) + ']'; //$NON-NLS-1$
112 }
113
114 }
This page took 0.032134 seconds and 5 git commands to generate.