/// <summary> /// Return the next line in the file as a collection of words. /// </summary> /// <param name="inData">The in.</param> /// <param name="words">The words.</param> /// <returns></returns> /// <exception cref="System.Exception">Invalid number of values on line: + Line + \r\nin file: + _FileName</exception> private bool GetNextLine(StreamReaderRandomAccess inData, ref StringCollection words) { if (inData.EndOfStream) { return(false); } string Line = inData.ReadLine(); if (Line == null || Line.Length == 0) { return(false); } if (Line.IndexOf("!") > 0) //used to ignore "!" in a row { Line = Line.Substring(0, Line.IndexOf("!") - 1); } if (IsCSVFile) { words.Clear(); Line = Line.TrimEnd(','); words.AddRange(Line.Split(",".ToCharArray())); } else { words = StringUtilities.SplitStringHonouringQuotes(Line, " \t"); } if (words.Count != Headings.Count) { throw new Exception("Invalid number of values on line: " + Line + "\r\nin file: " + _FileName); } // Remove leading / trailing double quote chars. for (int i = 0; i < words.Count; i++) { words[i] = words[i].Trim("\"".ToCharArray()); } return(true); }
/// <summary> /// Read in the APSIM header - headings/units and constants. /// </summary> /// <param name="inData">The in.</param> /// <exception cref="System.Exception">The number of headings and units doesn't match in file: + _FileName</exception> private void ReadApsimHeader(StreamReaderRandomAccess inData) { StringCollection ConstantLines = new StringCollection(); StringCollection HeadingLines = new StringCollection(); ReadApsimHeaderLines(inData, ref ConstantLines, ref HeadingLines); bool TitleFound = false; foreach (string ConstantLine in ConstantLines) { string Line = ConstantLine; string Comment = StringUtilities.SplitOffAfterDelimiter(ref Line, "!"); Comment.Trim(); int PosEquals = Line.IndexOf('='); if (PosEquals != -1) { string Name = Line.Substring(0, PosEquals).Trim(); if (Name.ToLower() == "title") { TitleFound = true; Name = "Title"; } string Value = Line.Substring(PosEquals + 1).Trim(); string Unit = string.Empty; if (Name != "Title") { Unit = StringUtilities.SplitOffBracketedValue(ref Value, '(', ')'); } _Constants.Add(new ApsimConstant(Name, Value, Unit, Comment)); } } if (HeadingLines.Count >= 1) { if (IsCSVFile) { HeadingLines[0] = HeadingLines[0].TrimEnd(','); Headings = new StringCollection(); Units = new StringCollection(); Headings.AddRange(HeadingLines[0].Split(",".ToCharArray())); for (int i = 0; i < Headings.Count; i++) { Headings[i] = Headings[i].Trim(); Headings[i] = Headings[i].Trim('"'); Units.Add("()"); } } else { Headings = StringUtilities.SplitStringHonouringQuotes(HeadingLines[0], " \t"); Units = StringUtilities.SplitStringHonouringQuotes(HeadingLines[1], " \t"); } TitleFound = TitleFound || StringUtilities.IndexOfCaseInsensitive(Headings, "title") != -1; if (Headings.Count != Units.Count) { throw new Exception("The number of headings and units doesn't match in file: " + _FileName); } } if (!TitleFound) { _Constants.Add(new ApsimConstant("Title", System.IO.Path.GetFileNameWithoutExtension(_FileName), "", "")); } }