Beispiel #1
0
 /// <summary>
 /// Parses an input po file.
 /// </summary>
 public void Parse(string text, IGettextParserRequestor requestor)
 {
     Parse(new StringReader(text), requestor);
 }
Beispiel #2
0
        /// <summary>
        /// Parses an input po file.
        /// </summary>
        public void Parse(TextReader reader, IGettextParserRequestor requestor)
        {
            const int StateWaitingKey     = 1;
            const int StateConsumingKey   = 2;
            const int StateConsumingValue = 3;

            int state = StateWaitingKey;

            StringBuilder currentKey   = null;
            StringBuilder currentValue = null;

            string line;

            while (true)
            {
                line = reader.ReadLine();
                line = line == null ? null : line.Trim();
                if (line == null || line.Length == 0)
                {
                    if (state == StateConsumingValue &&
                        currentKey != null &&
                        currentValue != null)
                    {
                        requestor.Handle(currentKey.ToString().Replace("\\n", "\n").Replace("\\\"", "\""),
                                         currentValue.ToString().Replace("\\n", "\n").Replace("\\\"", "\""));
                        currentKey   = null;
                        currentValue = null;
                    }

                    if (line == null)
                    {
                        break;
                    }

                    state = StateWaitingKey;
                    continue;
                }
                else if (line[0] == '#')
                {
                    continue;
                }

                bool isMsgId  = line.StartsWith("msgid ");
                bool isMsgStr = !isMsgId && line.StartsWith("msgstr ");

                if (isMsgId || isMsgStr)
                {
                    state = isMsgId ? StateConsumingKey : StateConsumingValue;

                    int firstQuote = line.IndexOf('"');
                    if (firstQuote == -1)
                    {
                        continue;
                    }

                    int secondQuote = line.IndexOf('"', firstQuote + 1);
                    while (secondQuote != -1 && line[secondQuote - 1] == '\\')
                    {
                        secondQuote = line.IndexOf('"', secondQuote + 1);
                    }
                    if (secondQuote == -1)
                    {
                        continue;
                    }

                    string piece = line.Substring(firstQuote + 1, secondQuote - firstQuote - 1);

                    if (isMsgId)
                    {
                        currentKey = new StringBuilder();
                        currentKey.Append(piece);
                    }
                    else
                    {
                        currentValue = new StringBuilder();
                        currentValue.Append(piece);
                    }
                }
                else if (line[0] == '"')
                {
                    if (line[line.Length - 1] == '"')
                    {
                        line = line.Substring(1, line.Length - 2);
                    }
                    else
                    {
                        line = line.Substring(1, line.Length - 1);
                    }

                    switch (state)
                    {
                    case StateConsumingKey:
                        currentKey.Append(line);
                        break;

                    case StateConsumingValue:
                        currentValue.Append(line);
                        break;
                    }
                }
            }
        }
Beispiel #3
0
 public void Parse(string text, IGettextParserRequestor requestor)
 {
     this.Parse(new StringReader(text), requestor);
 }
