private void ReadTextResources(string fileName)
        {
            using (LineNumberStreamReader reader = new LineNumberStreamReader(fileName, new UTF8Encoding(true), true))
            {
                StringBuilder builder = new StringBuilder(0xff);
                StringBuilder builder2 = new StringBuilder(0x800);
                int num = reader.Read();
                while (num != -1)
                {
                    switch (num)
                    {
                        case 10:
                        case 13:
                        {
                            num = reader.Read();
                            continue;
                        }
                        case 0x23:
                        case 9:
                        case 0x20:
                        case 0x3b:
                        {
                            reader.ReadLine();
                            num = reader.Read();
                            continue;
                        }
                        case 0x5b:
                        {
                            string str = reader.ReadLine();
                            if (!str.Equals("strings]"))
                            {
                                throw new TextFileException(this.logger.FormatResourceString("GenerateResource.UnexpectedInfBracket", new object[] { "[" + str }), fileName, reader.LineNumber - 1, 1);
                            }
                            this.logger.LogWarningWithCodeFromResources(null, fileName, reader.LineNumber - 1, 1, 0, 0, "GenerateResource.ObsoleteStringsTag", new object[0]);
                            num = reader.Read();
                            continue;
                        }
                        default:
                            builder.Length = 0;
                            while (num != 0x3d)
                            {
                                if ((num == 13) || (num == 10))
                                {
                                    throw new TextFileException(this.logger.FormatResourceString("GenerateResource.NoEqualsInLine", new object[] { builder }), fileName, reader.LineNumber, reader.LinePosition);
                                }
                                builder.Append((char) num);
                                num = reader.Read();
                                if (num == -1)
                                {
                                    break;
                                }
                            }
                            break;
                    }
                    if (builder.Length == 0)
                    {
                        throw new TextFileException(this.logger.FormatResourceString("GenerateResource.NoNameInLine", new object[0]), fileName, reader.LineNumber, reader.LinePosition);
                    }
                    if (builder[builder.Length - 1] == ' ')
                    {
                        builder.Length--;
                    }
                    num = reader.Read();
                    if (num == 0x20)
                    {
                        num = reader.Read();
                    }
                    builder2.Length = 0;
                    while (num != -1)
                    {
                        char[] chArray;
                        int num2;
                        int num3;
                        int num4;
                        bool flag = false;
                        if (num != 0x5c)
                        {
                            goto Label_03AA;
                        }
                        num = reader.Read();
                        switch (num)
                        {
                            case 0x72:
                                num = 13;
                                flag = true;
                                goto Label_03AA;

                            case 0x74:
                                num = 9;
                                goto Label_03AA;

                            case 0x75:
                                chArray = new char[4];
                                num2 = 4;
                                num3 = 0;
                                goto Label_02B0;

                            case 110:
                                num = 10;
                                flag = true;
                                goto Label_03AA;

                            case 0x22:
                                num = 0x22;
                                goto Label_03AA;

                            case 0x5c:
                                goto Label_03AA;

                            default:
                                throw new TextFileException(this.logger.FormatResourceString("GenerateResource.InvalidEscape", new object[] { builder.ToString(), (char) num }), fileName, reader.LineNumber, reader.LinePosition);
                        }
                    Label_024E:
                        num4 = reader.Read(chArray, num3, num2);
                        if (num4 == 0)
                        {
                            throw new TextFileException(this.logger.FormatResourceString("GenerateResource.InvalidEscape", new object[] { builder.ToString(), (char) num }), fileName, reader.LineNumber, reader.LinePosition);
                        }
                        num3 += num4;
                        num2 -= num4;
                    Label_02B0:
                        if (num2 > 0)
                        {
                            goto Label_024E;
                        }
                        try
                        {
                            num = ushort.Parse(new string(chArray), NumberStyles.HexNumber, CultureInfo.CurrentCulture);
                        }
                        catch (FormatException)
                        {
                            throw new TextFileException(this.logger.FormatResourceString("GenerateResource.InvalidHexEscapeValue", new object[] { builder.ToString(), new string(chArray) }), fileName, reader.LineNumber, reader.LinePosition);
                        }
                        catch (OverflowException)
                        {
                            throw new TextFileException(this.logger.FormatResourceString("GenerateResource.InvalidHexEscapeValue", new object[] { builder.ToString(), new string(chArray) }), fileName, reader.LineNumber, reader.LinePosition);
                        }
                        flag = (num == 10) || (num == 13);
                    Label_03AA:
                        if (!flag)
                        {
                            if (num == 13)
                            {
                                num = reader.Read();
                                if (num != -1)
                                {
                                    if (num != 10)
                                    {
                                        goto Label_03DA;
                                    }
                                    num = reader.Read();
                                }
                                break;
                            }
                            if (num == 10)
                            {
                                num = reader.Read();
                                break;
                            }
                        }
                    Label_03DA:
                        builder2.Append((char) num);
                        num = reader.Read();
                    }
                    this.AddResource(builder.ToString(), builder2.ToString(), fileName, reader.LineNumber, reader.LinePosition);
                }
            }
        }
        private void ReadTextResources(string fileName)
        {
            using (LineNumberStreamReader reader = new LineNumberStreamReader(fileName, new UTF8Encoding(true), true))
            {
                StringBuilder builder  = new StringBuilder(0xff);
                StringBuilder builder2 = new StringBuilder(0x800);
                int           num      = reader.Read();
                while (num != -1)
                {
                    switch (num)
                    {
                    case 10:
                    case 13:
                    {
                        num = reader.Read();
                        continue;
                    }

                    case 0x23:
                    case 9:
                    case 0x20:
                    case 0x3b:
                    {
                        reader.ReadLine();
                        num = reader.Read();
                        continue;
                    }

                    case 0x5b:
                    {
                        string str = reader.ReadLine();
                        if (!str.Equals("strings]"))
                        {
                            throw new TextFileException(this.logger.FormatResourceString("GenerateResource.UnexpectedInfBracket", new object[] { "[" + str }), fileName, reader.LineNumber - 1, 1);
                        }
                        this.logger.LogWarningWithCodeFromResources(null, fileName, reader.LineNumber - 1, 1, 0, 0, "GenerateResource.ObsoleteStringsTag", new object[0]);
                        num = reader.Read();
                        continue;
                    }

                    default:
                        builder.Length = 0;
                        while (num != 0x3d)
                        {
                            if ((num == 13) || (num == 10))
                            {
                                throw new TextFileException(this.logger.FormatResourceString("GenerateResource.NoEqualsInLine", new object[] { builder }), fileName, reader.LineNumber, reader.LinePosition);
                            }
                            builder.Append((char)num);
                            num = reader.Read();
                            if (num == -1)
                            {
                                break;
                            }
                        }
                        break;
                    }
                    if (builder.Length == 0)
                    {
                        throw new TextFileException(this.logger.FormatResourceString("GenerateResource.NoNameInLine", new object[0]), fileName, reader.LineNumber, reader.LinePosition);
                    }
                    if (builder[builder.Length - 1] == ' ')
                    {
                        builder.Length--;
                    }
                    num = reader.Read();
                    if (num == 0x20)
                    {
                        num = reader.Read();
                    }
                    builder2.Length = 0;
                    while (num != -1)
                    {
                        char[] chArray;
                        int    num2;
                        int    num3;
                        int    num4;
                        bool   flag = false;
                        if (num != 0x5c)
                        {
                            goto Label_03AA;
                        }
                        num = reader.Read();
                        switch (num)
                        {
                        case 0x72:
                            num  = 13;
                            flag = true;
                            goto Label_03AA;

                        case 0x74:
                            num = 9;
                            goto Label_03AA;

                        case 0x75:
                            chArray = new char[4];
                            num2    = 4;
                            num3    = 0;
                            goto Label_02B0;

                        case 110:
                            num  = 10;
                            flag = true;
                            goto Label_03AA;

                        case 0x22:
                            num = 0x22;
                            goto Label_03AA;

                        case 0x5c:
                            goto Label_03AA;

                        default:
                            throw new TextFileException(this.logger.FormatResourceString("GenerateResource.InvalidEscape", new object[] { builder.ToString(), (char)num }), fileName, reader.LineNumber, reader.LinePosition);
                        }
Label_024E:
                        num4 = reader.Read(chArray, num3, num2);
                        if (num4 == 0)
                        {
                            throw new TextFileException(this.logger.FormatResourceString("GenerateResource.InvalidEscape", new object[] { builder.ToString(), (char)num }), fileName, reader.LineNumber, reader.LinePosition);
                        }
                        num3 += num4;
                        num2 -= num4;
Label_02B0:
                        if (num2 > 0)
                        {
                            goto Label_024E;
                        }
                        try
                        {
                            num = ushort.Parse(new string(chArray), NumberStyles.HexNumber, CultureInfo.CurrentCulture);
                        }
                        catch (FormatException)
                        {
                            throw new TextFileException(this.logger.FormatResourceString("GenerateResource.InvalidHexEscapeValue", new object[] { builder.ToString(), new string(chArray) }), fileName, reader.LineNumber, reader.LinePosition);
                        }
                        catch (OverflowException)
                        {
                            throw new TextFileException(this.logger.FormatResourceString("GenerateResource.InvalidHexEscapeValue", new object[] { builder.ToString(), new string(chArray) }), fileName, reader.LineNumber, reader.LinePosition);
                        }
                        flag = (num == 10) || (num == 13);
Label_03AA:
                        if (!flag)
                        {
                            if (num == 13)
                            {
                                num = reader.Read();
                                if (num != -1)
                                {
                                    if (num != 10)
                                    {
                                        goto Label_03DA;
                                    }
                                    num = reader.Read();
                                }
                                break;
                            }
                            if (num == 10)
                            {
                                num = reader.Read();
                                break;
                            }
                        }
Label_03DA:
                        builder2.Append((char)num);
                        num = reader.Read();
                    }
                    this.AddResource(builder.ToString(), builder2.ToString(), fileName, reader.LineNumber, reader.LinePosition);
                }
            }
        }
