// Copyright (c) Dr. Dirk Lellinger. All rights reserved. // Licensed under the MIT license. See LICENSE file in the project root for full license information. using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Text; using System.Threading.Tasks; namespace SlobViewer.Text { /// /// Reads a text file as can be found at the TU Chemnitz (see ) /// public class TextReader { /// /// The full file name of the text file. /// string _fileName; static readonly char[] _partSeparator = new char[] { '|' }; static readonly char[] _subPartSeparator = new char[] { ';' }; /// Initializes a new instance of the class. /// Full file name of the text file. public TextReader(string fileName) { _fileName = fileName; } /// /// Reads from the text file given in the constructor of this class and generates a dictionary. /// /// A dictionary of key-value pairs. public Dictionary Read() { var dictionary = new Dictionary(); var keyList = new List(); using (var stream = new FileStream(_fileName, FileMode.Open, FileAccess.Read, FileShare.Read)) { string line; using (var xr = new StreamReader(stream)) { while (null != (line = xr.ReadLine())) { if (line.Length == 0 || line[0] == '#') continue; // a line is separated by two colons in the part of the one language and the part with the other language // inside every part there may be additional separations by | (hopefully the same number in both language parts) var idx = line.IndexOf("::"); if (idx < 0) continue; var part1 = line.Substring(0, idx); var part2 = line.Substring(idx + 2); var parts1 = part1.Split(_partSeparator, StringSplitOptions.None); var parts2 = part2.Split(_partSeparator, StringSplitOptions.None); for (int i = 0; i < parts2.Length; ++i) { var keyt = parts2[i].Trim(); var value = parts1[i].Trim(); var keyParts = keyt.Split(_subPartSeparator, StringSplitOptions.RemoveEmptyEntries); foreach (var keya in keyParts) { var key = keya.Trim(); if (!string.IsNullOrEmpty(key) && !string.IsNullOrEmpty(value)) { if (dictionary.TryGetValue(key, out var existingValue)) { existingValue += "\r\n" + value; dictionary[key] = existingValue; } else { dictionary.Add(key, value); } } } } } } } return dictionary; } } }