Beispiel #4
0
 public void Parse(TextReader reader, IGettextParserRequestor requestor)
 {
     int num = 1;
     StringBuilder stringBuilder = null;
     StringBuilder stringBuilder2 = null;
     while (true)
     {
         string text = reader.ReadLine();
         text = ((text == null) ? null : text.Trim());
         if (text == null || text.Length == 0)
         {
             if (num == 3 && stringBuilder != null && stringBuilder2 != null)
             {
                 requestor.Handle(stringBuilder.ToString().Replace("\\n", "\n"), stringBuilder2.ToString().Replace("\\n", "\n"));
                 stringBuilder = null;
                 stringBuilder2 = null;
             }
             if (text == null)
             {
                 break;
             }
             num = 1;
         }
         else
         {
             if (text[0] != '#')
             {
                 bool flag = text.StartsWith("msgid ");
                 bool flag2 = !flag && text.StartsWith("msgstr ");
                 if (flag || flag2)
                 {
                     num = (flag ? 2 : 3);
                     int num2 = text.IndexOf('"');
                     if (num2 != -1)
                     {
                         int num3 = text.IndexOf('"', num2 + 1);
                         if (num3 != -1)
                         {
                             string value = text.Substring(num2 + 1, num3 - num2 - 1);
                             if (flag)
                             {
                                 stringBuilder = new StringBuilder();
                                 stringBuilder.Append(value);
                             }
                             else
                             {
                                 stringBuilder2 = new StringBuilder();
                                 stringBuilder2.Append(value);
                             }
                         }
                     }
                 }
                 else
                 {
                     if (text[0] == '"')
                     {
                         if (text[text.Length - 1] == '"')
                         {
                             text = text.Substring(1, text.Length - 2);
                         }
                         else
                         {
                             text = text.Substring(1, text.Length - 1);
                         }
                         switch (num)
                         {
                             case 2:
                                 {
                                     stringBuilder.Append(text);
                                     break;
                                 }
                             case 3:
                                 {
                                     stringBuilder2.Append(text);
                                     break;
                                 }
                         }
                     }
                 }
             }
         }
     }
 }
        /// <summary>
        /// Parses an input po file.
        /// </summary>
        public void Parse(TextReader reader, IGettextParserRequestor requestor)
        {
            const int StateWaitingKey = 1;
            const int StateConsumingKey = 2;
            const int StateConsumingValue = 3;

            int state = StateWaitingKey;

            StringBuilder currentKey = null;
            StringBuilder currentValue = null;

            string line;
            while(true) {
                line = reader.ReadLine();
                line = line == null ? null : line.Trim();
                if (line == null || line.Length == 0)
                {
                    if (state == StateConsumingValue &&
                        currentKey != null &&
                        currentValue != null)
                    {
                        requestor.Handle(currentKey.ToString().Replace("\\n", "\n").Replace("\\\"", "\""),
                            currentValue.ToString().Replace("\\n", "\n").Replace("\\\"", "\""));
                        currentKey = null;
                        currentValue = null;
                    }

                    if (line == null)
                        break;

                    state = StateWaitingKey;
                    continue;
                }
                else if (line[0] == '#')
                {
                    continue;
                }

                bool isMsgId = line.StartsWith("msgid ");
                bool isMsgStr = !isMsgId && line.StartsWith("msgstr ");

                if (isMsgId || isMsgStr)
                {
                    state = isMsgId ? StateConsumingKey : StateConsumingValue;

                    int firstQuote = line.IndexOf('"');
                    if (firstQuote == -1)
                        continue;

                    int secondQuote = line.IndexOf('"', firstQuote + 1);
                    while (secondQuote != -1 && line[secondQuote - 1] == '\\')
                        secondQuote = line.IndexOf('"', secondQuote + 1);
                    if (secondQuote == -1)
                        continue;

                    string piece = line.Substring(firstQuote + 1, secondQuote - firstQuote - 1);

                    if (isMsgId)
                    {
                        currentKey = new StringBuilder();
                        currentKey.Append(piece);
                    }
                    else
                    {
                        currentValue = new StringBuilder();
                        currentValue.Append(piece);
                    }
                }
                else if (line[0] == '"')
                {
                    if (line[line.Length - 1] == '"')
                    {
                        line = line.Substring(1, line.Length - 2);
                    }
                    else
                    {
                        line = line.Substring(1, line.Length - 1);
                    }

                    switch (state)
                    {
                        case StateConsumingKey:
                            currentKey.Append(line);
                            break;
                        case StateConsumingValue:
                            currentValue.Append(line);
                            break;
                    }
                }
            }
        }
