9
24
2010
3

C#:Ruby文件IO

目前功能尚不完全。仅作备份

 

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.IO;

namespace Marshal
{
    public partial class Marshal
    {
        static BinaryWriter w_stream;
        static Dictionary<rb_Symbol, int> w_symbols = new Dictionary<rb_Symbol, int> { };
        static Dictionary<object, int> w_objects = new Dictionary<object, int> { };
        static StringBuilder write_str;
        static public string rb_marshal_dump(object data, Stream s = null)
        {
            w_symbols.Clear();
            w_objects.Clear();
            write_str = new StringBuilder();
            dump(data);
            if (s != null && s.CanWrite)
            {
                w_stream = new BinaryWriter(s);
                foreach (byte i in write_str.ToString())
                {
                    s.WriteByte(i);
                }
            }
            return write_str.ToString();
        }
        static void dump(object ob, bool _pass = false)
        {
            if (!_pass)
            {
                write_str.Append((char)4);
                write_str.Append((char)8);
            }
            // 第一操作 请求Objects(Symbol判定已经排除)
            if (!(ob is int || ob is long || ob is rb_Nil || ob is bool || ob is rb_Symbol))
            {
                if (w_objects.ContainsKey(ob))
                {
                    write_str.Append((char)64);
                    writeint(w_objects[ob]);
                    return;
                }
            }
            // 第二操作 顺序输入存储 
            if (ob is long || ob is int)
            {
                write_str.Append((char)105);
                long i = Convert.ToInt64(ob);
                writeint(i);
            }
            else if (ob is string)
            {
                string s = (string)ob;
                write_str.Append((char)34);
                writestr(s);
            }
            else if (ob is rb_Symbol)
            {
                rb_Symbol sy = (rb_Symbol)ob;
                writesym(sy);
            }
            else if (ob is rb_Nil)
            {
                write_str.Append((char)48);
            }
            else if (ob is rb_Object)
            {
                writeobject((rb_Object)ob);
            }
            else if (ob is bool)
            {
                write_str.Append((char)((bool)ob ? 84 : 70));
            }

            else if (ob is rb_Table)
            {
                writeTable((rb_Table)ob);
            }
            else if (ob is object[])
            {
                writearray((object[])ob);
            }
            else if (ob is float || ob is double)
            {
                write_float((double)ob);
            }
            else
            {
                throw new Exception(ob.GetType().ToString());
            }
        }
        static List<rb_Symbol> r_symbols = new List<rb_Symbol> { };
        static List<object> r_objects = new List<object> { };
        static BinaryReader r_stream;
        static public object rb_marshal_load(Stream s)
        {
            if (!s.CanRead)
            { return null; }
            r_symbols.Clear();
            r_objects.Clear();
            r_stream = new BinaryReader(s);
            return read();
        }
        static object read(bool _pass = true)
        {
            if (_pass)
            {
                r_stream.Read();
                r_stream.Read();
            }
            object gotten;
            byte c = r_stream.ReadByte();
            long length = 0; int i = 0; object[] a;
            switch (c)
            {
                case 34:               // " 字符串
                    gotten = readstr(); break;
                case 48:               // 0 NIL
                    return new rb_Nil();
                case 47:               // '/' 正则表达式
                    string re = readstr();
                    int options = r_stream.ReadByte();
                    RegexOptions reg = RegexOptions.None;
                    if (options >= 4) { reg |= RegexOptions.Multiline; options -= 4; }
                    if (options >= 2) { reg |= RegexOptions.IgnorePatternWhitespace; options -= 2; }
                    if (options >= 1) { reg |= RegexOptions.IgnoreCase; options -= 1; }
                    gotten = new Regex(re, reg);
                    break;
                case 58:               // : 符号
                    return readsym();
                case 59:               // ; 符号引用
                    i = (int)readint();
                    return r_symbols[i];
                case 64:               // @ 物体引用
                    i = (int)readint();
                    return r_objects[i];
                case 70:               // F 伪值
                    return false;
                case 73:               // I 扩展自定义类
                    rb_expand_Object oba = new rb_expand_Object();
                    oba.real = read(false);
                    length = readint();
                    for (int j = 0; j < length; j++)
                    {
                        oba.variables[(rb_Symbol)read(false)] = read(false);
                    }
                    gotten = oba; break;
                case 84:               // T 真
                    return true;
                case 91:               // [ 数组
                    length = readint();
                    a = new object[length];
                    r_objects.Add(a);
                    for (int j = 0; j < length; j++)
                    {
                        a[j] = read(false);
                    }
                    return a;
                case 102:            // f 浮点
                    gotten = Convert.ToSingle(readstr()); break;
                case 105:              // i 整数
                    return readint();
                case 111:              // o 自定义类
                    rb_Object ob = new rb_Object();
                    r_objects.Add(ob);
                    ob.class_name = (rb_Symbol)read(false);
                    length = readint();
                    for (int j = 0; j < length; j++)
                    {
                        ob.variables[(rb_Symbol)read(false)] = read(false);
                    }
                    return ob;
                case 117:             // Table
                    string s = ((rb_Symbol)read(false)).getStr();
                    if (s == "Table")
                    {
                        long check1 = readint();
                        int d = readint4();
                        int x = readint4();
                        int y = readint4();
                        int z = readint4();
                        int check2 = readint4();
                        rb_Table array = new rb_Table(d, x, y, z);
                        for (int j = 0; j < z; j++)
                        {
                            for (int k = 0; k < y; k++)
                            {
                                for (int l = 0; l < x; l++)
                                {
                                    array.value[l, k, j] = readint4(2);
                                }
                            }
                        }
                        gotten = array;
                        break;
                    }
                    else
                    { throw new Exception("不支持的用户定义类型:" + s); }
                case 123:
                    Dictionary<Object, Object> dictionary = new Dictionary<object, object> { };
                    length = readint();
                    for (int j = 0; j < length; j++)
                    {
                        dictionary[read(false)] = read(false);
                    }
                    gotten = dictionary; break;
                default:
                    throw new Exception("不可预知的标识符:" + (char)c);
            }

