Example #1
0
 void RunTest(string c_code, string expectedXml)
 {
     StringReader reader = null;
     StringWriter writer = null;
     try
     {
         reader = new StringReader(c_code);
         writer = new StringWriter();
         var xWriter = new XmlnsHidingWriter(writer)
         {
             Formatting = Formatting.Indented
         };
         var xc = new XmlConverter(reader, xWriter);
         xc.Convert();
         writer.Flush();
         Assert.AreEqual(expectedXml, writer.ToString());
     }
     catch
     {
         Debug.WriteLine(writer.ToString());
         throw;
     }
     finally
     {
         if (writer != null)
             writer.Dispose();
         if (reader != null)
             reader.Dispose();
     }
 }
Example #2
0
 public int Execute(string []args)
 {
     TextReader input = Console.In;
     TextWriter output = Console.Out;
     if (args.Length > 2)
     {
         Usage();
         return 1;
     }
     if (args.Length >= 1)
     {
         try
         {
             input = new StreamReader(args[0]);
         }
         catch (Exception ex)
         {
             Console.Error.WriteLine("c2xml: Unable to open file {0} for reading. {1}", args[0], ex.Message);
             return 1;
         }
     }
     if (args.Length == 2)
     {
         try
         {
             output = new StreamWriter(args[1]);
         }
         catch (Exception ex)
         {
             Console.Error.WriteLine("c2xml: Unable to open file {0} for writing. {1}", args[1], ex.Message);
             return 1;
         }
     }
     var xWriter = new XmlTextWriter(output)
     {
         Formatting = Formatting.Indented
     };
     XmlConverter c = new XmlConverter(input, xWriter);
     c.Convert();
     output.Flush();
     return 0;
 }
Example #3
0
        public float ToSingle()
        {
            ValueHandleType type = _type;

            if (type == ValueHandleType.Single)
            {
                return(GetSingle());
            }
            if (type == ValueHandleType.Double)
            {
                double value = GetDouble();

                if ((value >= float.MinValue && value <= float.MaxValue) || !double.IsFinite(value))
                {
                    return((float)value);
                }
            }
            if (type == ValueHandleType.Zero)
            {
                return(0);
            }
            if (type == ValueHandleType.One)
            {
                return(1);
            }
            if (type == ValueHandleType.Int8)
            {
                return(GetInt8());
            }
            if (type == ValueHandleType.Int16)
            {
                return(GetInt16());
            }
            if (type == ValueHandleType.UTF8)
            {
                return(XmlConverter.ToSingle(_bufferReader.Buffer, _offset, _length));
            }
            return(XmlConverter.ToSingle(GetString()));
        }
Example #4
0
        public double ToDouble()
        {
            ValueHandleType type = _type;

            if (type == ValueHandleType.Double)
            {
                return(GetDouble());
            }
            if (type == ValueHandleType.Single)
            {
                return(GetSingle());
            }
            if (type == ValueHandleType.Zero)
            {
                return(0);
            }
            if (type == ValueHandleType.One)
            {
                return(1);
            }
            if (type == ValueHandleType.Int8)
            {
                return(GetInt8());
            }
            if (type == ValueHandleType.Int16)
            {
                return(GetInt16());
            }
            if (type == ValueHandleType.Int32)
            {
                return(GetInt32());
            }
            if (type == ValueHandleType.UTF8)
            {
                return(XmlConverter.ToDouble(_bufferReader.Buffer, _offset, _length));
            }
            return(XmlConverter.ToDouble(GetString()));
        }
        public Single ToSingle()
        {
            ValueHandleType type = this.type;

            if (type == ValueHandleType.Single)
            {
                return(GetSingle());
            }
            if (type == ValueHandleType.Double)
            {
                double value = GetDouble();
                if ((value >= Single.MinValue && value <= Single.MaxValue) || double.IsInfinity(value) || double.IsNaN(value))
                {
                    return((Single)value);
                }
            }
            if (type == ValueHandleType.Zero)
            {
                return(0);
            }
            if (type == ValueHandleType.One)
            {
                return(1);
            }
            if (type == ValueHandleType.Int8)
            {
                return(GetInt8());
            }
            if (type == ValueHandleType.Int16)
            {
                return(GetInt16());
            }
            if (type == ValueHandleType.UTF8)
            {
                return(XmlConverter.ToSingle(bufferReader.Buffer, offset, length));
            }
            return(XmlConverter.ToSingle(GetString()));
        }
