/// <summary> /// Given a collection of strings from a raw NVP file, organizes them into a collection of NVP Records. /// This function empties the list supplied to it. /// </summary> public static List <NvpRecord> CreateNvpRecords(List <String> dataTextLines) { List <NvpRecord> nvpRecords = new List <NvpRecord>(); Int32 currentLineIndex = 1; while (dataTextLines.Count > 0) { Int32 endLineIndex; List <String> currentBundle; CreateLineBundle(dataTextLines, currentLineIndex, out endLineIndex, out currentBundle); if (currentBundle.Count > 0) // If the bundle length is 0, the file probably contained only blank lines and comments. { NvpRecord record = CreateNvpRecordFromBundle(currentBundle, currentLineIndex, endLineIndex); nvpRecords.Add(record); currentLineIndex = record.LineRangeEnd + 1; } } return(nvpRecords); }
private static NvpRecord CreateNvpRecordFromBundle(List <String> bundle, Int32 startLine, Int32 endLine) { NvpRecord record = new NvpRecord(); record.LineRangeStart = startLine; record.LineRangeEnd = endLine; if (bundle.Count > 0) { // First, get the record type. Int32 recordTypeIndex = bundle[0].IndexOf(NvpRecordStartSymbol); record.RecordType = bundle[0].Substring(0, recordTypeIndex); bundle[0] = bundle[0].Substring(recordTypeIndex); // Strip away the record type part of the record. while (bundle.Count > 0) { // At this point, every line either starts with "|" or ">" (for record start, or continuation) String currentLine = bundle[0].Substring(1); // Grab all but the first character. String[] nvpSet = currentLine.Split(NvpPairSeparator); foreach (String nvp in nvpSet) { if (nvp.Length > 0) // Blank lines after a | or > are allowed, but basically ignored. { String[] nvpSplit = nvp.Split(NameValueDividerSymbol); String nvpKey = nvpSplit[0]; String nvpValue = nvpSplit[1]; if (record.NameValuePairs.ContainsKey(nvpKey)) { String currentValue = record.NameValuePairs[nvpKey]; record.NameValuePairs[nvpKey] = String.Concat(currentValue, NvpDupeKeyValueSeparator, nvpValue); } else { record.NameValuePairs.Add(nvpKey, nvpValue); } } } bundle.RemoveAt(0); // Remove the first line of the bundle. } } return(record); }