-
Notifications
You must be signed in to change notification settings - Fork 0
/
STT.cs
395 lines (367 loc) · 15.9 KB
/
STT.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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
namespace STT_CLI
{
public class STT
{
string file; // name of input backup file
// imported types
public List<recordType> recordTypes;
public List<record> records;
public List<category> categories;
public List<recordTypeCategory> recordTypeCategories;
public List<recordTag> recordTags;
public List<recordToRecordTag> recordToRecordTags;
// these are added via CLI but haven't been flushed yet
public List<recordType> add_recordTypes;
public List<record> add_records;
public List<recordToRecordTag> add_recordToRecordTags;
// these are to be deleted by CLI but haven't been flushed yet
public List<int> delete_recordTypes; // both recordTypes and records have ids, so only need one property per type
public List<int> delete_records;
public List<int> delete_recordToRecordTags;
// highest ids for each type with ids (new types increment from here on STT)
public int top_recordType_id;
public int top_record_id;
public int top_category_id;
public int top_recordTag_id;
public STT(string _file)
{
recordTypes = new List<recordType>();
records = new List<record>();
categories = new List<category>();
recordTypeCategories = new List<recordTypeCategory>();
recordTags = new List<recordTag>();
recordToRecordTags = new List<recordToRecordTag>();
add_recordTypes = new List<recordType>();
add_records = new List<record>();
add_recordToRecordTags = new List<recordToRecordTag>();
delete_recordTypes = new List<int>();
delete_records = new List<int>();
delete_recordToRecordTags = new List<int>();
file = _file;
string[] contents = File.ReadAllLines(file);
int i = 1; // first line is header
string[] line;
while (contents[i].Substring(0, "recordType\t".Length) == "recordType\t")
{
line = contents[i].Split('\t');
recordTypes.Add(new recordType(
int.Parse(line[1]),
line[2],
line[3],
int.Parse(line[4]),
line[5],
int.Parse(line[6]),
line[7]
));
i++;
if (i == contents.Length) { break; }
}
top_recordType_id = recordTypes.Max(n => n.id);
while (contents[i].Substring(0, "record\t".Length) == "record\t")
{
line = contents[i].Split('\t');
records.Add(new record(
int.Parse(line[1]),
int.Parse(line[2]),
long.Parse(line[3]),
long.Parse(line[4]),
line[5]
));
i++;
if (i == contents.Length) { break; }
}
top_record_id = records.Max(n => n.id);
while (contents[i].Substring(0, "category\t".Length) == "category\t")
{
line = contents[i].Split('\t');
categories.Add(new category(
int.Parse(line[1]),
line[2],
int.Parse(line[3]),
line[4]
));
i++;
if (i == contents.Length) { break; }
}
top_category_id = categories.Max(n => n.id);
while (contents[i].Substring(0, "typeCategory\t".Length) == "typeCategory\t")
{
line = contents[i].Split('\t');
recordTypeCategories.Add(new recordTypeCategory(
int.Parse(line[1]),
int.Parse(line[2])
));
i++;
if (i == contents.Length) { break; }
}
while (contents[i].Substring(0, "recordTag\t".Length) == "recordTag\t")
{
line = contents[i].Split('\t');
recordTags.Add(new recordTag(
int.Parse(line[1]),
int.Parse(line[2]),
line[3],
int.Parse(line[4]),
line[5],
line[6]
));
i++;
if (i == contents.Length) { break; }
}
top_recordTag_id = recordTags.Max(n => n.id);
while (contents[i].Substring(0, "recordToRecordTag\t".Length) == "recordToRecordTag\t")
{
line = contents[i].Split('\t');
recordToRecordTags.Add(new recordToRecordTag(
int.Parse(line[1]),
int.Parse(line[2])
));
i++;
if (i == contents.Length) { break; }
}
}
// true = successful, false = fail (overlapping property (usually name) with non-flushed deletion)
public bool Add_recordType(int id, string name, string icon, int color, string color_int, int hidden, string goal_time)
{
switch (delete_recordTypes.Any(n => recordTypes[n].name == name))
{
case true:
return false;
case false:
add_recordTypes.Add(new recordType(
id,
name,
icon,
color,
color_int,
hidden,
goal_time
));
top_recordType_id++;
return true;
}
}
public void Add_record(int id, int type_id, long time_started, long time_ended, string comment)
{
add_records.Add(new record(
id,
type_id,
time_started,
time_ended,
comment
));
top_record_id++;
}
public bool Add_recordToRecordTag(int record_id, int record_tag_id)
{
switch (
delete_recordToRecordTags.Any(n => recordToRecordTags[n].record_id == record_id) &&
delete_recordToRecordTags.Any(n => recordToRecordTags[n].record_tag_id == record_tag_id)
) {
case true:
return false;
case false:
add_recordToRecordTags.Add(new recordToRecordTag(
record_id,
record_tag_id
));
return true;
}
}
public void Delete_recordType(int elem)
{
// if the highest id of a type is deleted and needs to be changed
if (recordTypes[elem].id == top_recordType_id)
{
// emulates recordTypes without recordTypes set to be deleted (can't be included in getting top ID
List<recordType> temp = new List<recordType>();
temp.AddRange(recordTypes);
// removing at a list of elements requires a descending order
delete_recordTypes.Sort();
delete_recordTypes.Reverse();
for (int i = 0; i < delete_recordTypes.Count; i++) { temp.RemoveAt(delete_recordTypes[i]); }
top_recordType_id = temp.Max(n => n.id);
}
delete_recordTypes.Add(elem);
}
public void Delete_add_recordType(int elem) { add_recordTypes.RemoveAt(elem); }
public void Delete_record(int elem)
{
if (records[elem].id == top_record_id)
{
List<record> temp = new List<record>();
temp.AddRange(records);
delete_records.Sort();
delete_records.Reverse();
for (int i = 0; i < delete_records.Count; i++) { temp.RemoveAt(delete_records[i]); }
top_record_id = temp.Max(n => n.id);
}
delete_recordTypes.Add(elem);
}
public void Delete_add_record(int elem) { add_records.RemoveAt(elem); }
public void Delete_recordToRecordTag(int elem) { delete_recordTypes.Add(elem); }
public void Delete_add_recordToRecordTag(int elem) { add_recordTypes.RemoveAt(elem); }
public void Flush()
{
List<string> contents = File.ReadAllLines(file).ToList();
int i = contents.Count;
Console.WriteLine("added:");
for (int j = add_recordToRecordTags.Count - 1; j >= 0; j--)
{
contents.Insert(i, String.Format(
"recordToRecordTag\t{0}\t{1}",
add_recordToRecordTags[j].record_id,
add_recordToRecordTags[j].record_tag_id
));
recordToRecordTags.Insert(recordToRecordTags.Count - add_recordToRecordTags.Count, add_recordToRecordTags[j]);
Console.WriteLine(String.Format(
" recordToRecordTag\t{0}\t{1}",
add_recordToRecordTags[j].record_id,
add_recordToRecordTags[j].record_tag_id
));
}
i -= recordToRecordTags.Count - add_recordToRecordTags.Count;
i -= recordTags.Count;
i -= recordTypeCategories.Count;
i -= categories.Count;
for (int j = add_records.Count - 1; j >= 0; j--)
{
contents.Insert(i, String.Format(
"record\t{0}\t{1}\t{2}\t{3}\t{4}",
add_records[j].id,
add_records[j].type_id,
add_records[j].time_started,
add_records[j].time_ended,
add_records[j].comment
));
records.Insert(records.Count - add_records.Count, add_records[j]);
Console.WriteLine(String.Format(
" record\t{0}\t{1}\t{2}\t{3}\t{4}",
add_records[j].id,
add_records[j].type_id,
add_records[j].time_started,
add_records[j].time_ended,
add_records[j].comment
));
}
i -= records.Count - add_records.Count;
for (int j = add_recordTypes.Count - 1; j >= 0; j--)
{
contents.Insert(i, String.Format(
"recordType\t{0}\t{1}\t{2}\t{3}\t{4}\t{5}\t{6}",
add_recordTypes[j].id,
add_recordTypes[j].name,
add_recordTypes[j].icon,
add_recordTypes[j].color,
add_recordTypes[j].color_int,
add_recordTypes[j].hidden,
add_recordTypes[j].goal_time
));
recordTypes.Insert(recordTypes.Count - add_recordTypes.Count, add_recordTypes[j]);
Console.WriteLine(String.Format(
" recordType\t{0}\t{1}\t{2}\t{3}\t{4}\t{5}\t{6}",
add_recordTypes[j].id,
add_recordTypes[j].name,
add_recordTypes[j].icon,
add_recordTypes[j].color,
add_recordTypes[j].color_int,
add_recordTypes[j].hidden,
add_recordTypes[j].goal_time
));
}
i -= recordTypes.Count - add_recordTypes.Count;
Console.WriteLine("deleted:");
delete_recordToRecordTags.Sort();
delete_recordToRecordTags.Reverse();
for (int j = delete_recordToRecordTags.Count - 1; j >= 0; j--)
{
contents.RemoveAt(contents.FindIndex(
0,
contents.Count,
n => n == String.Format(
"recordToRecordTag\t{0}\t{1}",
recordToRecordTags[delete_recordToRecordTags[j]].record_id,
recordToRecordTags[delete_recordToRecordTags[j]].record_tag_id
)
));
Console.WriteLine(String.Format(
" recordToRecordTag\t{0}\t{1}",
recordToRecordTags[delete_recordToRecordTags[j]].record_id,
recordToRecordTags[delete_recordToRecordTags[j]].record_tag_id
));
recordToRecordTags.RemoveAt(delete_recordToRecordTags[j]);
}
i -= recordToRecordTags.Count + delete_recordToRecordTags.Count;
i -= recordTags.Count;
i -= recordTypeCategories.Count;
i -= categories.Count;
// don't need to sort delete_records (already done in delete_record())
for (int j = delete_records.Count - 1; j >= 0; j--)
{
contents.RemoveAt(contents.FindIndex(
0,
contents.Count,
n => n == String.Format(
"record\t{0}\t{1}\t{2}\t{3}\t{4}",
records[delete_records[j]].id,
records[delete_records[j]].type_id,
records[delete_records[j]].time_started,
records[delete_records[j]].time_ended,
records[delete_records[j]].comment
)
));
Console.WriteLine(String.Format(
" record\t{0}\t{1}\t{2}\t{3}\t{4}",
records[delete_records[j]].id,
records[delete_records[j]].type_id,
records[delete_records[j]].time_started,
records[delete_records[j]].time_ended,
records[delete_records[j]].comment
));
records.RemoveAt(delete_records[j]);
}
i -= records.Count + delete_records.Count;
for (int j = delete_recordTypes.Count - 1; j >= 0; j--)
{
contents.RemoveAt(contents.FindIndex(
0,
contents.Count,
n => n == String.Format(
"recordType\t{0}\t{1}\t{2}\t{3}\t{4}\t{5}\t{6}",
recordTypes[delete_recordTypes[j]].id,
recordTypes[delete_recordTypes[j]].name,
recordTypes[delete_recordTypes[j]].icon,
recordTypes[delete_recordTypes[j]].color,
recordTypes[delete_recordTypes[j]].color_int,
recordTypes[delete_recordTypes[j]].hidden,
recordTypes[delete_recordTypes[j]].goal_time
)
));
Console.WriteLine(String.Format(
" recordType\t{0}\t{1}\t{2}\t{3}\t{4}\t{5}\t{6}",
recordTypes[delete_recordTypes[j]].id,
recordTypes[delete_recordTypes[j]].name,
recordTypes[delete_recordTypes[j]].icon,
recordTypes[delete_recordTypes[j]].color,
recordTypes[delete_recordTypes[j]].color_int,
recordTypes[delete_recordTypes[j]].hidden,
recordTypes[delete_recordTypes[j]].goal_time
));
recordTypes.RemoveAt(delete_recordTypes[j]);
}
i -= recordTypes.Count + delete_recordTypes.Count;
File.WriteAllLines("stt_out.backup", contents);
file = "stt_out.backup";
add_recordTypes = new List<recordType>();
add_records = new List<record>();
add_recordToRecordTags = new List<recordToRecordTag>();
delete_recordTypes = new List<int>();
delete_records = new List<int>();
delete_recordToRecordTags = new List<int>();
}
}
}