-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTLODDataParser.cs
127 lines (114 loc) · 4.6 KB
/
TLODDataParser.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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
using System;
using System.Collections.Generic;
using System.Globalization;
using System.IO;
using System.Text;
using System.Text.RegularExpressions;
namespace StepAnalyzer
{
/// <summary>
/// Implementation of DataParser to support files from TodoListOfDeath
/// </summary>
internal class TLODDataParser : DataParser
{
private const string _format = "TodoList Of Death";
public TLODDataParser(string filePath)
{
var content = Normalize(File.ReadAllText(filePath));
var year = getYearFromMetadata(content);
this.stepLengthCm = getStepLengthFromMetadata(content);
content = removeMetadata(content);
this.days = GenerateSADays(content, year);
this.format = _format;
}
private string removeMetadata(string content)
{
StringBuilder newContent = new StringBuilder();
List<string> splittedContent = new List<string>(Regex.Split(content, "##"));
for (int i=2; i < splittedContent.Count; i++)
{
newContent.Append("##");
newContent.Append(splittedContent[i]);
}
return newContent.ToString();
}
private int getStepLengthFromMetadata(string content)
{
content = Regex.Split(content, "##")[1];
string stepLength = Regex.Split(content, "-")[1];
return int.Parse(stepLength.Substring(12));
}
private int getYearFromMetadata(string content)
{
content = Regex.Split(content, "##")[1];
string year = Regex.Split(content, "-")[2];
return int.Parse(year.Substring(9));
}
/// <summary>
/// List of Days for the UI to display
/// </summary>
public List<SADay> days { get; }
/// <summary>
/// Name of the Dataformat this DataParser is implementing
/// </summary>
public string format { get; }
/// <summary>
/// stepLength to calculate kilometers
/// </summary>
public int stepLengthCm { get; }
/// <summary>
/// Normalize input; assume the syntax is always correct
/// </summary>
/// <param name="content">of a TLOD file.</param>
/// <returns></returns>
private string Normalize(string content)
{
// remove unneccesary newlines
content = Regex.Replace(content, "[\r\n]+","", RegexOptions.Multiline | RegexOptions.IgnoreCase);
// remove Details in braces
content = Regex.Replace(content, "\\(.*?\\)", "", RegexOptions.Multiline | RegexOptions.IgnoreCase);
// remove whitespaces
content = Regex.Replace(content, "[ ]", "", RegexOptions.Multiline | RegexOptions.IgnoreCase);
// Output is something like: "##April-08.8762-07.9673-06.9006-05.10062"
return content;
}
/// <summary>
/// Helper to generate a List of the Models later used by the UI
/// </summary>
/// <param name="content">of a TLOD file. Assume content is normalized</param>
/// <param name="year">in format YYYY</param>
/// <returns></returns>
private List<SADay> GenerateSADays(string content, int year)
{
List<SADay> days = new List<SADay>();
List<string> months = new List<string>(Regex.Split(content, "##"));
// remove first item which is always empty
months.RemoveAt(0);
foreach (string month in months)
{
days.AddRange(generateDaysPerMonth(month, year));
}
return days;
}
/// <summary>
/// Helper to generate a List of the Models of a specific month
/// </summary>
/// <param name="month">A written month in German</param>
/// <param name="year">in format YYYY</param>
/// <returns></returns>
private IEnumerable<SADay> generateDaysPerMonth(string month, int year)
{
List<SADay> days = new List<SADay>();
List<string> daysString = new List<string>(Regex.Split(month, "-"));
string monthName = daysString[0];
daysString.RemoveAt(0);
foreach (string day in daysString) {
string dateString = day.Split('.')[0] + monthName + year;
DateTime date = DateTime.ParseExact(dateString, "ddMMMMyyyy", new CultureInfo("de-DE"));
int steps = Int32.Parse(day.Split('.')[1]);
days.Add(new SADay(date, steps));
}
return days;
}
}
}