            r_objects.Add(gotten);
            return gotten;
        }
    }
    public class rb_Symbol
    {
        string real;
        public rb_Symbol(string s)
        {
            real = s;
        }
        public override string ToString()
        {
            return ":" + real;
        }
        public string getStr()
        {
            return real;
        }
    }
    public class rb_Table
    {
        public readonly int size;
        public int[, ,] value;
        public rb_Table(int size, int x_size, int y_size = 1, int z_size = 1)
        {
            this.size = size;
            value = new int[x_size, y_size, z_size];
        }
        int get_Value(int x, int y, int z)
        {
            return value[x, y, z];
        }
        public override string ToString()
        {
            string s = "Ruby::Table{Size = ";
            s += size.ToString() + "} [";
            for (int i = 0; i < value.GetLength(2); i++)
            {
                s += "[";
                for (int j = 0; j < value.GetLength(1); j++)
                {
                    s += "[";
                    for (int k = 0; k < value.GetLength(0); k++)
                    {
                        s += value[k,j,i].ToString() + "  ";
                    }
                    s += "]";
                }
                s += "]";
            }
            s += "]";
            return s;
        }
    }
    public class rb_Object
    {
        public Dictionary<rb_Symbol, Object> variables = new Dictionary<rb_Symbol, object> { };
        public rb_Symbol class_name { get; set; }
        override public string ToString()
        {
            string s = "#<" + class_name + "> {";
            foreach (rb_Symbol i in variables.Keys)
            {
                s += (i + " = " + variables[i] + " ");
            }
            s += "}";
            return s;
        }
    }
    public class rb_expand_Object : rb_Object
    {
        public object real { get; set; }
        override public string ToString()
        {
            string s = "#<" + class_name + "> [" + real.ToString() + "]{";
            foreach (rb_Symbol i in variables.Keys)
            {
                s += (i + " = " + variables[i] + " , ");
            }
            s += "}";
            return s;
        }
    }
    public class rb_Nil : rb_Object
    {
        public override String ToString()
        { return "Ruby::Nil"; }
    }
}

第二代码

 

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;