Example #6
0
        public bool IsWhitespace()
        {
            switch (_type)
            {
            case ValueHandleType.UTF8:
                return(_bufferReader.IsWhitespaceUTF8(_offset, _length));

            case ValueHandleType.Dictionary:
                return(_bufferReader.IsWhitespaceKey(_offset));

            case ValueHandleType.Char:
                int ch = GetChar();
                if (ch > char.MaxValue)
                {
                    return(false);
                }
                return(XmlConverter.IsWhitespace((char)ch));

            case ValueHandleType.EscapedUTF8:
                return(_bufferReader.IsWhitespaceUTF8(_offset, _length));

            case ValueHandleType.Unicode:
                return(_bufferReader.IsWhitespaceUnicode(_offset, _length));

            case ValueHandleType.True:
            case ValueHandleType.False:
            case ValueHandleType.Zero:
            case ValueHandleType.One:
                return(false);

            case ValueHandleType.ConstString:
                return(s_constStrings[_offset].Length == 0);

            default:
                return(_length == 0);
            }
        }
Example #7
0
        public long ToLong()
        {
            switch (this.type)
            {
            case ValueHandleType.Zero:
                return(0L);

            case ValueHandleType.One:
                return(1L);

            case ValueHandleType.Int8:
                return((long)this.GetInt8());

            case ValueHandleType.Int16:
                return((long)this.GetInt16());

            case ValueHandleType.Int32:
                return((long)this.GetInt32());

            case ValueHandleType.Int64:
                return(this.GetInt64());

            case ValueHandleType.UInt64:
            {
                ulong num = this.GetUInt64();
                if (num <= 0x7fffffffffffffffL)
                {
                    return((long)num);
                }
                break;
            }

            case ValueHandleType.UTF8:
                return(XmlConverter.ToInt64(this.bufferReader.Buffer, this.offset, this.length));
            }
            return(XmlConverter.ToInt64(this.GetString()));
        }
Example #8
0
        public string GetString()
        {
            ValueHandleType type = _type;

            if (type == ValueHandleType.UTF8)
            {
                return(GetCharsText());
            }

            switch (type)
            {
            case ValueHandleType.False:
                return("false");

            case ValueHandleType.True:
                return("true");

            case ValueHandleType.Zero:
                return("0");

            case ValueHandleType.One:
                return("1");

            case ValueHandleType.Int8:
            case ValueHandleType.Int16:
            case ValueHandleType.Int32:
                return(XmlConverter.ToString(ToInt()));

            case ValueHandleType.Int64:
                return(XmlConverter.ToString(GetInt64()));

            case ValueHandleType.UInt64:
                return(XmlConverter.ToString(GetUInt64()));

            case ValueHandleType.Single:
                return(XmlConverter.ToString(GetSingle()));

            case ValueHandleType.Double:
                return(XmlConverter.ToString(GetDouble()));

            case ValueHandleType.Decimal:
                return(XmlConverter.ToString(GetDecimal()));

            case ValueHandleType.DateTime:
                return(XmlConverter.ToString(ToDateTime()));

            case ValueHandleType.Empty:
                return(string.Empty);

            case ValueHandleType.Unicode:
                return(GetUnicodeCharsText());

            case ValueHandleType.EscapedUTF8:
                return(GetEscapedCharsText());

            case ValueHandleType.Char:
                return(GetCharText());

            case ValueHandleType.Dictionary:
                return(GetDictionaryString().Value);

            case ValueHandleType.Base64:
                byte[] bytes = ToByteArray();
                DiagnosticUtility.DebugAssert(bytes != null, "");
                return(Base64Encoding.GetString(bytes, 0, bytes.Length));

            case ValueHandleType.List:
                return(XmlConverter.ToString(ToList()));

            case ValueHandleType.UniqueId:
                return(XmlConverter.ToString(ToUniqueId()));

            case ValueHandleType.Guid:
                return(XmlConverter.ToString(ToGuid()));

            case ValueHandleType.TimeSpan:
                return(XmlConverter.ToString(ToTimeSpan()));

            case ValueHandleType.QName:
                return(GetQNameDictionaryText());

            case ValueHandleType.ConstString:
                return(s_constStrings[_offset]);

            default:
                throw System.Runtime.Serialization.DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidOperationException());
            }
        }
