This repository was archived by the owner on Feb 23, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMyData.cs
More file actions
361 lines (334 loc) · 11.9 KB
/
Copy pathMyData.cs
File metadata and controls
361 lines (334 loc) · 11.9 KB
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
// Lic:
// MyData II
// Actual database class
//
//
//
// (c) Jeroen P. Broks, 2018, 2021, 2023
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
//
// Please note that some references to data like pictures or audio, do not automatically
// fall under this licenses. Mostly this is noted in the respective files.
//
// Version: 23.08.20
// EndLic
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Diagnostics.Eventing.Reader;
using System.IO;
using System.Linq;
using System.Reflection.Emit;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using TrickyUnits;
namespace MyData_II {
enum MyDataTypes { None,Info, Strike, String, Int, Bool, MC};
enum MyDataParseState { None, Sys, Page, Template, PrefixToTemplate, Record };
internal class MyData {
internal readonly string FileName;
internal static readonly Dictionary<string, MyDataTypes> MyDataTypesMap = new Dictionary<string, MyDataTypes>();
internal readonly List<MyDataPage> Pages = new List<MyDataPage>();
internal readonly Dictionary<string, MyDataField> Fields = new Dictionary<string, MyDataField>();
internal readonly SortedDictionary<string, MyDataRecord> Records = new SortedDictionary<string, MyDataRecord>();
internal bool AllModified {
get {
var r = true;
foreach (MyDataRecord record in Records.Values) r = r && record.Modified;
return r;
}
set {
foreach (MyDataRecord record in Records.Values) record.Modified = value;
}
}
internal bool AnyModified {
get {
var r = false;
foreach (MyDataRecord record in Records.Values) r = r || record.Modified;
return r;
}
}
internal Dictionary<string,string> DefaultValues { get {
var ret = new Dictionary<string, string>();
foreach (var f in Fields) ret[f.Key] = f.Value.DefaultValue("Default");
return ret;
}
}
internal class TSys {
private Dictionary<string, string> TrueSys = new Dictionary<string, string>();
internal Dictionary<string,string> ExportRec = new Dictionary<string, string>();
internal Dictionary<string, string> ExportBase = new Dictionary<string, string>();
void AutoOutPutUpdate() {
ExportRec.Clear();
ExportBase.Clear();
foreach (var K in TrueSys) {
if (qstr.Prefixed(K.Key, "OUTPUT") && qstr.Suffixed(K.Key, "BASE")) {
var TK = qstr.Left(K.Key, K.Key.Length - 4); TK = TK.Substring(6);
ExportBase[TK] = K.Value;
} else if (qstr.Prefixed(K.Key, "OUTPUT") && qstr.Suffixed(K.Key, "REC")) {
var TK = qstr.Left(K.Key, K.Key.Length - 3); TK = TK.Substring(6);
ExportRec[TK] = K.Value;
}
}
}
internal bool ContainsKey(string k) => TrueSys.ContainsKey(k.ToUpper());
internal string this[string key] {
get {
key = key.ToUpper();
if (TrueSys.ContainsKey(key)) return TrueSys[key];
return "";
}
set {
TrueSys[key.ToUpper()] = value;
AutoOutPutUpdate();
}
}
Dictionary<string, string>.KeyCollection Keys => Keys;
Dictionary<string, string>.ValueCollection Values => Values;
}
#region Sys
internal TSys Sys = new TSys();
internal static MyData CurrentDatabase = null;
/*
internal string Sys_OutPutGINIEBase { get; private set; } = "";
internal string Sys_OutPutGINIERec { get; private set; } = "";
*/
internal string Sys_Author => Sys["Author"];
internal string Sys_License => Sys["License"];
internal bool Sys_AutoExport => Sys["AutoExport"].ToUpper() == "TRUE" || Sys["AutoExport"].ToUpper() == "YES";
internal bool Sys_RemoveNonExistent => Sys["RemoveNonExistent"].ToUpper() == "TRUE" || Sys["RemoveNonExistent"].ToUpper() == "YES";
#endregion
/*
public string eol {
get {
if (!MyDataBase.sys.ContainsKey("EOL")) return "\n";
switch (MyDataBase.sys["EOL"].ToUpper()) {
case "DOS":
case "WINDOWS":
// return "\r\n"; // Fuck Windows
return "\n";
case "UNIX":
case "LINUX":
case "MAC":
case "MACOSX":
return "\n";
default:
TrickyUnits.GTK.QuickGTK.Error($"I do not know EOL type {MyDataBase.sys[""]}, so reverting back to Unix format."EOL);
MyDataBase.sys["EOL"] = "UNIX"; // Won't alter the database files, but it will prevent the message to pop up over and over and over and over and over etc.
return "\n";
}
}
}
*/
internal static bool ValidRecName(string k) {
bool ret = true;
for(int a = 0; a < k.Length; ++a) {
ret = ret && (
k[a] == '_' ||
(k[a]>='A' && k[a]<='Z') ||
(k[a]>='a' && k[a]<='z') ||
(k[a]>='0' && k[a]<='9')
);
}
return ret;
}
internal MyDataRecord Record (string rec) {
rec = rec.ToUpper();
if (!Records.ContainsKey(rec)) {
Error.Err($"Record {rec} not found.");
return null;
}
return Records[rec];
}
internal bool RecordExists(string rec) {
rec = rec.ToUpper();
return Records.ContainsKey(rec);
}
internal string this[string rec, string fld] {
get {
var r = Record(rec);
if (r == null) { return "ERROR!"; }
return r[fld];
}
set {
var r = Record(rec);
if (r == null) { return; }
r[fld] = value;
}
}
private MyData(string _FileName) {
FileName = _FileName;
var values = Enum.GetValues(typeof(MyDataTypes));
if (!MyDataTypesMap.ContainsKey("INT")) {
foreach (var value in values) {
var ev = (MyDataTypes)value;
var tag = $"{ev}";
MyDataTypesMap[tag.ToUpper()] = ev;
Debug.WriteLine($"typedef {ev} => ({tag});");
}
}
var L = QuickStream.LoadLines(FileName);
Parse(L,FileName);
}
private void Parse(string[] Line,string filename="???") {
var Stage=MyDataParseState.None;
MyDataPage CurrentPage = null;
MyDataRecord CurrentRecord = null;
string CurrentTemplate = "";
uint strikes = 0;
for(int lnum=0; lnum<Line.Length; lnum++) {
var ErrTag = $"{filename}:{lnum + 1}\n";
var cline = Line[lnum].Trim();
if (cline.Length>0 && cline[0] != '#') { // ignore white lines and comments
if (cline[0] == '[' && cline[cline.Length - 1] == ']') {
CurrentRecord = null;
var l = cline.Substring(1); l = l.Substring(0, l.Length - 1).Trim();
if (l.ToUpper() == "SYS" || l.ToUpper() == "SYSTEM") {
Stage = MyDataParseState.Sys;
} else if (qstr.Prefixed(l.ToUpper(), "PAGE:")) {
var namepage = l.Substring(5);
CurrentPage = new MyDataPage(this, namepage);
Stage = MyDataParseState.Page;
} else if (l.ToUpper() == "DEFAULT") {
// [Default] is deprecated. [Template:Default] should be used in stead
CurrentTemplate = "DEFAULT";
Stage = MyDataParseState.Template;
if (!MainWindow.Templates.Contains(CurrentTemplate)) MainWindow.Templates.Add(CurrentTemplate);
} else if (qstr.Prefixed(l.ToUpper(), "TEMPLATE:")) {
var p = l.IndexOf(':');
CurrentTemplate = l.Substring(p + 1).Trim();
Stage = MyDataParseState.Template;
if (!MainWindow.Templates.Contains(CurrentTemplate)) MainWindow.Templates.Add(CurrentTemplate);
} else if (l.ToUpper()=="RECORDS") {
Stage = MyDataParseState.Record;
} else {
throw new Exception($"\n\n\n{ErrTag}New stage definition unknown\n{cline}");
}
} else {
switch (Stage) {
case MyDataParseState.None: throw new Exception($"Parse error!\n{ErrTag}\nThe stage has not yet been set!\n\n>>{cline}<<");
case MyDataParseState.Sys: {
var p = cline.IndexOf('=');
if (p<0) {
Error.Err($"{ErrTag}Syntax error in sys definition");
} else {
var key = cline.Substring(0, p).Trim().ToUpper();
var value=cline.Substring(p+1).Trim();
/*
switch(key) {
case "AUTOOUTPUT": // Deprecated!
case "AUTOEXPORT": // AutoExport is the new value
Sys_AutoExport = value.ToUpper() == "TRUE" || value.ToUpper() == "YES";
break;
case "LICENSE":
case "LIC":
Sys_License = value;
break;
case "OUTPUTGINIEREC":
Sys_OutPutGINIERec = value;
break;
case "OUTPUTGINIEBASE":
Sys_OutPutGINIEBase = value;
break;
default:
Error.Err($"{ErrTag}Unknown system variable: {key}");
break;
}
*/
if (key == "AUTOOUTPUT") key = "AUTOEXPORT"; // Support for deprecated behavior.
Sys[key] = value;
}
}
break;
case MyDataParseState.Page: {
if (qstr.Prefixed(cline.ToUpper(), "STRIKE")) {
CurrentPage = CurrentPage.NewField("STRIKE", $"*_STRIKE{strikes++}_*", "");
} else {
var p = cline.IndexOf(' ');
if (p < 0) {
Error.Err($"{ErrTag}Syntax error in field declaration");
}
var t = cline.Substring(0, p).ToUpper();
var rest = cline.Substring(p + 1).Trim();
if (t[0] == '@') {
if (MyDataField.NewestField == null)
Error.Err($"{ErrTag}Item addition request without field");
else if (MyDataField.NewestField.Type != MyDataTypes.MC)
Error.Err($"{ErrTag}Item addition only possible at MC (Multiple Choice fields) and not with {MyDataField.NewestField.Type} fields");
else
MyDataField.NewestField.FieldItems.Add(rest);
} else {
var f = rest;
var d = "";
p = rest.IndexOf("=");
if (p >= 0) {
f = rest.Substring(0, p);
d = rest.Substring(p + 1);
}
if (!MyDataTypesMap.ContainsKey(t))
Error.Err($"{ErrTag}Unknown type: {t}");
else
CurrentPage = CurrentPage.NewField(t, f, d);
}
}
}
break;
case MyDataParseState.Template: {
var p = cline.IndexOf('=');
if (p < 1)
Error.Err("Template defintion syntax error");
else {
var k = cline.Substring(0, p).Trim();
var v = cline.Substring(p + 1).Trim();
if (Fields.ContainsKey(k)) {
Debug.WriteLine($"Template: {CurrentTemplate}.{k} = {v}");
Fields[k].DefaultValue(CurrentTemplate, v);
}
}
}
break;
case MyDataParseState.Record: {
if (qstr.Prefixed(cline.ToUpper(), "REC:")) {
// Enhancement: I might work it out with the auto prefix to template when the template feature is fully working
CurrentRecord = new MyDataRecord(this);
Records[cline.Substring(4).ToUpper().Trim()] = CurrentRecord;
} else {
var p = cline.IndexOf('=');
if (p < 1)
Error.Err("Value defintion syntax error");
var f=cline.Substring(0, p).Trim();
var v=cline.Substring(p + 1).Trim();
CurrentRecord[f] = v;
CurrentRecord.Modified = false;
}
} break;
default: throw new Exception($"Internal error!\n{ErrTag}\nUnknown Stage ({Stage})");
}
}
}
}
}
static internal MyData Load(string _FileName) {
var RFN= Dirry.AD(_FileName.Replace("\\", "/"));
try {
if (!File.Exists(RFN)) { Error.Err($"Database file not found.\nGFN: {_FileName}\nRFN:{RFN}"); return null; }
return new MyData(RFN);
} catch(Exception ex) {
Error.Err($"Loading database failed due to .NET throwing an exception.\nGFN: {_FileName}\nRFN:{RFN}\nNEX:{ex.Message}");
return null;
}
}
}
}