-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathZTXFormat.cs
73 lines (62 loc) · 2.59 KB
/
ZTXFormat.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
using System;
using System.Collections.Generic;
using System.Linq;
namespace MedSQL_Reader
{
public class ZTXFormat
{
public bool IsValidZTX(string content)
{
return content.Contains("col_01") && content.Contains("<zs>");
}
public string FormatContent(List<Dictionary<string, string>> entries)
{
return string.Join(Environment.NewLine + Environment.NewLine, entries.Select(entry =>
{
return string.Join(Environment.NewLine, entry.Select(kv => $"{kv.Key}: {kv.Value}"));
}));
}
public List<Dictionary<string, string>> ParseLines(string content)
{
List<Dictionary<string, string>> entries = new List<Dictionary<string, string>>();
string[] dataParts = content.Split(new string[] { "<zs>col_09<zs><zs>" }, StringSplitOptions.RemoveEmptyEntries);
foreach (string part in dataParts)
{
Dictionary<string, string> entry = ParsePart(part);
entries.Add(entry);
}
return entries;
}
private Dictionary<string, string> ParsePart(string part)
{
Dictionary<string, string> entry = new Dictionary<string, string>();
string[] tokens = part.Split(new string[] { "<zs>col_01<zs>" }, StringSplitOptions.RemoveEmptyEntries);
string[] firstTokenKeyValue = tokens[0].Split(new string[] { "<zs>," }, StringSplitOptions.RemoveEmptyEntries);
foreach (string keyValue in firstTokenKeyValue)
{
string[] pair = keyValue.Split(new string[] { "<zs>" }, StringSplitOptions.RemoveEmptyEntries);
if (pair.Length == 2)
{
string key = pair[0].Trim();
string value = pair[1].Trim();
entry[key] = value;
}
}
for (int i = 1; i < tokens.Length; i++)
{
string[] keyValuePairs = tokens[i].Split(new string[] { "<zs>," }, StringSplitOptions.RemoveEmptyEntries);
foreach (string pair in keyValuePairs)
{
string[] pairValues = pair.Split(new string[] { "<zs>" }, StringSplitOptions.RemoveEmptyEntries);
if (pairValues.Length == 2)
{
string key = pairValues[0].Trim();
string value = pairValues[1].Trim();
entry[key] = value;
}
}
}
return entry;
}
}
}