DEADSOFTWARE

Исправлена процедура Files.Register и реализована процедура Files.Write. Исправлен...
[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 filepath = File.createTempFile(SYSTEM.STRING(name), "tmp");
103 fileregpath = new File(SYSTEM.STRING(name));
104 filedesc = new RandomAccessFile(filepath, "rw");
106 catch(IOException e)
108 return null;
111 file = new RECORD0();
112 file.path = filepath;
113 file.regpath = fileregpath.getAbsoluteFile();
114 file.desc = filedesc;
115 return file;
118 public static void Register(RECORD0 f)
119 throws
120 IOException
122 java.nio.file.Files.move(
123 java.nio.file.Paths.get(f.path.getPath()),
124 java.nio.file.Paths.get(f.regpath.getPath()),
125 java.nio.file.StandardCopyOption.REPLACE_EXISTING
126 );
127 f.path = f.regpath;
130 public static void Close(RECORD0 f)
134 public static void Purge(RECORD0 f)
135 throws
136 IOException
138 f.desc.setLength(0);
141 public static void Delete(byte name[], short res[], int res$)
143 File filepath = new File(SYSTEM.STRING(name));
144 res[res$] = (short) (filepath.delete() ? (0) : (1));
147 public static void Rename(byte old[], byte _new[], short res[], int res$)
149 File oldpath = new File(SYSTEM.STRING(old));
150 File newpath = new File(SYSTEM.STRING(_new));
151 res[res$] = (short) (oldpath.renameTo(newpath) ? (0) : (1));
154 public static int Length(RECORD0 f)
155 throws
156 IOException
158 return (int) f.desc.length();
161 public static void GetDate(RECORD0 f, int t[], int t$, int d[], int d$)
163 long time = f.path.lastModified();
164 Calendar cal = Calendar.getInstance();
165 cal.setTimeInMillis(time);
166 t[t$] = cal.get(Calendar.HOUR_OF_DAY) * 4096 + cal.get(Calendar.MINUTE) * 64 + cal.get(Calendar.SECOND);
167 d[d$] = cal.get(Calendar.YEAR) * 512 + cal.get(Calendar.MONTH) * 32 + cal.get(Calendar.DAY_OF_WEEK);
170 public static void Set(RECORD1 r[], int r$, RECORD0 f, int pos)
172 RECORD1 rider = r[r$];
173 rider.base = f;
174 rider.position = pos;
175 rider.res[0] = 0;
176 rider.eof[0] = false;
179 public static int Pos(RECORD1 r[], int r$)
181 return (int) r[r$].position;
184 public static RECORD0 Base(RECORD1 r[], int r$)
186 return r[r$].base;
189 public static void Read(RECORD1 r[], int r$, byte x[], int x$)
191 RECORD1 rider = r[r$];
192 RandomAccessFile desc = rider.base.desc;
194 try
196 rider.base.SetActive(rider);
197 x[x$] = (byte) desc.read();
198 rider.position += 1;
200 catch(IOException e)
202 rider.res[0] = 1;
203 rider.eof[0] = true;
207 public static void ReadInt(RECORD1 R[], int R$, short x[], int x$)
209 RECORD1 rider = R[R$];
210 RandomAccessFile desc = rider.base.desc;
212 try
214 rider.base.SetActive(rider);
215 byte[] buf = new byte[2];
216 rider.res[0] = desc.read(buf, 0, 2);
217 rider.eof[0] = (rider.res[0] != 0);
218 x[x$] = (short) (((buf[1] & 0xff) << 8) | (buf[0] & 0xff));
219 rider.position += 2 - rider.res[0];
221 catch(IOException e)
223 rider.res[0] = 2;
224 rider.eof[0] = true;
228 public static void ReadLInt(RECORD1 R[], int R$, int x[], int x$)
230 RECORD1 rider = R[R$];
231 RandomAccessFile desc = rider.base.desc;
233 try
235 rider.base.SetActive(rider);
236 byte[] buf = new byte[4];
237 rider.res[0] = desc.read(buf, 0, 4);
238 rider.eof[0] = (rider.res[0] != 0);
239 x[x$] = ((buf[3] & 0xff) << 24) | ((buf[2] & 0xff) << 16) | ((buf[1] & 0xff) << 8) | (buf[0] & 0xff);
240 rider.position += 4 - rider.res[0];
242 catch(IOException e)
244 rider.res[0] = 4;
245 rider.eof[0] = true;
249 public static void ReadReal(RECORD1 R[], int R$, float x[], int x$)
251 RECORD1 rider = R[R$];
252 RandomAccessFile desc = rider.base.desc;
254 try
256 rider.base.SetActive(rider);
257 x[x$] = desc.readFloat();
258 rider.position += 4;
260 catch(IOException e)
262 rider.res[0] = 4;
263 rider.eof[0] = true;
267 public static void ReadLReal(RECORD1 R[], int R$, double x[], int x$)
269 RECORD1 rider = R[R$];
270 RandomAccessFile desc = rider.base.desc;
272 try
274 rider.base.SetActive(rider);
275 x[x$] = desc.readDouble();
276 rider.position += 8;
278 catch(IOException e)
280 rider.res[0] = 8;
281 rider.eof[0] = true;
285 public static void ReadNum(RECORD1 R[], int R$, int x[], int x$)
287 SYSTEM.TRAP(-3);
290 public static void ReadString(RECORD1 R[], int R$, byte x[][], int x$)
292 RECORD1 rider = R[R$];
293 RandomAccessFile desc = rider.base.desc;
295 int i = 0;
296 try
298 rider.base.SetActive(rider);
299 int ch;
300 do {
301 ch = x[x$][i] = (byte) desc.read();
302 i += 1;
303 } while(ch != 0);
304 rider.position += i;
306 catch(IOException e)
308 x[x$][i] = 0;
309 rider.res[0] = 1;
310 rider.eof[0] = true;
314 public static void ReadSet(RECORD1 R[], int R$, int x[], int x$)
316 ReadLInt(R, R$, x, x$);
319 public static void ReadBool(RECORD1 R[], int R$, boolean x[], int x$)
321 RECORD1 rider = R[R$];
322 RandomAccessFile desc = rider.base.desc;
324 try
326 rider.base.SetActive(rider);
327 int i = desc.read();
328 x[x$] = (i != 0);
329 rider.position += 1;
331 catch(IOException e)
333 rider.res[0] = 1;
334 rider.eof[0] = true;
335 }
338 public static void ReadBytes(RECORD1 r[], int r$, byte x[][], int x$, int n)
340 RECORD1 rider = r[r$];
341 RandomAccessFile desc = rider.base.desc;
343 try
345 rider.base.SetActive(rider);
346 rider.res[0] = desc.read(x[x$], 0, n);
347 rider.eof[0] = (rider.res[0] != 0);
348 rider.position += n - rider.res[0];
350 catch(IOException e)
352 e.printStackTrace();
353 rider.res[0] = n;
354 rider.eof[0] = true;
358 public static void Write(RECORD1 r[], int r$, byte x)
360 RECORD1 rider = r[r$];
361 RandomAccessFile desc = rider.base.desc;
363 try
365 rider.base.SetActive(rider);
366 desc.write(x);
367 rider.position += 1;
369 catch(IOException e)
371 rider.res[0] = 1;
372 rider.eof[0] = true;
376 public static void WriteInt(RECORD1 R[], int R$, short x)
378 SYSTEM.TRAP(-3);
381 public static void WriteLInt(RECORD1 R[], int R$, int x)
383 SYSTEM.TRAP(-3);
386 public static void WriteReal(RECORD1 R[], int R$, float x)
388 SYSTEM.TRAP(-3);
391 public static void WriteLReal(RECORD1 R[], int R$, double x)
393 SYSTEM.TRAP(-3);
396 public static void WriteNum(RECORD1 R[], int R$, int x)
398 SYSTEM.TRAP(-3);
401 public static void WriteString(RECORD1 R[], int R$, byte x[])
403 SYSTEM.TRAP(-3);
406 public static void WriteSet(RECORD1 R[], int R$, int x)
408 SYSTEM.TRAP(-3);
411 public static void WriteBool(RECORD1 R[], int R$, boolean x)
413 SYSTEM.TRAP(-3);
416 public static void WriteBytes(RECORD1 r[], int r$, byte[][] x, int x$, int n)
418 SYSTEM.TRAP(-3);
421 public static void BEGIN()