Ejemplo n.º 3
0
        /// <summary>
        /// Read resources from a text format file
        /// </summary>
        /// <param name="fileName">Input resources filename</param>
        private void ReadTextResources(ReaderInfo reader, String fileName)
        {
            // Check for byte order marks in the beginning of the input file, but
            // default to UTF-8.
            using (LineNumberStreamReader sr = new LineNumberStreamReader(fileName, new UTF8Encoding(true), true))
            {
                StringBuilder name = new StringBuilder(255);
                StringBuilder value = new StringBuilder(2048);

                int ch = sr.Read();
                while (ch != -1)
                {
                    if (ch == '\n' || ch == '\r')
                    {
                        ch = sr.Read();
                        continue;
                    }

                    // Skip over commented lines or ones starting with whitespace.
                    // Support LocStudio INF format's comment char, ';'
                    if (ch == '#' || ch == '\t' || ch == ' ' || ch == ';')
                    {
                        // comment char (or blank line) - skip line.
                        sr.ReadLine();
                        ch = sr.Read();
                        continue;
                    }
                    // Note that in Beta of version 1 we recommended users should put a [strings] 
                    // section in their file.  Now it's completely unnecessary and can 
                    // only cause bugs.  We will not parse anything using '[' stuff now
                    // and we should give a warning about seeing [strings] stuff.
                    // In V1.1 or V2, we can rip this out completely, I hope.
                    if (ch == '[')
                    {
                        String skip = sr.ReadLine();
                        if (skip.Equals("strings]"))
                            _logger.LogWarningWithCodeFromResources(null, fileName, sr.LineNumber - 1, 1, 0, 0, "GenerateResource.ObsoleteStringsTag");
                        else
                        {
                            throw new TextFileException(_logger.FormatResourceString("GenerateResource.UnexpectedInfBracket", "[" + skip), fileName, sr.LineNumber - 1, 1);
                        }
                        ch = sr.Read();
                        continue;
                    }

                    // Read in name
                    name.Length = 0;
                    while (ch != '=')
                    {
                        if (ch == '\r' || ch == '\n')
                            throw new TextFileException(_logger.FormatResourceString("GenerateResource.NoEqualsInLine", name), fileName, sr.LineNumber, sr.LinePosition);

                        name.Append((char)ch);
                        ch = sr.Read();
                        if (ch == -1)
                            break;
                    }
                    if (name.Length == 0)
                        throw new TextFileException(_logger.FormatResourceString("GenerateResource.NoNameInLine"), fileName, sr.LineNumber, sr.LinePosition);

                    // For the INF file, we must allow a space on both sides of the equals
                    // sign.  Deal with it.
                    if (name[name.Length - 1] == ' ')
                    {
                        name.Length = name.Length - 1;
                    }
                    ch = sr.Read(); // move past =
                    // If it exists, move past the first space after the equals sign.
                    if (ch == ' ')
                        ch = sr.Read();

                    // Read in value
                    value.Length = 0;

                    while (ch != -1)
                    {
                        // Did we read @"\r" or @"\n"?
                        bool quotedNewLine = false;
                        if (ch == '\\')
                        {
                            ch = sr.Read();
                            switch (ch)
                            {
                                case '\\':
                                    // nothing needed
                                    break;
                                case 'n':
                                    ch = '\n';
                                    quotedNewLine = true;
                                    break;
                                case 'r':
                                    ch = '\r';
                                    quotedNewLine = true;
                                    break;
                                case 't':
                                    ch = '\t';
                                    break;
                                case '"':
                                    ch = '\"';
                                    break;
                                case 'u':
                                    char[] hex = new char[4];
                                    int numChars = 4;
                                    int index = 0;
                                    while (numChars > 0)
                                    {
                                        int n = sr.Read(hex, index, numChars);
                                        if (n == 0)
                                            throw new TextFileException(_logger.FormatResourceString("GenerateResource.InvalidEscape", name.ToString(), (char)ch), fileName, sr.LineNumber, sr.LinePosition);
                                        index += n;
                                        numChars -= n;
                                    }
                                    try
                                    {
                                        ch = (char)UInt16.Parse(new String(hex), NumberStyles.HexNumber, CultureInfo.CurrentCulture);
                                    }
                                    catch (FormatException)
                                    {
                                        // We know about this one...
                                        throw new TextFileException(_logger.FormatResourceString("GenerateResource.InvalidHexEscapeValue", name.ToString(), new String(hex)), fileName, sr.LineNumber, sr.LinePosition);
                                    }
                                    catch (OverflowException)
                                    {
                                        // We know about this one, too...
                                        throw new TextFileException(_logger.FormatResourceString("GenerateResource.InvalidHexEscapeValue", name.ToString(), new String(hex)), fileName, sr.LineNumber, sr.LinePosition);
                                    }
                                    quotedNewLine = (ch == '\n' || ch == '\r');
                                    break;

                                default:
                                    throw new TextFileException(_logger.FormatResourceString("GenerateResource.InvalidEscape", name.ToString(), (char)ch), fileName, sr.LineNumber, sr.LinePosition);
                            }
                        }

                        // Consume endline...
                        //   Endline can be \r\n or \n.  But do not treat a 
                        //   quoted newline (ie, @"\r" or @"\n" in text) as a
                        //   real new line.  They aren't the end of a line.
                        if (!quotedNewLine)
                        {
                            if (ch == '\r')
                            {
                                ch = sr.Read();
                                if (ch == -1)
                                {
                                    break;
                                }
                                else if (ch == '\n')
                                {
                                    ch = sr.Read();
                                    break;
                                }
                            }
                            else if (ch == '\n')
                            {
                                ch = sr.Read();
                                break;
                            }
                        }

                        value.Append((char)ch);
                        ch = sr.Read();
                    }

                    // Note that value can be an empty string
                    AddResource(reader, name.ToString(), value.ToString(), fileName, sr.LineNumber, sr.LinePosition);
                }
            }
        }