namespace Marshal
{
    public partial class Help
    {
        public class TrueStrLength
        {
            public TrueStrLength() { }
            static public int trueLength(string str)
            {
                int lenTotal = 0;
                int n = str.Length;
                string strWord = "";
                int asc;
                for (int i = 0; i < n; i++)
                {
                    strWord = str.Substring(i, 1);
                    asc = Convert.ToChar(strWord);
                    if (asc < 0 || asc > 127)
                        lenTotal = lenTotal + 3;
                    else
                        lenTotal = lenTotal + 1;
                }

                return lenTotal;
            }
            static public string cutTrueLength(string strOriginal, int maxTrueLength, char chrPad, bool blnCutTail)
            {
                string strNew = strOriginal;
                if (strOriginal == null || maxTrueLength <= 0)
                {
                    strNew = "";
                    return strNew;
                }

                int trueLen = trueLength(strOriginal);
                if (trueLen > maxTrueLength)
                {
                    if (blnCutTail)
                    {
                        for (int i = strOriginal.Length - 1; i > 0; i--)
                        {
                            strNew = strNew.Substring(0, i);
                            if (trueLength(strNew) == maxTrueLength)
                                break;
                            else if (trueLength(strNew) < maxTrueLength)
                            {
                                strNew += chrPad.ToString();
                                break;
                            }
                        }
                    }
                }
                else
                {
                    for (int i = 0; i < maxTrueLength - trueLen; i++)
                    {
                        strNew += chrPad.ToString();
                    }
                }

                return strNew;
            }
        }
    }
    partial class Help
    {
        public static string Dictionary_String(Dictionary<Object, Object> d)
        {
            string s = "{";
            foreach (object i in d.Keys)
            {
                s += (i + " => " + d[i] + " , ");
            }
            s += "}";
            return s;
        }
        public static string Array_String(object[] ar)
        {
            string s = "[";
            foreach (object i in ar)
            {
                s += ((i.GetType() == ar.GetType()) ? Array_String((object[])i) : i.ToString()) + ",";
            }
            return s + "]";
        }
    }
}
namespace Marshal
{
    partial class Marshal
    {
        static void writeint(long i)
        {
            if (i > 122)
            {
                write_str.Append((char)Math.Ceiling(Math.Log(i, 256f)));
                while (i > 0)
                {
                    write_str.Append((char)(i & 0xff));
                    i = i >> 8;
                }
            }
            else if (i > 0)
            {
                write_str.Append((char)(i + 5));
            }
            else if (i == 0)
            {
                write_str.Append((char)0);
            }
            else if (i >= -123)
            {
                write_str.Append((char)(i - 5));
            }
            else
            {
                long t = ~i;
                write_str.Append((char)(sbyte)Math.Ceiling((Math.Log(t, 256))) * (-1));
                while (t > 0)
                {
                    write_str.Append((char)(t & 0xff));
                    t = t >> 8;
                }
            }
        }
        static void writestr(string s)
        {
            byte[] cs = Encoding.Unicode.GetBytes(s);
            byte[] bt = Encoding.Convert(Encoding.Unicode, Encoding.UTF8, cs);
            string st = Encoding.UTF8.GetString(bt);
            writeint(Help.TrueStrLength.trueLength(st));
            //System.Windows.Forms.MessageBox.Show(st + (st == s).ToString());
            //write_str.Append(st);
            foreach (byte i in bt)
            {
                write_str.Append((char)i);
            }
        }
        static void writearray(object[] ar)
        {
            write_str.Append((char)91);
            writeint(ar.Length);
            foreach (object i in ar)
            {
                dump(i, true);
            }
        }
        static void writeobject(rb_Object o)
        {
            write_str.Append((char)111);
            writesym(o.class_name);
            writeint(o.variables.Count);
            foreach (rb_Symbol i in o.variables.Keys)
            {
                writesym(i);
                dump(o.variables[i], true);
            }
        }
        static void writesym(rb_Symbol s)
        {
            // 符号序列检查
            if (w_symbols.ContainsKey(s))
            {
                int index = w_symbols[s];
                write_str.Append((char)59);
                writeint(index);
            }
            else
            {
                write_str.Append((char)58);
                writestr(s.getStr());
                w_symbols[s] = w_symbols.Count;
            }
        }
        static void writeTable(rb_Table t)
        {

            write_str.Append((char)117);
            writesym(new rb_Symbol("Table"));
            int x = t.value.GetLength(0);
            int y = t.value.GetLength(1);
            int z = t.value.GetLength(2);
            writeint(x * y * z * 2 + 20);
            write_str.Append(special_num(t.size));
            write_str.Append(special_num(x));
            write_str.Append(special_num(y));
            write_str.Append(special_num(z));
            write_str.Append(special_num(x * y * z));
            for (int i = 0; i < z; i++)
            {
                for (int j = 0; j < y; j++)
                {
                    for (int k = 0; k < x; k++)
                    {
                        
                        write_str.Append(special_num(t.value[k, j, i],2));
                    }
                }
            }
        }
        static string special_num(int num,int length = 4)
        {
            string s = "";
            int count = 0;
            while (num != 0)
            {
                s = (char)(num % 256) + s;
                num = num >> 8;
                count += 1;
                if (count >= length) { break; }
            }
            for (int j = 0; j < length - count; j++)
            {
                s = (char)0 + s;
            }
            return s;
        }
        static void write_float(double f)
        {
            write_str.Append((char)102);
            writestr(string.Format("{0:g}", f));
        }
        static long readint()
        {
            sbyte c = r_stream.ReadSByte();
            if (c <= -5)
            { return c + 5; }
            else if (c < 0)
            {
                long g = 0;
                for (int i = 0; i < -c; i++)
                {
                    int t = 255 - r_stream.ReadByte();
                    g = g + (t << (8 * i));
                }
                return -g - 1;
            }
            else if (c == 0)
            { return 0; }
            else if (c <= 4)
            {
                long g = 0;
                for (int i = 0; i < c; i++)
                {
                    int t = r_stream.ReadByte();
                    g = g + (t << (8 * i));
                }
                return g;
            }
            else
            { return c - 5; }
        }
        static string readstr()
        {
            long l = readint();
            byte[] cs = new byte[l];
            for (int i = 0; i < l; i++)
            { cs[i] = r_stream.ReadByte(); }
            UnicodeEncoding uni = new UnicodeEncoding();
            UTF8Encoding utf = new UTF8Encoding();
            byte[] bt = Encoding.Convert(Encoding.UTF8, Encoding.Unicode, cs);
            return uni.GetString(bt);
        }
        static rb_Symbol readsym()
        {
            rb_Symbol s = new rb_Symbol(readstr());
            r_symbols.Add(s);
            return s;
        }
        static int readint4(int d = 4)
        {
            int g = 0;
            for (int i = 0; i < d; i++)
            {
                int t = r_stream.ReadByte();
                g = g + (t << (8 * i));
            }
            return g;
        }
    }
}
Category: RM | Tags: ruby c# io | Read Count: 7689
Avatar_small
Junior Certificate R 说:
2022年8月28日 15:50

