コード例 #1
0
        void ParseQRC(List<string> lines)
        {
            const string parseIdError = "Could not parse text '{0}' to an int. Expected message ID value.";

            for (int i = 0; i < lines.Count; i++)
            {
                // Skip empty lines while scanning for start of message block
                if (string.IsNullOrEmpty(lines[i].Trim()))
                    continue;

                // Check for start of message block
                // Only present in QRC section
                // Begins with field Message: (or fixed message type)
                List<Message> messages = new List<Message>();
                string[] parts = SplitField(lines[i]);
                if (messageTypes.HasValue(parts[0]))
                {
                    // Read ID of message
                    int messageID = 0;
                    if (parts[1].StartsWith("[") && parts[1].EndsWith("]"))
                    {
                        // Fixed message types use ID from table
                        messageID = messageTypes.GetInt(idCol, parts[0]);
                        if (messageID == -1)
                            throw new Exception(string.Format(parseIdError, messageTypes.GetInt(idCol, parts[0])));
                    }
                    else
                    {
                        // Other messages use ID from message block header
                        if (!int.TryParse(parts[1], out messageID))
                            throw new Exception(string.Format(parseIdError, parts[1]));
                    }

                    // Keep reading message lines until empty line is found, indicating end of block
                    List<string> messageLines = new List<string>();
                    while (true)
                    {
                        string text = lines[++i].TrimEnd('\r');
                        if (string.IsNullOrEmpty(text))
                            break;
                        else
                            messageLines.Add(text);
                    }

                    // Instantiate message
                    Message message = new Message(messageID, messageLines.ToArray());

                    // Add message to list
                    messages.Add(message);
                }
            }
        }