DEADSOFTWARE

Слегка перепилена реализация модуля Files
authorDeaDDooMER <deaddoomer@deadsoftware.ru>
Fri, 18 Aug 2017 18:36:14 +0000 (21:36 +0300)
committerDeaDDooMER <deaddoomer@deadsoftware.ru>
Fri, 18 Aug 2017 18:36:14 +0000 (21:36 +0300)
rtl/Files.java

index b557fe87a85c5185d510eb801e6c969448d2b1d2..5fe0074ca6293714b95895c070a78113ee14fcbe 100644 (file)
@@ -6,60 +6,76 @@ class Files
        /* File / Handle */
        public static class RECORD0
        {
-               String fname;
-               File f, fr;
+               File path;
+               File regpath;
+               RandomAccessFile desc;
 
-               public static void $COPY$(RECORD0 src, RECORD0 dst)
+               public static void $COPY$(RECORD0 dst, RECORD0 src)
                {
-                       dst.fname = src.fname;
-                       dst.f = src.f;
-                       dst.fr = src.fr;
+                       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
        {
-               RandomAccessFile raf = null;
-               RECORD0 base = new RECORD0();
-
+               long position;
+               RECORD0 base;
                public boolean[] eof = new boolean[1];
                public int[] res = new int[1];
 
-               public static void $COPY$(RECORD1 src, RECORD1 dst)
-                       throws
-                               FileNotFoundException
+               public static void $COPY$(RECORD1 dst, RECORD1 src)
                {
-                       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];
+                       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[])
        {
-               String fname = SYSTEM.STRING(name);
-               File f = new File(fname);
+               File filepath;
+               RandomAccessFile filedesc;
+               RECORD0 file;
 
-               if(!f.exists())
+               filepath = new File(SYSTEM.STRING(name));
+
+               if(!filepath.exists() || !filepath.isFile())
                {
-                       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;
+               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)
@@ -75,29 +91,34 @@ class Files
 
        public static RECORD0 New(byte name[])
        {
-               String fname = SYSTEM.STRING(name);
-               String tempname = RandomString(8);
-               File f, fr;
+               RandomAccessFile filedesc;
+               File filepath;
+               File fileregpath;
+               String filename;
+               RECORD0 file;
 
                try
                {
-                       fr = new File(fname);
-                       f = File.createTempFile(tempname, "tmp");
+                       filepath = File.createTempFile(RandomString(8), "tmp");
+                       fileregpath = new File(SYSTEM.STRING(name));
+                       filedesc = new RandomAccessFile(filepath, "rw");
                }
                catch(IOException e)
                {
                        return null;
                }
 
-               RECORD0 obfile = new RECORD0();
-               obfile.fname = fname;
-               obfile.f = f;
-               return obfile;
+               file = new RECORD0();
+               file.path = filepath;
+               file.regpath = fileregpath;
+               file.desc = filedesc;
+               return file;
        }
 
        public static void Register(RECORD0 f)
        {
-               f.f.renameTo(f.fr);
+               f.path.renameTo(f.regpath);
+               f.path = f.regpath;
        }
 
        public static void Close(RECORD0 f)
@@ -108,34 +129,32 @@ class Files
                throws
                        IOException
        {
-               f.f.delete();
-               f.f.createNewFile();
+               f.desc.setLength(0);
        }
 
        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);
+               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$)
        {
-               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);
+               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.f.length();
+               return (int) f.desc.length();
        }
 
        public static void GetDate(RECORD0 f, int t[], int t$, int d[], int d$)
        {
-               long time = f.f.lastModified();
+               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);
@@ -143,24 +162,17 @@ class Files
        }
 
        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;
+               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$)
-               throws
-                       IOException
        {
-               return (int) r[r$].raf.getFilePointer();
+               return (int) r[r$].position;
        }
 
        public static RECORD0 Base(RECORD1 r[], int r$)