Example #9
0
 public string GetUnicodeString(int offset, int length)
 {
     return(XmlConverter.ToStringUnicode(_buffer, offset, length));
 }
Example #10
0
        public unsafe bool Equals2(int offset1, int length1, string s2)
        {
            int byteLength = length1;
            int charLength = s2.Length;

            // N Unicode chars will be represented in at least N bytes, but
            // no more than N * 3 bytes.  If the byte count falls outside of this
            // range, then the strings cannot be equal.
            if (byteLength < charLength || byteLength > charLength * maxBytesPerChar)
            {
                return(false);
            }

            byte[] buffer = _buffer;
            if (length1 < 8)
            {
                int length = Math.Min(byteLength, charLength);
                int offset = offset1;
                for (int i = 0; i < length; i++)
                {
                    byte b = buffer[offset + i];
                    if (b >= 0x80)
                    {
                        return(XmlConverter.ToString(buffer, offset1, length1) == s2);
                    }
                    if (s2[i] != (char)b)
                    {
                        return(false);
                    }
                }
                return(byteLength == charLength);
            }
            else
            {
                int length = Math.Min(byteLength, charLength);
                fixed(byte *_pb = &buffer[offset1])
                {
                    byte *pb    = _pb;
                    byte *pbMax = pb + length;

                    fixed(char *_pch = s2)
                    {
                        char *pch = _pch;
                        // Try to do the fast comparison in ASCII space
                        int t = 0;

                        while (pb < pbMax && *pb < 0x80)
                        {
                            t = *pb - (byte)*pch;
                            // The code generated is better if we break out then return
                            if (t != 0)
                            {
                                break;
                            }
                            pb++;
                            pch++;
                        }
                        if (t != 0)
                        {
                            return(false);
                        }
                        if (pb == pbMax)
                        {
                            return(byteLength == charLength);
                        }
                    }
                }

                return(XmlConverter.ToString(buffer, offset1, length1) == s2);
            }
        }
Example #11
0
        public void Save(string filename)
        {
            string folder = Path.GetDirectoryName(filename);

            if (!Directory.Exists(folder)) Directory.CreateDirectory(folder);

            XmlConverter xmlConverter = new XmlConverter();
            XmlDocument xmlDocument = new XmlDocument();

            xmlDocument.AppendChild(xmlDocument.CreateXmlDeclaration("1.0", null, null));

            ConversionContext context = new ConversionContext { Culture = Culture.Invariant, Device = xmlDocument };

            XmlNode xmlNode = xmlConverter.ConvertTo(this, context) as XmlNode;

            if (xmlNode != null)
            {
                xmlDocument.AppendChild(xmlNode);

                xmlDocument.Save(filename);
            }
        }
Example #12
0
        public static AppSettings Load(string filename)
        {
            if (!File.Exists(filename))
                throw new FileNotFoundException(filename);

            XmlDocument xmlDocument = new XmlDocument();
            xmlDocument.Load(filename);

            XmlConverter xmlConverter = new XmlConverter();
            ConversionContext context = new ConversionContext { Culture = Culture.Invariant, Device = xmlDocument };

            AppSettings settings = xmlConverter.ConvertFrom(xmlDocument, context) as AppSettings;

            return settings;
        }
Example #13
0
 public override void WriteValue(XmlDictionaryString value)
 {
     if (((this.depth != 0) || (this.mimeWriter.WriteState != MimeWriterState.Closed)) || !XmlConverter.IsWhitespace(value.Value))
     {
         this.WriteBase64InlineIfPresent();
         this.Writer.WriteValue(value);
     }
 }
