wasCSharpSQLite – Blame information for rev 4
?pathlinks?
Rev | Author | Line No. | Line |
---|---|---|---|
1 | office | 1 | // |
2 | // In order to convert some functionality to Visual C#, the Java Language Conversion Assistant |
||
3 | // creates "support classes" that duplicate the original functionality. |
||
4 | // |
||
5 | // Support classes replicate the functionality of the original code, but in some cases they are |
||
6 | // substantially different architecturally. Although every effort is made to preserve the |
||
7 | // original architecture of the application in the converted project, the user should be aware that |
||
8 | // the primary goal of these support classes is to replicate functionality, and that at times |
||
9 | // the architecture of the resulting solution may differ somewhat. |
||
10 | // |
||
11 | // Included in SQLite3 port to C# for use in testharness only; 2008 Noah B Hart |
||
12 | //$Header$ |
||
13 | |||
14 | using System; |
||
15 | using System.Collections; |
||
16 | using System.Globalization; |
||
17 | using System.IO; |
||
18 | using System.Reflection; |
||
19 | using System.Threading; |
||
20 | |||
21 | /// <summary> |
||
22 | /// This interface should be implemented by any class whose instances are intended |
||
23 | /// to be executed by a thread. |
||
24 | /// </summary> |
||
25 | public interface IThreadRunnable |
||
26 | { |
||
27 | /// <summary> |
||
28 | /// This method has to be implemented in order that starting of the thread causes the object's |
||
29 | /// run method to be called in that separately executing thread. |
||
30 | /// </summary> |
||
31 | void Run(); |
||
32 | } |
||
33 | |||
34 | /// <summary> |
||
35 | /// Contains conversion support elements such as classes, interfaces and static methods. |
||
36 | /// </summary> |
||
37 | public class SupportClass |
||
38 | { |
||
39 | /// <summary> |
||
40 | /// Support class used to handle threads |
||
41 | /// </summary> |
||
42 | public class ThreadClass : IThreadRunnable |
||
43 | { |
||
44 | /// <summary> |
||
45 | /// The instance of Thread |
||
46 | /// </summary> |
||
47 | private Thread threadField; |
||
48 | |||
49 | /// <summary> |
||
50 | /// Initializes a new instance of the ThreadClass class |
||
51 | /// </summary> |
||
52 | public ThreadClass() |
||
53 | { |
||
54 | threadField = new Thread( new ThreadStart( Run ) ); |
||
55 | } |
||
56 | |||
57 | /// <summary> |
||
58 | /// Initializes a new instance of the Thread class. |
||
59 | /// </summary> |
||
60 | /// <param name="Name">The name of the thread</param> |
||
61 | public ThreadClass( string Name ) |
||
62 | { |
||
63 | threadField = new Thread( new ThreadStart( Run ) ); |
||
64 | this.Name = Name; |
||
65 | } |
||
66 | |||
67 | /// <summary> |
||
68 | /// Initializes a new instance of the Thread class. |
||
69 | /// </summary> |
||
70 | /// <param name="Start">A ThreadStart delegate that references the methods to be invoked when this thread begins executing</param> |
||
71 | public ThreadClass( ThreadStart Start ) |
||
72 | { |
||
73 | threadField = new Thread( Start ); |
||
74 | } |
||
75 | |||
76 | /// <summary> |
||
77 | /// Initializes a new instance of the Thread class. |
||
78 | /// </summary> |
||
79 | /// <param name="Start">A ThreadStart delegate that references the methods to be invoked when this thread begins executing</param> |
||
80 | /// <param name="Name">The name of the thread</param> |
||
81 | public ThreadClass( ThreadStart Start, string Name ) |
||
82 | { |
||
83 | threadField = new Thread( Start ); |
||
84 | this.Name = Name; |
||
85 | } |
||
86 | |||
87 | /// <summary> |
||
88 | /// This method has no functionality unless the method is overridden |
||
89 | /// </summary> |
||
90 | public virtual void Run() |
||
91 | { |
||
92 | } |
||
93 | |||
94 | /// <summary> |
||
95 | /// Causes the operating system to change the state of the current thread instance to ThreadState.Running |
||
96 | /// </summary> |
||
97 | public void Start() |
||
98 | { |
||
99 | threadField.Start(); |
||
100 | } |
||
101 | |||
102 | /// <summary> |
||
103 | /// Interrupts a thread that is in the WaitSleepJoin thread state |
||
104 | /// </summary> |
||
105 | public void Interrupt() |
||
106 | { |
||
107 | threadField.Interrupt(); |
||
108 | } |
||
109 | |||
110 | /// <summary> |
||
111 | /// Gets the current thread instance |
||
112 | /// </summary> |
||
113 | public Thread Instance |
||
114 | { |
||
115 | get |
||
116 | { |
||
117 | return threadField; |
||
118 | } |
||
119 | set |
||
120 | { |
||
121 | threadField = value; |
||
122 | } |
||
123 | } |
||
124 | |||
125 | /// <summary> |
||
126 | /// Gets or sets the name of the thread |
||
127 | /// </summary> |
||
128 | public string Name |
||
129 | { |
||
130 | get |
||
131 | { |
||
132 | return threadField.Name; |
||
133 | } |
||
134 | set |
||
135 | { |
||
136 | if ( threadField.Name == null ) |
||
137 | threadField.Name = value; |
||
138 | } |
||
139 | } |
||
140 | |||
141 | /// <summary> |
||
142 | /// Gets or sets a value indicating the scheduling priority of a thread |
||
143 | /// </summary> |
||
144 | public ThreadPriority Priority |
||
145 | { |
||
146 | get |
||
147 | { |
||
148 | return threadField.Priority; |
||
149 | } |
||
150 | set |
||
151 | { |
||
152 | threadField.Priority = value; |
||
153 | } |
||
154 | } |
||
155 | |||
156 | /// <summary> |
||
157 | /// Gets a value indicating the execution status of the current thread |
||
158 | /// </summary> |
||
159 | public bool IsAlive |
||
160 | { |
||
161 | get |
||
162 | { |
||
163 | return threadField.IsAlive; |
||
164 | } |
||
165 | } |
||
166 | |||
167 | /// <summary> |
||
168 | /// Gets or sets a value indicating whether or not a thread is a background thread. |
||
169 | /// </summary> |
||
170 | public bool IsBackground |
||
171 | { |
||
172 | get |
||
173 | { |
||
174 | return threadField.IsBackground; |
||
175 | } |
||
176 | set |
||
177 | { |
||
178 | threadField.IsBackground = value; |
||
179 | } |
||
180 | } |
||
181 | |||
182 | /// <summary> |
||
183 | /// Blocks the calling thread until a thread terminates |
||
184 | /// </summary> |
||
185 | public void Join() |
||
186 | { |
||
187 | threadField.Join(); |
||
188 | } |
||
189 | |||
190 | /// <summary> |
||
191 | /// Blocks the calling thread until a thread terminates or the specified time elapses |
||
192 | /// </summary> |
||
193 | /// <param name="MiliSeconds">Time of wait in milliseconds</param> |
||
194 | public void Join( long MiliSeconds ) |
||
195 | { |
||
196 | lock ( this ) |
||
197 | { |
||
198 | threadField.Join( new TimeSpan( MiliSeconds * 10000 ) ); |
||
199 | } |
||
200 | } |
||
201 | |||
202 | /// <summary> |
||
203 | /// Blocks the calling thread until a thread terminates or the specified time elapses |
||
204 | /// </summary> |
||
205 | /// <param name="MiliSeconds">Time of wait in milliseconds</param> |
||
206 | /// <param name="NanoSeconds">Time of wait in nanoseconds</param> |
||
207 | public void Join( long MiliSeconds, int NanoSeconds ) |
||
208 | { |
||
209 | lock ( this ) |
||
210 | { |
||
211 | threadField.Join( new TimeSpan( MiliSeconds * 10000 + NanoSeconds * 100 ) ); |
||
212 | } |
||
213 | } |
||
214 | |||
215 | /// <summary> |
||
216 | /// Resumes a thread that has been suspended |
||
217 | /// </summary> |
||
218 | public void Resume() |
||
219 | { |
||
220 | threadField.Resume(); |
||
221 | } |
||
222 | |||
223 | /// <summary> |
||
224 | /// Raises a ThreadAbortException in the thread on which it is invoked, |
||
225 | /// to begin the process of terminating the thread. Calling this method |
||
226 | /// usually terminates the thread |
||
227 | /// </summary> |
||
228 | public void Abort() |
||
229 | { |
||
230 | threadField.Abort(); |
||
231 | } |
||
232 | |||
233 | /// <summary> |
||
234 | /// Raises a ThreadAbortException in the thread on which it is invoked, |
||
235 | /// to begin the process of terminating the thread while also providing |
||
236 | /// exception information about the thread termination. |
||
237 | /// Calling this method usually terminates the thread. |
||
238 | /// </summary> |
||
239 | /// <param name="stateInfo">An object that contains application-specific information, such as state, which can be used by the thread being aborted</param> |
||
240 | public void Abort( Object stateInfo ) |
||
241 | { |
||
242 | lock ( this ) |
||
243 | { |
||
244 | threadField.Abort( stateInfo ); |
||
245 | } |
||
246 | } |
||
247 | |||
248 | /// <summary> |
||
249 | /// Suspends the thread, if the thread is already suspended it has no effect |
||
250 | /// </summary> |
||
251 | public void Suspend() |
||
252 | { |
||
253 | threadField.Suspend(); |
||
254 | } |
||
255 | |||
256 | /// <summary> |
||
257 | /// Obtain a String that represents the current Object |
||
258 | /// </summary> |
||
259 | /// <returns>A String that represents the current Object</returns> |
||
260 | public override string ToString() |
||
261 | { |
||
262 | return "Thread[" + Name + "," + Priority.ToString() + "," + "" + "]"; |
||
263 | } |
||
264 | |||
265 | /// <summary> |
||
266 | /// Gets the currently running thread |
||
267 | /// </summary> |
||
268 | /// <returns>The currently running thread</returns> |
||
269 | public static ThreadClass Current() |
||
270 | { |
||
271 | ThreadClass CurrentThread = new ThreadClass(); |
||
272 | CurrentThread.Instance = Thread.CurrentThread; |
||
273 | return CurrentThread; |
||
274 | } |
||
275 | } |
||
276 | |||
277 | |||
278 | /*******************************/ |
||
279 | /// <summary> |
||
280 | /// Removes the first occurrence of an specific object from an ArrayList instance. |
||
281 | /// </summary> |
||
282 | /// <param name="arrayList">The ArrayList instance</param> |
||
283 | /// <param name="element">The element to remove</param> |
||
284 | /// <returns>True if item is found in the ArrayList; otherwise, false</returns> |
||
285 | public static Boolean VectorRemoveElement( ArrayList arrayList, Object element ) |
||
286 | { |
||
287 | Boolean containsItem = arrayList.Contains( element ); |
||
288 | arrayList.Remove( element ); |
||
289 | return containsItem; |
||
290 | } |
||
291 | |||
292 | /*******************************/ |
||
293 | /// <summary> |
||
294 | /// Converts an array of sbytes to an array of bytes |
||
295 | /// </summary> |
||
296 | /// <param name="sbyteArray">The array of sbytes to be converted</param> |
||
297 | /// <returns>The new array of bytes</returns> |
||
298 | public static byte[] ToByteArray( sbyte[] sbyteArray ) |
||
299 | { |
||
300 | byte[] byteArray = new byte[sbyteArray.Length]; |
||
301 | for ( int index = 0; index < sbyteArray.Length; index++ ) |
||
302 | byteArray[index] = (byte)sbyteArray[index]; |
||
303 | return byteArray; |
||
304 | } |
||
305 | |||
306 | /// <summary> |
||
307 | /// Converts a string to an array of bytes |
||
308 | /// </summary> |
||
309 | /// <param name="sourceString">The string to be converted</param> |
||
310 | /// <returns>The new array of bytes</returns> |
||
311 | public static byte[] ToByteArray( string sourceString ) |
||
312 | { |
||
313 | byte[] byteArray = new byte[sourceString.Length]; |
||
314 | for ( int index = 0; index < sourceString.Length; index++ ) |
||
315 | byteArray[index] = (byte)sourceString[index]; |
||
316 | return byteArray; |
||
317 | } |
||
318 | |||
319 | /// <summary> |
||
320 | /// Converts a array of object-type instances to a byte-type array. |
||
321 | /// </summary> |
||
322 | /// <param name="tempObjectArray">Array to convert.</param> |
||
323 | /// <returns>An array of byte type elements.</returns> |
||
324 | public static byte[] ToByteArray( object[] tempObjectArray ) |
||
325 | { |
||
326 | byte[] byteArray = new byte[tempObjectArray.Length]; |
||
327 | for ( int index = 0; index < tempObjectArray.Length; index++ ) |
||
328 | byteArray[index] = (byte)tempObjectArray[index]; |
||
329 | return byteArray; |
||
330 | } |
||
331 | |||
332 | |||
333 | /*******************************/ |
||
334 | /// <summary> |
||
335 | /// This method returns the literal value received |
||
336 | /// </summary> |
||
337 | /// <param name="literal">The literal to return</param> |
||
338 | /// <returns>The received value</returns> |
||
339 | public static long Identity( long literal ) |
||
340 | { |
||
341 | return literal; |
||
342 | } |
||
343 | |||
344 | /// <summary> |
||
345 | /// This method returns the literal value received |
||
346 | /// </summary> |
||
347 | /// <param name="literal">The literal to return</param> |
||
348 | /// <returns>The received value</returns> |
||
349 | public static ulong Identity( ulong literal ) |
||
350 | { |
||
351 | return literal; |
||
352 | } |
||
353 | |||
354 | /// <summary> |
||
355 | /// This method returns the literal value received |
||
356 | /// </summary> |
||
357 | /// <param name="literal">The literal to return</param> |
||
358 | /// <returns>The received value</returns> |
||
359 | public static float Identity( float literal ) |
||
360 | { |
||
361 | return literal; |
||
362 | } |
||
363 | |||
364 | /// <summary> |
||
365 | /// This method returns the literal value received |
||
366 | /// </summary> |
||
367 | /// <param name="literal">The literal to return</param> |
||
368 | /// <returns>The received value</returns> |
||
369 | public static double Identity( double literal ) |
||
370 | { |
||
371 | return literal; |
||
372 | } |
||
373 | |||
374 | /*******************************/ |
||
375 | /// <summary> |
||
376 | /// Copies an array of chars obtained from a String into a specified array of chars |
||
377 | /// </summary> |
||
378 | /// <param name="sourceString">The String to get the chars from</param> |
||
379 | /// <param name="sourceStart">Position of the String to start getting the chars</param> |
||
380 | /// <param name="sourceEnd">Position of the String to end getting the chars</param> |
||
381 | /// <param name="destinationArray">Array to return the chars</param> |
||
382 | /// <param name="destinationStart">Position of the destination array of chars to start storing the chars</param> |
||
383 | /// <returns>An array of chars</returns> |
||
384 | public static void GetCharsFromString( string sourceString, int sourceStart, int sourceEnd, ref char[] destinationArray, int destinationStart ) |
||
385 | { |
||
386 | int sourceCounter; |
||
387 | int destinationCounter; |
||
388 | sourceCounter = sourceStart; |
||
389 | destinationCounter = destinationStart; |
||
390 | while ( sourceCounter < sourceEnd ) |
||
391 | { |
||
392 | destinationArray[destinationCounter] = (char)sourceString[sourceCounter]; |
||
393 | sourceCounter++; |
||
394 | destinationCounter++; |
||
395 | } |
||
396 | } |
||
397 | |||
398 | /*******************************/ |
||
399 | /// <summary> |
||
400 | /// This class manages different issues for calendars. |
||
401 | /// The different calendars are internally managed using a hash table structure. |
||
402 | /// </summary> |
||
403 | public class CalendarManager |
||
404 | { |
||
405 | /// <summary> |
||
406 | /// Field number for get and set indicating the year. |
||
407 | /// </summary> |
||
408 | public const int YEAR = 0; |
||
409 | |||
410 | /// <summary> |
||
411 | /// Field number for get and set indicating the month. |
||
412 | /// </summary> |
||
413 | public const int MONTH = 1; |
||
414 | |||
415 | /// <summary> |
||
416 | /// Field number for get and set indicating the day of the month. |
||
417 | /// </summary> |
||
418 | public const int DATE = 2; |
||
419 | |||
420 | /// <summary> |
||
421 | /// Field number for get and set indicating the hour of the morning or afternoon. |
||
422 | /// </summary> |
||
423 | public const int HOUR = 3; |
||
424 | |||
425 | /// <summary> |
||
426 | /// Field number for get and set indicating the minute within the hour. |
||
427 | /// </summary> |
||
428 | public const int MINUTE = 4; |
||
429 | |||
430 | /// <summary> |
||
431 | /// Field number for get and set indicating the second within the minute. |
||
432 | /// </summary> |
||
433 | public const int SECOND = 5; |
||
434 | |||
435 | /// <summary> |
||
436 | /// Field number for get and set indicating the millisecond within the second. |
||
437 | /// </summary> |
||
438 | public const int MILLISECOND = 6; |
||
439 | |||
440 | /// <summary> |
||
441 | /// Field number for get and set indicating the day of the month. |
||
442 | /// </summary> |
||
443 | public const int DAY_OF_MONTH = 7; |
||
444 | |||
445 | /// <summary> |
||
446 | /// Field used to get or set the day of the week. |
||
447 | /// </summary> |
||
448 | public const int DAY_OF_WEEK = 8; |
||
449 | |||
450 | /// <summary> |
||
451 | /// Field number for get and set indicating the hour of the day. |
||
452 | /// </summary> |
||
453 | public const int HOUR_OF_DAY = 9; |
||
454 | |||
455 | /// <summary> |
||
456 | /// Field number for get and set indicating whether the HOUR is before or after noon. |
||
457 | /// </summary> |
||
458 | public const int AM_PM = 10; |
||
459 | |||
460 | /// <summary> |
||
461 | /// Value of the AM_PM field indicating the period of the day from midnight to just |
||
462 | /// before noon. |
||
463 | /// </summary> |
||
464 | public const int AM = 11; |
||
465 | |||
466 | /// <summary> |
||
467 | /// Value of the AM_PM field indicating the period of the day from noon to just before midnight. |
||
468 | /// </summary> |
||
469 | public const int PM = 12; |
||
470 | |||
471 | /// <summary> |
||
472 | /// The hash table that contains the type of calendars and its properties. |
||
473 | /// </summary> |
||
474 | static public CalendarHashTable manager = new CalendarHashTable(); |
||
475 | |||
476 | /// <summary> |
||
477 | /// Internal class that inherits from HashTable to manage the different calendars. |
||
478 | /// This structure will contain an instance of Calendar that represents |
||
479 | /// a type of calendar and its properties (represented by an instance of CalendarProperties |
||
480 | /// class). |
||
481 | /// </summary> |
||
482 | public class CalendarHashTable : Hashtable |
||
483 | { |
||
484 | /// <summary> |
||
485 | /// Gets the calendar current date and time. |
||
486 | /// </summary> |
||
487 | /// <param name="calendar">The calendar to get its current date and time.</param> |
||
488 | /// <returns>A DateTime value that indicates the current date and time for the |
||
489 | /// calendar given.</returns> |
||
490 | public DateTime GetDateTime( Calendar calendar ) |
||
491 | { |
||
492 | if ( this[calendar] != null ) |
||
493 | return ( (CalendarProperties)this[calendar] ).dateTime; |
||
494 | else |
||
495 | { |
||
496 | CalendarProperties tempProps = new CalendarProperties(); |
||
497 | tempProps.dateTime = DateTime.Now; |
||
498 | this.Add( calendar, tempProps ); |
||
499 | return this.GetDateTime( calendar ); |
||
500 | } |
||
501 | } |
||
502 | |||
503 | /// <summary> |
||
504 | /// Sets the specified DateTime value to the specified calendar. |
||
505 | /// </summary> |
||
506 | /// <param name="calendar">The calendar to set its date.</param> |
||
507 | /// <param name="date">The DateTime value to set to the calendar.</param> |
||
508 | public void SetDateTime( Calendar calendar, DateTime date ) |
||
509 | { |
||
510 | if ( this[calendar] != null ) |
||
511 | { |
||
512 | ( (CalendarProperties)this[calendar] ).dateTime = date; |
||
513 | } |
||
514 | else |
||
515 | { |
||
516 | CalendarProperties tempProps = new CalendarProperties(); |
||
517 | tempProps.dateTime = date; |
||
518 | this.Add( calendar, tempProps ); |
||
519 | } |
||
520 | } |
||
521 | |||
522 | /// <summary> |
||
523 | /// Sets the corresponding field in an specified calendar with the value given. |
||
524 | /// If the specified calendar does not have exist in the hash table, it creates a |
||
525 | /// new instance of the calendar with the current date and time and then assings it |
||
526 | /// the new specified value. |
||
527 | /// </summary> |
||
528 | /// <param name="calendar">The calendar to set its date or time.</param> |
||
529 | /// <param name="field">One of the fields that composes a date/time.</param> |
||
530 | /// <param name="fieldValue">The value to be set.</param> |
||
531 | public void Set( Calendar calendar, int field, int fieldValue ) |
||
532 | { |
||
533 | if ( this[calendar] != null ) |
||
534 | { |
||
535 | DateTime tempDate = ( (CalendarProperties)this[calendar] ).dateTime; |
||
536 | switch ( field ) |
||
537 | { |
||
538 | case CalendarManager.DATE: |
||
539 | tempDate = tempDate.AddDays( fieldValue - tempDate.Day ); |
||
540 | break; |
||
541 | case CalendarManager.HOUR: |
||
542 | tempDate = tempDate.AddHours( fieldValue - tempDate.Hour ); |
||
543 | break; |
||
544 | case CalendarManager.MILLISECOND: |
||
545 | tempDate = tempDate.AddMilliseconds( fieldValue - tempDate.Millisecond ); |
||
546 | break; |
||
547 | case CalendarManager.MINUTE: |
||
548 | tempDate = tempDate.AddMinutes( fieldValue - tempDate.Minute ); |
||
549 | break; |
||
550 | case CalendarManager.MONTH: |
||
551 | //Month value is 0-based. e.g., 0 for January |
||
552 | tempDate = tempDate.AddMonths( fieldValue - ( tempDate.Month + 1 ) ); |
||
553 | break; |
||
554 | case CalendarManager.SECOND: |
||
555 | tempDate = tempDate.AddSeconds( fieldValue - tempDate.Second ); |
||
556 | break; |
||
557 | case CalendarManager.YEAR: |
||
558 | tempDate = tempDate.AddYears( fieldValue - tempDate.Year ); |
||
559 | break; |
||
560 | case CalendarManager.DAY_OF_MONTH: |
||
561 | tempDate = tempDate.AddDays( fieldValue - tempDate.Day ); |
||
562 | break; |
||
563 | case CalendarManager.DAY_OF_WEEK: |
||
564 | ; |
||
565 | tempDate = tempDate.AddDays( ( fieldValue - 1 ) - (int)tempDate.DayOfWeek ); |
||
566 | break; |
||
567 | case CalendarManager.HOUR_OF_DAY: |
||
568 | tempDate = tempDate.AddHours( fieldValue - tempDate.Hour ); |
||
569 | break; |
||
570 | |||
571 | default: |
||
572 | break; |
||
573 | } |
||
574 | ( (CalendarProperties)this[calendar] ).dateTime = tempDate; |
||
575 | } |
||
576 | else |
||
577 | { |
||
578 | CalendarProperties tempProps = new CalendarProperties(); |
||
579 | tempProps.dateTime = DateTime.Now; |
||
580 | this.Add( calendar, tempProps ); |
||
581 | this.Set( calendar, field, fieldValue ); |
||
582 | } |
||
583 | } |
||
584 | |||
585 | /// <summary> |
||
586 | /// Sets the corresponding date (day, month and year) to the calendar specified. |
||
587 | /// If the calendar does not exist in the hash table, it creates a new instance and sets |
||
588 | /// its values. |
||
589 | /// </summary> |
||
590 | /// <param name="calendar">The calendar to set its date.</param> |
||
591 | /// <param name="year">Integer value that represent the year.</param> |
||
592 | /// <param name="month">Integer value that represent the month.</param> |
||
593 | /// <param name="day">Integer value that represent the day.</param> |
||
594 | public void Set( Calendar calendar, int year, int month, int day ) |
||
595 | { |
||
596 | if ( this[calendar] != null ) |
||
597 | { |
||
598 | this.Set( calendar, CalendarManager.YEAR, year ); |
||
599 | this.Set( calendar, CalendarManager.MONTH, month ); |
||
600 | this.Set( calendar, CalendarManager.DATE, day ); |
||
601 | } |
||
602 | else |
||
603 | { |
||
604 | CalendarProperties tempProps = new CalendarProperties(); |
||
605 | //Month value is 0-based. e.g., 0 for January |
||
606 | tempProps.dateTime = new DateTime( year, month + 1, day ); |
||
607 | this.Add( calendar, tempProps ); |
||
608 | } |
||
609 | } |
||
610 | |||
611 | /// <summary> |
||
612 | /// Sets the corresponding date (day, month and year) and hour (hour and minute) |
||
613 | /// to the calendar specified. |
||
614 | /// If the calendar does not exist in the hash table, it creates a new instance and sets |
||
615 | /// its values. |
||
616 | /// </summary> |
||
617 | /// <param name="calendar">The calendar to set its date and time.</param> |
||
618 | /// <param name="year">Integer value that represent the year.</param> |
||
619 | /// <param name="month">Integer value that represent the month.</param> |
||
620 | /// <param name="day">Integer value that represent the day.</param> |
||
621 | /// <param name="hour">Integer value that represent the hour.</param> |
||
622 | /// <param name="minute">Integer value that represent the minutes.</param> |
||
623 | public void Set( Calendar calendar, int year, int month, int day, int hour, int minute ) |
||
624 | { |
||
625 | if ( this[calendar] != null ) |
||
626 | { |
||
627 | this.Set( calendar, CalendarManager.YEAR, year ); |
||
628 | this.Set( calendar, CalendarManager.MONTH, month ); |
||
629 | this.Set( calendar, CalendarManager.DATE, day ); |
||
630 | this.Set( calendar, CalendarManager.HOUR, hour ); |
||
631 | this.Set( calendar, CalendarManager.MINUTE, minute ); |
||
632 | } |
||
633 | else |
||
634 | { |
||
635 | CalendarProperties tempProps = new CalendarProperties(); |
||
636 | //Month value is 0-based. e.g., 0 for January |
||
637 | tempProps.dateTime = new DateTime( year, month + 1, day, hour, minute, 0 ); |
||
638 | this.Add( calendar, tempProps ); |
||
639 | } |
||
640 | } |
||
641 | |||
642 | /// <summary> |
||
643 | /// Sets the corresponding date (day, month and year) and hour (hour, minute and second) |
||
644 | /// to the calendar specified. |
||
645 | /// If the calendar does not exist in the hash table, it creates a new instance and sets |
||
646 | /// its values. |
||
647 | /// </summary> |
||
648 | /// <param name="calendar">The calendar to set its date and time.</param> |
||
649 | /// <param name="year">Integer value that represent the year.</param> |
||
650 | /// <param name="month">Integer value that represent the month.</param> |
||
651 | /// <param name="day">Integer value that represent the day.</param> |
||
652 | /// <param name="hour">Integer value that represent the hour.</param> |
||
653 | /// <param name="minute">Integer value that represent the minutes.</param> |
||
654 | /// <param name="second">Integer value that represent the seconds.</param> |
||
655 | public void Set( Calendar calendar, int year, int month, int day, int hour, int minute, int second ) |
||
656 | { |
||
657 | if ( this[calendar] != null ) |
||
658 | { |
||
659 | this.Set( calendar, CalendarManager.YEAR, year ); |
||
660 | this.Set( calendar, CalendarManager.MONTH, month ); |
||
661 | this.Set( calendar, CalendarManager.DATE, day ); |
||
662 | this.Set( calendar, CalendarManager.HOUR, hour ); |
||
663 | this.Set( calendar, CalendarManager.MINUTE, minute ); |
||
664 | this.Set( calendar, CalendarManager.SECOND, second ); |
||
665 | } |
||
666 | else |
||
667 | { |
||
668 | CalendarProperties tempProps = new CalendarProperties(); |
||
669 | //Month value is 0-based. e.g., 0 for January |
||
670 | tempProps.dateTime = new DateTime( year, month + 1, day, hour, minute, second ); |
||
671 | this.Add( calendar, tempProps ); |
||
672 | } |
||
673 | } |
||
674 | |||
675 | /// <summary> |
||
676 | /// Gets the value represented by the field specified. |
||
677 | /// </summary> |
||
678 | /// <param name="calendar">The calendar to get its date or time.</param> |
||
679 | /// <param name="field">One of the field that composes a date/time.</param> |
||
680 | /// <returns>The integer value for the field given.</returns> |
||
681 | public int Get( Calendar calendar, int field ) |
||
682 | { |
||
683 | if ( this[calendar] != null ) |
||
684 | { |
||
685 | int tempHour; |
||
686 | switch ( field ) |
||
687 | { |
||
688 | case CalendarManager.DATE: |
||
689 | return ( (CalendarProperties)this[calendar] ).dateTime.Day; |
||
690 | case CalendarManager.HOUR: |
||
691 | tempHour = ( (CalendarProperties)this[calendar] ).dateTime.Hour; |
||
692 | return tempHour > 12 ? tempHour - 12 : tempHour; |
||
693 | case CalendarManager.MILLISECOND: |
||
694 | return ( (CalendarProperties)this[calendar] ).dateTime.Millisecond; |
||
695 | case CalendarManager.MINUTE: |
||
696 | return ( (CalendarProperties)this[calendar] ).dateTime.Minute; |
||
697 | case CalendarManager.MONTH: |
||
698 | //Month value is 0-based. e.g., 0 for January |
||
699 | return ( (CalendarProperties)this[calendar] ).dateTime.Month - 1; |
||
700 | case CalendarManager.SECOND: |
||
701 | return ( (CalendarProperties)this[calendar] ).dateTime.Second; |
||
702 | case CalendarManager.YEAR: |
||
703 | return ( (CalendarProperties)this[calendar] ).dateTime.Year; |
||
704 | case CalendarManager.DAY_OF_MONTH: |
||
705 | return ( (CalendarProperties)this[calendar] ).dateTime.Day; |
||
706 | case CalendarManager.DAY_OF_WEEK: |
||
707 | return (int)( ( (CalendarProperties)this[calendar] ).dateTime.DayOfWeek ); |
||
708 | case CalendarManager.HOUR_OF_DAY: |
||
709 | return ( (CalendarProperties)this[calendar] ).dateTime.Hour; |
||
710 | case CalendarManager.AM_PM: |
||
711 | tempHour = ( (CalendarProperties)this[calendar] ).dateTime.Hour; |
||
712 | return tempHour > 12 ? CalendarManager.PM : CalendarManager.AM; |
||
713 | |||
714 | default: |
||
715 | return 0; |
||
716 | } |
||
717 | } |
||
718 | else |
||
719 | { |
||
720 | CalendarProperties tempProps = new CalendarProperties(); |
||
721 | tempProps.dateTime = DateTime.Now; |
||
722 | this.Add( calendar, tempProps ); |
||
723 | return this.Get( calendar, field ); |
||
724 | } |
||
725 | } |
||
726 | |||
727 | /// <summary> |
||
728 | /// Sets the time in the specified calendar with the long value. |
||
729 | /// </summary> |
||
730 | /// <param name="calendar">The calendar to set its date and time.</param> |
||
731 | /// <param name="milliseconds">A long value that indicates the milliseconds to be set to |
||
732 | /// the hour for the calendar.</param> |
||
733 | public void SetTimeInMilliseconds( Calendar calendar, long milliseconds ) |
||
734 | { |
||
735 | if ( this[calendar] != null ) |
||
736 | { |
||
737 | ( (CalendarProperties)this[calendar] ).dateTime = new DateTime( milliseconds ); |
||
738 | } |
||
739 | else |
||
740 | { |
||
741 | CalendarProperties tempProps = new CalendarProperties(); |
||
742 | tempProps.dateTime = new DateTime( TimeSpan.TicksPerMillisecond * milliseconds ); |
||
743 | this.Add( calendar, tempProps ); |
||
744 | } |
||
745 | } |
||
746 | |||
747 | /// <summary> |
||
748 | /// Gets what the first day of the week is; e.g., Sunday in US, Monday in France. |
||
749 | /// </summary> |
||
750 | /// <param name="calendar">The calendar to get its first day of the week.</param> |
||
751 | /// <returns>A DayOfWeek value indicating the first day of the week.</returns> |
||
752 | public DayOfWeek GetFirstDayOfWeek( Calendar calendar ) |
||
753 | { |
||
754 | if ( this[calendar] != null && ( (CalendarProperties)this[calendar] ).dateTimeFormat != null ) |
||
755 | { |
||
756 | return ( (CalendarProperties)this[calendar] ).dateTimeFormat.FirstDayOfWeek; |
||
757 | } |
||
758 | else |
||
759 | { |
||
760 | CalendarProperties tempProps = new CalendarProperties(); |
||
761 | tempProps.dateTimeFormat = new DateTimeFormatInfo(); |
||
762 | tempProps.dateTimeFormat.FirstDayOfWeek = DayOfWeek.Sunday; |
||
763 | this.Add( calendar, tempProps ); |
||
764 | return this.GetFirstDayOfWeek( calendar ); |
||
765 | } |
||
766 | } |
||
767 | |||
768 | /// <summary> |
||
769 | /// Sets what the first day of the week is; e.g., Sunday in US, Monday in France. |
||
770 | /// </summary> |
||
771 | /// <param name="calendar">The calendar to set its first day of the week.</param> |
||
772 | /// <param name="firstDayOfWeek">A DayOfWeek value indicating the first day of the week |
||
773 | /// to be set.</param> |
||
774 | public void SetFirstDayOfWeek( Calendar calendar, DayOfWeek firstDayOfWeek ) |
||
775 | { |
||
776 | if ( this[calendar] != null && ( (CalendarProperties)this[calendar] ).dateTimeFormat != null ) |
||
777 | { |
||
778 | ( (CalendarProperties)this[calendar] ).dateTimeFormat.FirstDayOfWeek = firstDayOfWeek; |
||
779 | } |
||
780 | else |
||
781 | { |
||
782 | CalendarProperties tempProps = new CalendarProperties(); |
||
783 | tempProps.dateTimeFormat = new DateTimeFormatInfo(); |
||
784 | this.Add( calendar, tempProps ); |
||
785 | this.SetFirstDayOfWeek( calendar, firstDayOfWeek ); |
||
786 | } |
||
787 | } |
||
788 | |||
789 | /// <summary> |
||
790 | /// Removes the specified calendar from the hash table. |
||
791 | /// </summary> |
||
792 | /// <param name="calendar">The calendar to be removed.</param> |
||
793 | public void Clear( Calendar calendar ) |
||
794 | { |
||
795 | if ( this[calendar] != null ) |
||
796 | this.Remove( calendar ); |
||
797 | } |
||
798 | |||
799 | /// <summary> |
||
800 | /// Removes the specified field from the calendar given. |
||
801 | /// If the field does not exists in the calendar, the calendar is removed from the table. |
||
802 | /// </summary> |
||
803 | /// <param name="calendar">The calendar to remove the value from.</param> |
||
804 | /// <param name="field">The field to be removed from the calendar.</param> |
||
805 | public void Clear( Calendar calendar, int field ) |
||
806 | { |
||
807 | if ( this[calendar] != null ) |
||
808 | this.Remove( calendar ); |
||
809 | else |
||
810 | this.Set( calendar, field, 0 ); |
||
811 | } |
||
812 | |||
813 | /// <summary> |
||
814 | /// Internal class that represents the properties of a calendar instance. |
||
815 | /// </summary> |
||
816 | class CalendarProperties |
||
817 | { |
||
818 | /// <summary> |
||
819 | /// The date and time of a calendar. |
||
820 | /// </summary> |
||
821 | public DateTime dateTime; |
||
822 | |||
823 | /// <summary> |
||
824 | /// The format for the date and time in a calendar. |
||
825 | /// </summary> |
||
826 | public DateTimeFormatInfo dateTimeFormat; |
||
827 | } |
||
828 | } |
||
829 | } |
||
830 | |||
831 | /*******************************/ |
||
832 | /// <summary> |
||
833 | /// Provides support for DateFormat |
||
834 | /// </summary> |
||
835 | public class DateTimeFormatManager |
||
836 | { |
||
837 | static public DateTimeFormatHashTable manager = new DateTimeFormatHashTable(); |
||
838 | |||
839 | /// <summary> |
||
840 | /// Hashtable class to provide functionality for dateformat properties |
||
841 | /// </summary> |
||
842 | public class DateTimeFormatHashTable : Hashtable |
||
843 | { |
||
844 | /// <summary> |
||
845 | /// Sets the format for datetime. |
||
846 | /// </summary> |
||
847 | /// <param name="format">DateTimeFormat instance to set the pattern</param> |
||
848 | /// <param name="newPattern">A string with the pattern format</param> |
||
849 | public void SetDateFormatPattern( DateTimeFormatInfo format, string newPattern ) |
||
850 | { |
||
851 | if ( this[format] != null ) |
||
852 | ( (DateTimeFormatProperties)this[format] ).DateFormatPattern = newPattern; |
||
853 | else |
||
854 | { |
||
855 | DateTimeFormatProperties tempProps = new DateTimeFormatProperties(); |
||
856 | tempProps.DateFormatPattern = newPattern; |
||
857 | Add( format, tempProps ); |
||
858 | } |
||
859 | } |
||
860 | |||
861 | /// <summary> |
||
862 | /// Gets the current format pattern of the DateTimeFormat instance |
||
863 | /// </summary> |
||
864 | /// <param name="format">The DateTimeFormat instance which the value will be obtained</param> |
||
865 | /// <returns>The string representing the current datetimeformat pattern</returns> |
||
866 | public string GetDateFormatPattern( DateTimeFormatInfo format ) |
||
867 | { |
||
868 | if ( this[format] == null ) |
||
869 | return "d-MMM-yy"; |
||
870 | else |
||
871 | return ( (DateTimeFormatProperties)this[format] ).DateFormatPattern; |
||
872 | } |
||
873 | |||
874 | /// <summary> |
||
875 | /// Sets the datetimeformat pattern to the giving format |
||
876 | /// </summary> |
||
877 | /// <param name="format">The datetimeformat instance to set</param> |
||
878 | /// <param name="newPattern">The new datetimeformat pattern</param> |
||
879 | public void SetTimeFormatPattern( DateTimeFormatInfo format, string newPattern ) |
||
880 | { |
||
881 | if ( this[format] != null ) |
||
882 | ( (DateTimeFormatProperties)this[format] ).TimeFormatPattern = newPattern; |
||
883 | else |
||
884 | { |
||
885 | DateTimeFormatProperties tempProps = new DateTimeFormatProperties(); |
||
886 | tempProps.TimeFormatPattern = newPattern; |
||
887 | Add( format, tempProps ); |
||
888 | } |
||
889 | } |
||
890 | |||
891 | /// <summary> |
||
892 | /// Gets the current format pattern of the DateTimeFormat instance |
||
893 | /// </summary> |
||
894 | /// <param name="format">The DateTimeFormat instance which the value will be obtained</param> |
||
895 | /// <returns>The string representing the current datetimeformat pattern</returns> |
||
896 | public string GetTimeFormatPattern( DateTimeFormatInfo format ) |
||
897 | { |
||
898 | if ( this[format] == null ) |
||
899 | return "h:mm:ss tt"; |
||
900 | else |
||
901 | return ( (DateTimeFormatProperties)this[format] ).TimeFormatPattern; |
||
902 | } |
||
903 | |||
904 | /// <summary> |
||
905 | /// Internal class to provides the DateFormat and TimeFormat pattern properties on .NET |
||
906 | /// </summary> |
||
907 | class DateTimeFormatProperties |
||
908 | { |
||
909 | public string DateFormatPattern = "d-MMM-yy"; |
||
910 | public string TimeFormatPattern = "h:mm:ss tt"; |
||
911 | } |
||
912 | } |
||
913 | } |
||
914 | /*******************************/ |
||
915 | /// <summary> |
||
916 | /// Gets the DateTimeFormat instance using the culture passed as parameter and sets the pattern to the time or date depending of the value |
||
917 | /// </summary> |
||
918 | /// <param name="dateStyle">The desired date style.</param> |
||
919 | /// <param name="timeStyle">The desired time style</param> |
||
920 | /// <param name="culture">The CultureInfo instance used to obtain the DateTimeFormat</param> |
||
921 | /// <returns>The DateTimeFomatInfo of the culture and with the desired date or time style</returns> |
||
922 | public static DateTimeFormatInfo GetDateTimeFormatInstance( int dateStyle, int timeStyle, CultureInfo culture ) |
||
923 | { |
||
924 | DateTimeFormatInfo format = culture.DateTimeFormat; |
||
925 | |||
926 | switch ( timeStyle ) |
||
927 | { |
||
928 | case -1: |
||
929 | DateTimeFormatManager.manager.SetTimeFormatPattern( format, "" ); |
||
930 | break; |
||
931 | |||
932 | case 0: |
||
933 | DateTimeFormatManager.manager.SetTimeFormatPattern( format, "h:mm:ss 'o clock' tt zzz" ); |
||
934 | break; |
||
935 | |||
936 | case 1: |
||
937 | DateTimeFormatManager.manager.SetTimeFormatPattern( format, "h:mm:ss tt zzz" ); |
||
938 | break; |
||
939 | |||
940 | case 2: |
||
941 | DateTimeFormatManager.manager.SetTimeFormatPattern( format, "h:mm:ss tt" ); |
||
942 | break; |
||
943 | |||
944 | case 3: |
||
945 | DateTimeFormatManager.manager.SetTimeFormatPattern( format, "h:mm tt" ); |
||
946 | break; |
||
947 | } |
||
948 | |||
949 | switch ( dateStyle ) |
||
950 | { |
||
951 | case -1: |
||
952 | DateTimeFormatManager.manager.SetDateFormatPattern( format, "" ); |
||
953 | break; |
||
954 | |||
955 | case 0: |
||
956 | DateTimeFormatManager.manager.SetDateFormatPattern( format, "dddd, MMMM dd%, yyy" ); |
||
957 | break; |
||
958 | |||
959 | case 1: |
||
960 | DateTimeFormatManager.manager.SetDateFormatPattern( format, "MMMM dd%, yyy" ); |
||
961 | break; |
||
962 | |||
963 | case 2: |
||
964 | DateTimeFormatManager.manager.SetDateFormatPattern( format, "d-MMM-yy" ); |
||
965 | break; |
||
966 | |||
967 | case 3: |
||
968 | DateTimeFormatManager.manager.SetDateFormatPattern( format, "M/dd/yy" ); |
||
969 | break; |
||
970 | } |
||
971 | |||
972 | return format; |
||
973 | } |
||
974 | |||
975 | /*******************************/ |
||
976 | /// <summary> |
||
977 | /// Gets the DateTimeFormat instance and date instance to obtain the date with the format passed |
||
978 | /// </summary> |
||
979 | /// <param name="format">The DateTimeFormat to obtain the time and date pattern</param> |
||
980 | /// <param name="date">The date instance used to get the date</param> |
||
981 | /// <returns>A string representing the date with the time and date patterns</returns> |
||
982 | public static string FormatDateTime( DateTimeFormatInfo format, DateTime date ) |
||
983 | { |
||
984 | string timePattern = DateTimeFormatManager.manager.GetTimeFormatPattern( format ); |
||
985 | string datePattern = DateTimeFormatManager.manager.GetDateFormatPattern( format ); |
||
986 | return date.ToString( datePattern + " " + timePattern, format ); |
||
987 | } |
||
988 | |||
989 | /*******************************/ |
||
990 | /// <summary> |
||
991 | /// Adds a new key-and-value pair into the hash table |
||
992 | /// </summary> |
||
993 | /// <param name="collection">The collection to work with</param> |
||
994 | /// <param name="key">Key used to obtain the value</param> |
||
995 | /// <param name="newValue">Value asociated with the key</param> |
||
996 | /// <returns>The old element associated with the key</returns> |
||
997 | public static Object PutElement( IDictionary collection, Object key, Object newValue ) |
||
998 | { |
||
999 | Object element = collection[key]; |
||
1000 | collection[key] = newValue; |
||
1001 | return element; |
||
1002 | } |
||
1003 | |||
1004 | /*******************************/ |
||
1005 | /// <summary> |
||
1006 | /// Provides support functions to create read-write random acces files and write functions |
||
1007 | /// </summary> |
||
1008 | public class RandomAccessFileSupport |
||
1009 | { |
||
1010 | /// <summary> |
||
1011 | /// Creates a new random acces stream with read-write or read rights |
||
1012 | /// </summary> |
||
1013 | /// <param name="fileName">A relative or absolute path for the file to open</param> |
||
1014 | /// <param name="mode">Mode to open the file in</param> |
||
1015 | /// <returns>The new FileStream</returns> |
||
1016 | public static FileStream CreateRandomAccessFile( string fileName, string mode ) |
||
1017 | { |
||
1018 | FileStream newFile = null; |
||
1019 | |||
1020 | if ( mode.CompareTo( "rw" ) == 0 ) |
||
1021 | newFile = new FileStream( fileName, FileMode.OpenOrCreate, FileAccess.ReadWrite ); |
||
1022 | else if ( mode.CompareTo( "r" ) == 0 ) |
||
1023 | newFile = new FileStream( fileName, FileMode.Open, FileAccess.Read ); |
||
1024 | else |
||
1025 | throw new ArgumentException(); |
||
1026 | |||
1027 | return newFile; |
||
1028 | } |
||
1029 | |||
1030 | /// <summary> |
||
1031 | /// Creates a new random acces stream with read-write or read rights |
||
1032 | /// </summary> |
||
1033 | /// <param name="fileName">File infomation for the file to open</param> |
||
1034 | /// <param name="mode">Mode to open the file in</param> |
||
1035 | /// <returns>The new FileStream</returns> |
||
1036 | public static FileStream CreateRandomAccessFile( FileInfo fileName, string mode ) |
||
1037 | { |
||
1038 | return CreateRandomAccessFile( fileName.FullName, mode ); |
||
1039 | } |
||
1040 | |||
1041 | /// <summary> |
||
1042 | /// Writes the data to the specified file stream |
||
1043 | /// </summary> |
||
1044 | /// <param name="data">Data to write</param> |
||
1045 | /// <param name="fileStream">File to write to</param> |
||
1046 | public static void WriteBytes( string data, FileStream fileStream ) |
||
1047 | { |
||
1048 | int index = 0; |
||
1049 | int length = data.Length; |
||
1050 | |||
1051 | while ( index < length ) |
||
1052 | fileStream.WriteByte( (byte)data[index++] ); |
||
1053 | } |
||
1054 | |||
1055 | /// <summary> |
||
1056 | /// Writes the received string to the file stream |
||
1057 | /// </summary> |
||
1058 | /// <param name="data">String of information to write</param> |
||
1059 | /// <param name="fileStream">File to write to</param> |
||
1060 | public static void WriteChars( string data, FileStream fileStream ) |
||
1061 | { |
||
1062 | WriteBytes( data, fileStream ); |
||
1063 | } |
||
1064 | |||
1065 | /// <summary> |
||
1066 | /// Writes the received data to the file stream |
||
1067 | /// </summary> |
||
1068 | /// <param name="sByteArray">Data to write</param> |
||
1069 | /// <param name="fileStream">File to write to</param> |
||
1070 | public static void WriteRandomFile( sbyte[] sByteArray, FileStream fileStream ) |
||
1071 | { |
||
1072 | byte[] byteArray = ToByteArray( sByteArray ); |
||
1073 | fileStream.Write( byteArray, 0, byteArray.Length ); |
||
1074 | } |
||
1075 | } |
||
1076 | |||
1077 | /*******************************/ |
||
1078 | /// <summary> |
||
1079 | /// Checks if a file have write permissions |
||
1080 | /// </summary> |
||
1081 | /// <param name="file">The file instance to check</param> |
||
1082 | /// <returns>True if have write permissions otherwise false</returns> |
||
1083 | public static bool FileCanWrite( FileInfo file ) |
||
1084 | { |
||
1085 | return ( File.GetAttributes( file.FullName ) & FileAttributes.ReadOnly ) != FileAttributes.ReadOnly; |
||
1086 | } |
||
1087 | |||
1088 | /*******************************/ |
||
1089 | /// <summary> |
||
1090 | /// Checks if the giving File instance is a directory or file, and returns his Length |
||
1091 | /// </summary> |
||
1092 | /// <param name="file">The File instance to check</param> |
||
1093 | /// <returns>The length of the file</returns> |
||
1094 | public static long FileLength( FileInfo file ) |
||
1095 | { |
||
1096 | if ( Directory.Exists( file.FullName ) ) |
||
1097 | return 0; |
||
1098 | else |
||
1099 | return file.Length; |
||
1100 | } |
||
1101 | |||
1102 | /*******************************/ |
||
1103 | /// <summary>Reads a number of characters from the current source Stream and writes the data to the target array at the specified index.</summary> |
||
1104 | /// <param name="sourceStream">The source Stream to read from.</param> |
||
1105 | /// <param name="target">Contains the array of characteres read from the source Stream.</param> |
||
1106 | /// <param name="start">The starting index of the target array.</param> |
||
1107 | /// <param name="count">The maximum number of characters to read from the source Stream.</param> |
||
1108 | /// <returns>The number of characters read. The number will be less than or equal to count depending on the data available in the source Stream. Returns -1 if the end of the stream is reached.</returns> |
||
1109 | public static Int32 ReadInput( Stream sourceStream, ref byte[] target, int start, int count ) |
||
1110 | { |
||
1111 | // Returns 0 bytes if not enough space in target |
||
1112 | if ( target.Length == 0 ) |
||
1113 | return 0; |
||
1114 | |||
1115 | byte[] receiver = new byte[target.Length]; |
||
1116 | int bytesRead = sourceStream.Read( receiver, start, count ); |
||
1117 | |||
1118 | // Returns -1 if EOF |
||
1119 | if ( bytesRead == 0 ) |
||
1120 | return -1; |
||
1121 | |||
1122 | for ( int i = start; i < start + bytesRead; i++ ) |
||
1123 | target[i] = (byte)receiver[i]; |
||
1124 | |||
1125 | return bytesRead; |
||
1126 | } |
||
1127 | |||
1128 | /// <summary>Reads a number of characters from the current source TextReader and writes the data to the target array at the specified index.</summary> |
||
1129 | /// <param name="sourceTextReader">The source TextReader to read from</param> |
||
1130 | /// <param name="target">Contains the array of characteres read from the source TextReader.</param> |
||
1131 | /// <param name="start">The starting index of the target array.</param> |
||
1132 | /// <param name="count">The maximum number of characters to read from the source TextReader.</param> |
||
1133 | /// <returns>The number of characters read. The number will be less than or equal to count depending on the data available in the source TextReader. Returns -1 if the end of the stream is reached.</returns> |
||
1134 | public static Int32 ReadInput( TextReader sourceTextReader, ref sbyte[] target, int start, int count ) |
||
1135 | { |
||
1136 | // Returns 0 bytes if not enough space in target |
||
1137 | if ( target.Length == 0 ) |
||
1138 | return 0; |
||
1139 | |||
1140 | char[] charArray = new char[target.Length]; |
||
1141 | int bytesRead = sourceTextReader.Read( charArray, start, count ); |
||
1142 | |||
1143 | // Returns -1 if EOF |
||
1144 | if ( bytesRead == 0 ) |
||
1145 | return -1; |
||
1146 | |||
1147 | for ( int index = start; index < start + bytesRead; index++ ) |
||
1148 | target[index] = (sbyte)charArray[index]; |
||
1149 | |||
1150 | return bytesRead; |
||
1151 | } |
||
1152 | |||
1153 | /*******************************/ |
||
1154 | /// <summary> |
||
1155 | /// Performs an unsigned bitwise right shift with the specified number |
||
1156 | /// </summary> |
||
1157 | /// <param name="number">Number to operate on</param> |
||
1158 | /// <param name="bits">Ammount of bits to shift</param> |
||
1159 | /// <returns>The resulting number from the shift operation</returns> |
||
1160 | public static int URShift( int number, int bits ) |
||
1161 | { |
||
1162 | if ( number >= 0 ) |
||
1163 | return number >> bits; |
||
1164 | else |
||
1165 | return ( number >> bits ) + ( 2 << ~bits ); |
||
1166 | } |
||
1167 | |||
1168 | /// <summary> |
||
1169 | /// Performs an unsigned bitwise right shift with the specified number |
||
1170 | /// </summary> |
||
1171 | /// <param name="number">Number to operate on</param> |
||
1172 | /// <param name="bits">Ammount of bits to shift</param> |
||
1173 | /// <returns>The resulting number from the shift operation</returns> |
||
1174 | public static int URShift( int number, long bits ) |
||
1175 | { |
||
1176 | return URShift( number, (int)bits ); |
||
1177 | } |
||
1178 | |||
1179 | /// <summary> |
||
1180 | /// Performs an unsigned bitwise right shift with the specified number |
||
1181 | /// </summary> |
||
1182 | /// <param name="number">Number to operate on</param> |
||
1183 | /// <param name="bits">Ammount of bits to shift</param> |
||
1184 | /// <returns>The resulting number from the shift operation</returns> |
||
1185 | public static long URShift( long number, int bits ) |
||
1186 | { |
||
1187 | if ( number >= 0 ) |
||
1188 | return number >> bits; |
||
1189 | else |
||
1190 | return ( number >> bits ) + ( 2L << ~bits ); |
||
1191 | } |
||
1192 | |||
1193 | /// <summary> |
||
1194 | /// Performs an unsigned bitwise right shift with the specified number |
||
1195 | /// </summary> |
||
1196 | /// <param name="number">Number to operate on</param> |
||
1197 | /// <param name="bits">Ammount of bits to shift</param> |
||
1198 | /// <returns>The resulting number from the shift operation</returns> |
||
1199 | public static long URShift( long number, long bits ) |
||
1200 | { |
||
1201 | return URShift( number, (int)bits ); |
||
1202 | } |
||
1203 | |||
1204 | /*******************************/ |
||
1205 | /// <summary> |
||
1206 | /// Writes the exception stack trace to the received stream |
||
1207 | /// </summary> |
||
1208 | /// <param name="throwable">Exception to obtain information from</param> |
||
1209 | /// <param name="stream">Output sream used to write to</param> |
||
1210 | public static void WriteStackTrace( Exception throwable, TextWriter stream ) |
||
1211 | { |
||
1212 | stream.Write( throwable.StackTrace ); |
||
1213 | stream.Flush(); |
||
1214 | } |
||
1215 | |||
1216 | /*******************************/ |
||
1217 | /// <summary> |
||
1218 | /// Removes the element with the specified key from a Hashtable instance. |
||
1219 | /// </summary> |
||
1220 | /// <param name="hashtable">The Hashtable instance</param> |
||
1221 | /// <param name="key">The key of the element to remove</param> |
||
1222 | /// <returns>The element removed</returns> |
||
1223 | public static Object HashtableRemove( Hashtable hashtable, Object key ) |
||
1224 | { |
||
1225 | Object element = hashtable[key]; |
||
1226 | hashtable.Remove( key ); |
||
1227 | return element; |
||
1228 | } |
||
1229 | |||
1230 | /*******************************/ |
||
1231 | /// <summary> |
||
1232 | /// Converts an array of sbytes to an array of chars |
||
1233 | /// </summary> |
||
1234 | /// <param name="sByteArray">The array of sbytes to convert</param> |
||
1235 | /// <returns>The new array of chars</returns> |
||
1236 | public static char[] ToCharArray( sbyte[] sByteArray ) |
||
1237 | { |
||
1238 | char[] charArray = new char[sByteArray.Length]; |
||
1239 | sByteArray.CopyTo( charArray, 0 ); |
||
1240 | return charArray; |
||
1241 | } |
||
1242 | |||
1243 | /// <summary> |
||
1244 | /// Converts an array of bytes to an array of chars |
||
1245 | /// </summary> |
||
1246 | /// <param name="byteArray">The array of bytes to convert</param> |
||
1247 | /// <returns>The new array of chars</returns> |
||
1248 | public static char[] ToCharArray( byte[] byteArray ) |
||
1249 | { |
||
1250 | char[] charArray = new char[byteArray.Length]; |
||
1251 | byteArray.CopyTo( charArray, 0 ); |
||
1252 | return charArray; |
||
1253 | } |
||
1254 | |||
1255 | /*******************************/ |
||
1256 | /// <summary> |
||
1257 | /// Receives a byte array and returns it transformed in an sbyte array |
||
1258 | /// </summary> |
||
1259 | /// <param name="byteArray">Byte array to process</param> |
||
1260 | /// <returns>The transformed array</returns> |
||
1261 | public static sbyte[] ToSByteArray( byte[] byteArray ) |
||
1262 | { |
||
1263 | sbyte[] sbyteArray = new sbyte[byteArray.Length]; |
||
1264 | for ( int index = 0; index < byteArray.Length; index++ ) |
||
1265 | sbyteArray[index] = (sbyte)byteArray[index]; |
||
1266 | return sbyteArray; |
||
1267 | } |
||
1268 | /*******************************/ |
||
1269 | /// <summary> |
||
1270 | /// Returns the last element of an ArrayList instance. |
||
1271 | /// </summary> |
||
1272 | /// <param name="arrayList">The ArrayList instance</param> |
||
1273 | /// <returns>The last element of the ArrayList</returns> |
||
1274 | public static Object VectorLastElement( ArrayList arrayList ) |
||
1275 | { |
||
1276 | return arrayList[arrayList.Count - 1]; |
||
1277 | } |
||
1278 | |||
1279 | /// <summary> |
||
1280 | /// Returns the last element of a Stack instance. |
||
1281 | /// </summary> |
||
1282 | /// <param name="stack">The Stack instance</param> |
||
1283 | /// <returns>The last element of the Stack</returns> |
||
1284 | public static Object VectorLastElement( Stack stack ) |
||
1285 | { |
||
1286 | return stack.ToArray()[0]; |
||
1287 | } |
||
1288 | |||
1289 | |||
1290 | /*******************************/ |
||
1291 | /// <summary> |
||
1292 | /// Adds an element to the top end of a Stack instance. |
||
1293 | /// </summary> |
||
1294 | /// <param name="stack">The Stack instance</param> |
||
1295 | /// <param name="element">The element to add</param> |
||
1296 | /// <returns>The element added</returns> |
||
1297 | public static Object StackPush( Stack stack, Object element ) |
||
1298 | { |
||
1299 | stack.Push( element ); |
||
1300 | return element; |
||
1301 | } |
||
1302 | |||
1303 | /*******************************/ |
||
1304 | /// <summary> |
||
1305 | /// Creates an instance of a received Type. |
||
1306 | /// </summary> |
||
1307 | /// <param name="classType">The Type of the new class instance to return.</param> |
||
1308 | /// <returns>An Object containing the new instance.</returns> |
||
1309 | public static Object CreateNewInstance( Type classType ) |
||
1310 | { |
||
1311 | Object instance = null; |
||
1312 | Type[] constructor = new Type[] { }; |
||
1313 | ConstructorInfo[] constructors = null; |
||
1314 | |||
1315 | constructors = classType.GetConstructors(); |
||
1316 | |||
1317 | if ( constructors.Length == 0 ) |
||
1318 | throw new UnauthorizedAccessException(); |
||
1319 | else |
||
1320 | { |
||
1321 | for ( int i = 0; i < constructors.Length; i++ ) |
||
1322 | { |
||
1323 | ParameterInfo[] parameters = constructors[i].GetParameters(); |
||
1324 | |||
1325 | if ( parameters.Length == 0 ) |
||
1326 | { |
||
1327 | instance = classType.GetConstructor( constructor ).Invoke( new Object[] { } ); |
||
1328 | break; |
||
1329 | } |
||
1330 | else if ( i == constructors.Length - 1 ) |
||
1331 | throw new MethodAccessException(); |
||
1332 | } |
||
1333 | } |
||
1334 | return instance; |
||
1335 | } |
||
1336 | |||
1337 | |||
1338 | /*******************************/ |
||
1339 | /// <summary> |
||
1340 | /// Obtains the int value depending of the type of modifiers that the constructor have |
||
1341 | /// </summary> |
||
1342 | /// <param name="constructor">The ConstructorInfo used to obtain the int value</param> |
||
1343 | /// <returns>The int value of the modifier present in the constructor. 1 if it's public, 2 if it's private, otherwise 4</returns> |
||
1344 | public static int GetConstructorModifiers( ConstructorInfo constructor ) |
||
1345 | { |
||
1346 | int temp; |
||
1347 | if ( constructor.IsPublic ) |
||
1348 | temp = 1; |
||
1349 | else if ( constructor.IsPrivate ) |
||
1350 | temp = 2; |
||
1351 | else |
||
1352 | temp = 4; |
||
1353 | return temp; |
||
1354 | } |
||
1355 | |||
1356 | /*******************************/ |
||
1357 | /// <summary> |
||
1358 | /// Write an array of bytes int the FileStream specified. |
||
1359 | /// </summary> |
||
1360 | /// <param name="FileStreamWrite">FileStream that must be updated.</param> |
||
1361 | /// <param name="Source">Array of bytes that must be written in the FileStream.</param> |
||
1362 | public static void WriteOutput( FileStream FileStreamWrite, sbyte[] Source ) |
||
1363 | { |
||
1364 | FileStreamWrite.Write( ToByteArray( Source ), 0, Source.Length ); |
||
1365 | } |
||
1366 | |||
1367 | |||
1368 | } |