Beispiel #6
0
        public void Parse(TextReader reader, IGettextParserRequestor requestor)
        {
            int           num            = 1;
            StringBuilder stringBuilder  = null;
            StringBuilder stringBuilder2 = null;

            while (true)
            {
                string text = reader.ReadLine();
                text = ((text == null) ? null : text.Trim());
                if (text == null || text.Length == 0)
                {
                    if (num == 3 && stringBuilder != null && stringBuilder2 != null)
                    {
                        requestor.Handle(stringBuilder.ToString().Replace("\\n", "\n"), stringBuilder2.ToString().Replace("\\n", "\n"));
                        stringBuilder  = null;
                        stringBuilder2 = null;
                    }
                    if (text == null)
                    {
                        break;
                    }
                    num = 1;
                }
                else
                {
                    if (text[0] != '#')
                    {
                        bool flag  = text.StartsWith("msgid ");
                        bool flag2 = !flag && text.StartsWith("msgstr ");
                        if (flag || flag2)
                        {
                            num = (flag ? 2 : 3);
                            int num2 = text.IndexOf('"');
                            if (num2 != -1)
                            {
                                int num3 = text.IndexOf('"', num2 + 1);
                                if (num3 != -1)
                                {
                                    string value = text.Substring(num2 + 1, num3 - num2 - 1);
                                    if (flag)
                                    {
                                        stringBuilder = new StringBuilder();
                                        stringBuilder.Append(value);
                                    }
                                    else
                                    {
                                        stringBuilder2 = new StringBuilder();
                                        stringBuilder2.Append(value);
                                    }
                                }
                            }
                        }
                        else
                        {
                            if (text[0] == '"')
                            {
                                if (text[text.Length - 1] == '"')
                                {
                                    text = text.Substring(1, text.Length - 2);
                                }
                                else
                                {
                                    text = text.Substring(1, text.Length - 1);
                                }
                                switch (num)
                                {
                                case 2:
                                {
                                    stringBuilder.Append(text);
                                    break;
                                }

                                case 3:
                                {
                                    stringBuilder2.Append(text);
                                    break;
                                }
                                }
                            }
                        }
                    }
                }
            }
        }
Beispiel #7
0
        public void Parse(TextReader reader, IGettextParserRequestor requestor)
        {
            const int     Finkey = 2, FindPluralKey = 4, FindMessage = 8;
            string        line;
            bool          isMsgId = false, isMsgPluralId = false, isMsgStr = false;
            string        currentKey = null, pluralKey = null, piece = null;
            StringBuilder msgstrs = new StringBuilder();
            int           state = 0, emptycount = 0;

            while (true)
            {
                line = reader.ReadLine();
                line = line == null ? null : line.Trim();
                if (string.IsNullOrEmpty(line))
                {
                    emptycount++;
                    if (emptycount > 5)
                    {
                        break;
                    }

                    if ((state != Finkey && state != FindPluralKey) && state != FindMessage)
                    {
                        continue;
                    }


                    string[] values = msgstrs.ToString().Split(new string[] { "|~|" }, StringSplitOptions.RemoveEmptyEntries);

                    for (int i = 0; i < values.Length; i++)
                    {
                        values[i] = values[i].Replace("\\n", "\n").Replace("\\\"", "\"");
                    }

                    if (values.Length >= 1)
                    {
                        requestor.Handle(currentKey, values[0]);
                    }

                    if (!string.IsNullOrEmpty(pluralKey))
                    {
                        requestor.Handle(pluralKey, values);
                    }


                    isMsgPluralId = isMsgId = isMsgStr = false;
                    currentKey    = pluralKey = null;
                    state         = 0;
                    if (msgstrs.Length > 0)
                    {
                        msgstrs.Remove(0, msgstrs.Length);
                    }
                    continue;
                }
                else if (line[0] == '#')
                {
                    continue;
                }


                isMsgId = line.StartsWith("msgid ");

                isMsgPluralId = line.StartsWith("msgid_plural ");

                isMsgStr = !isMsgId && System.Text.RegularExpressions.Regex.IsMatch(line, "^msgstr(\\[\\d\\])?(\\s)");

                if (isMsgId || isMsgPluralId || isMsgStr)
                {
                    int firstQuote = line.IndexOf('"');
                    if (firstQuote == -1)
                    {
                        continue;
                    }

                    int secondQuote = line.IndexOf('"', firstQuote + 1);
                    while (secondQuote != -1 && line[secondQuote - 1] == '\\')
                    {
                        secondQuote = line.IndexOf('"', secondQuote + 1);
                    }
                    if (secondQuote == -1)
                    {
                        continue;
                    }

                    piece = line.Substring(firstQuote + 1, secondQuote - firstQuote - 1);

                    if (isMsgId)
                    {
                        currentKey = piece;
                        state      = Finkey;
                        emptycount = 0;
                    }
                    else if (isMsgPluralId)
                    {
                        pluralKey = piece;
                        state     = FindPluralKey;
                    }
                    else if (isMsgStr)
                    {
                        msgstrs.Append(piece);
                        msgstrs.Append("|~|");
                        state = FindMessage;
                    }
                }
            }
        }