DEADSOFTWARE

В Files реализовано ещё несколько процедур
[dsw-obn.git] / rtl / java / Files.java
index 5fe0074ca6293714b95895c070a78113ee14fcbe..9cb6aee0731c85ddb3da5ca2a3276ccaec686b16 100644 (file)
@@ -99,8 +99,9 @@ class Files
 
                try
                {
-                       filepath = File.createTempFile(RandomString(8), "tmp");
-                       fileregpath = new File(SYSTEM.STRING(name));
+                       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)
@@ -110,14 +111,20 @@ class Files
 
                file = new RECORD0();
                file.path = filepath;
-               file.regpath = fileregpath;
+               file.regpath = fileregpath.getAbsoluteFile();
                file.desc = filedesc;
                return file;
        }
 
        public static void Register(RECORD0 f)
+               throws
+                       IOException
        {
-               f.path.renameTo(f.regpath);
+               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;
        }
 
@@ -200,80 +207,48 @@ class Files
 
        public static void ReadInt(RECORD1 R[], int R$, short x[], int x$)
        {
-               RECORD1 rider = R[R$];
-               RandomAccessFile desc = rider.base.desc;
-
-               try
-               {
-                       rider.base.SetActive(rider);
-                       byte[] buf = new byte[2];
-                       rider.res[0] = desc.read(buf, 0, 2);
-                       rider.eof[0] = (rider.res[0] != 0);
-                       x[x$] = (short) (((buf[1] & 0xff) << 8) | (buf[0] & 0xff));
-                       rider.position += 2 - rider.res[0];
-               }
-               catch(IOException e)
-               {
-                       rider.res[0] = 2;
-                       rider.eof[0] = true;
-               }
+               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$)
        {
-               RECORD1 rider = R[R$];
-               RandomAccessFile desc = rider.base.desc;
+               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));
+       }
 
-               try
-               {
-                       rider.base.SetActive(rider);
-                       byte[] buf = new byte[4];
-                       rider.res[0] = desc.read(buf, 0, 4);
-                       rider.eof[0] = (rider.res[0] != 0);
-                       x[x$] = ((buf[3] & 0xff) << 24) | ((buf[2] & 0xff) << 16) | ((buf[1] & 0xff) << 8) | (buf[0] & 0xff);
-                       rider.position += 4 - rider.res[0];
-               }
-               catch(IOException e)
-               {
-                       rider.res[0] = 4;
-                       rider.eof[0] = true;
-               }
+       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$)
        {
-               RECORD1 rider = R[R$];
-               RandomAccessFile desc = rider.base.desc;
-
-               try
-               {
-                       rider.base.SetActive(rider);
-                       x[x$] = desc.readFloat();
-                       rider.position += 4;
-               }
-               catch(IOException e)
-               {
-                       rider.res[0] = 4;
-                       rider.eof[0] = true;
-               }
+               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$)
        {
-               RECORD1 rider = R[R$];
-               RandomAccessFile desc = rider.base.desc;
-
-               try
-               {
-                       rider.base.SetActive(rider);
-                       x[x$] = desc.readDouble();
-                       rider.position += 8;
-               }
-               catch(IOException e)
-               {
-                       rider.res[0] = 8;
-                       rider.eof[0] = true;
-               }
+               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$)
@@ -312,24 +287,33 @@ class Files
 
        public static void ReadBool(RECORD1 R[], int R$, boolean x[], int x$)
        {
-               RECORD1 rider = R[R$];
+               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 i = desc.read();
-                       x[x$] = (i != 0);
-                       rider.position += 1;
+                       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)
                {
-                       rider.res[0] = 1;
+                       e.printStackTrace();
+                       rider.res[0] = n;
                        rider.eof[0] = true;
-               }               
+               }
        }
 
-       public static void ReadBytes(RECORD1 r[], int r$, byte x[][], int x$, int n)
+       public static void Write(RECORD1 r[], int r$, byte x)
        {
                RECORD1 rider = r[r$];
                RandomAccessFile desc = rider.base.desc;
@@ -337,41 +321,56 @@ class Files
                try
                {
                        rider.base.SetActive(rider);
-                       rider.res[0] = desc.read(x[x$], 0, n);
-                       rider.eof[0] = (rider.res[0] != 0);
-                       rider.position += n - rider.res[0];
+                       desc.write(x);
+                       rider.position += 1;
                }
                catch(IOException e)
                {
-                       e.printStackTrace();
-                       rider.res[0] = n;
+                       rider.res[0] = 1;
                        rider.eof[0] = true;
                }
        }
 
-       public static void Write(RECORD1 r[], int r$, byte x)
+       public static void WriteInt(RECORD1 R[], int R$, short x)
        {
-               SYSTEM.TRAP(-3);
+               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 WriteInt(RECORD1 R[], int R$, short x)
+       public static void WriteLInt(RECORD1 R[], int R$, int x)
        {
-               SYSTEM.TRAP(-3);
+               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 WriteLInt(RECORD1 R[], int R$, int x)
+       public static void WriteHInt(RECORD1 R[], int R$, long x)
        {
-               SYSTEM.TRAP(-3);
+               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)
        {
-               SYSTEM.TRAP(-3);
+               WriteLInt(R, R$, Float.floatToRawIntBits(x));
        }
 
        public static void WriteLReal(RECORD1 R[], int R$, double x)
        {
-               SYSTEM.TRAP(-3);
+               WriteHInt(R, R$, Double.doubleToRawLongBits(x));
        }
 
        public static void WriteNum(RECORD1 R[], int R$, int x)
@@ -381,22 +380,44 @@ class Files
 
        public static void WriteString(RECORD1 R[], int R$, byte x[])
        {
-               SYSTEM.TRAP(-3);
+               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)
        {
-               SYSTEM.TRAP(-3);
+               WriteLInt(R, R$, x);
        }
 
        public static void WriteBool(RECORD1 R[], int R$, boolean x)
        {
-               SYSTEM.TRAP(-3);
+               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)
        {
-               SYSTEM.TRAP(-3);
+               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()