1 | // Utils.cs HDO, 2006-08-28; SN, 10.9.2007
|
---|
2 | // --------
|
---|
3 | // Collection of useful utilities (constants, types, and methods).
|
---|
4 | //=====================================|========================================
|
---|
5 |
|
---|
6 | #undef TEST_UTILS
|
---|
7 |
|
---|
8 | using System;
|
---|
9 | using System.IO;
|
---|
10 |
|
---|
11 | public class Utils {
|
---|
12 |
|
---|
13 | public const String MODULENAME = "Utils";
|
---|
14 |
|
---|
15 | public static void UtilsMethod(ModuleAction action, out String moduleName) {
|
---|
16 | //-----------------------------------|----------------------------------------
|
---|
17 | moduleName = MODULENAME;
|
---|
18 | switch (action) {
|
---|
19 | case ModuleAction.getModuleName:
|
---|
20 | return;
|
---|
21 | case ModuleAction.initModule:
|
---|
22 | timerRuns = false;
|
---|
23 | break;
|
---|
24 | case ModuleAction.resetModule:
|
---|
25 | timerRuns = false;
|
---|
26 | break;
|
---|
27 | case ModuleAction.cleanupModule:
|
---|
28 | return;
|
---|
29 | } // switch
|
---|
30 | } // UtilsMethod
|
---|
31 |
|
---|
32 | // --- general char constants ---
|
---|
33 | public const char EF = '\0';
|
---|
34 | public const char LF = '\n';
|
---|
35 | public const char NL = '\n';
|
---|
36 | public const char HT = '\t';
|
---|
37 | public const char VT = '\v';
|
---|
38 | public const char BS = '\b';
|
---|
39 | public const char CR = '\r';
|
---|
40 | public const char FF = '\f';
|
---|
41 | public const char BEL = '\a';
|
---|
42 |
|
---|
43 | public const int UCHAR_MAX = 255;
|
---|
44 | public const int CHAR_BIT = 8;
|
---|
45 | public const long INT_MAX = 2147483647;
|
---|
46 | public const long INT_MIN = -INT_MAX;
|
---|
47 | public const int BYTE_MAX = UCHAR_MAX;
|
---|
48 | public const int BYTE_BIT = CHAR_BIT;
|
---|
49 |
|
---|
50 | // --- general integer constants ---
|
---|
51 | public const int EXIT_FAILURE = -1;
|
---|
52 | public const int EXIT_SUCCESS = 0;
|
---|
53 | public const int KB = 1024;
|
---|
54 | public const int MB = KB * KB;
|
---|
55 |
|
---|
56 | // --- module handling types and program module handling ---
|
---|
57 |
|
---|
58 | public enum ModuleAction {
|
---|
59 | getModuleName,
|
---|
60 | initModule,
|
---|
61 | resetModule,
|
---|
62 | cleanupModule
|
---|
63 | } // ModuleAction
|
---|
64 |
|
---|
65 | public delegate void ModuleMethodDelegate(ModuleAction action,
|
---|
66 | out String moduleName);
|
---|
67 |
|
---|
68 | private struct ModuleInfo {
|
---|
69 | public String moduleName;
|
---|
70 | public ModuleMethodDelegate moduleMethod;
|
---|
71 | public bool init;
|
---|
72 | } // ModuleInfo
|
---|
73 |
|
---|
74 | private const int MAXMODULES = 20;
|
---|
75 | private static ModuleInfo[] mil = new ModuleInfo[MAXMODULES];
|
---|
76 | private static int modCnt;
|
---|
77 |
|
---|
78 | public static void InstallModule(String moduleName, ModuleMethodDelegate moduleMethod) {
|
---|
79 | //-----------------------------------|----------------------------------------
|
---|
80 | if (moduleName == MODULENAME) {
|
---|
81 | modCnt = 0;
|
---|
82 | for (int i = 0; i < MAXMODULES; i++) {
|
---|
83 | mil[i].moduleName = "";
|
---|
84 | mil[i].moduleMethod = null;
|
---|
85 | mil[i].init = false;
|
---|
86 | } // for
|
---|
87 | } // if
|
---|
88 | if (modCnt == MAXMODULES)
|
---|
89 | FatalError(MODULENAME, "InstallModule", "too many modules");
|
---|
90 | String mn;
|
---|
91 | moduleMethod(ModuleAction.getModuleName, out mn);
|
---|
92 | if (moduleName != mn)
|
---|
93 | FatalError(MODULENAME, "InstallModule",
|
---|
94 | "incorrect module name \"{0}\"", moduleName);
|
---|
95 | mil[modCnt].moduleName = moduleName;
|
---|
96 | mil[modCnt].moduleMethod = moduleMethod;
|
---|
97 | mil[modCnt].init = false;
|
---|
98 | modCnt++;
|
---|
99 | } // InstallModule
|
---|
100 |
|
---|
101 | private static bool withinCleanUp = false;
|
---|
102 |
|
---|
103 | public static void Modules(ModuleAction action) {
|
---|
104 | //-----------------------------------|----------------------------------------
|
---|
105 | String dummy;
|
---|
106 | switch (action) {
|
---|
107 | case ModuleAction.initModule:
|
---|
108 | for (int i = 0; i < modCnt; i++) {
|
---|
109 | if (!mil[i].init) {
|
---|
110 | mil[i].moduleMethod(ModuleAction.initModule, out dummy);
|
---|
111 | mil[i].init = true;
|
---|
112 | } else
|
---|
113 | FatalError(MODULENAME, "Modules",
|
---|
114 | "{0} reinitialized", mil[i].moduleName);
|
---|
115 | } // for
|
---|
116 | break;
|
---|
117 | case ModuleAction.resetModule:
|
---|
118 | case ModuleAction.cleanupModule:
|
---|
119 | if (!withinCleanUp) {
|
---|
120 | withinCleanUp = true;
|
---|
121 | for (int i = modCnt - 1; i >= 0; i--)
|
---|
122 | if (mil[i].init) {
|
---|
123 | mil[i].moduleMethod(action, out dummy);
|
---|
124 | mil[i].init = action != ModuleAction.cleanupModule;
|
---|
125 | } else
|
---|
126 | FatalError(MODULENAME, "Modules",
|
---|
127 | "{0} not initialized", mil[i].moduleName);
|
---|
128 | withinCleanUp = false;
|
---|
129 | } // if
|
---|
130 | break;
|
---|
131 | default:
|
---|
132 | FatalError(MODULENAME, "Modules",
|
---|
133 | "invalid ModuleAction {0}", action.ToString());
|
---|
134 | break;
|
---|
135 | } // switch
|
---|
136 | } // Modules
|
---|
137 |
|
---|
138 |
|
---|
139 | // --- misc. utilities ---
|
---|
140 |
|
---|
141 | public static void FatalError(String moduleName, String methodName,
|
---|
142 | String fmt, params Object[] p) {
|
---|
143 | //-----------------------------------|----------------------------------------
|
---|
144 | Console.WriteLine();
|
---|
145 | Console.WriteLine("*** FATAL ERROR in module {0}, method {1}",
|
---|
146 | moduleName, methodName);
|
---|
147 | Console.WriteLine("*** " + fmt, p);
|
---|
148 | Modules(ModuleAction.cleanupModule);
|
---|
149 | Environment.Exit(EXIT_FAILURE);
|
---|
150 | } // FatalError
|
---|
151 |
|
---|
152 | public static void GetInputFileName(String prompt, out String fileName) {
|
---|
153 | //-----------------------------------|----------------------------------------
|
---|
154 | Console.WriteLine();
|
---|
155 | Console.Write("{0}", prompt);
|
---|
156 | fileName = Console.ReadLine();
|
---|
157 | } // GetInputFileName
|
---|
158 |
|
---|
159 |
|
---|
160 | // --- timer utilities ---
|
---|
161 |
|
---|
162 | private static bool timerRuns = false;
|
---|
163 | private static DateTime startedAt, stoppedAt;
|
---|
164 |
|
---|
165 | public static void StartTimer() {
|
---|
166 | //-----------------------------------|----------------------------------------
|
---|
167 | if (timerRuns)
|
---|
168 | FatalError(MODULENAME, "StartTimer", "timer still running");
|
---|
169 | timerRuns = true;
|
---|
170 | startedAt = DateTime.Now;
|
---|
171 | } // StartTimer
|
---|
172 |
|
---|
173 | public static void StopTimer() {
|
---|
174 | //-----------------------------------|----------------------------------------
|
---|
175 | if (!timerRuns)
|
---|
176 | FatalError(MODULENAME, "StopTimer", "timer not running");
|
---|
177 | stoppedAt = DateTime.Now;
|
---|
178 | timerRuns = false;
|
---|
179 | } // StopTimer
|
---|
180 |
|
---|
181 | public static TimeSpan ElapsedTime() {
|
---|
182 | //-----------------------------------|----------------------------------------
|
---|
183 | TimeSpan elapsed = stoppedAt - startedAt;
|
---|
184 | return elapsed;
|
---|
185 | } // ElapsedTime
|
---|
186 |
|
---|
187 |
|
---|
188 | // --- Min/Max methods ---
|
---|
189 |
|
---|
190 | public static T Min<T>(T a, T b) where T : IComparable {
|
---|
191 | //-----------------------------------|----------------------------------------
|
---|
192 | return a.CompareTo(b) < 0 ? a : b;
|
---|
193 | } // Min
|
---|
194 |
|
---|
195 | public static T Max<T>(T a, T b) where T : IComparable {
|
---|
196 | //-----------------------------------|----------------------------------------
|
---|
197 | return a.CompareTo(b) > 0 ? a : b;
|
---|
198 | } // Max
|
---|
199 |
|
---|
200 |
|
---|
201 | // --- shift methods ---
|
---|
202 |
|
---|
203 | public static int BSL(int x, int i) {
|
---|
204 | //-----------------------------------|----------------------------------------
|
---|
205 | return x << i;
|
---|
206 | } // BSL
|
---|
207 |
|
---|
208 | public static int BSR(int x, int i) {
|
---|
209 | //-----------------------------------|----------------------------------------
|
---|
210 | return x >> i;
|
---|
211 | } // BSR
|
---|
212 |
|
---|
213 |
|
---|
214 | // --- bit manipulation methods ---
|
---|
215 |
|
---|
216 | public static ushort Bit(ushort i) {
|
---|
217 | //-----------------------------------|----------------------------------------
|
---|
218 | return (ushort)(1 << i);
|
---|
219 | } // Bit
|
---|
220 |
|
---|
221 | public static void SetBit(ref ushort x, ushort i) {
|
---|
222 | //-----------------------------------|----------------------------------------
|
---|
223 | x |= Bit(i);
|
---|
224 | } // SetBit
|
---|
225 |
|
---|
226 | public static void ClrBit(ref ushort x, ushort i) {
|
---|
227 | //-----------------------------------|----------------------------------------
|
---|
228 | x &= (ushort)(~Bit(i));
|
---|
229 | } // ClrBit
|
---|
230 |
|
---|
231 | public static bool TestBit(ushort x, ushort i) {
|
---|
232 | //-----------------------------------|----------------------------------------
|
---|
233 | return (x & Bit(i)) != 0;
|
---|
234 | } // TestBit
|
---|
235 |
|
---|
236 | public static int Strquotechar(ref string s, char ch) {
|
---|
237 | // -------------------------------------|--------------------------------------
|
---|
238 | switch (ch) {
|
---|
239 | case EF:
|
---|
240 | s = "EF";
|
---|
241 | break;
|
---|
242 | case LF:
|
---|
243 | s = "LF";
|
---|
244 | break;
|
---|
245 | case HT:
|
---|
246 | s = "HT";
|
---|
247 | break;
|
---|
248 | case VT:
|
---|
249 | s = "VT";
|
---|
250 | break;
|
---|
251 | case BS:
|
---|
252 | s = "BS";
|
---|
253 | break;
|
---|
254 | case CR:
|
---|
255 | s = "CR";
|
---|
256 | break;
|
---|
257 | case FF:
|
---|
258 | s = "FF";
|
---|
259 | break;
|
---|
260 | case BEL:
|
---|
261 | s = "BEL";
|
---|
262 | break;
|
---|
263 | case '\\':
|
---|
264 | case '\'':
|
---|
265 | case '\"':
|
---|
266 | s = "\'\\" + (char)ch + "\'";
|
---|
267 | break;
|
---|
268 | default:
|
---|
269 | if (!Char.IsControl(ch))
|
---|
270 | s = "\'" + (char)ch + "\'";
|
---|
271 | else
|
---|
272 | s = "\'\\" + (char)ch + "\'";
|
---|
273 | break;
|
---|
274 | }
|
---|
275 | return s.Length;
|
---|
276 | } // Strquotechar
|
---|
277 |
|
---|
278 | public static int Strquotestr(ref string s, string t) {
|
---|
279 | // -------------------------------------|--------------------------------------
|
---|
280 | char ch = ' ';
|
---|
281 | string s1;
|
---|
282 | int i = 0;
|
---|
283 |
|
---|
284 | s1 = s;
|
---|
285 | s += '"';
|
---|
286 | while (i < t.Length) {
|
---|
287 | ch = t[i];
|
---|
288 | i++;
|
---|
289 | switch (ch) {
|
---|
290 | case LF:
|
---|
291 | s += '\\';
|
---|
292 | s += 'n';
|
---|
293 | break;
|
---|
294 | case HT:
|
---|
295 | s += '\\';
|
---|
296 | s += 't';
|
---|
297 | break;
|
---|
298 | case VT:
|
---|
299 | s += '\\';
|
---|
300 | s += 'v';
|
---|
301 | break;
|
---|
302 | case BS:
|
---|
303 | s += '\\';
|
---|
304 | s += 'b';
|
---|
305 | break;
|
---|
306 | case CR:
|
---|
307 | s += '\\';
|
---|
308 | s += 'r';
|
---|
309 | break;
|
---|
310 | case FF:
|
---|
311 | s += '\\';
|
---|
312 | s += 'f';
|
---|
313 | break;
|
---|
314 | case BEL:
|
---|
315 | s += '\\';
|
---|
316 | s += 'a';
|
---|
317 | break;
|
---|
318 | case '\\':
|
---|
319 | case '\'':
|
---|
320 | case '"':
|
---|
321 | s += '\\';
|
---|
322 | s += ch;
|
---|
323 | break;
|
---|
324 | default:
|
---|
325 | if (!Char.IsControl(ch))
|
---|
326 | s += ch;
|
---|
327 | else {
|
---|
328 | s = "\\" + (char)ch;
|
---|
329 | s += s.Length.ToString();
|
---|
330 | }
|
---|
331 | break;
|
---|
332 | }
|
---|
333 | }
|
---|
334 | s += '"';
|
---|
335 | return s1.Length;
|
---|
336 | } // Strquotestr
|
---|
337 |
|
---|
338 | #if TEST_UTILS
|
---|
339 |
|
---|
340 | public static void Main(String[] args) {
|
---|
341 | Console.WriteLine("START: Utils");
|
---|
342 |
|
---|
343 | Console.WriteLine("installing ...");
|
---|
344 | InstallModule("Utils", new ModuleMethodDelegate(UtilsMethod));
|
---|
345 | Console.WriteLine("initModule ...");
|
---|
346 | Modules(ModuleAction.initModule);
|
---|
347 |
|
---|
348 | // FatalError(MODULENAME, "Main",
|
---|
349 | // "any {0} error {1}", "additional", "messages");
|
---|
350 |
|
---|
351 | Console.WriteLine("StartTimer ...");
|
---|
352 | StartTimer();
|
---|
353 | String fn;
|
---|
354 | GetInputFileName("input file name > ", out fn);
|
---|
355 | Console.WriteLine("input file name = " + fn);
|
---|
356 | Console.WriteLine("StopTimer ...");
|
---|
357 | StopTimer();
|
---|
358 | Console.WriteLine(ElapsedTime());
|
---|
359 |
|
---|
360 | Console.WriteLine("Min(17, 4) = " + Min(17, 4));
|
---|
361 | Console.WriteLine("Max(17, 4) = " + Max(17, 4));
|
---|
362 |
|
---|
363 | Console.WriteLine();
|
---|
364 | Console.WriteLine("BSL(1, 2) = " + BSL(1, 2));
|
---|
365 | Console.WriteLine("BSR(4, 2) = " + BSR(4, 2));
|
---|
366 | Console.WriteLine("Bit(4) = " + Bit(4));
|
---|
367 | ushort x = 1;
|
---|
368 | Console.WriteLine("x = " + x);
|
---|
369 | SetBit (ref x, 2);
|
---|
370 | Console.WriteLine("after SetBit (x, 2), x = " + x);
|
---|
371 | Console.WriteLine("TestBit(x, 2) = " + TestBit(x, 2));
|
---|
372 | ClrBit (ref x, 2);
|
---|
373 | Console.WriteLine("after ClrBit (x, 2), x = " + x);
|
---|
374 | Console.WriteLine("TestBit(x, 2) = " + TestBit(x, 2));
|
---|
375 |
|
---|
376 |
|
---|
377 | Console.WriteLine("resetModule ...");
|
---|
378 | Modules(ModuleAction.resetModule);
|
---|
379 |
|
---|
380 | Console.WriteLine("cleanupModule ...");
|
---|
381 | Modules(ModuleAction.cleanupModule);
|
---|
382 |
|
---|
383 | Console.WriteLine("END");
|
---|
384 | // Console.WriteLine("type [CR] to continue ...");
|
---|
385 | // Console.ReadLine();
|
---|
386 | } // Main
|
---|
387 |
|
---|
388 | #endif
|
---|
389 |
|
---|
390 | } // Utils
|
---|
391 |
|
---|
392 | // End of Utils.cs
|
---|
393 | //=====================================|========================================
|
---|
394 |
|
---|
395 |
|
---|