DEADSOFTWARE

17694523dbba69ef510b4d21ffb0b4965c3f72df
[dsw-obn.git] / rtl / java / Files.java
1 import java.io.*;
2 import java.util.*;
3
4 class Files
5 {
6 /* File / Handle */
7 public static class RECORD0
8 {
9 File path;
10 File regpath;
11 RandomAccessFile desc;
12
13 public static void $COPY$(RECORD0 dst, RECORD0 src)
14 {
15 dst.path = src.path;
16 dst.regpath = src.regpath;
17 dst.desc = src.desc;
18 }
19
20 void SetActive(RECORD1 rider)
21 throws
22 IOException
23 {
24 desc.seek(rider.position);
25 }
26 }
27
28 /* Rider */
29 public static class RECORD1
30 {
31 long position;
32 RECORD0 base;
33 public boolean[] eof = new boolean[1];
34 public int[] res = new int[1];
35
36 public static void $COPY$(RECORD1 dst, RECORD1 src)
37 {
38 dst.base = src.base;
39 dst.position = src.position;
40 dst.eof[0] = src.eof[0];
41 dst.res[0] = src.res[0];
42 }
43 }
44
45 public static RECORD0 Old(byte name[])
46 {
47 File filepath;
48 RandomAccessFile filedesc;
49 RECORD0 file;
50
51 filepath = new File(SYSTEM.STRING(name));
52
53 if(!filepath.exists() || !filepath.isFile())
54 {
55 return null;
56 }
57
58 try
59 {
60 filedesc = new RandomAccessFile(filepath, "rw");
61 }
62 catch(IOException e)
63 {
64 try
65 {
66 filedesc = new RandomAccessFile(filepath, "r");
67 }
68 catch(IOException g)
69 {
70 return null;
71 }
72 }
73
74 file = new RECORD0();
75 file.path = filepath;
76 file.regpath = filepath;
77 file.desc = filedesc;
78 return file;
79 }
80
81 private static String RandomString(int len)
82 {
83 Random x = new Random();
84 String s = new String();
85 for(int i = 0; i < len; i++)
86 {
87 s += (char) (x.nextInt(25) + 65);
88 }
89 return s;
90 }
91
92 public static RECORD0 New(byte name[])
93 {
94 RandomAccessFile filedesc;
95 File filepath;
96 File fileregpath;
97 String filename;
98 RECORD0 file;
99
100 try
101 {
102 String s = (name[0] == 0) ? (".TMP") : SYSTEM.STRING(name);
103 filepath = File.createTempFile(s, "tmp");
104 fileregpath = new File(s);
105 filedesc = new RandomAccessFile(filepath, "rw");
106 }
107 catch(IOException e)
108 {
109 return null;
110 }
111
112 file = new RECORD0();
113 file.path = filepath;
114 file.regpath = fileregpath.getAbsoluteFile();
115 file.desc = filedesc;
116 return file;
117 }
118
119 public static void Register(RECORD0 f)
120 throws
121 IOException
122 {
123 java.nio.file.Files.move(
124 java.nio.file.Paths.get(f.path.getPath()),
125 java.nio.file.Paths.get(f.regpath.getPath()),
126 java.nio.file.StandardCopyOption.REPLACE_EXISTING
127 );
128 f.path = f.regpath;
129 }
130
131 public static void Close(RECORD0 f)
132 {
133 }
134
135 public static void Purge(RECORD0 f)
136 throws
137 IOException
138 {
139 f.desc.setLength(0);
140 }
141
142 public static void Delete(byte name[], short res[], int res$)
143 {
144 File filepath = new File(SYSTEM.STRING(name));
145 res[res$] = (short) (filepath.delete() ? (0) : (1));
146 }
147
148 public static void Rename(byte old[], byte _new[], short res[], int res$)
149 {
150 File oldpath = new File(SYSTEM.STRING(old));
151 File newpath = new File(SYSTEM.STRING(_new));
152 res[res$] = (short) (oldpath.renameTo(newpath) ? (0) : (1));
153 }
154
155 public static int Length(RECORD0 f)
156 throws
157 IOException
158 {
159 return (int) f.desc.length();
160 }
161
162 public static void GetDate(RECORD0 f, int t[], int t$, int d[], int d$)
163 {
164 long time = f.path.lastModified();
165 Calendar cal = Calendar.getInstance();
166 cal.setTimeInMillis(time);
167 t[t$] = cal.get(Calendar.HOUR_OF_DAY) * 4096 + cal.get(Calendar.MINUTE) * 64 + cal.get(Calendar.SECOND);
168 d[d$] = cal.get(Calendar.YEAR) * 512 + cal.get(Calendar.MONTH) * 32 + cal.get(Calendar.DAY_OF_WEEK);
169 }
170
171 public static void Set(RECORD1 r[], int r$, RECORD0 f, int pos)
172 {
173 RECORD1 rider = r[r$];
174 rider.base = f;
175 rider.position = pos;
176 rider.res[0] = 0;
177 rider.eof[0] = false;
178 }
179
180 public static int Pos(RECORD1 r[], int r$)
181 {
182 return (int) r[r$].position;
183 }
184
185 public static RECORD0 Base(RECORD1 r[], int r$)
186 {
187 return r[r$].base;
188 }
189
190 public static void Read(RECORD1 r[], int r$, byte x[], int x$)
191 {
192 RECORD1 rider = r[r$];
193 RandomAccessFile desc = rider.base.desc;
194
195 try
196 {
197 rider.base.SetActive(rider);
198 x[x$] = (byte) desc.read();
199 rider.position += 1;
200 }
201 catch(IOException e)
202 {
203 rider.res[0] = 1;
204 rider.eof[0] = true;
205 }
206 }
207
208 public static void ReadInt(RECORD1 R[], int R$, short x[], int x$)
209 {
210 byte[][] buf = new byte[1][2];
211 ReadBytes(R, R$, buf, 0, 2);
212 x[x$] = (short) ((buf[0][1] & 0xff) << 8);
213 x[x$] |= (short) (buf[0][0] & 0xff);
214 }
215
216 public static void ReadLInt(RECORD1 R[], int R$, int x[], int x$)
217 {
218 byte[][] buf = new byte[1][4];
219 ReadBytes(R, R$, buf, 0, 4);
220 x[x$] = (int) ((buf[0][3] & 0xff) << 24);
221 x[x$] |= (int) ((buf[0][2] & 0xff) << 16);
222 x[x$] |= (int) ((buf[0][1] & 0xff) << 8);
223 x[x$] |= (int) ((buf[0][0] & 0xff));
224 }
225
226 public static void ReadHInt(RECORD1 R[], int R$, long x[], int x$)
227 {
228 byte[][] buf = new byte[1][8];
229 ReadBytes(R, R$, buf, 0, 8);
230 x[x$] = (buf[0][7] & 0xff) << 56;
231 x[x$] |= (buf[0][6] & 0xff) << 48;
232 x[x$] |= (buf[0][5] & 0xff) << 40;
233 x[x$] |= (buf[0][4] & 0xff) << 32;
234 x[x$] |= (buf[0][3] & 0xff) << 24;
235 x[x$] |= (buf[0][2] & 0xff) << 16;
236 x[x$] |= (buf[0][1] & 0xff) << 8;
237 x[x$] |= (buf[0][0] & 0xff);
238 }
239
240 public static void ReadReal(RECORD1 R[], int R$, float x[], int x$)
241 {
242 int[] i = new int[1];
243 ReadLInt(R, R$, i, 0);
244 x[x$] = Float.intBitsToFloat(i[0]);
245 }
246
247 public static void ReadLReal(RECORD1 R[], int R$, double x[], int x$)
248 {
249 long[] i = new long[1];
250 ReadHInt(R, R$, i, 0);
251 x[x$] = Double.longBitsToDouble(i[0]);
252 }
253
254 public static void ReadNum(RECORD1 R[], int R$, int x[], int x$)
255 {
256 SYSTEM.TRAP(-3);
257 }
258
259 public static void ReadString(RECORD1 R[], int R$, byte x[][], int x$)
260 {
261 RECORD1 rider = R[R$];
262 RandomAccessFile desc = rider.base.desc;
263
264 int i = 0;
265 try
266 {
267 rider.base.SetActive(rider);
268 int ch;
269 do {
270 ch = x[x$][i] = (byte) desc.read();
271 i += 1;
272 } while(ch != 0);
273 rider.position += i;
274 }
275 catch(IOException e)
276 {
277 x[x$][i] = 0;
278 rider.res[0] = 1;
279 rider.eof[0] = true;
280 }
281 }
282
283 public static void ReadSet(RECORD1 R[], int R$, int x[], int x$)
284 {
285 ReadLInt(R, R$, x, x$);
286 }
287
288 public static void ReadBool(RECORD1 R[], int R$, boolean x[], int x$)
289 {
290 byte[] i = new byte[1];
291 Read(R, R$, i, 0);
292 x[x$] = (i[0] != 0);
293 }
294
295 public static void ReadBytes(RECORD1 r[], int r$, byte x[][], int x$, int n)
296 {
297 RECORD1 rider = r[r$];
298 RandomAccessFile desc = rider.base.desc;
299
300 try
301 {
302 rider.base.SetActive(rider);
303 int readed = desc.read(x[x$], 0, n);
304 rider.res[0] = (readed >= 0) ? (n - readed) : (0);
305 rider.eof[0] = (rider.res[0] != 0);
306 rider.position += (readed >= 0) ? (readed) : (0);
307 }
308 catch(IOException e)
309 {
310 e.printStackTrace();
311 rider.res[0] = n;
312 rider.eof[0] = true;
313 }
314 }
315
316 public static void Write(RECORD1 r[], int r$, byte x)
317 {
318 RECORD1 rider = r[r$];
319 RandomAccessFile desc = rider.base.desc;
320
321 try
322 {
323 rider.base.SetActive(rider);
324 desc.write(x);
325 rider.position += 1;
326 }
327 catch(IOException e)
328 {
329 rider.res[0] = 1;
330 rider.eof[0] = true;
331 }
332 }
333
334 public static void WriteInt(RECORD1 R[], int R$, short x)
335 {
336 byte[][] i = new byte[1][2];
337 i[0][1] = (byte) ((x >>> 8) & 0xff);
338 i[0][0] = (byte) ((x) & 0xff);
339 WriteBytes(R, R$, i, 0, 2);
340 }
341
342 public static void WriteLInt(RECORD1 R[], int R$, int x)
343 {
344 byte[][] i = new byte[1][4];
345 i[0][3] = (byte) ((x >>> 24) & 0xff);
346 i[0][2] = (byte) ((x >>> 16) & 0xff);
347 i[0][1] = (byte) ((x >>> 8) & 0xff);
348 i[0][0] = (byte) ((x) & 0xff);
349 WriteBytes(R, R$, i, 0, 4);
350 }
351
352 public static void WriteReal(RECORD1 R[], int R$, float x)
353 {
354 SYSTEM.TRAP(-3);
355 }
356
357 public static void WriteLReal(RECORD1 R[], int R$, double x)
358 {
359 SYSTEM.TRAP(-3);
360 }
361
362 public static void WriteNum(RECORD1 R[], int R$, int x)
363 {
364 SYSTEM.TRAP(-3);
365 }
366
367 public static void WriteString(RECORD1 R[], int R$, byte x[])
368 {
369 byte[][] i = new byte[1][];
370 i[0] = x;
371 WriteBytes(R, R$, i, 0, SYSTEM.LEN(x) + 1);
372 }
373
374 public static void WriteSet(RECORD1 R[], int R$, int x)
375 {
376 SYSTEM.TRAP(-3);
377 }
378
379 public static void WriteBool(RECORD1 R[], int R$, boolean x)
380 {
381 SYSTEM.TRAP(-3);
382 }
383
384 public static void WriteBytes(RECORD1 r[], int r$, byte[][] x, int x$, int n)
385 {
386 RECORD1 rider = r[r$];
387 RandomAccessFile desc = rider.base.desc;
388
389 try
390 {
391 rider.base.SetActive(rider);
392 desc.write(x[x$], 0, n);
393 rider.position += n;
394 }
395 catch(IOException e)
396 {
397 rider.res[0] = n;
398 rider.eof[0] = true;
399 }
400 }
401
402 public static void BEGIN()
403 {
404
405 }
406 }