Exemple #1
0
        static FileHandle[] openFiles = new FileHandle[100];   // all file handles in use (0 will not be used at any time)


        private static Primitive OpenWriteImpl(Primitive filename, bool append)
        {
            String f = (filename == null ? "" : filename.ToString());

            if (!f.StartsWith("/"))
            {
                f = "/home/root/lms2012/prjs/" + f;
            }
            lock (openFiles)
            {
                if (findHandle(f) >= 0)       // already have open file with this name
                {
                    return(new Primitive(0));
                }
                int i = findUnusedHandleSlot();
                if (i <= 0)                   // can not hold any more files
                {
                    return(new Primitive(0));
                }

                // create a new empty file. must close handle immediately because it can not be kept accross direct commands
                ByteCodeBuffer c = new ByteCodeBuffer();
                c.OP(0xC0);                   // opFile
                c.CONST(append ? 0x00: 0x02); // OPEN_APPEND = 0x00   OPEN_WRITE = 0x02
                c.STRING(f);
                c.GLOBVAR(0);                 // result: 16-bit handle
                c.OP(0xC0);                   // opFile
                c.CONST(0x07);                // CLOSE = 0x07
                c.GLOBVAR(0);

                // check if could indeed create/append file on the brick
                byte[] reply = EV3RemoteControler.DirectCommand(c, 2, 0);

                if (reply == null || reply.Length < 2 || (reply[0] == 0 && reply[1] == 0))
                {
                    return(new Primitive(0));
                }

                // memorize the open file
                openFiles[i] = new FileHandle(f);

                return(new Primitive((double)i));
            }
        }
Exemple #2
0
        /// <summary>
        /// Read one byte of data from the file.
        /// </summary>
        /// <param name="handle">The file handle (previously obtained from an Open... call)</param>
        /// <returns>The next byte from the file</returns>
        public static Primitive ReadByte(Primitive handle)
        {
            int hdl = handle;

            lock (openFiles)
            {
                if (hdl >= 0 && hdl < openFiles.Length && openFiles[hdl] != null)
                {
                    FileHandle fh = openFiles[hdl];
                    if (fh.content != null && fh.readcursor < fh.content.Length)
                    {
                        byte b = fh.content[fh.readcursor];
                        fh.readcursor++;
                        return(new Primitive((int)b));
                    }
                }
            }
            return(new Primitive(0));
        }
Exemple #3
0
        /// <summary>
        /// Open a file for reading data. When the file does not exist, a 0 is returned.
        /// </summary>
        /// <param name="filename">Name of the file to read from</param>
        /// <returns>A number that identifies this open file (a.k.a. file handle) or 0 if file does not exist</returns>
        public static Primitive OpenRead(Primitive filename)
        {
            String f = (filename == null ? "" : filename.ToString());

            if (!f.StartsWith("/"))
            {
                f = "/home/root/lms2012/prjs/" + f;
            }
            lock (openFiles)
            {
                if (findHandle(f) >= 0)       // already have open file with this name
                {
                    return(new Primitive(0));
                }
                int i = findUnusedHandleSlot();
                if (i <= 0)                   // can not hold any more files
                {
                    return(new Primitive(0));
                }

                byte[] content = null;
                try
                {
                    content = EV3RemoteControler.ReadEV3File(f);
                }
                catch (Exception) { }

                if (content == null)
                {
                    return(new Primitive(0));
                }

                FileHandle fh = new FileHandle(f, content);
                openFiles[i] = fh;
                return(new Primitive((double)i));
            }
        }
Exemple #4
0
        /// <summary>
        /// Read a whole array of numbers in binary form from the file. The numbers are encoded in IEEE single precision floating point representation.
        /// </summary>
        /// <param name="handle">The file handle (previously obtained from an Open... call)</param>
        /// <param name="size">Number of values to read</param>
        /// <returns>An array of size elements holding the values</returns>
        public static Primitive ReadNumberArray(Primitive handle, Primitive size)
        {
            int hdl = handle;
            int siz = size;

            lock (openFiles)
            {
                if (hdl >= 0 && hdl < openFiles.Length && openFiles[hdl] != null && siz > 0)
                {
                    FileHandle fh = openFiles[hdl];
                    if (fh.content != null && fh.readcursor + 4 * siz <= fh.content.Length)
                    {
                        double[] values = new double[siz];
                        for (int i = 0; i < siz; i++)
                        {
                            values[i] = BitConverter.ToSingle(fh.content, fh.readcursor + 4 * i);
                        }
                        fh.readcursor += 4 * siz;
                        return(A2P(values));
                    }
                }
            }
            return(A2P(new double[0]));
        }
Exemple #5
0
        /// <summary>
        /// Read one line of text from the file. The line will be decoded using the ISO-8859-1 encoding and must be terminated with a newline-character (code 10).
        /// </summary>
        /// <param name="handle">The file handle (previously obtained from an Open... call)</param>
        /// <returns>The text from the current line in the file</returns>
        public static Primitive ReadLine(Primitive handle)
        {
            int hdl = handle;

            lock (openFiles)
            {
                if (hdl >= 0 && hdl < openFiles.Length && openFiles[hdl] != null)
                {
                    FileHandle fh = openFiles[hdl];
                    if (fh.content != null && fh.readcursor < fh.content.Length)
                    {
                        StringBuilder b = new StringBuilder();
                        while (fh.readcursor < fh.content.Length && fh.content[fh.readcursor] != '\n')
                        {
                            b.Append((char)fh.content[fh.readcursor]);
                            fh.readcursor++;
                        }
                        fh.readcursor++;
                        return(new Primitive(b.ToString()));
                    }
                }
            }
            return(new Primitive(""));
        }