Details | Last modification | View Log | RSS feed
Rev | Author | Line No. | Line |
---|---|---|---|
1702 | - | 1 | #region Usings |
2 | using System; |
||
3 | using System.IO; |
||
4 | using System.Text; |
||
5 | using System.Text.RegularExpressions; |
||
6 | #endregion |
||
7 | |||
8 | namespace AT.MIN |
||
9 | { |
||
10 | public static class Tools |
||
11 | { |
||
12 | #region Public Methods |
||
13 | #region IsNumericType |
||
14 | /// <summary> |
||
15 | /// Determines whether the specified value is of numeric type. |
||
16 | /// </summary> |
||
17 | /// <param name="o">The object to check.</param> |
||
18 | /// <returns> |
||
19 | /// <c>true</c> if o is a numeric type; otherwise, <c>false</c>. |
||
20 | /// </returns> |
||
21 | public static bool IsNumericType(object o) |
||
22 | { |
||
23 | return (o is byte || |
||
24 | o is sbyte || |
||
25 | o is short || |
||
26 | o is ushort || |
||
27 | o is int || |
||
28 | o is uint || |
||
29 | o is long || |
||
30 | o is ulong || |
||
31 | o is float || |
||
32 | o is double || |
||
33 | o is decimal); |
||
34 | } |
||
35 | #endregion |
||
36 | #region IsPositive |
||
37 | /// <summary> |
||
38 | /// Determines whether the specified value is positive. |
||
39 | /// </summary> |
||
40 | /// <param name="Value">The value.</param> |
||
41 | /// <param name="ZeroIsPositive">if set to <c>true</c> treats 0 as positive.</param> |
||
42 | /// <returns> |
||
43 | /// <c>true</c> if the specified value is positive; otherwise, <c>false</c>. |
||
44 | /// </returns> |
||
45 | public static bool IsPositive(object Value, bool ZeroIsPositive) |
||
46 | { |
||
47 | switch (Type.GetTypeCode(Value.GetType())) |
||
48 | { |
||
49 | case TypeCode.SByte: |
||
50 | return (ZeroIsPositive ? (sbyte)Value >= 0 : (sbyte)Value > 0); |
||
51 | case TypeCode.Int16: |
||
52 | return (ZeroIsPositive ? (short)Value >= 0 : (short)Value > 0); |
||
53 | case TypeCode.Int32: |
||
54 | return (ZeroIsPositive ? (int)Value >= 0 : (int)Value > 0); |
||
55 | case TypeCode.Int64: |
||
56 | return (ZeroIsPositive ? (long)Value >= 0 : (long)Value > 0); |
||
57 | case TypeCode.Single: |
||
58 | return (ZeroIsPositive ? (float)Value >= 0 : (float)Value > 0); |
||
59 | case TypeCode.Double: |
||
60 | return (ZeroIsPositive ? (double)Value >= 0 : (double)Value > 0); |
||
61 | case TypeCode.Decimal: |
||
62 | return (ZeroIsPositive ? (decimal)Value >= 0 : (decimal)Value > 0); |
||
63 | case TypeCode.Byte: |
||
64 | return (ZeroIsPositive ? true : (byte)Value > 0); |
||
65 | case TypeCode.UInt16: |
||
66 | return (ZeroIsPositive ? true : (ushort)Value > 0); |
||
67 | case TypeCode.UInt32: |
||
68 | return (ZeroIsPositive ? true : (uint)Value > 0); |
||
69 | case TypeCode.UInt64: |
||
70 | return (ZeroIsPositive ? true : (ulong)Value > 0); |
||
71 | case TypeCode.Char: |
||
72 | return (ZeroIsPositive ? true : (char)Value != '\0'); |
||
73 | default: |
||
74 | return false; |
||
75 | } |
||
76 | } |
||
77 | #endregion |
||
78 | #region ToUnsigned |
||
79 | /// <summary> |
||
80 | /// Converts the specified values boxed type to its correpsonding unsigned |
||
81 | /// type. |
||
82 | /// </summary> |
||
83 | /// <param name="Value">The value.</param> |
||
84 | /// <returns>A boxed numeric object whos type is unsigned.</returns> |
||
85 | public static object ToUnsigned(object Value) |
||
86 | { |
||
87 | switch (Type.GetTypeCode(Value.GetType())) |
||
88 | { |
||
89 | case TypeCode.SByte: |
||
90 | return (byte)((sbyte)Value); |
||
91 | case TypeCode.Int16: |
||
92 | return (ushort)((short)Value); |
||
93 | case TypeCode.Int32: |
||
94 | return (uint)((int)Value); |
||
95 | case TypeCode.Int64: |
||
96 | return (ulong)((long)Value); |
||
97 | |||
98 | case TypeCode.Byte: |
||
99 | return Value; |
||
100 | case TypeCode.UInt16: |
||
101 | return Value; |
||
102 | case TypeCode.UInt32: |
||
103 | return Value; |
||
104 | case TypeCode.UInt64: |
||
105 | return Value; |
||
106 | |||
107 | case TypeCode.Single: |
||
108 | return (UInt32)((float)Value); |
||
109 | case TypeCode.Double: |
||
110 | return (ulong)((double)Value); |
||
111 | case TypeCode.Decimal: |
||
112 | return (ulong)((decimal)Value); |
||
113 | |||
114 | default: |
||
115 | return null; |
||
116 | } |
||
117 | } |
||
118 | #endregion |
||
119 | #region ToInteger |
||
120 | /// <summary> |
||
121 | /// Converts the specified values boxed type to its correpsonding integer |
||
122 | /// type. |
||
123 | /// </summary> |
||
124 | /// <param name="Value">The value.</param> |
||
125 | /// <returns>A boxed numeric object whos type is an integer type.</returns> |
||
126 | public static object ToInteger(object Value, bool Round) |
||
127 | { |
||
128 | switch (Type.GetTypeCode(Value.GetType())) |
||
129 | { |
||
130 | case TypeCode.SByte: |
||
131 | return Value; |
||
132 | case TypeCode.Int16: |
||
133 | return Value; |
||
134 | case TypeCode.Int32: |
||
135 | return Value; |
||
136 | case TypeCode.Int64: |
||
137 | return Value; |
||
138 | |||
139 | case TypeCode.Byte: |
||
140 | return Value; |
||
141 | case TypeCode.UInt16: |
||
142 | return Value; |
||
143 | case TypeCode.UInt32: |
||
144 | return Value; |
||
145 | case TypeCode.UInt64: |
||
146 | return Value; |
||
147 | |||
148 | case TypeCode.Single: |
||
149 | return (Round ? (int)Math.Round((float)Value) : (int)((float)Value)); |
||
150 | case TypeCode.Double: |
||
151 | return (Round ? (long)Math.Round((double)Value) : (long)((double)Value)); |
||
152 | case TypeCode.Decimal: |
||
153 | return (Round ? Math.Round((decimal)Value) : (decimal)Value); |
||
154 | |||
155 | default: |
||
156 | return null; |
||
157 | } |
||
158 | } |
||
159 | #endregion |
||
160 | #region UnboxToLong |
||
161 | public static long UnboxToLong(object Value, bool Round) |
||
162 | { |
||
163 | switch (Type.GetTypeCode(Value.GetType())) |
||
164 | { |
||
165 | case TypeCode.SByte: |
||
166 | return (long)((sbyte)Value); |
||
167 | case TypeCode.Int16: |
||
168 | return (long)((short)Value); |
||
169 | case TypeCode.Int32: |
||
170 | return (long)((int)Value); |
||
171 | case TypeCode.Int64: |
||
172 | return (long)Value; |
||
173 | |||
174 | case TypeCode.Byte: |
||
175 | return (long)((byte)Value); |
||
176 | case TypeCode.UInt16: |
||
177 | return (long)((ushort)Value); |
||
178 | case TypeCode.UInt32: |
||
179 | return (long)((uint)Value); |
||
180 | case TypeCode.UInt64: |
||
181 | return (long)((ulong)Value); |
||
182 | |||
183 | case TypeCode.Single: |
||
184 | return (Round ? (long)Math.Round((float)Value) : (long)((float)Value)); |
||
185 | case TypeCode.Double: |
||
186 | return (Round ? (long)Math.Round((double)Value) : (long)((double)Value)); |
||
187 | case TypeCode.Decimal: |
||
188 | return (Round ? (long)Math.Round((decimal)Value) : (long)((decimal)Value)); |
||
189 | |||
190 | default: |
||
191 | return 0; |
||
192 | } |
||
193 | } |
||
194 | #endregion |
||
195 | #region ReplaceMetaChars |
||
196 | /// <summary> |
||
197 | /// Replaces the string representations of meta chars with their corresponding |
||
198 | /// character values. |
||
199 | /// </summary> |
||
200 | /// <param name="input">The input.</param> |
||
201 | /// <returns>A string with all string meta chars are replaced</returns> |
||
202 | public static string ReplaceMetaChars(string input) |
||
203 | { |
||
204 | return Regex.Replace(input, @"(\\)(\d{3}|[^\d])?", new MatchEvaluator(ReplaceMetaCharsMatch)); |
||
205 | } |
||
206 | private static string ReplaceMetaCharsMatch(Match m) |
||
207 | { |
||
208 | // convert octal quotes (like \040) |
||
209 | if (m.Groups[2].Length == 3) |
||
210 | return Convert.ToChar(Convert.ToByte(m.Groups[2].Value, 8)).ToString(); |
||
211 | else |
||
212 | { |
||
213 | // convert all other special meta characters |
||
214 | //TODO: \xhhh hex and possible dec !! |
||
215 | switch (m.Groups[2].Value) |
||
216 | { |
||
217 | case "0": // null |
||
218 | return "\0"; |
||
219 | case "a": // alert (beep) |
||
220 | return "\a"; |
||
221 | case "b": // BS |
||
222 | return "\b"; |
||
223 | case "f": // FF |
||
224 | return "\f"; |
||
225 | case "v": // vertical tab |
||
226 | return "\v"; |
||
227 | case "r": // CR |
||
228 | return "\r"; |
||
229 | case "n": // LF |
||
230 | return "\n"; |
||
231 | case "t": // Tab |
||
232 | return "\t"; |
||
233 | default: |
||
234 | // if neither an octal quote nor a special meta character |
||
235 | // so just remove the backslash |
||
236 | return m.Groups[2].Value; |
||
237 | } |
||
238 | } |
||
239 | } |
||
240 | #endregion |
||
241 | #region printf |
||
242 | public static void printf(string Format, params object[] Parameters) |
||
243 | { |
||
244 | Console.Write(Tools.sprintf(Format, Parameters)); |
||
245 | } |
||
246 | #endregion |
||
247 | #region fprintf |
||
248 | public static void fprintf(TextWriter Destination, string Format, params object[] Parameters) |
||
249 | { |
||
250 | Destination.Write(Tools.sprintf(Format, Parameters)); |
||
251 | } |
||
252 | #endregion |
||
253 | #region sprintf |
||
254 | public static string sprintf(string Format, params object[] Parameters) |
||
255 | { |
||
256 | #region Variables |
||
257 | StringBuilder f = new StringBuilder(); |
||
258 | Regex r = new Regex(@"\%(\d*\$)?([\'\#\-\+ ]*)(\d*)(?:\.(\d+))?([hl])?([dioxXucsfeEgGpn%])"); |
||
259 | //"%[parameter][flags][width][.precision][length]type" |
||
260 | Match m = null; |
||
261 | string w = String.Empty; |
||
262 | int defaultParamIx = 0; |
||
263 | int paramIx; |
||
264 | object o = null; |
||
265 | |||
266 | bool flagLeft2Right = false; |
||
267 | bool flagAlternate = false; |
||
268 | bool flagPositiveSign = false; |
||
269 | bool flagPositiveSpace = false; |
||
270 | bool flagZeroPadding = false; |
||
271 | bool flagGroupThousands = false; |
||
272 | |||
273 | int fieldLength = 0; |
||
274 | int fieldPrecision = 0; |
||
275 | char shortLongIndicator = '\0'; |
||
276 | char formatSpecifier = '\0'; |
||
277 | char paddingCharacter = ' '; |
||
278 | #endregion |
||
279 | |||
280 | // find all format parameters in format string |
||
281 | f.Append(Format); |
||
282 | m = r.Match(f.ToString()); |
||
283 | while (m.Success) |
||
284 | { |
||
285 | #region parameter index |
||
286 | paramIx = defaultParamIx; |
||
287 | if (m.Groups[1] != null && m.Groups[1].Value.Length > 0) |
||
288 | { |
||
289 | string val = m.Groups[1].Value.Substring(0, m.Groups[1].Value.Length - 1); |
||
290 | paramIx = Convert.ToInt32(val) - 1; |
||
291 | }; |
||
292 | #endregion |
||
293 | |||
294 | #region format flags |
||
295 | // extract format flags |
||
296 | flagAlternate = false; |
||
297 | flagLeft2Right = false; |
||
298 | flagPositiveSign = false; |
||
299 | flagPositiveSpace = false; |
||
300 | flagZeroPadding = false; |
||
301 | flagGroupThousands = false; |
||
302 | if (m.Groups[2] != null && m.Groups[2].Value.Length > 0) |
||
303 | { |
||
304 | string flags = m.Groups[2].Value; |
||
305 | |||
306 | flagAlternate = (flags.IndexOf('#') >= 0); |
||
307 | flagLeft2Right = (flags.IndexOf('-') >= 0); |
||
308 | flagPositiveSign = (flags.IndexOf('+') >= 0); |
||
309 | flagPositiveSpace = (flags.IndexOf(' ') >= 0); |
||
310 | flagGroupThousands = (flags.IndexOf('\'') >= 0); |
||
311 | |||
312 | // positive + indicator overrides a |
||
313 | // positive space character |
||
314 | if (flagPositiveSign && flagPositiveSpace) |
||
315 | flagPositiveSpace = false; |
||
316 | } |
||
317 | #endregion |
||
318 | |||
319 | #region field length |
||
320 | // extract field length and |
||
321 | // pading character |
||
322 | paddingCharacter = ' '; |
||
323 | fieldLength = int.MinValue; |
||
324 | if (m.Groups[3] != null && m.Groups[3].Value.Length > 0) |
||
325 | { |
||
326 | fieldLength = Convert.ToInt32(m.Groups[3].Value); |
||
327 | flagZeroPadding = (m.Groups[3].Value[0] == '0'); |
||
328 | } |
||
329 | #endregion |
||
330 | |||
331 | if (flagZeroPadding) |
||
332 | paddingCharacter = '0'; |
||
333 | |||
334 | // left2right allignment overrides zero padding |
||
335 | if (flagLeft2Right && flagZeroPadding) |
||
336 | { |
||
337 | flagZeroPadding = false; |
||
338 | paddingCharacter = ' '; |
||
339 | } |
||
340 | |||
341 | #region field precision |
||
342 | // extract field precision |
||
343 | fieldPrecision = int.MinValue; |
||
344 | if (m.Groups[4] != null && m.Groups[4].Value.Length > 0) |
||
345 | fieldPrecision = Convert.ToInt32(m.Groups[4].Value); |
||
346 | #endregion |
||
347 | |||
348 | #region short / long indicator |
||
349 | // extract short / long indicator |
||
350 | shortLongIndicator = Char.MinValue; |
||
351 | if (m.Groups[5] != null && m.Groups[5].Value.Length > 0) |
||
352 | shortLongIndicator = m.Groups[5].Value[0]; |
||
353 | #endregion |
||
354 | |||
355 | #region format specifier |
||
356 | // extract format |
||
357 | formatSpecifier = Char.MinValue; |
||
358 | if (m.Groups[6] != null && m.Groups[6].Value.Length > 0) |
||
359 | formatSpecifier = m.Groups[6].Value[0]; |
||
360 | #endregion |
||
361 | |||
362 | // default precision is 6 digits if none is specified except |
||
363 | if (fieldPrecision == int.MinValue && |
||
364 | formatSpecifier != 's' && |
||
365 | formatSpecifier != 'c' && |
||
366 | Char.ToUpper(formatSpecifier) != 'X' && |
||
367 | formatSpecifier != 'o') |
||
368 | fieldPrecision = 6; |
||
369 | |||
370 | #region get next value parameter |
||
371 | // get next value parameter and convert value parameter depending on short / long indicator |
||
372 | if (Parameters == null || paramIx >= Parameters.Length) |
||
373 | o = null; |
||
374 | else |
||
375 | { |
||
376 | o = Parameters[paramIx]; |
||
377 | |||
378 | if (shortLongIndicator == 'h') |
||
379 | { |
||
380 | if (o is int) |
||
381 | o = (short)((int)o); |
||
382 | else if (o is long) |
||
383 | o = (short)((long)o); |
||
384 | else if (o is uint) |
||
385 | o = (ushort)((uint)o); |
||
386 | else if (o is ulong) |
||
387 | o = (ushort)((ulong)o); |
||
388 | } |
||
389 | else if (shortLongIndicator == 'l') |
||
390 | { |
||
391 | if (o is short) |
||
392 | o = (long)((short)o); |
||
393 | else if (o is int) |
||
394 | o = (long)((int)o); |
||
395 | else if (o is ushort) |
||
396 | o = (ulong)((ushort)o); |
||
397 | else if (o is uint) |
||
398 | o = (ulong)((uint)o); |
||
399 | } |
||
400 | } |
||
401 | #endregion |
||
402 | |||
403 | // convert value parameters to a string depending on the formatSpecifier |
||
404 | w = String.Empty; |
||
405 | switch (formatSpecifier) |
||
406 | { |
||
407 | #region % - character |
||
408 | case '%': // % character |
||
409 | w = "%"; |
||
410 | break; |
||
411 | #endregion |
||
412 | #region d - integer |
||
413 | case 'd': // integer |
||
414 | w = FormatNumber((flagGroupThousands ? "n" : "d"), flagAlternate, |
||
415 | fieldLength, int.MinValue, flagLeft2Right, |
||
416 | flagPositiveSign, flagPositiveSpace, |
||
417 | paddingCharacter, o); |
||
418 | defaultParamIx++; |
||
419 | break; |
||
420 | #endregion |
||
421 | #region i - integer |
||
422 | case 'i': // integer |
||
423 | goto case 'd'; |
||
424 | #endregion |
||
425 | #region o - octal integer |
||
426 | case 'o': // octal integer - no leading zero |
||
427 | w = FormatOct("o", flagAlternate, |
||
428 | fieldLength, int.MinValue, flagLeft2Right, |
||
429 | paddingCharacter, o); |
||
430 | defaultParamIx++; |
||
431 | break; |
||
432 | #endregion |
||
433 | #region x - hex integer |
||
434 | case 'x': // hex integer - no leading zero |
||
435 | w = FormatHex("x", flagAlternate, |
||
436 | fieldLength, fieldPrecision, flagLeft2Right, |
||
437 | paddingCharacter, o); |
||
438 | defaultParamIx++; |
||
439 | break; |
||
440 | #endregion |
||
441 | #region X - hex integer |
||
442 | case 'X': // same as x but with capital hex characters |
||
443 | w = FormatHex("X", flagAlternate, |
||
444 | fieldLength, fieldPrecision, flagLeft2Right, |
||
445 | paddingCharacter, o); |
||
446 | defaultParamIx++; |
||
447 | break; |
||
448 | #endregion |
||
449 | #region u - unsigned integer |
||
450 | case 'u': // unsigned integer |
||
451 | w = FormatNumber((flagGroupThousands ? "n" : "d"), flagAlternate, |
||
452 | fieldLength, int.MinValue, flagLeft2Right, |
||
453 | false, false, |
||
454 | paddingCharacter, ToUnsigned(o)); |
||
455 | defaultParamIx++; |
||
456 | break; |
||
457 | #endregion |
||
458 | #region c - character |
||
459 | case 'c': // character |
||
460 | if (IsNumericType(o)) |
||
461 | w = Convert.ToChar(o).ToString(); |
||
462 | else if (o is char) |
||
463 | w = ((char)o).ToString(); |
||
464 | else if (o is string && ((string)o).Length > 0) |
||
465 | w = ((string)o)[0].ToString(); |
||
466 | defaultParamIx++; |
||
467 | break; |
||
468 | #endregion |
||
469 | #region s - string |
||
470 | case 's': // string |
||
471 | string t = "{0" + (fieldLength != int.MinValue ? "," + (flagLeft2Right ? "-" : String.Empty) + fieldLength.ToString() : String.Empty) + ":s}"; |
||
472 | w = o.ToString(); |
||
473 | if (fieldPrecision >= 0) |
||
474 | w = w.Substring(0, fieldPrecision); |
||
475 | |||
476 | if (fieldLength != int.MinValue) |
||
477 | if (flagLeft2Right) |
||
478 | w = w.PadRight(fieldLength, paddingCharacter); |
||
479 | else |
||
480 | w = w.PadLeft(fieldLength, paddingCharacter); |
||
481 | defaultParamIx++; |
||
482 | break; |
||
483 | #endregion |
||
484 | #region f - double number |
||
485 | case 'f': // double |
||
486 | w = FormatNumber((flagGroupThousands ? "n" : "f"), flagAlternate, |
||
487 | fieldLength, fieldPrecision, flagLeft2Right, |
||
488 | flagPositiveSign, flagPositiveSpace, |
||
489 | paddingCharacter, o); |
||
490 | defaultParamIx++; |
||
491 | break; |
||
492 | #endregion |
||
493 | #region e - exponent number |
||
494 | case 'e': // double / exponent |
||
495 | w = FormatNumber("e", flagAlternate, |
||
496 | fieldLength, fieldPrecision, flagLeft2Right, |
||
497 | flagPositiveSign, flagPositiveSpace, |
||
498 | paddingCharacter, o); |
||
499 | defaultParamIx++; |
||
500 | break; |
||
501 | #endregion |
||
502 | #region E - exponent number |
||
503 | case 'E': // double / exponent |
||
504 | w = FormatNumber("E", flagAlternate, |
||
505 | fieldLength, fieldPrecision, flagLeft2Right, |
||
506 | flagPositiveSign, flagPositiveSpace, |
||
507 | paddingCharacter, o); |
||
508 | defaultParamIx++; |
||
509 | break; |
||
510 | #endregion |
||
511 | #region g - general number |
||
512 | case 'g': // double / exponent |
||
513 | w = FormatNumber("g", flagAlternate, |
||
514 | fieldLength, fieldPrecision, flagLeft2Right, |
||
515 | flagPositiveSign, flagPositiveSpace, |
||
516 | paddingCharacter, o); |
||
517 | defaultParamIx++; |
||
518 | break; |
||
519 | #endregion |
||
520 | #region G - general number |
||
521 | case 'G': // double / exponent |
||
522 | w = FormatNumber("G", flagAlternate, |
||
523 | fieldLength, fieldPrecision, flagLeft2Right, |
||
524 | flagPositiveSign, flagPositiveSpace, |
||
525 | paddingCharacter, o); |
||
526 | defaultParamIx++; |
||
527 | break; |
||
528 | #endregion |
||
529 | #region p - pointer |
||
530 | case 'p': // pointer |
||
531 | if (o is IntPtr) |
||
532 | w = "0x" + ((IntPtr)o).ToString("x"); |
||
533 | defaultParamIx++; |
||
534 | break; |
||
535 | #endregion |
||
536 | #region n - number of processed chars so far |
||
537 | case 'n': // number of characters so far |
||
538 | w = FormatNumber("d", flagAlternate, |
||
539 | fieldLength, int.MinValue, flagLeft2Right, |
||
540 | flagPositiveSign, flagPositiveSpace, |
||
541 | paddingCharacter, m.Index); |
||
542 | break; |
||
543 | #endregion |
||
544 | default: |
||
545 | w = String.Empty; |
||
546 | defaultParamIx++; |
||
547 | break; |
||
548 | } |
||
549 | |||
550 | // replace format parameter with parameter value |
||
551 | // and start searching for the next format parameter |
||
552 | // AFTER the position of the current inserted value |
||
553 | // to prohibit recursive matches if the value also |
||
554 | // includes a format specifier |
||
555 | f.Remove(m.Index, m.Length); |
||
556 | f.Insert(m.Index, w); |
||
557 | m = r.Match(f.ToString(), m.Index + w.Length); |
||
558 | } |
||
559 | |||
560 | return f.ToString(); |
||
561 | } |
||
562 | #endregion |
||
563 | #endregion |
||
564 | |||
565 | #region Private Methods |
||
566 | #region FormatOCT |
||
567 | private static string FormatOct(string NativeFormat, bool Alternate, |
||
568 | int FieldLength, int FieldPrecision, |
||
569 | bool Left2Right, |
||
570 | char Padding, object Value) |
||
571 | { |
||
572 | string w = String.Empty; |
||
573 | string lengthFormat = "{0" + (FieldLength != int.MinValue ? |
||
574 | "," + (Left2Right ? |
||
575 | "-" : |
||
576 | String.Empty) + FieldLength.ToString() : |
||
577 | String.Empty) + "}"; |
||
578 | |||
579 | if (IsNumericType(Value)) |
||
580 | { |
||
581 | w = Convert.ToString(UnboxToLong(Value, true), 8); |
||
582 | |||
583 | if (Left2Right || Padding == ' ') |
||
584 | { |
||
585 | if (Alternate && w != "0") |
||
586 | w = "0" + w; |
||
587 | w = String.Format(lengthFormat, w); |
||
588 | } |
||
589 | else |
||
590 | { |
||
591 | if (FieldLength != int.MinValue) |
||
592 | w = w.PadLeft(FieldLength - (Alternate && w != "0" ? 1 : 0), Padding); |
||
593 | if (Alternate && w != "0") |
||
594 | w = "0" + w; |
||
595 | } |
||
596 | } |
||
597 | |||
598 | return w; |
||
599 | } |
||
600 | #endregion |
||
601 | #region FormatHEX |
||
602 | private static string FormatHex(string NativeFormat, bool Alternate, |
||
603 | int FieldLength, int FieldPrecision, |
||
604 | bool Left2Right, |
||
605 | char Padding, object Value) |
||
606 | { |
||
607 | string w = String.Empty; |
||
608 | string lengthFormat = "{0" + (FieldLength != int.MinValue ? |
||
609 | "," + (Left2Right ? |
||
610 | "-" : |
||
611 | String.Empty) + FieldLength.ToString() : |
||
612 | String.Empty) + "}"; |
||
613 | string numberFormat = "{0:" + NativeFormat + (FieldPrecision != int.MinValue ? |
||
614 | FieldPrecision.ToString() : |
||
615 | String.Empty) + "}"; |
||
616 | |||
617 | if (IsNumericType(Value)) |
||
618 | { |
||
619 | w = String.Format(numberFormat, Value); |
||
620 | |||
621 | if (Left2Right || Padding == ' ') |
||
622 | { |
||
623 | if (Alternate) |
||
624 | w = (NativeFormat == "x" ? "0x" : "0X") + w; |
||
625 | w = String.Format(lengthFormat, w); |
||
626 | } |
||
627 | else |
||
628 | { |
||
629 | if (FieldLength != int.MinValue) |
||
630 | w = w.PadLeft(FieldLength - (Alternate ? 2 : 0), Padding); |
||
631 | if (Alternate) |
||
632 | w = (NativeFormat == "x" ? "0x" : "0X") + w; |
||
633 | } |
||
634 | } |
||
635 | |||
636 | return w; |
||
637 | } |
||
638 | #endregion |
||
639 | #region FormatNumber |
||
640 | private static string FormatNumber(string NativeFormat, bool Alternate, |
||
641 | int FieldLength, int FieldPrecision, |
||
642 | bool Left2Right, |
||
643 | bool PositiveSign, bool PositiveSpace, |
||
644 | char Padding, object Value) |
||
645 | { |
||
646 | string w = String.Empty; |
||
647 | string lengthFormat = "{0" + (FieldLength != int.MinValue ? |
||
648 | "," + (Left2Right ? |
||
649 | "-" : |
||
650 | String.Empty) + FieldLength.ToString() : |
||
651 | String.Empty) + "}"; |
||
652 | string numberFormat = "{0:" + NativeFormat + (FieldPrecision != int.MinValue ? |
||
653 | FieldPrecision.ToString() : |
||
654 | "0") + "}"; |
||
655 | |||
656 | if (IsNumericType(Value)) |
||
657 | { |
||
658 | w = String.Format(numberFormat, Value); |
||
659 | |||
660 | if (Left2Right || Padding == ' ') |
||
661 | { |
||
662 | if (IsPositive(Value, true)) |
||
663 | w = (PositiveSign ? |
||
664 | "+" : (PositiveSpace ? " " : String.Empty)) + w; |
||
665 | w = String.Format(lengthFormat, w); |
||
666 | } |
||
667 | else |
||
668 | { |
||
669 | if (w.StartsWith("-")) |
||
670 | w = w.Substring(1); |
||
671 | if (FieldLength != int.MinValue) |
||
672 | w = w.PadLeft(FieldLength - 1, Padding); |
||
673 | if (IsPositive(Value, true)) |
||
674 | w = (PositiveSign ? |
||
675 | "+" : (PositiveSpace ? |
||
676 | " " : (FieldLength != int.MinValue ? |
||
677 | Padding.ToString() : String.Empty))) + w; |
||
678 | else |
||
679 | w = "-" + w; |
||
680 | } |
||
681 | } |
||
682 | |||
683 | return w; |
||
684 | } |
||
685 | #endregion |
||
686 | #endregion |
||
687 | } |
||
688 | } |
||
689 | |||
690 |