-
Notifications
You must be signed in to change notification settings - Fork 3
/
Config.cs
380 lines (315 loc) · 8.74 KB
/
Config.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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Timers;
using System.IO;
using System.Drawing;
namespace Grepy2
{
static class Config
{
public enum KEY // everthing is 'int' unless otherwise noted
{
PosX,
PosY,
Width,
Height,
Maximized, // bool
SplitterType, // int (0=horizontal, 1=vertical)
SplitterHorizontalDistance,
SplitterVerticalDistance,
PreviewBeforeSlider,
PreviewAfterSlider,
ListViewColumnWidth, // array of int
NumWorkerThreads,
OptionsPosX,
OptionsPosY,
OptionsFontName, // string
OptionsFontSize, // float
OptionsFontBold, // bool
OptionsFontItalic, // bool
OptionsDeferRichTextDisplay, // bool
OptionsUseWindowsFileAssociation, // bool
OptionsCustomEditor, // string
SearchPosX,
SearchPosY,
SearchRegularExpression, // bool
SearchMatchCase, // bool
SearchRecursive, // bool
SearchText, // array of string
SearchFileSpec, // array of string
SearchFolder, // array of string
SearchHelpPosX,
SearchHelpPosY,
DisplayLineNumbers, // bool
FontPickerPosX,
FontPickerPosY,
FileListFont,
SearchResultsFont
};
private static string ConfigFilename;
private static Dictionary<string, string> ConfigDictionary;
private static Timer timer;
static Config()
{
string AppDataPath = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData);
string AppDataFolder = AppDataPath + "\\Grepy2";
Directory.CreateDirectory(AppDataFolder);
ConfigFilename = AppDataPath + "\\Grepy2\\Grepy2.ini";
ConfigDictionary = new Dictionary<string,string>();
Load();
timer = new System.Timers.Timer();
timer.Elapsed += new ElapsedEventHandler(OnTimerEvent);
timer.AutoReset = false;
timer.Enabled = false;
}
private static void OnTimerEvent(object source, ElapsedEventArgs e)
{
Save();
}
private static void Load()
{
string line;
try
{
using( StreamReader sr = new StreamReader(ConfigFilename) )
{
while (sr.Peek() >= 0)
{
line = sr.ReadLine();
while( (line.Length > 0) && ((line[0] == ' ') || (line[0] == '\t')) ) // remove leading whitespace
{
line = line.Substring(1, line.Length-1);
}
int end = line.Length-1;
while( (line.Length > 0) && ((line[end] == ' ') || (line[end] == '\t')) ) // remove trailing whitespace
{
if( end > 0 )
line = line.Substring(0, end);
else
line = "";
end = line.Length-1;
}
if( (line.Length > 0) && (line[0] != '[') ) // not a blank line or a section?
{
int pos = line.IndexOf("=");
if( (pos > 0) && (pos < (line.Length-1)) ) // '=' must not be the first or last character of the line
{
string key = line.Substring(0, pos);
string value = line.Substring(pos+1, (line.Length-pos)-1);
ConfigDictionary.Add(key, value);
}
}
}
}
}
catch( Exception )
{
Console.WriteLine("Config() - can't read config file: '{0}'", ConfigFilename);
}
}
public static void Save()
{
timer.Enabled = false; // turn off the timer (in the event that Save was called directly)
if( !Globals.bIsGrepyReadOnly )
{
try
{
using( StreamWriter sw = new StreamWriter(ConfigFilename) )
{
sw.WriteLine("[Grepy2]");
foreach( KEY key in Enum.GetValues(typeof(KEY)) )
{
if( key == KEY.ListViewColumnWidth ||
key == KEY.SearchText ||
key == KEY.SearchFileSpec ||
key == KEY.SearchFolder )
{
int count = 1;
bool found = false;
do
{
string key_string = string.Format("{0}{1}", key.ToString(), count);
found = ConfigDictionary.ContainsKey(key_string);
if( found )
{
sw.WriteLine(string.Format("{0}={1}", key_string, ConfigDictionary[key_string]));
}
count++;
} while( found );
}
else
{
string key_string = key.ToString();
if( ConfigDictionary.ContainsKey(key_string) )
{
sw.WriteLine(string.Format("{0}={1}", key_string, ConfigDictionary[key_string]));
}
}
}
}
}
catch( Exception )
{
Console.WriteLine("SaveConfig() - Error saving config file!");
}
}
}
public static bool Get(KEY key, ref string value)
{
string key_string = key.ToString();
if( ConfigDictionary.ContainsKey(key_string) )
{
value = ConfigDictionary[key_string];
return true;
}
return false;
}
public static void Set(KEY key, string value)
{
string key_string = key.ToString();
if( ConfigDictionary.ContainsKey(key_string) ) // if the key/value pair exists...
{
ConfigDictionary.Remove(key_string); // ...remove the old key/value pair
}
ConfigDictionary.Add(key_string, value); // add the key/value pair to the dictionary
timer.Enabled = true;
timer.Interval = 1000;
}
public static bool Get(KEY key, ref int value)
{
string key_string = key.ToString();
if( ConfigDictionary.ContainsKey(key_string) )
{
value = 0;
Int32.TryParse(ConfigDictionary[key_string], out value);
return true;
}
return false;
}
public static void Set(KEY key, int value)
{
Set(key, value.ToString());
}
public static bool Get(KEY key, ref bool value)
{
string key_string = key.ToString();
if( ConfigDictionary.ContainsKey(key_string) )
{
value = ConfigDictionary[key_string] == "True";
return true;
}
return false;
}
public static void Set(KEY key, bool value)
{
Set(key, value.ToString()); // set to 'False' or 'True'
}
public static bool Get(KEY key, ref List<string> value)
{
value = new List<string>();
int count = 1;
bool found = false;
do
{
string key_string = string.Format("{0}{1}", key.ToString(), count);
found = ConfigDictionary.ContainsKey(key_string);
if( found )
{
value.Add(ConfigDictionary[key_string]);
}
count++;
} while( found );
return (value.Count() > 0);
}
public static void Set(KEY key, List<string> StringList)
{
int count = 1;
bool found = false;
// remove ALL old key/value pairs from the dictionary (since the list parameter passed in can be different size than what's currently in dictionary)
do
{
string key_string = string.Format("{0}{1}", key.ToString(), count);
found = ConfigDictionary.ContainsKey(key_string);
if( found )
{
ConfigDictionary.Remove(key_string);
}
count++;
} while( found );
// add the new key/value pairs to the dictionary
for( int index = 0; index < StringList.Count; index++ )
{
string key_string = string.Format("{0}{1}", key.ToString(), index+1);
ConfigDictionary.Add(key_string, StringList[index]);
}
timer.Enabled = true;
timer.Interval = 1000;
}
public static bool Get(KEY key, ref List<int> value)
{
value = new List<int>();
List<string> StringList = new List<string>();
Get(key, ref StringList);
for( int index = 0; index < StringList.Count; index++ )
{
value.Add(Convert.ToInt32(StringList[index]));
}
return (value.Count() > 0);
}
public static void Set(KEY key, List<int> IntList)
{
List<string> StringList = new List<string>();
for( int index = 0; index < IntList.Count; index++ )
{
StringList.Add(IntList[index].ToString());
}
Set(key, StringList);
}
public static bool Get(KEY key, ref Font value)
{
string FontString = "";
if (Get(key, ref FontString))
{
string[] fields = FontString.Split(',');
if (fields.Length == 4)
{
if ((fields[0].Substring(0,1) == "\"") && (fields[0].Substring(fields[0].Length - 1,1) == "\""))
{
fields[0] = fields[0].Substring(1, fields[0].Length - 2); // chop off the leading and trailing double quote
}
FontFamily font_family = new FontFamily(fields[0]);
float size = float.Parse(fields[1]);
int int_style = int.Parse(fields[2]);
FontStyle style = FontStyle.Regular;
if ((int_style & 1) != 0)
{
style = style | FontStyle.Bold;
}
if ((int_style & 2) != 0)
{
style = style | FontStyle.Italic;
}
GraphicsUnit graphics_unit = (GraphicsUnit) Enum.Parse(typeof(GraphicsUnit), fields[3]);
value = new Font(font_family, size, style, graphics_unit);
return true;
}
}
return false;
}
public static void Set(KEY key, Font value)
{
int Style = 0; // Regular
if( value.Style.HasFlag(FontStyle.Bold) )
{
Style |= 1;
}
if( value.Style.HasFlag(FontStyle.Italic) )
{
Style |= 2;
}
int GraphicsUnit = (int)value.Unit;
Set(key, String.Format("\"{0}\",{1},{2},{3}", value.FontFamily.Name, value.SizeInPoints, Style, GraphicsUnit));
}
}
}