import java.io.*; import java.util.*; class Files { /* File / Handle */ public static class RECORD0 { File path; File regpath; RandomAccessFile desc; public static void $COPY$(RECORD0 dst, RECORD0 src) { dst.path = src.path; dst.regpath = src.regpath; dst.desc = src.desc; } void SetActive(RECORD1 rider) throws IOException { desc.seek(rider.position); } } /* Rider */ public static class RECORD1 { long position; RECORD0 base; public boolean[] eof = new boolean[1]; public int[] res = new int[1]; public static void $COPY$(RECORD1 dst, RECORD1 src) { dst.base = src.base; dst.position = src.position; dst.eof[0] = src.eof[0]; dst.res[0] = src.res[0]; } } public static RECORD0 Old(byte name[]) { File filepath; RandomAccessFile filedesc; RECORD0 file; filepath = new File(SYSTEM.STRING(name)); if(!filepath.exists() || !filepath.isFile()) { return null; } try { filedesc = new RandomAccessFile(filepath, "rw"); } catch(IOException e) { try { filedesc = new RandomAccessFile(filepath, "r"); } catch(IOException g) { return null; } } file = new RECORD0(); file.path = filepath; file.regpath = filepath; file.desc = filedesc; return file; } 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[]) { RandomAccessFile filedesc; File filepath; File fileregpath; String filename; RECORD0 file; try { String s = (name[0] == 0) ? (".TMP") : SYSTEM.STRING(name); filepath = File.createTempFile(s, "tmp"); fileregpath = new File(s); filedesc = new RandomAccessFile(filepath, "rw"); } catch(IOException e) { return null; } file = new RECORD0(); file.path = filepath; file.regpath = fileregpath.getAbsoluteFile(); file.desc = filedesc; return file; } public static void Register(RECORD0 f) throws IOException { java.nio.file.Files.move( java.nio.file.Paths.get(f.path.getPath()), java.nio.file.Paths.get(f.regpath.getPath()), java.nio.file.StandardCopyOption.REPLACE_EXISTING ); f.path = f.regpath; } public static void Close(RECORD0 f) { } public static void Purge(RECORD0 f) throws IOException { f.desc.setLength(0); } public static void Delete(byte name[], short res[], int res$) { File filepath = new File(SYSTEM.STRING(name)); res[res$] = (short) (filepath.delete() ? (0) : (1)); } public static void Rename(byte old[], byte _new[], short res[], int res$) { File oldpath = new File(SYSTEM.STRING(old)); File newpath = new File(SYSTEM.STRING(_new)); res[res$] = (short) (oldpath.renameTo(newpath) ? (0) : (1)); } public static int Length(RECORD0 f) throws IOException { return (int) f.desc.length(); } public static void GetDate(RECORD0 f, int t[], int t$, int d[], int d$) { long time = f.path.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) { RECORD1 rider = r[r$]; rider.base = f; rider.position = pos; rider.res[0] = 0; rider.eof[0] = false; } public static int Pos(RECORD1 r[], int r$) { return (int) r[r$].position; } public static RECORD0 Base(RECORD1 r[], int r$) { return r[r$].base; } public static void Read(RECORD1 r[], int r$, byte x[], int x$) { RECORD1 rider = r[r$]; RandomAccessFile desc = rider.base.desc; try { rider.base.SetActive(rider); x[x$] = (byte) desc.read(); rider.position += 1; } catch(IOException e) { rider.res[0] = 1; rider.eof[0] = true; } } public static void ReadInt(RECORD1 R[], int R$, short x[], int x$) { byte[][] buf = new byte[1][2]; ReadBytes(R, R$, buf, 0, 2); x[x$] = (short) ((buf[0][1] & 0xff) << 8); x[x$] |= (short) (buf[0][0] & 0xff); } public static void ReadLInt(RECORD1 R[], int R$, int x[], int x$) { byte[][] buf = new byte[1][4]; ReadBytes(R, R$, buf, 0, 4); x[x$] = (int) ((buf[0][3] & 0xff) << 24); x[x$] |= (int) ((buf[0][2] & 0xff) << 16); x[x$] |= (int) ((buf[0][1] & 0xff) << 8); x[x$] |= (int) ((buf[0][0] & 0xff)); } public static void ReadHInt(RECORD1 R[], int R$, long x[], int x$) { byte[][] buf = new byte[1][8]; ReadBytes(R, R$, buf, 0, 8); x[x$] = (buf[0][7] & 0xff) << 56; x[x$] |= (buf[0][6] & 0xff) << 48; x[x$] |= (buf[0][5] & 0xff) << 40; x[x$] |= (buf[0][4] & 0xff) << 32; x[x$] |= (buf[0][3] & 0xff) << 24; x[x$] |= (buf[0][2] & 0xff) << 16; x[x$] |= (buf[0][1] & 0xff) << 8; x[x$] |= (buf[0][0] & 0xff); } public static void ReadReal(RECORD1 R[], int R$, float x[], int x$) { int[] i = new int[1]; ReadLInt(R, R$, i, 0); x[x$] = Float.intBitsToFloat(i[0]); } public static void ReadLReal(RECORD1 R[], int R$, double x[], int x$) { long[] i = new long[1]; ReadHInt(R, R$, i, 0); x[x$] = Double.longBitsToDouble(i[0]); } 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$) { RECORD1 rider = R[R$]; RandomAccessFile desc = rider.base.desc; int i = 0; try { rider.base.SetActive(rider); int ch; do { ch = x[x$][i] = (byte) desc.read(); i += 1; } while(ch != 0); rider.position += i; } catch(IOException e) { x[x$][i] = 0; rider.res[0] = 1; rider.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$) { byte[] i = new byte[1]; Read(R, R$, i, 0); x[x$] = (i[0] != 0); } public static void ReadBytes(RECORD1 r[], int r$, byte x[][], int x$, int n) { RECORD1 rider = r[r$]; RandomAccessFile desc = rider.base.desc; try { rider.base.SetActive(rider); int readed = desc.read(x[x$], 0, n); rider.res[0] = (readed >= 0) ? (n - readed) : (0); rider.eof[0] = (rider.res[0] != 0); rider.position += (readed >= 0) ? (readed) : (0); } catch(IOException e) { e.printStackTrace(); rider.res[0] = n; rider.eof[0] = true; } } public static void Write(RECORD1 r[], int r$, byte x) { RECORD1 rider = r[r$]; RandomAccessFile desc = rider.base.desc; try { rider.base.SetActive(rider); desc.write(x); rider.position += 1; } catch(IOException e) { rider.res[0] = 1; rider.eof[0] = true; } } public static void WriteInt(RECORD1 R[], int R$, short x) { byte[][] i = new byte[1][2]; i[0][1] = (byte) ((x >>> 8) & 0xff); i[0][0] = (byte) ((x) & 0xff); WriteBytes(R, R$, i, 0, 2); } public static void WriteLInt(RECORD1 R[], int R$, int x) { byte[][] i = new byte[1][4]; i[0][3] = (byte) ((x >>> 24) & 0xff); i[0][2] = (byte) ((x >>> 16) & 0xff); i[0][1] = (byte) ((x >>> 8) & 0xff); i[0][0] = (byte) ((x) & 0xff); WriteBytes(R, R$, i, 0, 4); } public static void WriteHInt(RECORD1 R[], int R$, long x) { byte[][] i = new byte[1][8]; i[0][7] = (byte) ((x >>> 56) & 0xff); i[0][6] = (byte) ((x >>> 48) & 0xff); i[0][5] = (byte) ((x >>> 40) & 0xff); i[0][4] = (byte) ((x >>> 32) & 0xff); i[0][3] = (byte) ((x >>> 24) & 0xff); i[0][2] = (byte) ((x >>> 16) & 0xff); i[0][1] = (byte) ((x >>> 8) & 0xff); i[0][0] = (byte) ((x) & 0xff); WriteBytes(R, R$, i, 0, 8); } public static void WriteReal(RECORD1 R[], int R$, float x) { WriteLInt(R, R$, Float.floatToRawIntBits(x)); } public static void WriteLReal(RECORD1 R[], int R$, double x) { WriteHInt(R, R$, Double.doubleToRawLongBits(x)); } public static void WriteNum(RECORD1 R[], int R$, int x) { SYSTEM.TRAP(-3); } public static void WriteString(RECORD1 R[], int R$, byte x[]) { byte[][] i = new byte[1][]; i[0] = x; WriteBytes(R, R$, i, 0, SYSTEM.LEN(x) + 1); } public static void WriteSet(RECORD1 R[], int R$, int x) { WriteLInt(R, R$, x); } public static void WriteBool(RECORD1 R[], int R$, boolean x) { if(x) { Write(R, R$, (byte) 1); } else { Write(R, R$, (byte) 0); } } public static void WriteBytes(RECORD1 r[], int r$, byte[][] x, int x$, int n) { RECORD1 rider = r[r$]; RandomAccessFile desc = rider.base.desc; try { rider.base.SetActive(rider); desc.write(x[x$], 0, n); rider.position += n; } catch(IOException e) { rider.res[0] = n; rider.eof[0] = true; } } public static void BEGIN() { } }