Ejemplo n.º 4
0
        private static void ReadTextResources(String fileName)
        {
            // Check for byte order marks in the beginning of the input file, but
            // default to UTF-8.
            using (LineNumberStreamReader sr = new LineNumberStreamReader(fileName, new UTF8Encoding(true), true))
            {
                StringBuilder name  = new StringBuilder(255);
                StringBuilder value = new StringBuilder(2048);

                int ch = sr.Read();
                while (ch != -1)
                {
                    if (ch == '\n' || ch == '\r')
                    {
                        ch = sr.Read();
                        continue;
                    }

                    // Skip over commented lines or ones starting with whitespace.
                    // Support LocStudio INF format's comment char, ';'
                    if (ch == '#' || ch == '\t' || ch == ' ' || ch == ';')
                    {
                        // comment char (or blank line) - skip line.
                        sr.ReadLine();
                        ch = sr.Read();
                        continue;
                    }
                    // Note that in Beta we recommended users should put a [strings]
                    // section in their file.  Now it's completely unnecessary and can
                    // only cause bugs.  We will not parse anything using '[' stuff now
                    // and we should give a warning about seeing [strings] stuff.
                    // In V1.1 or V2, we can rip this out completely, I hope.
                    if (ch == '[')
                    {
                        String skip = sr.ReadLine();
                        if (skip.Equals("strings]"))
                        {
                            Warning(SR.GetString(SR.StringsTagObsolete), fileName, sr.LineNumber - 1, 1);
                        }
                        else
                        {
                            throw new TextFileException(SR.GetString(SR.INFFileBracket, skip), fileName, sr.LineNumber - 1, 1);
                        }
                        ch = sr.Read();
                        continue;
                    }

                    // Read in name
                    name.Length = 0;
                    while (ch != '=')
                    {
                        if (ch == '\r' || ch == '\n')
                        {
                            throw new TextFileException(SR.GetString(SR.NoEqualsWithNewLine, name.Length, name), fileName, sr.LineNumber, sr.LinePosition);
                        }

                        name.Append((char)ch);
                        ch = sr.Read();
                        if (ch == -1)
                        {
                            break;
                        }
                    }
                    if (name.Length == 0)
                    {
                        throw new TextFileException(SR.GetString(SR.NoEquals), fileName, sr.LineNumber, sr.LinePosition);
                    }

                    // For the INF file, we must allow a space on both sides of the equals
                    // sign.  Deal with it.
                    if (name[name.Length - 1] == ' ')
                    {
                        name.Length = name.Length - 1;
                    }
                    ch = sr.Read(); // move past =
                    // If it exists, move past the first space after the equals sign.
                    if (ch == ' ')
                    {
                        ch = sr.Read();
                    }

                    // Read in value
                    value.Length = 0;

                    while (ch != -1)
                    {
                        // Did we read @"\r" or @"\n"?
                        bool quotedNewLine = false;
                        if (ch == '\\')
                        {
                            ch = sr.Read();
                            switch (ch)
                            {
                            case '\\':
                                // nothing needed
                                break;

                            case 'n':
                                ch            = '\n';
                                quotedNewLine = true;
                                break;

                            case 'r':
                                ch            = '\r';
                                quotedNewLine = true;
                                break;

                            case 't':
                                ch = '\t';
                                break;

                            case '"':
                                ch = '\"';
                                break;

                            case 'u':
                                char[] hex      = new char[4];
                                int    numChars = 4;
                                int    index    = 0;
                                while (numChars > 0)
                                {
                                    int n = sr.Read(hex, index, numChars);
                                    if (n == 0)
                                    {
                                        throw new TextFileException(SR.GetString(SR.BadEscape, (char)ch, name.ToString()), fileName, sr.LineNumber, sr.LinePosition);
                                    }
                                    index    += n;
                                    numChars -= n;
                                }
                                ch            = (char)UInt16.Parse(new String(hex), NumberStyles.HexNumber, CultureInfo.InvariantCulture);
                                quotedNewLine = (ch == '\n' || ch == '\r');
                                break;

                            default:
                                throw new TextFileException(SR.GetString(SR.BadEscape, (char)ch, name.ToString()), fileName, sr.LineNumber, sr.LinePosition);
                            }
                        }

                        // Consume endline...
                        //   Endline can be \r\n or \n.  But do not treat a
                        //   quoted newline (ie, @"\r" or @"\n" in text) as a
                        //   real new line.  They aren't the end of a line.
                        if (!quotedNewLine)
                        {
                            if (ch == '\r')
                            {
                                ch = sr.Read();
                                if (ch == -1)
                                {
                                    break;
                                }
                                else if (ch == '\n')
                                {
                                    ch = sr.Read();
                                    break;
                                }
                            }
                            else if (ch == '\n')
                            {
                                ch = sr.Read();
                                break;
                            }
                        }

                        value.Append((char)ch);
                        ch = sr.Read();
                    }

                    // Note that value can be an empty string

                    AddResource(name.ToString(), value.ToString(), fileName, sr.LineNumber, sr.LinePosition);
                }
            }
        }