Example #14
0
 public override void WriteString(string text)
 {
     if (((this.depth != 0) || (this.mimeWriter.WriteState != MimeWriterState.Closed)) || !XmlConverter.IsWhitespace(text))
     {
         this.WriteBase64InlineIfPresent();
         this.Writer.WriteString(text);
     }
 }
        public unsafe bool Equals2(int offset1, int length1, string s2)
        {
            int num    = length1;
            int length = s2.Length;

            if ((num < length) || (num > (length * 3)))
            {
                return(false);
            }
            byte[] buffer = this.buffer;
            if (length1 < 8)
            {
                int num3 = Math.Min(num, length);
                int num4 = offset1;
                for (int i = 0; i < num3; i++)
                {
                    byte num6 = buffer[num4 + i];
                    if (num6 >= 0x80)
                    {
                        return(XmlConverter.ToString(buffer, offset1, length1) == s2);
                    }
                    if (s2[i] != num6)
                    {
                        return(false);
                    }
                }
                return(num == length);
            }
            int num7 = Math.Min(num, length);

            fixed(byte *numRef = &(buffer[offset1]))
            {
                byte *numPtr  = numRef;
                byte *numPtr2 = numPtr + num7;

                fixed(char *str = ((char *)s2))
                {
                    char *chPtr2 = str;
                    int   num8   = 0;

                    while ((numPtr < numPtr2) && (numPtr[0] < 0x80))
                    {
                        num8 = numPtr[0] - ((byte)chPtr2[0]);
                        if (num8 != 0)
                        {
                            break;
                        }
                        numPtr++;
                        chPtr2++;
                    }
                    if (num8 != 0)
                    {
                        return(false);
                    }
                    if (numPtr == numPtr2)
                    {
                        return(num == length);
                    }
                }
            }

            return(XmlConverter.ToString(buffer, offset1, length1) == s2);
        }
Example #16
0
        public string GetString()
        {
            ValueHandleType type = this.type;

            if (type == ValueHandleType.UTF8)
            {
                return(this.GetCharsText());
            }
            switch (type)
            {
            case ValueHandleType.Empty:
                return(string.Empty);

            case ValueHandleType.True:
                return("true");

            case ValueHandleType.False:
                return("false");

            case ValueHandleType.Zero:
                return("0");

            case ValueHandleType.One:
                return("1");

            case ValueHandleType.Int8:
            case ValueHandleType.Int16:
            case ValueHandleType.Int32:
                return(XmlConverter.ToString(this.ToInt()));

            case ValueHandleType.Int64:
                return(XmlConverter.ToString(this.GetInt64()));

            case ValueHandleType.UInt64:
                return(XmlConverter.ToString(this.GetUInt64()));

            case ValueHandleType.Single:
                return(XmlConverter.ToString(this.GetSingle()));

            case ValueHandleType.Double:
                return(XmlConverter.ToString(this.GetDouble()));

            case ValueHandleType.Decimal:
                return(XmlConverter.ToString(this.GetDecimal()));

            case ValueHandleType.DateTime:
                return(XmlConverter.ToString(this.ToDateTime()));

            case ValueHandleType.TimeSpan:
                return(XmlConverter.ToString(this.ToTimeSpan()));

            case ValueHandleType.Guid:
                return(XmlConverter.ToString(this.ToGuid()));

            case ValueHandleType.UniqueId:
                return(XmlConverter.ToString(this.ToUniqueId()));

            case ValueHandleType.UTF8:
                return(this.GetCharsText());

            case ValueHandleType.EscapedUTF8:
                return(this.GetEscapedCharsText());

            case ValueHandleType.Base64:
                return(Base64Encoding.GetString(this.ToByteArray()));

            case ValueHandleType.Dictionary:
                return(this.GetDictionaryString().Value);

            case ValueHandleType.List:
                return(XmlConverter.ToString(this.ToList()));

            case ValueHandleType.Char:
                return(this.GetCharText());

            case ValueHandleType.Unicode:
                return(this.GetUnicodeCharsText());

            case ValueHandleType.QName:
                return(this.GetQNameDictionaryText());

            case ValueHandleType.ConstString:
                return(constStrings[this.offset]);
            }
            throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidOperationException());
        }