Click here to Skip to main content
15,891,184 members
Articles / Containers / Virtual Machine

Twiggery Scripting Language

Rate me:
Please Sign up or sign in to vote.
4.82/5 (14 votes)
12 Aug 2010LGPL313 min read 64.2K   1.1K   36  
Twiggery Scripting Language
using System;
using System.Collections.Generic;
using System.Text;
using System.Reflection;
using System.Windows.Forms;
using System.Drawing;
using System.IO;
using System.Threading;
using num = System.Single;
using arguments = System.Collections.Generic.List<object>;

namespace Twiggery.Plugin.IO
{
    public class IO
    {
        internal class FileAttrib
        {
            public FileStream fileStream = null;
            public StreamReader streamReader = null;
            public StreamWriter streamWriter = null;

            public FileAttrib(FileStream fs, StreamWriter sw)
            {
                fileStream = fs;
                streamWriter = sw;
            }
            public FileAttrib(FileStream fs, StreamReader sr)
            {
                fileStream = fs;
                streamReader = sr;
            }
            public FileAttrib(FileStream fs, StreamReader sr, StreamWriter sw)
            {
                fileStream = fs;
                streamReader = sr;
                streamWriter = sw;
            }

            public void Close()
            {
                if (null != streamWriter)
                {
                    streamWriter.Close();
                }
                if (null != streamReader)
                {
                    streamReader.Close();
                }
                if (null != fileStream)
                {
                    fileStream.Close();
                }
            }
        }

        private static int idf = 0;
        private static Dictionary<int, FileAttrib> files = new Dictionary<int, FileAttrib>();

        public static void openFile(arguments args)
        {
            string access = (string)args[0];
            string name = (string)args[1];
            args.Clear();

            num result = (num)(-1);

            switch (access)
            {
                case "rw":
                    {
                        FileStream fs = new FileStream(name, FileMode.OpenOrCreate, FileAccess.ReadWrite);
                        StreamWriter sw = new StreamWriter(fs);
                        StreamReader sr = new StreamReader(fs);
                        FileAttrib fa = new FileAttrib(fs, sw);
                        fa.streamReader = sr;
                        files[++idf] = fa;
                        result = (num)idf;
                    }
                    break;
                case "w":
                    {
                        FileStream fs = new FileStream(name, FileMode.OpenOrCreate, FileAccess.Write);
                        fs.Close();
                        fs = new FileStream(name, FileMode.Truncate, FileAccess.Write);
                        StreamWriter sw = new StreamWriter(fs);
                        FileAttrib fa = new FileAttrib(fs, sw);
                        files[++idf] = fa;
                        result = (num)idf;
                    }
                    break;
                case "r":
                    {
                        FileStream fs = new FileStream(name, FileMode.Open, FileAccess.Read);
                        StreamReader sr = new StreamReader(fs);
                        FileAttrib fa = new FileAttrib(fs, sr);
                        files[++idf] = fa;
                        result = (num)idf;
                    }
                    break;
                default:
                    throw new Exception("Invalid file access option " + access);
            }
            args.Add(result);
        }

        private static void closeFile(arguments args)
        {
            int id = (int)(num)args[0];
            if (files.ContainsKey(id))
            {
                FileAttrib fa = files[id];
                fa.Close();
                files.Remove(id);
            }
        }

        private static void writeFile(arguments args)
        {
            int id = (int)(num)args[args.Count - 1];
            if (files.ContainsKey(id))
            {
                FileAttrib fa = files[id];
                args.RemoveAt(args.Count - 1);
                for (int i = args.Count - 1; i >= 0; i--)
                {
                    object obj = args[i];
                    if (obj.Equals("\\n"))
                    {
                        obj = "\r\n";
                    }
                    fa.streamWriter.Write(obj);
                }
                args.Clear();
            }
        }

        public static void readFile(arguments args)
        {
            int count = 0;
            int start = 0;
            int id = 0;
            if (args.Count == 3)
            {
                count = (int)(num)args[0];
                start = (int)(num)args[1];
                id = (int)(num)args[2];
            }
            else if (args.Count == 1)
            {
                count = -1;
                start = -1;
                id = (int)(num)args[0];
            }
            else
            {
                throw new Exception("Invalid read file arguments");
            }
            args.Clear();
            if (files.ContainsKey(id))
            {
                FileAttrib fa = files[id];
                if (null != fa.streamReader)
                {
                    string str_buf = null;
                    if (-1 == count)
                    {
                        str_buf = fa.streamReader.ReadLine();
                    }
                    else
                    {
                        char[] buffer = new char[count];
                        fa.streamReader.Read(buffer, start, count);
                        str_buf = new string(buffer);
                    }
                    num result = num.Parse(str_buf);
                    args.Add(result);
                }
            }
        }
    }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The GNU Lesser General Public License (LGPLv3)


Written By
Architect
China China
Video game player & creator; Hardware geek & maker.

Comments and Discussions