@@ -170,72 +182,97 @@ class Files
 
        public static void Read(RECORD1 r[], int r$, byte x[], int x$)
        {
+               RECORD1 rider = r[r$];
+               RandomAccessFile desc = rider.base.desc;
+
                try
                {
-                       x[x$] = (byte) r[r$].raf.read();
+                       rider.base.SetActive(rider);
+                       x[x$] = (byte) desc.read();
+                       rider.position += 1;
                }
                catch(IOException e)
                {
-                       r[r$].res[0] = 1;
-                       r[r$].eof[0] = true;
+                       rider.res[0] = 1;
+                       rider.eof[0] = true;
                }
        }
 
        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];
-                       R[R$].res[0] = R[R$].raf.read(buf, 0, 2);
-                       R[R$].eof[0] = (R[R$].res[0] != 0);
+                       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)
                {
-                       R[R$].res[0] = 2;
-                       R[R$].eof[0] = true;
+                       rider.res[0] = 2;
+                       rider.eof[0] = true;
                }
        }
 
        public static void ReadLInt(RECORD1 R[], int R$, int x[], int x$)
        {
+               RECORD1 rider = R[R$];
+               RandomAccessFile desc = rider.base.desc;
+
                try
                {
+                       rider.base.SetActive(rider);
                        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);
+                       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)
                {
-                       R[R$].res[0] = 4;
-                       R[R$].eof[0] = true;
+                       rider.res[0] = 4;
+                       rider.eof[0] = true;
                }
        }
 
        public static void ReadReal(RECORD1 R[], int R$, float x[], int x$)
        {
+               RECORD1 rider = R[R$];
+               RandomAccessFile desc = rider.base.desc;
+
                try
                {
-                       x[x$] = R[R$].raf.readFloat();
+                       rider.base.SetActive(rider);
+                       x[x$] = desc.readFloat();
+                       rider.position += 4;
                }
                catch(IOException e)
                {
-                       R[R$].res[0] = 4;
-                       R[R$].eof[0] = true;
+                       rider.res[0] = 4;
+                       rider.eof[0] = true;
                }
        }
 
        public static void ReadLReal(RECORD1 R[], int R$, double x[], int x$)
        {
+               RECORD1 rider = R[R$];
+               RandomAccessFile desc = rider.base.desc;
+
                try
                {
-                       x[x$] = R[R$].raf.readDouble();
+                       rider.base.SetActive(rider);
+                       x[x$] = desc.readDouble();
+                       rider.position += 8;
                }
                catch(IOException e)
                {
-                       R[R$].res[0] = 8;
-                       R[R$].eof[0] = true;
+                       rider.res[0] = 8;
+                       rider.eof[0] = true;
                }
        }
 
@@ -246,20 +283,25 @@ class Files
 
        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) R[R$].raf.read();
+                               ch = x[x$][i] = (byte) desc.read();
                                i += 1;
                        } while(ch != 0);
+                       rider.position += i;
                }
                catch(IOException e)
                {
                        x[x$][i] = 0;
-                       R[R$].res[0] = 1;
-                       R[R$].eof[0] = true;
+                       rider.res[0] = 1;
+                       rider.eof[0] = true;
                }
        }
 
@@ -270,29 +312,40 @@ class Files
 
        public static void ReadBool(RECORD1 R[], int R$, boolean x[], int x$)
        {
+               RECORD1 rider = R[R$];
+               RandomAccessFile desc = rider.base.desc;
+
                try
                {
-                       int i = R[R$].raf.read();
+                       rider.base.SetActive(rider);
+                       int i = desc.read();
                        x[x$] = (i != 0);
+                       rider.position += 1;
                }
                catch(IOException e)
                {
-                       R[R$].res[0] = 1;
-                       R[R$].eof[0] = true;
+                       rider.res[0] = 1;
+                       rider.eof[0] = true;
                }               
        }
 
        public static void ReadBytes(RECORD1 r[], int r$, byte x[][], int x$, int n)
        {
+               RECORD1 rider = r[r$];
+               RandomAccessFile desc = rider.base.desc;
+
                try
                {
-                       r[r$].res[0] = r[r$].raf.read(x[x$], 0, n);
-                       r[r$].eof[0] = (r[r$].res[0] != 0);
+                       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];
                }
                catch(IOException e)
                {
-                       r[r$].res[0] = n;
-                       r[r$].eof[0] = true;
+                       e.printStackTrace();
+                       rider.res[0] = n;
+                       rider.eof[0] = true;
                }
        }