DEADSOFTWARE

RTL перенесена в отдельную директорию
[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(RandomString(8), "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;
114 file.desc = filedesc;
115 return file;
118 public static void Register(RECORD0 f)
120 f.path.renameTo(f.regpath);
121 f.path = f.regpath;
124 public static void Close(RECORD0 f)
128 public static void Purge(RECORD0 f)
129 throws
130 IOException
132 f.desc.setLength(0);
135 public static void Delete(byte name[], short res[], int res$)
137 File filepath = new File(SYSTEM.STRING(name));
138 res[res$] = (short) (filepath.delete() ? (0) : (1));
141 public static void Rename(byte old[], byte _new[], short res[], int res$)
143 File oldpath = new File(SYSTEM.STRING(old));
144 File newpath = new File(SYSTEM.STRING(_new));
145 res[res$] = (short) (oldpath.renameTo(newpath) ? (0) : (1));
148 public static int Length(RECORD0 f)
149 throws
150 IOException
152 return (int) f.desc.length();
155 public static void GetDate(RECORD0 f, int t[], int t$, int d[], int d$)
157 long time = f.path.lastModified();
158 Calendar cal = Calendar.getInstance();
159 cal.setTimeInMillis(time);
160 t[t$] = cal.get(Calendar.HOUR_OF_DAY) * 4096 + cal.get(Calendar.MINUTE) * 64 + cal.get(Calendar.SECOND);
161 d[d$] = cal.get(Calendar.YEAR) * 512 + cal.get(Calendar.MONTH) * 32 + cal.get(Calendar.DAY_OF_WEEK);
164 public static void Set(RECORD1 r[], int r$, RECORD0 f, int pos)
166 RECORD1 rider = r[r$];
167 rider.base = f;
168 rider.position = pos;
169 rider.res[0] = 0;
170 rider.eof[0] = false;
173 public static int Pos(RECORD1 r[], int r$)
175 return (int) r[r$].position;
178 public static RECORD0 Base(RECORD1 r[], int r$)
180 return r[r$].base;
183 public static void Read(RECORD1 r[], int r$, byte x[], int x$)
185 RECORD1 rider = r[r$];
186 RandomAccessFile desc = rider.base.desc;
188 try
190 rider.base.SetActive(rider);
191 x[x$] = (byte) desc.read();
192 rider.position += 1;
194 catch(IOException e)
196 rider.res[0] = 1;
197 rider.eof[0] = true;
201 public static void ReadInt(RECORD1 R[], int R$, short x[], int x$)
203 RECORD1 rider = R[R$];
204 RandomAccessFile desc = rider.base.desc;
206 try
208 rider.base.SetActive(rider);
209 byte[] buf = new byte[2];
210 rider.res[0] = desc.read(buf, 0, 2);
211 rider.eof[0] = (rider.res[0] != 0);
212 x[x$] = (short) (((buf[1] & 0xff) << 8) | (buf[0] & 0xff));
213 rider.position += 2 - rider.res[0];
215 catch(IOException e)
217 rider.res[0] = 2;
218 rider.eof[0] = true;
222 public static void ReadLInt(RECORD1 R[], int R$, int x[], int x$)
224 RECORD1 rider = R[R$];
225 RandomAccessFile desc = rider.base.desc;
227 try
229 rider.base.SetActive(rider);
230 byte[] buf = new byte[4];
231 rider.res[0] = desc.read(buf, 0, 4);
232 rider.eof[0] = (rider.res[0] != 0);
233 x[x$] = ((buf[3] & 0xff) << 24) | ((buf[2] & 0xff) << 16) | ((buf[1] & 0xff) << 8) | (buf[0] & 0xff);
234 rider.position += 4 - rider.res[0];
236 catch(IOException e)
238 rider.res[0] = 4;
239 rider.eof[0] = true;
243 public static void ReadReal(RECORD1 R[], int R$, float x[], int x$)
245 RECORD1 rider = R[R$];
246 RandomAccessFile desc = rider.base.desc;
248 try
250 rider.base.SetActive(rider);
251 x[x$] = desc.readFloat();
252 rider.position += 4;
254 catch(IOException e)
256 rider.res[0] = 4;
257 rider.eof[0] = true;
261 public static void ReadLReal(RECORD1 R[], int R$, double x[], int x$)
263 RECORD1 rider = R[R$];
264 RandomAccessFile desc = rider.base.desc;
266 try
268 rider.base.SetActive(rider);
269 x[x$] = desc.readDouble();
270 rider.position += 8;
272 catch(IOException e)
274 rider.res[0] = 8;
275 rider.eof[0] = true;
279 public static void ReadNum(RECORD1 R[], int R$, int x[], int x$)
281 SYSTEM.TRAP(-3);
284 public static void ReadString(RECORD1 R[], int R$, byte x[][], int x$)
286 RECORD1 rider = R[R$];
287 RandomAccessFile desc = rider.base.desc;
289 int i = 0;
290 try
292 rider.base.SetActive(rider);
293 int ch;
294 do {
295 ch = x[x$][i] = (byte) desc.read();
296 i += 1;
297 } while(ch != 0);
298 rider.position += i;
300 catch(IOException e)
302 x[x$][i] = 0;
303 rider.res[0] = 1;
304 rider.eof[0] = true;
308 public static void ReadSet(RECORD1 R[], int R$, int x[], int x$)
310 ReadLInt(R, R$, x, x$);
313 public static void ReadBool(RECORD1 R[], int R$, boolean x[], int x$)
315 RECORD1 rider = R[R$];
316 RandomAccessFile desc = rider.base.desc;
318 try
320 rider.base.SetActive(rider);
321 int i = desc.read();
322 x[x$] = (i != 0);
323 rider.position += 1;
325 catch(IOException e)
327 rider.res[0] = 1;
328 rider.eof[0] = true;
329 }
332 public static void ReadBytes(RECORD1 r[], int r$, byte x[][], int x$, int n)
334 RECORD1 rider = r[r$];
335 RandomAccessFile desc = rider.base.desc;
337 try
339 rider.base.SetActive(rider);
340 rider.res[0] = desc.read(x[x$], 0, n);
341 rider.eof[0] = (rider.res[0] != 0);
342 rider.position += n - rider.res[0];
344 catch(IOException e)
346 e.printStackTrace();
347 rider.res[0] = n;
348 rider.eof[0] = true;
352 public static void Write(RECORD1 r[], int r$, byte x)
354 SYSTEM.TRAP(-3);
357 public static void WriteInt(RECORD1 R[], int R$, short x)
359 SYSTEM.TRAP(-3);
362 public static void WriteLInt(RECORD1 R[], int R$, int x)
364 SYSTEM.TRAP(-3);
367 public static void WriteReal(RECORD1 R[], int R$, float x)
369 SYSTEM.TRAP(-3);
372 public static void WriteLReal(RECORD1 R[], int R$, double x)
374 SYSTEM.TRAP(-3);
377 public static void WriteNum(RECORD1 R[], int R$, int x)
379 SYSTEM.TRAP(-3);
382 public static void WriteString(RECORD1 R[], int R$, byte x[])
384 SYSTEM.TRAP(-3);
387 public static void WriteSet(RECORD1 R[], int R$, int x)
389 SYSTEM.TRAP(-3);
392 public static void WriteBool(RECORD1 R[], int R$, boolean x)
394 SYSTEM.TRAP(-3);
397 public static void WriteBytes(RECORD1 r[], int r$, byte[][] x, int x$, int n)
399 SYSTEM.TRAP(-3);
402 public static void BEGIN()