import java.io.*; import java.util.*; class Files { /* File / Handle */ public static class RECORD0 { String fname; File f, fr; public static void $COPY$(RECORD0 src, RECORD0 dst) { dst.fname = src.fname; dst.f = src.f; dst.fr = src.fr; } } /* Rider */ public static class RECORD1 { RandomAccessFile raf = null; RECORD0 base = new RECORD0(); public boolean[] eof = new boolean[1]; public int[] res = new int[1]; public static void $COPY$(RECORD1 src, RECORD1 dst) throws FileNotFoundException { if(src.base == null || src.base.f == null) { dst.raf = null; } else { dst.raf = new RandomAccessFile(src.base.f, "rw"); } dst.base = src.base; dst.eof[0] = src.eof[0]; dst.res[0] = src.res[0]; } } public static RECORD0 Old(byte name[]) { String fname = SYSTEM.STRING(name); File f = new File(fname); if(!f.exists()) { System.out.println("Files.Old: " + fname + " not exists / not a file"); return null; } RECORD0 obfile = new RECORD0(); obfile.fname = fname; obfile.f = f; obfile.fr = f; return obfile; } private static String RandomString(int len) { Random x = new Random(); String s = new String(); for(int i = 0; i < len; i++) { s += (char) (x.nextInt(25) + 65); } return s; } public static RECORD0 New(byte name[]) { String fname = SYSTEM.STRING(name); String tempname = RandomString(8); File f, fr; try { fr = new File(fname); f = File.createTempFile(tempname, "tmp"); } catch(IOException e) { return null; } RECORD0 obfile = new RECORD0(); obfile.fname = fname; obfile.f = f; return obfile; } public static void Register(RECORD0 f) { f.f.renameTo(f.fr); } public static void Close(RECORD0 f) { } public static void Purge(RECORD0 f) throws IOException { f.f.delete(); f.f.createNewFile(); } public static void Delete(byte name[], short res[], int res$) { String fname = SYSTEM.STRING(name); File f = new File(fname); res[res$] = f.delete() ? ((short)0) : ((short)1); } public static void Rename(byte old[], byte _new[], short res[], int res$) { String foname = SYSTEM.STRING(old); String fnname = SYSTEM.STRING(_new); File fo = new File(foname); File fn = new File(fnname); res[res$] = fo.renameTo(fn) ? ((short)0) : ((short)1); } public static int Length(RECORD0 f) { return (int) f.f.length(); } public static void GetDate(RECORD0 f, int t[], int t$, int d[], int d$) { long time = f.f.lastModified(); Calendar cal = Calendar.getInstance(); cal.setTimeInMillis(time); t[t$] = cal.get(Calendar.HOUR_OF_DAY) * 4096 + cal.get(Calendar.MINUTE) * 64 + cal.get(Calendar.SECOND); d[d$] = cal.get(Calendar.YEAR) * 512 + cal.get(Calendar.MONTH) * 32 + cal.get(Calendar.DAY_OF_WEEK); } public static void Set(RECORD1 r[], int r$, RECORD0 f, int pos) throws IOException { int len = Length(f); pos = (pos > len) ? (len) : ((pos < 0) ? (0) : (pos)); r[r$].eof[0] = true; r[r$].base = f; r[r$].raf = new RandomAccessFile(f.f, "rw"); r[r$].raf.seek(pos); r[r$].eof[0] = false; } public static int Pos(RECORD1 r[], int r$) throws IOException { return (int) r[r$].raf.getFilePointer(); } public static RECORD0 Base(RECORD1 r[], int r$) { return r[r$].base; } public static void Read(RECORD1 r[], int r$, byte x[], int x$) { try { x[x$] = (byte) r[r$].raf.read(); } catch(IOException e) { r[r$].res[0] = 1; r[r$].eof[0] = true; } } public static void ReadInt(RECORD1 R[], int R$, short x[], int x$) { try { byte[] buf = new byte[2]; R[R$].res[0] = R[R$].raf.read(buf, 0, 2); R[R$].eof[0] = (R[R$].res[0] != 0); x[x$] = (short) (((buf[1] & 0xff) << 8) | (buf[0] & 0xff)); } catch(IOException e) { R[R$].res[0] = 2; R[R$].eof[0] = true; } } public static void ReadLInt(RECORD1 R[], int R$, int x[], int x$) { try { byte[] buf = new byte[4]; R[R$].res[0] = R[R$].raf.read(buf, 0, 4); R[R$].eof[0] = (R[R$].res[0] != 0); x[x$] = ((buf[3] & 0xff) << 24) | ((buf[2] & 0xff) << 16) | ((buf[1] & 0xff) << 8) | (buf[0] & 0xff); } catch(IOException e) { R[R$].res[0] = 4; R[R$].eof[0] = true; } } public static void ReadReal(RECORD1 R[], int R$, float x[], int x$) { try { x[x$] = R[R$].raf.readFloat(); } catch(IOException e) { R[R$].res[0] = 4; R[R$].eof[0] = true; } } public static void ReadLReal(RECORD1 R[], int R$, double x[], int x$) { try { x[x$] = R[R$].raf.readDouble(); } catch(IOException e) { R[R$].res[0] = 8; R[R$].eof[0] = true; } } public static void ReadNum(RECORD1 R[], int R$, int x[], int x$) { SYSTEM.TRAP(-3); } public static void ReadString(RECORD1 R[], int R$, byte x[][], int x$) { int i = 0; try { int ch; do { ch = x[x$][i] = (byte) R[R$].raf.read(); i += 1; } while(ch != 0); } catch(IOException e) { x[x$][i] = 0; R[R$].res[0] = 1; R[R$].eof[0] = true; } } public static void ReadSet(RECORD1 R[], int R$, int x[], int x$) { ReadLInt(R, R$, x, x$); } public static void ReadBool(RECORD1 R[], int R$, boolean x[], int x$) { try { int i = R[R$].raf.read(); x[x$] = (i != 0); } catch(IOException e) { R[R$].res[0] = 1; R[R$].eof[0] = true; } } public static void ReadBytes(RECORD1 r[], int r$, byte x[][], int x$, int n) { try { r[r$].res[0] = r[r$].raf.read(x[x$], 0, n); r[r$].eof[0] = (r[r$].res[0] != 0); } catch(IOException e) { r[r$].res[0] = n; r[r$].eof[0] = true; } } public static void Write(RECORD1 r[], int r$, byte x) { SYSTEM.TRAP(-3); } public static void WriteInt(RECORD1 R[], int R$, short x) { SYSTEM.TRAP(-3); } public static void WriteLInt(RECORD1 R[], int R$, int x) { SYSTEM.TRAP(-3); } public static void WriteReal(RECORD1 R[], int R$, float x) { SYSTEM.TRAP(-3); } public static void WriteLReal(RECORD1 R[], int R$, double x) { SYSTEM.TRAP(-3); } public static void WriteNum(RECORD1 R[], int R$, int x) { SYSTEM.TRAP(-3); } public static void WriteString(RECORD1 R[], int R$, byte x[]) { SYSTEM.TRAP(-3); } public static void WriteSet(RECORD1 R[], int R$, int x) { SYSTEM.TRAP(-3); } public static void WriteBool(RECORD1 R[], int R$, boolean x) { SYSTEM.TRAP(-3); } public static void WriteBytes(RECORD1 r[], int r$, byte[][] x, int x$, int n) { SYSTEM.TRAP(-3); } public static void BEGIN() { } }