ff27e849be33c820dd96bf08857d8bced2fdf2b4
[deliverable/tracecompass.git] / org.eclipse.linuxtools.ctf.core / src / org / eclipse / linuxtools / ctf / core / event / types / StringDefinition.java
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
15 import org.eclipse.linuxtools.ctf.core.event.io.BitBuffer;
16 import org.eclipse.linuxtools.ctf.core.trace.CTFReaderException;
17
18 /**
19 * A CTF string definition (similar to a C null-terminated byte array).
20 *
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
28 */
29 public class StringDefinition extends Definition {
30
31 // ------------------------------------------------------------------------
32 // Attributes
33 // ------------------------------------------------------------------------
34
35 private StringDeclaration fDeclaration;
36
37 private String fString;
38
39 // ------------------------------------------------------------------------
40 // Constructors
41 // ------------------------------------------------------------------------
42
43 /**
44 * Constructor
45 *
46 * @param declaration
47 * the parent declaration
48 * @param definitionScope
49 * the parent scope
50 * @param fieldName
51 * the field name
52 */
53 public StringDefinition(StringDeclaration declaration,
54 IDefinitionScope definitionScope, String fieldName) {
55 super(definitionScope, fieldName);
56
57 fDeclaration = declaration;
58
59 fString = ""; //$NON-NLS-1$
60 }
61
62 // ------------------------------------------------------------------------
63 // Getters/Setters/Predicates
64 // ------------------------------------------------------------------------
65
66 @Override
67 public StringDeclaration getDeclaration() {
68 return fDeclaration;
69 }
70
71 /**
72 * Sets the string declaration
73 *
74 * @param declaration
75 * the declaration
76 */
77 public void setDeclaration(StringDeclaration declaration) {
78 fDeclaration = declaration;
79 }
80
81 /**
82 * Gets the string (value)
83 *
84 * @return the string
85 */
86 public String getValue() {
87 return fString;
88 }
89
90 /**
91 * Sets the string (value)
92 *
93 * @param str the string
94 */
95 public void setValue(String str) {
96 fString = str;
97 }
98
99 // ------------------------------------------------------------------------
100 // Operations
101 // ------------------------------------------------------------------------
102
103 @Override
104 public void read(BitBuffer input) throws CTFReaderException {
105 /* Offset the buffer position wrt the current alignment */
106 alignRead(input, fDeclaration);
107
108 StringBuilder sb = new StringBuilder();
109 char c = (char) input.get(8, false);
110 while (c != 0) {
111 sb.append(c);
112 c = (char) input.get(8, false);
113 }
114 fString = sb.toString();
115 }
116
117 @Override
118 public String toString() {
119 return '\"' + getValue() + '\"';
120 }
121
122 }
This page took 0.032618 seconds and 5 git commands to generate.