Madrasah Education Board is most popular in the country of Bangladesh, and they have successfully conducted the Junior Dakhil Certificate examination tests for Grade 8 level students at all education boards at all divisions in district wise across in the country, and there are lakhs of students are appeared from all Madhrasah’s of the country, and the Secondary Education Board also completed the JDC terminal exams 2022 successfully based on JDC routine 2022. Junior Certificate Result Sylhet Board The Class 8th Grade student who has appeared to the annual final terminal exams they are waiting to check JDC Result 2022 Madrasah Board with full mark sheet, the student who is qualified in this examination they are promoted to higher class in Mass education board, every student who has appeared to the JDC exams 2022 are waiting to check their result with subject wise marks along with final GPA grade point.

Avatar_small
Jharkhand 6th Model 说:
2023年9月14日 15:15

State Council of Educational Research and Training (SCERT) Jharkhand has Going to Conducted High School Aunval Exam 2024, SCERT Jharkhand which is Mainly Responsible for Conducting Exam Every year, This SCERT Jharkhand 6th Exam Very Year More Than 25 Laks of Students Attended, SCERT Jharkhand is Administered by the State Government of Jharkhand and Thrives to Improve the Quality of Jharkhand 6th Model Paper 2024 Education in the state.SCERT Jharkhand is also Responsible for Prescribing the SCERT Jharkhand Model Paper 2024 for the Students Studying in the Jharkhand Affiliated Schools, Jharkhand Question Paper 2024 Designed by Senior Experts Study Materiel help the Students to get an idea about the Final Exam 2024.

Avatar_small
boardmodelpaper.com 说:
2024年1月22日 15:04

The Board model paper" typically refers to a sample or model question paper that is designed by educational boards or institutions for various exams. These papers serve as practice material for students preparing for exams, providing them with an idea of the question format, difficulty level, and the type of content that may be covered in the actual examination. boardmodelpaper.com Model papers are usually created for specific subjects or courses. They cover a range of topics and chapters that students are expected to have studied during the academic term.


登录 *


loading captcha image...
(输入验证码)
or Ctrl+Enter

Host by is-Programmer.com | Power by Chito 1.3.3 beta | Theme: Aeros 2.0 by TheBuckmaker.com