| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 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 |
|
| 80 |
|
| 81 |
return new CString(new ArrayList<Character>(this.data2), currentStart); |
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
|
| 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 |
|
| 113 |
|
| 114 |
|
| 115 |
|
| 116 |
|
| 117 |
|
| 118 |
|
| 119 |
|
| 120 |
|
| 121 |
|
| 122 |
|
| 123 |
|
| 124 |
|
| 125 |
|
| 126 |
|
| 127 |
|
| 128 |
|
| 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 |
|
| 144 |
|
| 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 |
} |