DEADSOFTWARE

17694523dbba69ef510b4d21ffb0b4965c3f72df
[dsw-obn.git] / rtl / java / Files.java
1 import java.io.*;
2 import java.util.*;
4 class Files
5 {
6 /* File / Handle */
7 public static class RECORD0
8 {
9 File path;
10 File regpath;
11 RandomAccessFile desc;
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 }
20 void SetActive(RECORD1 rider)
21 throws
22 IOException
23 {
24 desc.seek(rider.position);
25 }
26 }
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];
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 }
45 public static RECORD0 Old(byte name[])
46 {
47 File filepath;
48 RandomAccessFile filedesc;
49 RECORD0 file;
51 filepath = new File(SYSTEM.STRING(name));
53 if(!filepath.exists() || !filepath.isFile())
54 {
55 return null;
56 }
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 }
74 file = new RECORD0();
75 file.path = filepath;
76 file.regpath = filepath;
77 file.desc = filedesc;
78 return file;
79 }
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 }
92 public static RECORD0 New(byte name[])
93 {
94 RandomAccessFile filedesc;
95 File filepath;
96 File fileregpath;
97 String filename;
98 RECORD0 file;
100 try
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");
107 catch(IOException e)
109 return null;
112 file = new RECORD0();
113 file.path = filepath;
114 file.regpath = fileregpath.getAbsoluteFile();
115 file.desc = filedesc;
116 return file;
119 public static void Register(RECORD0 f)
120 throws
121 IOException
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;
131 public static void Close(RECORD0 f)
135 public static void Purge(RECORD0 f)
136 throws
137 IOException
139 f.desc.setLength(0);
142 public static void Delete(byte name[], short res[], int res$)
144 File filepath = new File(SYSTEM.STRING(name));
145 res[res$] = (short) (filepath.delete() ? (0) : (1));
148 public static void Rename(byte old[], byte _new[], short res[], int res$)
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));
155 public static int Length(RECORD0 f)
156 throws
157 IOException
159 return (int) f.desc.length();
162 public static void GetDate(RECORD0 f, int t[], int t$, int d[], int d$)
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);
171 public static void Set(RECORD1 r[], int r$, RECORD0 f, int pos)
173 RECORD1 rider = r[r$];
174 rider.base = f;
175 rider.position = pos;
176 rider.res[0] = 0;
177 rider.eof[0] = false;
180 public static int Pos(RECORD1 r[], int r$)
182 return (int) r[r$].position;
185 public static RECORD0 Base(RECORD1 r[], int r$)
187 return r[r$].base;
190 public static void Read(RECORD1 r[], int r$, byte x[], int x$)
192 RECORD1 rider = r[r$];
193 RandomAccessFile desc = rider.base.desc;
195 try
197 rider.base.SetActive(rider);
198 x[x$] = (byte) desc.read();
199 rider.position += 1;
201 catch(IOException e)
203 rider.res[0] = 1;
204 rider.eof[0] = true;
208 public static void ReadInt(RECORD1 R[], int R$, short x[], int x$)
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);
216 public static void ReadLInt(RECORD1 R[], int R$, int x[], int x$)
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));
226 public static void ReadHInt(RECORD1 R[], int R$, long x[], int x$)
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);
240 public static void ReadReal(RECORD1 R[], int R$, float x[], int x$)
242 int[] i = new int[1];
243 ReadLInt(R, R$, i, 0);
244 x[x$] = Float.intBitsToFloat(i[0]);
247 public static void ReadLReal(RECORD1 R[], int R$, double x[], int x$)
249 long[] i = new long[1];
250 ReadHInt(R, R$, i, 0);
251 x[x$] = Double.longBitsToDouble(i[0]);
254 public static void ReadNum(RECORD1 R[], int R$, int x[], int x$)
256 SYSTEM.TRAP(-3);
259 public static void ReadString(RECORD1 R[], int R$, byte x[][], int x$)
261 RECORD1 rider = R[R$];
262 RandomAccessFile desc = rider.base.desc;
264 int i = 0;
265 try
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;
275 catch(IOException e)
277 x[x$][i] = 0;
278 rider.res[0] = 1;
279 rider.eof[0] = true;
283 public static void ReadSet(RECORD1 R[], int R$, int x[], int x$)
285 ReadLInt(R, R$, x, x$);
288 public static void ReadBool(RECORD1 R[], int R$, boolean x[], int x$)
290 byte[] i = new byte[1];
291 Read(R, R$, i, 0);
292 x[x$] = (i[0] != 0);
295 public static void ReadBytes(RECORD1 r[], int r$, byte x[][], int x$, int n)
297 RECORD1 rider = r[r$];
298 RandomAccessFile desc = rider.base.desc;
300 try
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);
308 catch(IOException e)
310 e.printStackTrace();
311 rider.res[0] = n;
312 rider.eof[0] = true;
316 public static void Write(RECORD1 r[], int r$, byte x)
318 RECORD1 rider = r[r$];
319 RandomAccessFile desc = rider.base.desc;
321 try
323 rider.base.SetActive(rider);
324 desc.write(x);
325 rider.position += 1;
327 catch(IOException e)
329 rider.res[0] = 1;
330 rider.eof[0] = true;
334 public static void WriteInt(RECORD1 R[], int R$, short x)
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);
342 public static void WriteLInt(RECORD1 R[], int R$, int x)
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);
352 public static void WriteReal(RECORD1 R[], int R$, float x)
354 SYSTEM.TRAP(-3);
357 public static void WriteLReal(RECORD1 R[], int R$, double x)
359 SYSTEM.TRAP(-3);
362 public static void WriteNum(RECORD1 R[], int R$, int x)
364 SYSTEM.TRAP(-3);
367 public static void WriteString(RECORD1 R[], int R$, byte x[])
369 byte[][] i = new byte[1][];
370 i[0] = x;
371 WriteBytes(R, R$, i, 0, SYSTEM.LEN(x) + 1);
374 public static void WriteSet(RECORD1 R[], int R$, int x)
376 SYSTEM.TRAP(-3);
379 public static void WriteBool(RECORD1 R[], int R$, boolean x)
381 SYSTEM.TRAP(-3);
384 public static void WriteBytes(RECORD1 r[], int r$, byte[][] x, int x$, int n)
386 RECORD1 rider = r[r$];
387 RandomAccessFile desc = rider.base.desc;
389 try
391 rider.base.SetActive(rider);
392 desc.write(x[x$], 0, n);
393 rider.position += n;
395 catch(IOException e)
397 rider.res[0] = n;
398 rider.eof[0] = true;
402 public static void BEGIN()