-
Notifications
You must be signed in to change notification settings - Fork 0
/
Util.cs
568 lines (523 loc) · 20.7 KB
/
Util.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
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
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
using Microsoft.Win32;
using System;
using System.Collections.Generic;
using System.Collections;
using System.Diagnostics;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Security.Cryptography;
using System.Text.RegularExpressions;
using System.Text;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Media;
using System.Windows;
namespace qqEd
{
// Util
// Generic functions, not dependent on the current project
// Gestione errori: tramite codici di errore.
public partial class Util
{
// \param string command : command line, including parameters
// \param string currentDirectory : working directory ('null' if not defined)
// \param int waitForExit : if > 0, the method waits for the end of the process. The value 'waitForExit' is also the timeout (unit:msec).
// \param System.Diagnostics.ProcessWindowStyle p_windowStyle: the window style (Minimized, Maximized, ...)
// \param out string errMsg
// Return: 'false' in caso di errore
// Error management: by error code
public static bool CreateProcess(string command, string currentDirectory, int waitForExit, out string errMsg, System.Diagnostics.ProcessWindowStyle p_windowStyle)
{
errMsg = "";
System.Diagnostics.Process process = new System.Diagnostics.Process();
try
{
string exe = Util.StrListGetFirst(ref command, ' ');
process.StartInfo.FileName = exe;
process.StartInfo.Arguments = command;
process.StartInfo.WorkingDirectory = currentDirectory;
process.StartInfo.WindowStyle = p_windowStyle;
process.Start();
}
catch (Exception ex)
{
errMsg = ex.Message;
return (false); // Error
}
if (waitForExit > 0)
{
while ((waitForExit > 0)
&& (process.HasExited == false))
{
System.Threading.Thread.Sleep(100);
waitForExit -= 100;
}
if (process.HasExited == false) // If timeout ...
{
errMsg = "Process timeout";
return (false); // Error
}
}
return (true); // Ok
}
// \param string command : command line, including parameters
// \param string currentDirectory : working directory ('null' if not defined)
// \param int waitForExit : if > 0, the method waits for the end of the process. The value 'waitForExit' is also the timeout (unit:msec).
// \param out string errMsg
// Return: 'false' in caso di errore
// Error management: by error code
public static bool CreateProcess(string command, string currentDirectory, int waitForExit, out string errMsg)
{
return CreateProcess(command, currentDirectory, waitForExit, out errMsg, System.Diagnostics.ProcessWindowStyle.Maximized);
}
// Overload
public static bool CreateProcess(string command)
{
string errMsg = "";
return (CreateProcess(command, null, 0, out errMsg, System.Diagnostics.ProcessWindowStyle.Maximized));
}
// Overload
public static bool CreateProcess(string command, string currentDirectory)
{
string errMsg = "";
return (CreateProcess(command, currentDirectory, 0, out errMsg, System.Diagnostics.ProcessWindowStyle.Maximized));
}
// Overload
public static bool CreateProcess(string command, out string errMsg)
{
return (CreateProcess(command, null, 0, out errMsg, System.Diagnostics.ProcessWindowStyle.Maximized));
}
// Ex. textEditor = "C:\Program Files\Notepad++\notepad++.exe -n%line% %file%"
// Parameters:
// string lineNr = "1" : can be also 'null'
// Error management: no error returned
public static bool FileOpenInTextEditor(string fl, string lineNr = "1")
{
// if (Settings.Instance == null)
// {
// return (false);
// }
// string textEditor = Settings.Instance.ReadPath("TextEditor", true);
string textEditor = "";
fl = Util.StrAddQuotes(fl);
try
{
if (lineNr == null)
{
// Ugly patch that works only with Notepad++
// Ugly patch, but useful when a file is already opened on Notepad++ and no lineNr is specified
// => re-open at the same line as before
textEditor = textEditor.Replace(" -n%line%", "");
}
if (lineNr == null)
{
lineNr = "1";
}
// Set file name
string cmdLine = textEditor;
if (cmdLine.IndexOf("%file%") >= 0)
{
cmdLine = cmdLine.Replace("%file%", fl);
}
else
{
cmdLine = cmdLine + " " + fl;
}
// Set line number
if (cmdLine.IndexOf("%line%") >= 0)
{
cmdLine = cmdLine.Replace("%line%", lineNr);
}
if (CreateProcess(cmdLine) == false)
{
string errMsg = String.Format("Cannot open file:\n{0}" + "\nDetails: ");
MessageBox.Show(errMsg,
"Cannot open file",
MessageBoxButton.OK,
MessageBoxImage.Error);
}
}
catch
{
return (false);
}
return (true);
}
// Given a range of line numbers, return a set of lines from a text file.
// EOL characters are discarded (input file can be unix-EOL or windows-EOL).
// Last line of the file, if empty, is discarded.
// Parameters:
// int lineStart : number of the first line to be read (starting from 0)
// int lineEnd : number of the last line to be read (starting from 0)
// Error management: by error code
// Return: true if succeeded
public static bool FileRead(string filePath, int lineStart, int lineEnd, out List<string> lines)
{
lines = new List<string>();
if (!File.Exists(filePath))
{
return (false);
}
string line = "";
using (StreamReader sr = File.OpenText(filePath))
{
int i = 0;
while (sr.Peek() >= 0)
{
line = sr.ReadLine();
if ((i >= lineStart) && (i <= lineEnd))
{
lines.Add(line);
if (i > lineEnd)
{
break;
}
}
i++;
}
sr.Close();
}
if (lines.Count == 0)
{
return (false); // Noting read
}
return (true);
}
// Return the content of a file as an int array.
// Error management: by error code
// Return: true if succeeded
public static bool FileRead(string filePath, out int[] fileContent)
{
fileContent = null;
if (!File.Exists(filePath))
{
return (false);
}
StringBuilder sb = new StringBuilder();
try
{
using (BinaryReader reader = new BinaryReader(File.Open(filePath, FileMode.Open)))
{
int length = (int)reader.BaseStream.Length;
fileContent = new int[length / sizeof(int)];
int pos = 0;
int i = 0;
while (pos < length)
{
fileContent[i] = reader.ReadInt32();
pos += sizeof(int);
i++;
}
}
}
catch
{
return (false);
}
return (true);
}
public static void FileWrite(string fileOut, int[] buffer, int bufferSize)
{
using (BinaryWriter writer = new BinaryWriter(File.Open(fileOut, FileMode.Create)))
{
foreach (int i in buffer)
{
writer.Write(i);
}
}
}
public static void FileWrite(string fileOut, string fileContent)
{
StreamWriter sw = new StreamWriter(fileOut);
sw.Write(fileContent);
sw.Close();
}
public static void FileWrite(string fileOut, string fileContent, bool append)
{
StreamWriter sw = new StreamWriter(fileOut, append);
sw.Write(fileContent);
sw.Close();
}
public static void FileWrite(string fileOut, string[] fileContent)
{
using (StreamWriter file = new StreamWriter(fileOut))
{
foreach (string line in fileContent)
{
file.WriteLine(line);
}
}
}
// Given a byte array, return its digest (format 'hex string').
public static string Hash_ByteArray2HexDigest(byte[] ba, int maxLength)
{
byte[] hash = new MD5CryptoServiceProvider().ComputeHash(ba);
string outStr = StrByteArray2HexString(ref hash);
if (maxLength > outStr.Length)
{
maxLength = outStr.Length;
}
return (outStr.Substring(0, maxLength));
}
// Given an array of digests, extract a 'ngram' at a given position, and return the ngram-digest (i.e. digest of an ngram of digest).
public static int Hash_DigestArray2NGram(int[] digest, int pos, int nGramSize)
{
int start = pos - nGramSize + 1; // 'ngram' is calculated from 'start' to 'pos' (included)
if (start < 0)
{
start = 0;
}
// Get 'ngram' as a 'bytes_list'
List<byte> bytes_list = new List<byte>();
for (int i = start; i <= pos; i++)
{
byte[] bytes = BitConverter.GetBytes(digest[i]);
bytes_list.AddRange(bytes);
}
// From 'bytes_list' to hash
byte[] hash = new MD5CryptoServiceProvider().ComputeHash(bytes_list.ToArray());
int ngram = (hash[0] << 24)
+ (hash[1] << 16)
+ (hash[2] << 8)
+ hash[3]; // Use only 4 bytes of the hash code
return ngram;
}
// Given a list of strings, calculate the per-item-digest, that is an array with one digest (int) for each string.
// Similar to Hash_File2DigestArray().
// The per-line-digest is used to compare strings / files.
// Parameters:
// string str
// bool ignoreDigits
public static int[] Hash_StringList2DigestArray(List<string> listStr, bool ignoreDigits)
{
byte[] line2;
byte[] hash;
List<int> digest_list = new List<int>();
foreach (string l in listStr)
{
string line = l;
if (ignoreDigits)
{
line = StrRemoveDigits(line);
}
line2 = ASCIIEncoding.ASCII.GetBytes(line);
hash = new MD5CryptoServiceProvider().ComputeHash(line2);
digest_list.Add((hash[0] << 24)
+ (hash[1] << 16)
+ (hash[2] << 8)
+ hash[3]);
}
int[] digest = digest_list.ToArray();
return (digest);
}
public static bool RangeOverlapping(int start1, int end1, int start2, int end2)
{
if ((end1 < start2) || (start1 > end2))
{
return false;
}
return true;
}
// Least significant byte is on the left (i.e. address 0, 'little endian')
// Parameters:
// string separator :
// bool addQuotes : added to surround the description, only if the decsription contains spaces
public static string StrByteArray2HexString(ref byte[] array, int length, string separator, bool addQuotes)
{
StringBuilder sb = new StringBuilder(length);
for (int i = 0; i < length; i++)
{
sb.Append(separator);
sb.Append(array[i].ToString("X2"));
}
string sOutput = sb.ToString().Trim();
if ((addQuotes) && (sOutput.IndexOf(' ') >= 0))
{
sOutput = "'" + sOutput + "'";
}
return sOutput;
}
public static string StrByteArray2HexString(ref byte[] array)
{
return (StrByteArray2HexString(ref array, array.Length, "", false));
}
// Given a string that contains separated values, returns the first element, removing it from the list
public static string StrListGetFirst(ref string list, char sep)
{
string retVal = "";
if (list == "")
{
retVal = "";
}
else if (list.Substring(0, 1) == "\x22") // If starts with a "
{
if (list.Length == 1)
{
retVal = "";
list = "";
}
else if (list.IndexOf('\x22', 1) >= 0) // If there a second "
{
retVal = list.Substring(1, list.IndexOf('\x22', 1) - 1);
list = list.Substring(list.IndexOf('\x22', 1) + 1);
if (list.Trim().StartsWith(sep.ToString()))
{
list = list.Trim().Substring(1); // Remove the 'sep'
}
}
else
{
retVal = list.Substring(1);
list = "";
}
}
else
{
if (list.IndexOf(sep) >= 0)
{
retVal = list.Substring(0, list.IndexOf(sep));
list = list.Substring(list.IndexOf(sep) + 1);
}
else
{
retVal = list;
list = "";
}
}
return (retVal);
}
// Remove decimal and hexadecimal numbers from a string.
// This is used to ignore digits when comparing strings.
// Todo: well, this function does more and also contains application specific parts
public static string StrRemoveDigits(string s)
{
// Remove time stamp '33 28777809 250-1:7:96|...'
while (true)
{
Match match = Regex.Match(s, @"([^0-9]*)[0-9]+ [0-9]+ [0-9]+-[0-9]+:[0-9]+:[0-9]+\|(.*)");
if (!match.Success)
{
break;
}
s = match.Groups[1].Value + "{ts}" + match.Groups[2].Value;
}
// Remove hexadecimal e.g. '0xABCD'
while (true)
{
Match match = Regex.Match(s, @"(.*)0x[0-9a-fA-F]+(.*)");
if (!match.Success)
{
break;
}
s = match.Groups[1].Value + "{hex}" + match.Groups[2].Value;
}
// Remove decimal numbers e.g. 1234
while (true)
{
Match match = Regex.Match(s, @"([^0-9]*)[0-9]+(.*)");
if (!match.Success)
{
break;
}
s = match.Groups[1].Value + "{d}" + match.Groups[2].Value;
}
return s;
}
// Given a RichTextBox, return the column number of the current caret position
public static int Rtb_GetCaretColumnPos(RichTextBox rtb)
{
TextPointer caretPos = rtb.CaretPosition;
TextPointer lineStartPos = caretPos.GetLineStartPosition(0);
return Math.Max(lineStartPos.GetOffsetToPosition(caretPos) - 1, 0);
}
// Given a RichTextBox, return the current caret position
public static int Rtb_GetCaretPos(RichTextBox rtb)
{
TextPointer firstVisibleChar = rtb.GetPositionFromPoint(new Point(0, 0), true);
return firstVisibleChar.DocumentStart.GetOffsetToPosition(firstVisibleChar);
}
// Given a RichTextBox, return the line text of the current caret position
public static string Rtb_GetCaretLineText(RichTextBox rtb)
{
TextPointer caretPos = rtb.CaretPosition;
if (caretPos.GetLineStartPosition(1) == null)
{
return "<null>";
}
return new TextRange(caretPos.GetLineStartPosition(0), caretPos.GetLineStartPosition(1)).Text;
}
// Given a RichTextBox, return the line number of the current caret position
public static int Rtb_GetCaretLinePos(RichTextBox rtb)
{
TextPointer caretPos = rtb.CaretPosition;
TextPointer caretLineStart = caretPos.GetLineStartPosition(0);
TextPointer pp = rtb.Document.ContentStart.GetLineStartPosition(0);
int caretLinePos = 0;
while (true)
{
if (caretLineStart.CompareTo(pp) < 0)
{
break;
}
int result;
pp = pp.GetLineStartPosition(1, out result);
if (result == 0)
{
break;
}
caretLinePos++;
}
return caretLinePos;
}
public static string Rtb_GetContent(RichTextBox textBox)
{
string text_str = new TextRange(textBox.Document.ContentStart, textBox.Document.ContentEnd).Text;
if (text_str.EndsWith("\r\n"))
{
text_str = text_str.Substring(0, text_str.Length - 2); // Because RichTextBox always adds a new line at the end
}
else
{
throw new Exception(String.Format("Ma non doveva finire così!"));
}
return text_str;
}
public static void Rtb_SetContent(RichTextBox textBox, string text)
{
textBox.Document.Blocks.Clear();
Paragraph p = new Paragraph();
p.Margin = new Thickness(0, 0, 0, 0);
p.Inlines.Add(new Run(text));
textBox.Document.Blocks.Add(p); // This always add an EOL at the end of the file
textBox.IsUndoEnabled = false; // To remove programmatical modification from undo-stack
textBox.IsUndoEnabled = true;
}
// Set the content of a RTB by using a StrDelta.
public static void Rtb_SetContent(RichTextBox textBox, Paragraph p)
{
textBox.Document.Blocks.Clear();
textBox.Document.Blocks.Add(p);
textBox.IsUndoEnabled = false; // To remove programmatical modifications from undo-stack
textBox.IsUndoEnabled = true;
}
public static string StrTrimQuotes(string s)
{
char[] trimChars = { '"' };
s = s.Trim(trimChars);
return (s);
}
public static string StrAddQuotes(string s)
{
s = Util.StrTrimQuotes(s);
s = '"' + s + '"';
return (s);
}
private static void Swap<T>(ref T arg1, ref T arg2)
{
T temp = arg1;
arg1 = arg2;
arg2 = temp;
}
} // public class Util
}