DEADSOFTWARE

Исправлены VAR-параметры, добавлены модули Files и Strings
[dsw-obn.git] / rtl / Files.java
1 import java.io.*;
2 import java.util.*;
4 class Files
5 {
6 /* File / Handle */
7 public static class RECORD0
8 {
9 String fname;
10 File f, fr;
12 public static void $COPY$(RECORD0 src, RECORD0 dst)
13 {
14 dst.fname = src.fname;
15 dst.f = src.f;
16 dst.fr = src.fr;
17 }
18 }
20 /* Rider */
21 public static class RECORD1
22 {
23 RandomAccessFile raf = null;
24 RECORD0 base = new RECORD0();
26 public boolean[] eof = new boolean[1];
27 public int[] res = new int[1];
29 public static void $COPY$(RECORD1 src, RECORD1 dst)
30 throws
31 FileNotFoundException
32 {
33 if(src.base == null || src.base.f == null)
34 {
35 dst.raf = null;
36 }
37 else
38 {
39 dst.raf = new RandomAccessFile(src.base.f, "rw");
40 }
41 dst.base = src.base;
42 dst.eof[0] = src.eof[0];
43 dst.res[0] = src.res[0];
44 }
45 }
47 public static RECORD0 Old(byte name[])
48 {
49 String fname = SYSTEM.STRING(name);
50 File f = new File(fname);
52 if(!f.exists())
53 {
54 System.out.println("Files.Old: " + fname + " not exists / not a file");
55 return null;
56 }
58 RECORD0 obfile = new RECORD0();
59 obfile.fname = fname;
60 obfile.f = f;
61 obfile.fr = f;
62 return obfile;
63 }
65 private static String RandomString(int len)
66 {
67 Random x = new Random();
68 String s = new String();
69 for(int i = 0; i < len; i++)
70 {
71 s += (char) (x.nextInt(25) + 65);
72 }
73 return s;
74 }
76 public static RECORD0 New(byte name[])
77 {
78 String fname = SYSTEM.STRING(name);
79 String tempname = RandomString(8);
80 File f, fr;
82 try
83 {
84 fr = new File(fname);
85 f = File.createTempFile(tempname, "tmp");
86 }
87 catch(IOException e)
88 {
89 return null;
90 }
92 RECORD0 obfile = new RECORD0();
93 obfile.fname = fname;
94 obfile.f = f;
95 return obfile;
96 }
98 public static void Register(RECORD0 f)
99 {
100 f.f.renameTo(f.fr);
103 public static void Close(RECORD0 f)
107 public static void Purge(RECORD0 f)
108 throws
109 IOException
111 f.f.delete();
112 f.f.createNewFile();
115 public static void Delete(byte name[], short res[], int res$)
117 String fname = SYSTEM.STRING(name);
118 File f = new File(fname);
119 res[res$] = f.delete() ? ((short)0) : ((short)1);
122 public static void Rename(byte old[], byte _new[], short res[], int res$)
124 String foname = SYSTEM.STRING(old);
125 String fnname = SYSTEM.STRING(_new);
126 File fo = new File(foname);
127 File fn = new File(fnname);
128 res[res$] = fo.renameTo(fn) ? ((short)0) : ((short)1);
131 public static int Length(RECORD0 f)
133 return (int) f.f.length();
136 public static void GetDate(RECORD0 f, int t[], int t$, int d[], int d$)
138 long time = f.f.lastModified();
139 Calendar cal = Calendar.getInstance();
140 cal.setTimeInMillis(time);
141 t[t$] = cal.get(Calendar.HOUR_OF_DAY) * 4096 + cal.get(Calendar.MINUTE) * 64 + cal.get(Calendar.SECOND);
142 d[d$] = cal.get(Calendar.YEAR) * 512 + cal.get(Calendar.MONTH) * 32 + cal.get(Calendar.DAY_OF_WEEK);
145 public static void Set(RECORD1 r[], int r$, RECORD0 f, int pos)
146 throws
147 IOException
149 int len = Length(f);
150 pos = (pos > len) ? (len) : ((pos < 0) ? (0) : (pos));
151 r[r$].eof[0] = true;
153 r[r$].base = f;
154 r[r$].raf = new RandomAccessFile(f.f, "rw");
155 r[r$].raf.seek(pos);
156 r[r$].eof[0] = false;
159 public static int Pos(RECORD1 r[], int r$)
160 throws
161 IOException
163 return (int) r[r$].raf.getFilePointer();
166 public static RECORD0 Base(RECORD1 r[], int r$)
168 return r[r$].base;
171 public static void Read(RECORD1 r[], int r$, byte x[], int x$)
173 try
175 x[x$] = (byte) r[r$].raf.read();
177 catch(IOException e)
179 r[r$].res[0] = 1;
180 r[r$].eof[0] = true;
184 public static void ReadInt(RECORD1 R[], int R$, short x[], int x$)
186 try
188 byte[] buf = new byte[2];
189 R[R$].res[0] = R[R$].raf.read(buf, 0, 2);
190 R[R$].eof[0] = (R[R$].res[0] != 0);
191 x[x$] = (short) (((buf[1] & 0xff) << 8) | (buf[0] & 0xff));
193 catch(IOException e)
195 R[R$].res[0] = 2;
196 R[R$].eof[0] = true;
200 public static void ReadLInt(RECORD1 R[], int R$, int x[], int x$)
202 try
204 byte[] buf = new byte[4];
205 R[R$].res[0] = R[R$].raf.read(buf, 0, 4);
206 R[R$].eof[0] = (R[R$].res[0] != 0);
207 x[x$] = ((buf[3] & 0xff) << 24) | ((buf[2] & 0xff) << 16) | ((buf[1] & 0xff) << 8) | (buf[0] & 0xff);
209 catch(IOException e)
211 R[R$].res[0] = 4;
212 R[R$].eof[0] = true;
216 public static void ReadReal(RECORD1 R[], int R$, float x[], int x$)
218 try
220 x[x$] = R[R$].raf.readFloat();
222 catch(IOException e)
224 R[R$].res[0] = 4;
225 R[R$].eof[0] = true;
229 public static void ReadLReal(RECORD1 R[], int R$, double x[], int x$)
231 try
233 x[x$] = R[R$].raf.readDouble();
235 catch(IOException e)
237 R[R$].res[0] = 8;
238 R[R$].eof[0] = true;
242 public static void ReadNum(RECORD1 R[], int R$, int x[], int x$)
244 SYSTEM.TRAP(-3);
247 public static void ReadString(RECORD1 R[], int R$, byte x[][], int x$)
249 int i = 0;
250 try
252 int ch;
253 do {
254 ch = x[x$][i] = (byte) R[R$].raf.read();
255 i += 1;
256 } while(ch != 0);
258 catch(IOException e)
260 x[x$][i] = 0;
261 R[R$].res[0] = 1;
262 R[R$].eof[0] = true;
266 public static void ReadSet(RECORD1 R[], int R$, int x[], int x$)
268 ReadLInt(R, R$, x, x$);
271 public static void ReadBool(RECORD1 R[], int R$, boolean x[], int x$)
273 try
275 int i = R[R$].raf.read();
276 x[x$] = (i != 0);
278 catch(IOException e)
280 R[R$].res[0] = 1;
281 R[R$].eof[0] = true;
282 }
285 public static void ReadBytes(RECORD1 r[], int r$, byte x[][], int x$, int n)
287 try
289 r[r$].res[0] = r[r$].raf.read(x[x$], 0, n);
290 r[r$].eof[0] = (r[r$].res[0] != 0);
292 catch(IOException e)
294 r[r$].res[0] = n;
295 r[r$].eof[0] = true;
299 public static void Write(RECORD1 r[], int r$, byte x)
301 SYSTEM.TRAP(-3);
304 public static void WriteInt(RECORD1 R[], int R$, short x)
306 SYSTEM.TRAP(-3);
309 public static void WriteLInt(RECORD1 R[], int R$, int x)
311 SYSTEM.TRAP(-3);
314 public static void WriteReal(RECORD1 R[], int R$, float x)
316 SYSTEM.TRAP(-3);
319 public static void WriteLReal(RECORD1 R[], int R$, double x)
321 SYSTEM.TRAP(-3);
324 public static void WriteNum(RECORD1 R[], int R$, int x)
326 SYSTEM.TRAP(-3);
329 public static void WriteString(RECORD1 R[], int R$, byte x[])
331 SYSTEM.TRAP(-3);
334 public static void WriteSet(RECORD1 R[], int R$, int x)
336 SYSTEM.TRAP(-3);
339 public static void WriteBool(RECORD1 R[], int R$, boolean x)
341 SYSTEM.TRAP(-3);
344 public static void WriteBytes(RECORD1 r[], int r$, byte[][] x, int x$, int n)
346 SYSTEM.TRAP(-3);
349 public static void BEGIN()