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
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
|
/// <summary>
/// Klasse, um Dateien im Ini-Format zu verwalten.
/// </summary>
public class INIFile
{
/// <summary>
/// Inhalt der Datei
/// </summary>
private List<string> lines = new List<string>();
/// <summary>
/// Anzahl der Zeilen in der INIDatei
/// </summary>
public int LineCount
{
get { return lines.Count; }
}
/// <summary>
/// Verzeichnis der Datei
/// </summary>
/// <returns></returns>
public string FileDirectory
{
get { return Path.GetDirectoryName(FileName); }
}
/// <summary>
/// Voller Name der Datei bzw Titel der INI
/// </summary>
/// <returns></returns>
public string FileName
{
get { return FileName; }
}
/// <summary>
/// Voller Pfad und Name der Datei bzw Titel der INI
/// </summary>
private string fileName = "";
/// <summary>
/// Gibt an, welche Zeichen als Kommentarbeginn
/// gewertet werden sollen. Dabei wird das erste
/// Zeichen defaultm��ig f�r neue Kommentare
/// verwendet.
/// </summary>
private string CommentCharacters = "#;";
/// <summary>
/// Regul�rer Ausdruck f�r einen Kommentar in einer Zeile
/// </summary>
private string regCommentStr = "";
/// <summary>
/// Regul�rer Ausdruck f�r einen Eintrag
/// </summary>
private Regex regEntry = null;
/// <summary>
/// Regul�rer Ausdruck f�r einen Bereichskopf
/// </summary>
private Regex regCaption = null;
/// <summary>
/// �nderrungen an einem Eintrag vornemen. Achtung CaseSensitive!
/// </summary>
public string this[string Caption, string Entry]
{
get
{
return getValue(Caption, Entry, true);
}
set
{
setValue(Caption, Entry, value.ToString(), true, false);
}
}
/// <summary>
/// Leerer Standard-Konstruktor
/// </summary>
public INIFile()
{
regCommentStr = @"(\s*[" + CommentCharacters + "](?<comment>.*))?";
regEntry = new Regex(@"^[ \t]*(?<entry>([^=])+)=(?<value>([^=" + CommentCharacters + "])+)" + regCommentStr + "$");
regCaption = new Regex(@"^[ \t]*(\[(?<caption>([^\]])+)\]){1}" + regCommentStr + "$");
}
/// <summary>
/// Konstruktor, welcher sofort eine Datei einliest
/// </summary>
/// <param name="filename">Name der einzulesenden Datei</param>
public INIFile(string filename) : this ()
{
if(!File.Exists(filename))
throw new IOException("File " + filename + " not found");
Read(File.ReadAllText(filename));
}
/// <summary>
/// Methode die einen string im INI-Format einlie�t
/// </summary>
/// <param name="iniFile">String im INI-Format</param>
public void Read(string iniFile)
{
lines.AddRange(iniFile.Split(new string[] { "\r\n", "\n" }, StringSplitOptions.None));
}
/// <summary>
/// Methode die einen string im INI-Format einlie�t
/// </summary>
/// <param name="iniFile">String im INI-Format</param>
public void ReadFile(string iniFile)
{
Read(File.ReadAllText(iniFile));
}
/// <summary>
/// Kopie der Datei speichern (Objekt bleibt unber�hrt)
/// </summary>
/// <param name="file"></param>
/// <returns></returns>
public bool SaveAs(string file)
{
if(fileName == "") return false;
try
{
File.WriteAllLines(fileName, lines.ToArray());
}
catch(IOException ex)
{
throw new IOException("Fehler beim Schreiben der Datei " + fileName, ex);
}
catch(Exception ex)
{
throw new IOException("Fehler beim Schreiben der Datei " + fileName, ex);
}
return true;
}
/// <summary>
/// Datei speichern
/// </summary>
/// <returns></returns>
public bool Save()
{
return SaveAs(fileName);
}
/// <summary>
/// Datei unter anderrem Namen speichern
/// </summary>
/// <param name="file"></param>
/// <returns></returns>
public bool Save(string file)
{
fileName = file;
return SaveAs(fileName);
}
/// <summary>
/// Sucht die Zeilennummer (nullbasiert)
/// eines gew�nschten Eintrages
/// </summary>
/// <param name="Caption">Name des Bereiches</param>
/// <param name="CaseSensitive">true = Gross-/Kleinschreibung beachten</param>
/// <returns>Nummer der Zeile, sonst -1</returns>
public int GetCaptionLine(string Caption, bool CaseSensitive)
{
if(!CaseSensitive) Caption = Caption.ToLower();
for(int i = 0; i < lines.Count; i++)
{
string line = lines[i].Trim();
if(line == "") continue;
if(!CaseSensitive) line = line.ToLower();
// Erst den gew�nschten Abschnitt suchen
if(line == "[" + Caption + "]")
return i;
}
return -1;// Bereich nicht gefunden
}
/// <summary>
/// Pr�ft ob ein Bereich existiert.
/// </summary>
/// <param name="Caption">Name des Bereiches</param>
/// <param name="CaseSensitive">true = Gross-/Kleinschreibung beachten</param>
/// <returns>True wenn der Breich existiert, sonst false</returns>
public bool CaptionExist(string Caption, bool CaseSensitive)
{
return GetCaptionLine(Caption, CaseSensitive) != -1;
}
/// <summary>
/// Sucht die Zeilennummer (nullbasiert)
/// eines gew�nschten Eintrages
/// </summary>
/// <param name="Caption">Name des Bereiches</param>
/// <param name="Entry">Name des Eintrages</param>
/// <param name="CaseSensitive">true = Gross-/Kleinschreibung beachten</param>
/// <returns>Nummer der Zeile, sonst -1</returns>
public int GetEntryLine(string Caption, string Entry, bool CaseSensitive)
{
Caption = Caption.ToLower();
if(!CaseSensitive) Entry = Entry.ToLower();
int CaptionStart = GetCaptionLine(Caption, false);
if(CaptionStart < 0) return -1;
for(int i = CaptionStart + 1; i < lines.Count; i++)
{
string line = lines[i].Trim();
if(line == "") continue;
if(!CaseSensitive) line = line.ToLower();
if(line.StartsWith("["))
return -1;// Ende, wenn der n�chste Abschnitt beginnt
if(Regex.IsMatch(line, @"^[ \t]*[" + CommentCharacters + "]"))
continue; // Kommentar
if(line.StartsWith(Entry+"="))
return i;// Eintrag gefunden
}
return -1;// Eintrag nicht gefunden
}
/// <summary>
/// Pr�ft ob ein Eintrag existiert.
/// </summary>
/// <param name="Caption">Name des Bereiches in dem gesucht werden soll</param>
/// <param name="Entry">Name des Eintrag der gesucht werden soll</param>
/// <param name="CaseSensitive">true = Gross-/Kleinschreibung beachten</param>
/// <returns>True wenn der Eintrag existiert, sonst false</returns>
public bool EntryExist(string Caption, string Entry, bool CaseSensitive)
{
return GetCaptionLine(Caption, CaseSensitive) != -1;
}
/// <summary>
/// Kommentiert einen Wert aus
/// </summary>
/// <param name="Caption">Name des Bereiches</param>
/// <param name="Entry">Name des Eintrages</param>
/// <param name="CaseSensitive">true = Gross-/Kleinschreibung beachten</param>
/// <returns>true = Eintrag gefunden und auskommentiert</returns>
public bool commentValue(string Caption, string Entry, bool CaseSensitive)
{
int line = GetEntryLine(Caption, Entry, CaseSensitive);
if(line < 0)
return false;
lines[line] = CommentCharacters[0] + lines[line];
return true;
}
/// <summary>
/// L�scht einen Wert
/// </summary>
/// <param name="Caption">Name des Bereiches</param>
/// <param name="Entry">Name des Eintrages</param>
/// <param name="CaseSensitive">true = Gross-/Kleinschreibung beachten</param>
/// <returns>true = Eintrag gefunden und gel�scht</returns>
public bool deleteValue(string Caption, string Entry, bool CaseSensitive)
{
int line = GetEntryLine(Caption, Entry, CaseSensitive);
if(line < 0)
return false;
lines.RemoveAt(line);
return true;
}
/// <summary>
/// Liest den Wert eines Eintrages aus
/// </summary>
/// <param name="Caption">Name des Bereiches</param>
/// <param name="Entry">Name des Eintrages</param>
/// <param name="CaseSensitive">true = Gross-/Kleinschreibung beachten</param>
/// <returns>Wert des Eintrags oder leer</returns>
public string getValue(string Caption, string Entry, bool CaseSensitive)
{
int line = GetEntryLine(Caption, Entry, CaseSensitive);
if(line < 0)
return "";
int pos = lines[line].IndexOf("=");
if(pos < 0)
return "";
return lines[line].Substring(pos + 1).Trim();
// Evtl. noch abschliessende Kommentarbereiche entfernen
}
/// <summary>
/// Liest den Wert eines Eintrages aus
/// Ignoriert Gross-/Kleinschreibung
/// </summary>
/// <param name="Caption">Name des Bereiches</param>
/// <param name="Entry">Name des Eintrages</param>
/// <returns>Wert des Eintrags oder leer</returns>
public string getValue(string Caption, string Entry)
{
return getValue(Caption, Entry, false);
}
/// <summary>
/// Setzt einen Wert in einem Bereich. Wenn der Wert
/// (und der Bereich) noch nicht existiert, werden die
/// entsprechenden Eintr�ge erstellt.
/// </summary>
/// <param name="Caption">Name des Bereichs</param>
/// <param name="Entry">name des Eintrags</param>
/// <param name="Value">Wert des Eintrags</param>
/// <param name="CaseSensitive">true = Gross-/Kleinschreibung beachten</param>
/// <returns>true = Eintrag erfolgreich gesetzt</returns>
public bool setValue(string Caption, string Entry, string Value, bool CaseSensitive, bool SearchComments)
{
Caption = Caption.ToLower();
if(!CaseSensitive)
Entry = Entry.ToLower();
int lastCommentedFound = -1;
int CaptionStart = GetCaptionLine(Caption, false);
if(CaptionStart < 0)
{
lines.Add("[" + Caption + "]");
lines.Add(Entry + "=" + Value);
return true;
}
int EntryLine = GetEntryLine(Caption, Entry, CaseSensitive);
for(int i = CaptionStart + 1; i < lines.Count; i++)
{
string line = lines[i].Trim();
if(!CaseSensitive)
line = line.ToLower();
if(line == "")
continue;
// Ende, wenn der n�chste Abschnitt beginnt
if(line.StartsWith("["))
{
lines.Insert(i, Entry + "=" + Value);
return true;
}
// Suche aukommentierte, aber gesuchte Eintr�ge,
// falls der Eintrag noch nicht existiert.
if(SearchComments && EntryLine<0 && Regex.IsMatch(line, @"^[ \t]*[" + CommentCharacters + "]"))
{
string tmpLine = line.Substring(1).Trim();
if(tmpLine.StartsWith(Entry))
{
// Werte vergleichen, wenn gleich,
// nur Kommentarzeichen l�schen
int pos = tmpLine.IndexOf("=");
if(pos > 0)
{
if(Value == tmpLine.Substring(pos + 1).Trim())
{
lines[i] = tmpLine;
return true;
}
}
lastCommentedFound = i;
}
continue;// Kommentar
}
if(line.StartsWith(Entry))
{
lines[i] = Entry + "=" + Value;
return true;
}
}
if(lastCommentedFound > 0)
lines.Insert(lastCommentedFound + 1, Entry + "=" + Value);
else
lines.Insert(CaptionStart + 1, Entry + "=" + Value);
return true;
}
/// <summary>
/// Setzt einen Wert in einem Bereich. Wenn der Wert
/// (und der Bereich) noch nicht existiert, werden die
/// entsprechenden Eintr�ge erstellt.
/// </summary>
/// <param name="Caption">Name des Bereichs</param>
/// <param name="Entry">name des Eintrags</param>
/// <param name="Value">Wert des Eintrags</param>
/// <param name="CaseSensitive">true = Gross-/Kleinschreibung beachten</param>
/// <returns>true = Eintrag erfolgreich gesetzt</returns>
public bool setValue(string Caption, string Entry, string Value)
{
return setValue(Caption, Entry, Value, false, false);
}
/// <summary>
/// Liest alle Eintr�ge uns deren Werte eines Bereiches aus
/// </summary>
/// <param name="Caption">Name des Bereichs</param>
/// <param name="CaseSensitive">true = Gross-/Kleinschreibung beachten</param>
/// <returns>Sortierte Liste mit Eintr�gen und Werten</returns>
public SortedList<string, string> getCaption(string Caption, bool caseSensitive)
{
SortedList<string, string> result = new SortedList<string,string>();
bool CaptionFound = false;
for(int i = 0; i < lines.Count; i++)
{
string line = lines[i].Trim();
if(line == "")
continue;
if(caseSensitive)
Caption.ToLower();
// Erst den gew�nschten Abschnitt suchen
if(!CaptionFound)
{
if(caseSensitive)
line.ToLower();
if(line != "[" + Caption + "]")
{
continue;
}
else
{
CaptionFound = true;
continue;
}
}
if(line.StartsWith("[")) // Ende, wenn der n�chste Abschnitt beginnt
break;
if(Regex.IsMatch(line, @"^[ \t]*[" + CommentCharacters + "]")) // Kommentar
continue;
int pos = line.IndexOf("=");
if(pos < 0)
result.Add(line, "");
else
result.Add(line.Substring(0,pos).Trim(),line.Substring(pos + 1).Trim());
}
return result;
}
/// <summary>
/// Liest alle Eintr�ge uns deren Werte eines Bereiches aus.
/// Ignoriert Gross-/Kleinschreibung
/// </summary>
/// <param name="Caption">Name des Bereichs</param>
/// <param name="CaseSensitive">true = Gross-/Kleinschreibung beachten</param>
/// <returns>Sortierte Liste mit Eintr�gen und Werten</returns>
public SortedList<string, string> getCaption(string Caption)
{
return getCaption(Caption, false);
}
/// <summary>
/// Erstellt eine Liste aller enthaltenen Bereiche
/// </summary>
/// <returns>Liste mit gefundenen Bereichen</returns>
public List<string> getAllCaptions()
{
List<string> result = new List<string>();
for(int i = 0; i < lines.Count; i++)
{
string line = lines[i];
Match mCaption = regCaption.Match(lines[i]);
if(mCaption.Success)
result.Add(mCaption.Groups["caption"].Value.Trim());
}
return result;
}
/// <summary>
/// Exportiert s�mtliche Bereiche und deren Werte in ein XML-Dokument
/// </summary>
/// <returns>XML-Dokument</returns>
public XmlDocument exportToXml()
{
XmlDocument doc = new XmlDocument();
XmlElement root = null;
root = doc.CreateElement(Path.GetFileNameWithoutExtension(this.fileName));
doc.AppendChild(root);
XmlElement Caption = null;
for(int i = 0; i < lines.Count; i++)
{
Match mEntry = regEntry.Match(lines[i]);
Match mCaption = regCaption.Match(lines[i]);
if(mCaption.Success)
{
Caption = doc.CreateElement(mCaption.Groups["caption"].Value.Trim());
root.AppendChild(Caption);
continue;
}
if(mEntry.Success)
{
XmlElement xe = doc.CreateElement(mEntry.Groups["entry"].Value.Trim());
xe.InnerXml = mEntry.Groups["value"].Value.Trim();
if(Caption == null)
root.AppendChild(xe);
else
Caption.AppendChild(xe);
}
}
return doc;
}
}
|