|
|
|
|
|
|
|
|
using System; |
|
|
using System.Collections.Generic; |
|
|
using System.IO; |
|
|
using System.Linq; |
|
|
using System.Text; |
|
|
using System.Threading.Tasks; |
|
|
|
|
|
namespace SlobViewer.Text |
|
|
{ |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public class TextReader |
|
|
{ |
|
|
|
|
|
|
|
|
|
|
|
string _fileName; |
|
|
|
|
|
static readonly char[] _partSeparator = new char[] { '|' }; |
|
|
static readonly char[] _subPartSeparator = new char[] { ';' }; |
|
|
|
|
|
|
|
|
|
|
|
public TextReader(string fileName) |
|
|
{ |
|
|
_fileName = fileName; |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public Dictionary<string, string> Read() |
|
|
{ |
|
|
var dictionary = new Dictionary<string, string>(); |
|
|
|
|
|
var keyList = new List<string>(); |
|
|
|
|
|
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; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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; |
|
|
} |
|
|
|
|
|
|
|
|
} |
|
|
} |
|
|
|