コード例 #1
0
        /// <summary>
        /// Build the table from the supplied reader and the table name supplied
        /// </summary>
        /// <param name="reader">The reader</param>
        /// <param name="tableName">The table name</param>
        protected virtual InpTable BuildTable(IInpReader reader, string tableName)
        {
            _ = reader ?? throw new ArgumentNullException(nameof(reader));

            var table    = new InpTable(tableName);
            var comments = new StringBuilder();

            // While the reader is not at the end of a section and the
            // reader is not at the end of the stream build the table
            while (!reader.EndOfStream && !EndOfSection(reader.PeekLine() ?? ""))
            {
                // Get the next line from the reader
                var line = reader.ReadLine();

                // If the line contains a comment append it
                // to the comments strings builder
                if (IsComment(line))
                {
                    comments.Append(line);
                    continue;
                }

                // Get the tokens from the string and
                // add them to the table
                var tokens = GetTokens(line);
                if (tokens.Length > 0)
                {
                    table.AddRow(new InpTableRow(tokens[0], tokens, comments.ToString(), table));
                }
            }

            return(table);
        }
コード例 #2
0
        /// <summary>
        /// Parse the file supplied with the reader and construct the
        /// project
        /// </summary>
        /// <param name="inpProject">The project that will be updated</param>
        /// <param name="reader">The reader that will be used to read the file</param>
        public void ParseFile(IInpProject inpProject, IInpReader reader)
        {
            try
            {
                _ = reader ?? throw new ArgumentNullException(nameof(reader));
                _ = inpProject ?? throw new ArgumentNullException(nameof(inpProject));

                while (!reader.EndOfStream)
                {
                    // Read the next line from the stream
                    var line = reader.ReadLine();

                    // if the current string is the start of a section
                    // read the section name from the string
                    var sectionName = string.Empty;
                    if (line.Contains(StartHeaderString, StringComparison.CurrentCulture))
                    {
                        sectionName = line.Trim((StartHeaderString + EndHeaderString).ToCharArray());
                    }

                    // If the sectionName is null or empty then continue on to the next string
                    if (string.IsNullOrEmpty(sectionName))
                    {
                        continue;
                    }

                    // Add this table to the tables list
                    InpTables.Add(BuildTable(reader, sectionName));
                }

                // Update the database from the tables created above
                inpProject.Database.UpdateDatabase(InpTables);
            }
            catch (Exception inner)
            {
                throw new InpFileException($"The file at {inpProject?.InpFile} could not be read", inner);
            }
            finally
            {
                // Maybe should do something here
            }
        }