DEADSOFTWARE

Реализовано несколько процедур записи в Files, исправление проблем в SYSTEM и Oberon
[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 RECORD1 rider = R[R$];
211 RandomAccessFile desc = rider.base.desc;
213 try
215 rider.base.SetActive(rider);
216 byte[] buf = new byte[2];
217 rider.res[0] = desc.read(buf, 0, 2);
218 rider.eof[0] = (rider.res[0] != 0);
219 x[x$] = (short) (((buf[1] & 0xff) << 8) | (buf[0] & 0xff));
220 rider.position += 2 - rider.res[0];
222 catch(IOException e)
224 rider.res[0] = 2;
225 rider.eof[0] = true;
229 public static void ReadLInt(RECORD1 R[], int R$, int x[], int x$)
231 RECORD1 rider = R[R$];
232 RandomAccessFile desc = rider.base.desc;
234 try
236 rider.base.SetActive(rider);
237 byte[] buf = new byte[4];
238 rider.res[0] = desc.read(buf, 0, 4);
239 rider.eof[0] = (rider.res[0] != 0);
240 x[x$] = ((buf[3] & 0xff) << 24) | ((buf[2] & 0xff) << 16) | ((buf[1] & 0xff) << 8) | (buf[0] & 0xff);
241 rider.position += 4 - rider.res[0];
243 catch(IOException e)
245 rider.res[0] = 4;
246 rider.eof[0] = true;
250 public static void ReadReal(RECORD1 R[], int R$, float x[], int x$)
252 RECORD1 rider = R[R$];
253 RandomAccessFile desc = rider.base.desc;
255 try
257 rider.base.SetActive(rider);
258 x[x$] = desc.readFloat();
259 rider.position += 4;
261 catch(IOException e)
263 rider.res[0] = 4;
264 rider.eof[0] = true;
268 public static void ReadLReal(RECORD1 R[], int R$, double x[], int x$)
270 RECORD1 rider = R[R$];
271 RandomAccessFile desc = rider.base.desc;
273 try
275 rider.base.SetActive(rider);
276 x[x$] = desc.readDouble();
277 rider.position += 8;
279 catch(IOException e)
281 rider.res[0] = 8;
282 rider.eof[0] = true;
286 public static void ReadNum(RECORD1 R[], int R$, int x[], int x$)
288 SYSTEM.TRAP(-3);
291 public static void ReadString(RECORD1 R[], int R$, byte x[][], int x$)
293 RECORD1 rider = R[R$];
294 RandomAccessFile desc = rider.base.desc;
296 int i = 0;
297 try
299 rider.base.SetActive(rider);
300 int ch;
301 do {
302 ch = x[x$][i] = (byte) desc.read();
303 i += 1;
304 } while(ch != 0);
305 rider.position += i;
307 catch(IOException e)
309 x[x$][i] = 0;
310 rider.res[0] = 1;
311 rider.eof[0] = true;
315 public static void ReadSet(RECORD1 R[], int R$, int x[], int x$)
317 ReadLInt(R, R$, x, x$);
320 public static void ReadBool(RECORD1 R[], int R$, boolean x[], int x$)
322 RECORD1 rider = R[R$];
323 RandomAccessFile desc = rider.base.desc;
325 try
327 rider.base.SetActive(rider);
328 int i = desc.read();
329 x[x$] = (i != 0);
330 rider.position += 1;
332 catch(IOException e)
334 rider.res[0] = 1;
335 rider.eof[0] = true;
336 }
339 public static void ReadBytes(RECORD1 r[], int r$, byte x[][], int x$, int n)
341 RECORD1 rider = r[r$];
342 RandomAccessFile desc = rider.base.desc;
344 try
346 rider.base.SetActive(rider);
347 rider.res[0] = desc.read(x[x$], 0, n);
348 rider.eof[0] = (rider.res[0] != 0);
349 rider.position += n - rider.res[0];
351 catch(IOException e)
353 e.printStackTrace();
354 rider.res[0] = n;
355 rider.eof[0] = true;
359 public static void Write(RECORD1 r[], int r$, byte x)
361 RECORD1 rider = r[r$];
362 RandomAccessFile desc = rider.base.desc;
364 try
366 rider.base.SetActive(rider);
367 desc.write(x);
368 rider.position += 1;
370 catch(IOException e)
372 rider.res[0] = 1;
373 rider.eof[0] = true;
377 public static void WriteInt(RECORD1 R[], int R$, short x)
379 byte[][] i = new byte[1][2];
380 i[0][0] = (byte) ((x >>> 8) & 0xff);
381 i[0][1] = (byte) ((x) & 0xff);
382 WriteBytes(R, R$, i, 0, 2);
385 public static void WriteLInt(RECORD1 R[], int R$, int x)
387 byte[][] i = new byte[1][4];
388 i[0][0] = (byte) ((x >>> 24) & 0xff);
389 i[0][1] = (byte) ((x >>> 16) & 0xff);
390 i[0][2] = (byte) ((x >>> 8) & 0xff);
391 i[0][3] = (byte) ((x) & 0xff);
392 WriteBytes(R, R$, i, 0, 4);
395 public static void WriteReal(RECORD1 R[], int R$, float x)
397 SYSTEM.TRAP(-3);
400 public static void WriteLReal(RECORD1 R[], int R$, double x)
402 SYSTEM.TRAP(-3);
405 public static void WriteNum(RECORD1 R[], int R$, int x)
407 SYSTEM.TRAP(-3);
410 public static void WriteString(RECORD1 R[], int R$, byte x[])
412 byte[][] i = new byte[1][];
413 i[0] = x;
414 WriteBytes(R, R$, i, 0, SYSTEM.LEN(x));
417 public static void WriteSet(RECORD1 R[], int R$, int x)
419 SYSTEM.TRAP(-3);
422 public static void WriteBool(RECORD1 R[], int R$, boolean x)
424 SYSTEM.TRAP(-3);
427 public static void WriteBytes(RECORD1 r[], int r$, byte[][] x, int x$, int n)
429 RECORD1 rider = r[r$];
430 RandomAccessFile desc = rider.base.desc;
432 try
434 rider.base.SetActive(rider);
435 desc.write(x[x$], 0, n);
436 rider.position += n;
438 catch(IOException e)
440 rider.res[0] = n;
441 rider.eof[0] = true;
445 public static void BEGIN()