Debian Codegraph @

src/smetana/core/CString.java

raw symbols SCIP
1 /* ========================================================================
2 * PlantUML : a free UML diagram generator
3 * ========================================================================
4 *
5 * Project Info: http://plantuml.com
6 *
7 * If you like this project or if you find it useful, you can support us at:
8 *
9 * http://plantuml.com/patreon (only 1$ per month!)
10 * http://plantuml.com/paypal
11 *
12 * This file is part of Smetana.
13 * Smetana is a partial translation of Graphviz/Dot sources from C to Java.
14 *
15 * (C) Copyright 2009-2020, Arnaud Roques
16 *
17 * This translation is distributed under the same Licence as the original C program.
18 *
19 * THE ACCOMPANYING PROGRAM IS PROVIDED UNDER THE TERMS OF THIS ECLIPSE PUBLIC
20 * LICENSE ("AGREEMENT"). [Eclipse Public License - v 1.0]
21 *
22 * ANY USE, REPRODUCTION OR DISTRIBUTION OF THE PROGRAM CONSTITUTES
23 * RECIPIENT'S ACCEPTANCE OF THIS AGREEMENT.
24 *
25 * You may obtain a copy of the License at
26 *
27 * http://www.eclipse.org/legal/epl-v10.html
28 *
29 * Unless required by applicable law or agreed to in writing, software
30 * distributed under the License is distributed on an "AS IS" BASIS,
31 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
32 * See the License for the specific language governing permissions and
33 * limitations under the License.
34 *
35 */
36
37 package smetana.core;
38
39 import h.ST_refstr_t;
40
41 import java.util.ArrayList;
42 import java.util.List;
43
44 import smetana.core.amiga.Area;
45
46 public class CString extends UnsupportedC implements __ptr__, Area {
47
48 private static int UID = 100;
49
50 private final Throwable creation = new Throwable();
51 private final List<Character> data2;
52 private final int currentStart;
53
54 private final int uid;
55
56 public boolean isSameThan(CString other) {
57 if (this.data2 != other.data2) {
58 throw new UnsupportedOperationException();
59 }
60 return this.currentStart == other.currentStart;
61
62 }
63
64 public void memcopyFrom(Area source) {
65 final CString other = (CString) source;
66 this.data2.clear();
67 this.data2.addAll(other.data2);
68 }
69
70 public CString(String string) {
71 this(new ArrayList<Character>(), 0);
72 for (int i = 0; i < string.length(); i++) {
73 data2.add(string.charAt(i));
74 }
75 data2.add('\0');
76 }
77
78 public CString duplicate() {
79 // return this;
80
81 return new CString(new ArrayList<Character>(this.data2), currentStart);
82
83 // final CString result = new CString(this.data.size());
84 // for (int i = 0; i < result.data.size(); i++) {
85 // result.data.set(i, this.data.get(i));
86 // }
87 // return result;
88 }
89
90 public CString strdup() {
91 return duplicate();
92 }
93
94 public CString(int size) {
95 this(new ArrayList<Character>(), 0);
96 for (int i = 0; i < size; i++) {
97 data2.add('\0');
98 }
99 }
100
101 private CString(List<Character> data2, int currentStart) {
102 this.data2 = data2;
103 this.currentStart = currentStart;
104 this.uid = UID;
105 UID += 2;
106 creation.fillInStackTrace();
107 }
108
109 public __ptr__ addVirtualBytes(int bytes) {
110 JUtils.LOG("CString::addVirtualBytes " + bytes);
111 JUtils.LOG("AM " + this);
112 // if (bytes < 0) {
113 // OFFSET offset = OFFSET.fromInt(-bytes);
114 // JUtils.LOG("offset=" + offset);
115 // for (__ptr__ f : fathers) {
116 // JUtils.LOG("f=" + f);
117 // if (f instanceof StarStruct && ((StarStruct) f).getRealClass() == offset.getTheClass()) {
118 // JUtils.LOG("FOUND1!!");
119 // if (f.getPtr(offset.getField()).equals(this)) {
120 // JUtils.LOG("FOUND2!!");
121 // return f;
122 // }
123 // }
124 // }
125 // } else {
126 // JUtils.LOG("father=" + fathers);
127 // JUtils.LOG("CString created on the following place:");
128 // creation.printStackTrace();
129 // }
130 throw new UnsupportedOperationException();
131 }
132
133 private ST_refstr_t parent;
134
135 public ST_refstr_t getParent() {
136 if (parent != null) {
137 return parent;
138 }
139 throw new UnsupportedOperationException();
140 }
141
142 public void setParent(ST_refstr_t struct) {
143 // if (parent != null && parent != struct) {
144 // throw new IllegalStateException();
145 // }
146 if (struct == null) {
147 throw new IllegalStateException();
148 }
149 this.parent = struct;
150 }
151
152 @Override
153 public CString plus(int pointerMove) {
154 return new CString(data2, currentStart + pointerMove);
155 }
156
157 @Override
158 public String toString() {
159 final StringBuilder sb = new StringBuilder();
160 for (Character c : getData()) {
161 if (c == '\0') {
162 sb.append("(0)");
163 } else {
164 sb.append(c);
165 }
166 }
167 return "CString:" + sb;
168 }
169
170 private List<Character> getData() {
171 return data2.subList(currentStart, data2.size());
172 }
173
174 public String getContent() {
175 final StringBuilder sb = new StringBuilder();
176 for (Character c : getData()) {
177 if (c == '\0') {
178 return sb.toString();
179 } else {
180 sb.append(c);
181 }
182 }
183 throw new UnsupportedOperationException();
184 }
185
186 public char charAt(int i) {
187 if (i >= getData().size()) {
188 return '\0';
189 }
190 return getData().get(i);
191 }
192
193 public char setCharAt(int i, char c) {
194 getData().set(i, c);
195 return c;
196 }
197
198 public int length() {
199 int len = 0;
200 for (Character c : getData()) {
201 if (c == '\0') {
202 return len;
203 }
204 len++;
205 }
206 throw new IllegalStateException();
207 }
208
209 public int compareTo(CString other) {
210 for (int i = 0; i < data2.size() - currentStart; i++) {
211 final int diff = this.charAt(i) - other.charAt(i);
212 if (this.charAt(i) == '\0' || diff != 0) {
213 return diff;
214 }
215 }
216 throw new IllegalStateException();
217 }
218
219 public int compareTo(CString other, int num) {
220 for (int i = 0; i < data2.size() - currentStart && i < num; i++) {
221 final int diff = this.charAt(i) - other.charAt(i);
222 if (this.charAt(i) == '\0' || diff != 0) {
223 return diff;
224 }
225 }
226 return 0;
227 }
228
229 public void copyFrom(CString source, int nb) {
230 for (int i = 0; i < source.length() + 1 && i < nb; i++) {
231 setCharAt(i, source.charAt(i));
232 }
233 }
234
235 public CString strchr(char c) {
236 for (int i = currentStart; i < data2.size(); i++) {
237 if (data2.get(i).charValue() == c) {
238 return new CString(data2, i);
239 }
240 }
241 return null;
242 }
243
244 public boolean isSame(CString other) {
245 return getContent().equals(other.getContent());
246 }
247
248 public int getUid() {
249 return uid;
250 }
251
252 }