/// <summary> /// 从文档中读取一个 Hash /// 注意:焦点没有发生移动。 /// </summary> /// <returns></returns> protected FuzzyHash GetHash() { FuzzyHash fhash = new FuzzyHash(); x_node = x_node.FirstChild; if (x_node.Name == "Default") { XmlNode parent = x_node; x_node = x_node.FirstChild; fhash.DefaultValue = ReadObject(); x_node = parent.NextSibling; } object key = ReadObject(); while (x_node.NextSibling != null) { x_node = x_node.NextSibling; if (key == null) { key = ReadObject(); } else { fhash.Add(key, ReadObject()); key = null; } } x_node = x_node.ParentNode; return(fhash); }
public void ScanObject(FuzzyObject Value) { Items.Clear(); if (Value == null) { return; } dictionary.Add(AddItem(AnyTypeEditor.NAME_VALUE, Value.ClassName), new Key(FuzzySymbol.GetSymbol(AnyTypeEditor.NAME_VALUE), Key.KeyType.Object)); foreach (FuzzySymbol symbol in Value.InstanceVariables.Keys) { object Target = Value.InstanceVariables[symbol]; if (Any.SupportedSingle.Contains(Target.GetType())) { dictionary.Add(AddItem(symbol.Name, Target), new Key(symbol, Key.KeyType.Object)); } } if (Value is FuzzyArray) { FuzzyArray fa = Value as FuzzyArray; for (int i = 0; i < fa.Length; i++) { object Target = fa[i]; if (Any.SupportedSingle.Contains(Target.GetType())) { dictionary.Add(AddItem("[" + i.ToString() + "]", Target), new Key(i, Key.KeyType.Array)); } } } if (Value is FuzzyHash) { FuzzyHash fh = Value as FuzzyHash; foreach (object key in fh.Keys) { object Target = fh[key]; FuzzyObject target = Target as FuzzyObject; if (Any.SupportedSingle.Contains(Target.GetType())) { dictionary.Add(AddItem(key.ToString(), target), new Key(key, Key.KeyType.Hash)); } } } dictionary.Add(AddItem("", null), null); }
public void ScanObject(FuzzyObject Value, TreeNode ParentNode) { if (Value == null) { return; } foreach (FuzzySymbol symbol in Value.InstanceVariables.Keys) { object Target = Value.InstanceVariables[symbol]; if (Any.SupportedComplex.Contains(Target.GetType())) { ScanObject(Target as FuzzyObject, AddNode("[" + symbol.Name + "]", Target, ParentNode)); } } if (Value is FuzzyArray) { FuzzyArray fa = Value as FuzzyArray; for (int i = 0; i < fa.Length; i++) { object Target = fa[i]; if (Any.SupportedComplex.Contains(Target.GetType())) { ScanObject(Target as FuzzyObject, AddNode("[" + i.ToString() + "]", Target, ParentNode)); } } } if (Value is FuzzyHash) { FuzzyHash fh = Value as FuzzyHash; foreach (object key in fh.Keys) { object Target = fh[key]; FuzzyObject target = Target as FuzzyObject; if (Any.SupportedComplex.Contains(Target.GetType())) { ScanObject(target, AddNode("[" + Any.GetShortString(target) + "]", target, ParentNode)); } } } }
/// <summary> /// static void w_object(VALUE obj, struct dump_arg *arg, int limit) /// </summary> /// <param name="obj"></param> public void WriteObject(object obj) { int num; if (this.m_objects.TryGetValue(obj, out num)) { WriteByte(RubyMarshal.Types.Link); WriteLong(num); return; } if (obj == null || obj == FuzzyNil.Instance) { WriteByte(RubyMarshal.Types.Nil); } else if (obj is bool && (bool)obj == true) { WriteByte(RubyMarshal.Types.True); } else if (obj is bool && (bool)obj == false) { WriteByte(RubyMarshal.Types.False); } else if (obj is FuzzyBool && (obj as FuzzyBool).Value) { WriteByte(RubyMarshal.Types.True); } else if (obj is FuzzyBool && !(obj as FuzzyBool).Value) { WriteByte(RubyMarshal.Types.False); } else if (obj is int || obj is long || obj is FuzzyFixnum) { long v; if (obj is int | obj is long) { v = (long)obj; } else { v = (obj as FuzzyFixnum).Value; } // (2**30).class => Bignum // (2**30-1).class => Fixnum // (-2**30-1).class=> Bignum // (-2**30).class => Fixnum if (v <= 1073741823 && v >= -1073741824) { WriteByte(RubyMarshal.Types.Fixnum); WriteLong((int)v); } else { WriteObject(FuzzyBignum.Create(v)); } } else if (obj is FuzzySymbol) { WriteSymbol(obj as FuzzySymbol); } else { FuzzyObject fobj = obj as FuzzyObject; bool hasiv = false; if (fobj != null) { hasiv = (obj as FuzzyObject).InstanceVariables.Count > 0 || fobj.Encoding != null; } var factor = Serialization.Factory <byte[]> .Factor(fobj.GetType()); if (factor != null) { WriteSymbol(fobj.ClassName); factor.dump(m_stream, fobj, null); } if (obj is IRubyUserdefinedMarshalDumpObject) { this.m_objects.Add(obj, this.m_objects.Count); object result = (obj as IRubyUserdefinedMarshalDumpObject).Dump(); if (hasiv) { WriteByte(RubyMarshal.Types.InstanceVariable); } WriteClass(RubyMarshal.Types.UserMarshal, obj, false); WriteObject(result); if (hasiv) { WriteObjectInstanceVariable(fobj); } return; } if (obj is IRubyUserdefinedDumpObject) { byte[] result = (obj as IRubyUserdefinedDumpObject).Dump(); if (hasiv) { WriteByte(RubyMarshal.Types.InstanceVariable); } WriteClass(RubyMarshal.Types.UserDefined, obj, false); WriteBytes(result, result.Length); if (hasiv) { WriteObjectInstanceVariable(fobj); } this.m_objects.Add(obj, this.m_objects.Count); return; } this.m_objects.Add(obj, this.m_objects.Count); if (hasiv) { WriteByte(RubyMarshal.Types.InstanceVariable); } if (obj is FuzzyClass) { WriteByte(RubyMarshal.Types.Class); WriteCString((obj as FuzzyClass).Name); } else if (obj is FuzzyModule) { WriteByte(RubyMarshal.Types.Module); WriteCString((obj as FuzzyModule).Name); } else if (obj is float) { WriteByte(RubyMarshal.Types.Float); WriteFloat((float)obj); } else if (obj is double) { WriteByte(RubyMarshal.Types.Float); WriteFloat((double)obj); } else if (obj is FuzzyFloat) { WriteByte(RubyMarshal.Types.Float); WriteFloat((FuzzyFloat)obj); } else if (obj is FuzzyBignum || obj is FuzzyBignumAdapter) { FuzzyBignum value; if (obj is FuzzyBignumAdapter) { value = (obj as FuzzyBignumAdapter).Value; } else { value = (FuzzyBignum)obj; } char ch; if (value.Sign > 0) { ch = '+'; } else if (value.Sign < 0) { ch = '-'; } else { ch = '0'; } this.m_writer.Write((byte)ch); uint[] words = value.GetWords(); int num2 = words.Length * 2; int index = words.Length - 1; bool flag = false; if ((words.Length > 0) && ((words[index] >> 0x10) == 0)) { num--; flag = true; } this.WriteLong(num2); for (int i = 0; i < words.Length; i++) { if (flag && (i == index)) { this.m_writer.Write((ushort)words[i]); } else { this.m_writer.Write(words[i]); } } } else if (obj is FuzzyString || obj is string) { FuzzyString v; if (obj is string) { v = new FuzzyString(obj as string); } else { v = (FuzzyString)obj; } WriteUserClass(v, FuzzyClass.GetClass("String")); WriteByte(RubyMarshal.Types.String); WriteBytes(v.Raw); } else if (obj is FuzzyRegexp) { FuzzyRegexp v = (FuzzyRegexp)obj; WriteUserClass(obj, FuzzyClass.GetClass("Regexp")); WriteByte(RubyMarshal.Types.Regexp); WriteBytes(v.Pattern.Raw); WriteByte((byte)v.Options); } else if (obj is FuzzyArray || obj is List <object> ) { FuzzyArray v; if (obj is List <object> ) { v = new FuzzyArray(obj as List <object>); } else { v = (FuzzyArray)obj; } WriteUserClass(v, FuzzyClass.GetClass("Array")); WriteByte(RubyMarshal.Types.Array); WriteLong(v.Length); for (int i = 0; i < v.Count; i++) { WriteObject(v[i]); } } else if (obj is FuzzyHash) { FuzzyHash v = (FuzzyHash)obj; WriteUserClass(obj, FuzzyClass.GetClass("Hash")); WriteByte(v.DefaultValue != null ? RubyMarshal.Types.HashWithDefault : RubyMarshal.Types.Hash); WriteLong(v.Length); foreach (KeyValuePair <object, object> item in v) { WriteObject(item.Key); WriteObject(item.Value); } if (v.DefaultValue != null) { WriteObject(v.DefaultValue); } } else if (obj is FuzzyStruct) { FuzzyStruct v = (FuzzyStruct)obj; WriteUserClass(obj, FuzzyClass.GetClass("Struct")); WriteLong(v.InstanceVariables.Count); foreach (KeyValuePair <FuzzySymbol, object> item in v.InstanceVariables) { WriteObject(item.Key); WriteObject(item.Value); } } else if (obj is FuzzyObject) { WriteClass(RubyMarshal.Types.Object, obj, true); WriteObjectInstanceVariable((FuzzyObject)obj); } else { throw new InvalidDataException(string.Format("can't dump {0}", obj.GetType().FullName)); } if (hasiv) { WriteInstanceVariable(fobj, fobj.InstanceVariables); } } }
/// <summary> /// static VALUE r_object0(struct load_arg *arg, int *ivp, VALUE extmod) /// </summary> /// <param name="hasivp"></param> /// <param name="ivp"></param> /// <param name="extmod"></param> /// <returns></returns> public object ReadObject0(bool hasivp, ref bool ivp, List <FuzzyModule> extmod) { object v = null; int type = ReadByte(); int id; object link; switch (type) { case RubyMarshal.Types.Link: id = ReadLong(); if (!this.m_objects.TryGetValue(id, out link)) { throw new InvalidDataException("dump format error (unlinked)"); } v = link; if (this.m_proc != null) { v = this.m_proc(v); } break; case RubyMarshal.Types.InstanceVariable: { bool ivar = true; v = ReadObject0(ref ivar, extmod); bool hasenc = false; if (ivar) { ReadInstanceVariable(v, ref hasenc); } } break; case RubyMarshal.Types.Extended: { FuzzyModule m = FuzzyModule.GetModule(ReadUnique()); if (extmod == null) { extmod = new List <FuzzyModule>(); } extmod.Add(m); v = ReadObject0(extmod); FuzzyObject fobj = v as FuzzyObject; if (fobj != null) { fobj.ExtendModules.AddRange(extmod); } } break; case RubyMarshal.Types.UserClass: { FuzzyClass c = FuzzyClass.GetClass(ReadUnique()); v = ReadObject0(extmod); if (v is FuzzyObject) { (v as FuzzyObject).ClassName = c.Symbol; } } break; case RubyMarshal.Types.Nil: v = FuzzyNil.Instance; v = Leave(v); break; case RubyMarshal.Types.True: v = FuzzyBool.True; v = Leave(v); break; case RubyMarshal.Types.False: v = FuzzyBool.False; v = Leave(v); break; case RubyMarshal.Types.Fixnum: v = ReadLong(); v = new FuzzyFixnum(Convert.ToInt64(v)); v = Leave(v); break; case RubyMarshal.Types.Float: { double d; FuzzyString fstr = ReadString(); string str = fstr.Text; if (str == "inf") { d = double.PositiveInfinity; } else if (str == "-inf") { d = double.NegativeInfinity; } else if (str == "nan") { d = double.NaN; } else { if (str.Contains("\0")) { str = str.Remove(str.IndexOf("\0")); } d = Convert.ToDouble(str); } v = new FuzzyFloat(d); v = Entry(v); v = Leave(v); } break; case RubyMarshal.Types.Bignum: { int sign = 0; switch (ReadByte()) { case 0x2b: sign = 1; break; case 0x2d: sign = -1; break; default: sign = 0; break; } int num3 = ReadLong(); int index = num3 / 2; int num5 = (num3 + 1) / 2; uint[] data = new uint[num5]; for (int i = 0; i < index; i++) { data[i] = m_reader.ReadUInt32(); } if (index != num5) { data[index] = m_reader.ReadUInt16(); } v = new FuzzyBignum(sign, data); v = new FuzzyBignumAdapter(v as FuzzyBignum); v = Entry(v); v = Leave(v); } break; case RubyMarshal.Types.String: v = Entry(ReadString()); v = Leave(v); break; case RubyMarshal.Types.Regexp: { FuzzyString str = ReadString(); int options = ReadByte(); bool has_encoding = false; int idx = Prepare(); if (hasivp) { ReadInstanceVariable(str, ref has_encoding); ivp = false; } if (!has_encoding) { // TODO: 1.8 compatibility; remove escapes undefined in 1.8 /* * char *ptr = RSTRING_PTR(str), *dst = ptr, *src = ptr; * long len = RSTRING_LEN(str); * long bs = 0; * for (; len-- > 0; *dst++ = *src++) { * switch (*src) { * case '\\': bs++; break; * case 'g': case 'h': case 'i': case 'j': case 'k': case 'l': * case 'm': case 'o': case 'p': case 'q': case 'u': case 'y': * case 'E': case 'F': case 'H': case 'I': case 'J': case 'K': * case 'L': case 'N': case 'O': case 'P': case 'Q': case 'R': * case 'S': case 'T': case 'U': case 'V': case 'X': case 'Y': * if (bs & 1) --dst; * default: bs = 0; break; * } * } * rb_str_set_len(str, dst - ptr); */ } v = Entry0(new FuzzyRegexp(str, (FuzzyRegexpOptions)options), idx); v = Leave(v); } break; case RubyMarshal.Types.Array: { int len = ReadLong(); FuzzyArray ary = new FuzzyArray(); v = ary; v = Entry(v); while (len-- > 0) { ary.Push(ReadObject()); } v = Leave(v); } break; case RubyMarshal.Types.Hash: case RubyMarshal.Types.HashWithDefault: { int len = ReadLong(); FuzzyHash hash = new FuzzyHash(); v = hash; v = Entry(v); while (len-- > 0) { object key = ReadObject(); object value = ReadObject(); hash.Add(key, value); } if (type == RubyMarshal.Types.HashWithDefault) { hash.DefaultValue = ReadObject(); } v = Leave(v); } break; case RubyMarshal.Types.Struct: { int idx = Prepare(); FuzzyStruct obj = new FuzzyStruct(); FuzzySymbol klass = ReadUnique(); obj.ClassName = klass; int len = ReadLong(); v = obj; v = Entry0(v, idx); while (len-- > 0) { FuzzySymbol key = ReadSymbol(); object value = ReadObject(); obj.InstanceVariable[key] = value; } v = Leave(v); } break; case RubyMarshal.Types.UserDefined: { FuzzySymbol klass = ReadUnique(); var factor = Factory <byte[]> .Factor(klass.Name); if (factor == null) { FuzzyString data = ReadString(); if (hasivp) { ReadInstanceVariable(data); ivp = false; } FuzzyUserdefinedDumpObject obj = new FuzzyUserdefinedDumpObject(); obj.ClassName = klass; obj.DumpedObject = data.Raw; v = obj; v = Entry(v); v = Leave(v); } else { object obj = factor._dump(m_stream, null); v = obj; v = Entry(v); v = Leave(v); } } break; case RubyMarshal.Types.UserMarshal: { FuzzySymbol klass = ReadUnique(); FuzzyUserdefinedMarshalDumpObject obj = new FuzzyUserdefinedMarshalDumpObject(); v = obj; if (extmod != null) { AppendExtendedModule(obj, extmod); } v = Entry(v); object data = ReadObject(); obj.ClassName = klass; obj.DumpedObject = data; v = Leave(v); if (extmod != null) { extmod.Clear(); } } break; case RubyMarshal.Types.Object: { int idx = Prepare(); FuzzyObject obj = new FuzzyObject(); FuzzySymbol klass = ReadUnique(); obj.ClassName = klass; v = obj; v = Entry0(v, idx); ReadInstanceVariable(v); v = Leave(v); } break; case RubyMarshal.Types.Class: { FuzzyString str = ReadString(); v = FuzzyClass.GetClass(FuzzySymbol.GetSymbol(str)); v = Entry(v); v = Leave(v); } break; case RubyMarshal.Types.Module: { FuzzyString str = ReadString(); v = FuzzyModule.GetModule(FuzzySymbol.GetSymbol(str)); v = Entry(v); v = Leave(v); } break; case RubyMarshal.Types.Symbol: if (hasivp) { v = ReadSymbolReal(ivp); ivp = false; } else { v = ReadSymbolReal(false); } v = Leave(v); break; case RubyMarshal.Types.SymbolLink: v = ReadSymbolLink(); break; case RubyMarshal.Types.Data: /* TODO: Data Support * { * VALUE klass = path2class(r_unique(arg)); * VALUE oldclass = 0; * * v = obj_alloc_by_klass(klass, arg, &oldclass); * if (!RB_TYPE_P(v, T_DATA)) { * rb_raise(rb_eArgError, "dump format error"); * } * v = r_entry(v, arg); * if (!rb_respond_to(v, s_load_data)) { * rb_raise(rb_eTypeError, "class %s needs to have instance method `_load_data'", rb_class2name(klass)); * } * rb_funcall(v, s_load_data, 1, r_object0(arg, 0, extmod)); * check_load_arg(arg, s_load_data); * v = r_leave(v, arg); * } */ case RubyMarshal.Types.ModuleOld: /* * TODO: ModuleOld Support * { * volatile VALUE str = r_bytes(arg); * v = rb_path_to_class(str); * v = r_entry(v, arg); * v = r_leave(v, arg); * } */ default: throw new InvalidDataException(string.Format("dump format error(0x{0:X2})", type)); } return(v); }
public void WriteObject(object obj) { int num; if (this.x_objects.TryGetValue(obj, out num)) { WriteNode(XML.Types.Link, num.ToString()); return; } if (obj == null || obj == FuzzyNil.Instance) { WriteNode(XML.Types.Nil, ""); } else if (obj is bool && (bool)obj == true) { WriteNode(XML.Types.True, ""); } else if (obj is bool && (bool)obj == false) { WriteNode(XML.Types.False, ""); } else if (obj is int || obj is long || obj is FuzzyFixnum) { long v; if (obj is FuzzyFixnum) { v = (obj as FuzzyFixnum).Value; } else { v = (long)obj; } if (v <= 1073741823 && v >= -1073741824) { WriteNode(XML.Types.Fixnum, obj.ToString()); } else { WriteObject(FuzzyBignum.Create(v)); } } else if (obj is FuzzySymbol) { WriteSymbol(obj as FuzzySymbol); } else { FuzzyObject fobj = obj as FuzzyObject; bool hasiv = false; if (fobj != null) { hasiv = (obj as FuzzyObject).InstanceVariables.Count > 0 || fobj.Encoding != null; } if (obj is IXMLUserdefinedDumpObject) { this.x_objects.Add(obj, this.x_objects.Count); object result = (obj as IXMLUserdefinedMarshalDumpObject).Dump(); WritePopNode(XML.Types.UserMarshal, ""); WriteObject(result); x_parent_node.Pop(); return; } if (obj is IXMLUserdefinedDumpObject) { XmlNode result = (obj as IXMLUserdefinedDumpObject).Dump(); WritePopNode(XML.Types.UserDefined, "").AppendChild(result); this.x_objects.Add(obj, this.x_objects.Count); return; } this.x_objects.Add(obj, this.x_objects.Count); if (obj is FuzzyClass) { WriteNode(XML.Types.Class, (obj as FuzzyClass).Name); } else if (obj is FuzzyModule) { WriteNode(XML.Types.Module, (obj as FuzzyClass).Name); } else if (obj is float) { WriteFloat((float)obj); } else if (obj is double) { WriteFloat((double)obj); } else if (obj is FuzzyFloat) { WriteFloat((obj as FuzzyFloat).Value); } else if (obj is FuzzyBignum) { WriteNode(XML.Types.Bignum, obj.ToString()); } else if (obj is FuzzyBignumAdapter) { WriteNode(XML.Types.Bignum, (obj as FuzzyBignumAdapter).Value.ToString()); } else if (obj is FuzzyString || obj is string) { FuzzyString v; if (obj is string) { v = new FuzzyString(obj as string); } else { v = (FuzzyString)obj; } WritePopNode(XML.Types.String); WriteString(v.Text, v.Encoding); x_parent_node.Pop(); } else if (obj is FuzzyRegexp) { FuzzyRegexp v = (FuzzyRegexp)obj; WritePopNode(XML.Types.Regexp); WriteString(v.Pattern.Text, v.Pattern.Encoding); WriteNode("Options", ((int)v.Options).ToString()); } else if (obj is FuzzyArray || obj is List <object> ) { FuzzyArray v; if (obj is List <object> ) { v = new FuzzyArray(obj as List <object>); } else { v = (FuzzyArray)obj; } WritePopNode(XML.Types.Array); for (int i = 0; i < v.Count; i++) { WriteObject(v[i]); } x_parent_node.Pop(); } else if (obj is FuzzyHash) { FuzzyHash v = (FuzzyHash)obj; if (v.DefaultValue == null) { WritePopNode(XML.Types.Hash); } else { WritePopNode(XML.Types.HashWithDefault); WritePopNode("Default"); WriteObject(v.DefaultValue); x_parent_node.Pop(); } foreach (KeyValuePair <object, object> item in v) { WriteObject(item.Key); WriteObject(item.Value); } x_parent_node.Pop(); } else if (obj is FuzzyObject || obj is FuzzyStruct) { FuzzyObject v = fobj; if (obj is FuzzyObject) { WritePopNode(XML.Types.Object); } else { WritePopNode(XML.Types.Struct); } if (XML.Options.IgnoreSymbolEncoding) { WriteAttribute("Name", v.ClassName.Name); foreach (KeyValuePair <FuzzySymbol, object> item in v.InstanceVariables) { WritePopNode("Data"); WriteAttribute("Name", item.Key.Name); WriteObject(item.Value); x_parent_node.Pop(); } } else { var factor = Serialization.Factory <XmlNode> .Factor(fobj.GetType()); if (factor == null) { factor = Serialization.Factory <XmlNode> .Factor(fobj.ClassName.Name); } if (factor != null) { WritePopNode(XML.Types.UserDefined); XmlNode node = factor.dump(x_stream, fobj, null); node = x_document.ImportNode(node, true); x_parent_node.Pop().AppendChild(node); } else { WritePopNode("Name"); WriteObject(v.ClassName); x_parent_node.Pop(); foreach (KeyValuePair <FuzzySymbol, object> item in v.InstanceVariables) { WriteSymbol(item.Key); WriteObject(item.Value); } } } x_parent_node.Pop(); } else { throw new InvalidDataException(string.Format("can't dump {0}", obj.GetType().FullName)); } } }