//
// In order to convert some functionality to Visual C#, the Java Language Conversion Assistant
// creates "support classes" that duplicate the original functionality.
//
// Support classes replicate the functionality of the original code, but in some cases they are
// substantially different architecturally. Although every effort is made to preserve the
// original architecture of the application in the converted project, the user should be aware that
// the primary goal of these support classes is to replicate functionality, and that at times
// the architecture of the resulting solution may differ somewhat.
//
// Included in SQLite3 port to C# for use in testharness only; 2008 Noah B Hart
//$Header$
using System;
using System.Collections;
using System.Globalization;
using System.IO;
using System.Reflection;
using System.Threading;
///
/// This interface should be implemented by any class whose instances are intended
/// to be executed by a thread.
///
public interface IThreadRunnable
{
///
/// This method has to be implemented in order that starting of the thread causes the object's
/// run method to be called in that separately executing thread.
///
void Run();
}
///
/// Contains conversion support elements such as classes, interfaces and static methods.
///
public class SupportClass
{
///
/// Support class used to handle threads
///
public class ThreadClass : IThreadRunnable
{
///
/// The instance of Thread
///
private Thread threadField;
///
/// Initializes a new instance of the ThreadClass class
///
public ThreadClass()
{
threadField = new Thread( new ThreadStart( Run ) );
}
///
/// Initializes a new instance of the Thread class.
///
/// The name of the thread
public ThreadClass( string Name )
{
threadField = new Thread( new ThreadStart( Run ) );
this.Name = Name;
}
///
/// Initializes a new instance of the Thread class.
///
/// A ThreadStart delegate that references the methods to be invoked when this thread begins executing
public ThreadClass( ThreadStart Start )
{
threadField = new Thread( Start );
}
///
/// Initializes a new instance of the Thread class.
///
/// A ThreadStart delegate that references the methods to be invoked when this thread begins executing
/// The name of the thread
public ThreadClass( ThreadStart Start, string Name )
{
threadField = new Thread( Start );
this.Name = Name;
}
///
/// This method has no functionality unless the method is overridden
///
public virtual void Run()
{
}
///
/// Causes the operating system to change the state of the current thread instance to ThreadState.Running
///
public void Start()
{
threadField.Start();
}
///
/// Interrupts a thread that is in the WaitSleepJoin thread state
///
public void Interrupt()
{
threadField.Interrupt();
}
///
/// Gets the current thread instance
///
public Thread Instance
{
get
{
return threadField;
}
set
{
threadField = value;
}
}
///
/// Gets or sets the name of the thread
///
public string Name
{
get
{
return threadField.Name;
}
set
{
if ( threadField.Name == null )
threadField.Name = value;
}
}
///
/// Gets or sets a value indicating the scheduling priority of a thread
///
public ThreadPriority Priority
{
get
{
return threadField.Priority;
}
set
{
threadField.Priority = value;
}
}
///
/// Gets a value indicating the execution status of the current thread
///
public bool IsAlive
{
get
{
return threadField.IsAlive;
}
}
///
/// Gets or sets a value indicating whether or not a thread is a background thread.
///
public bool IsBackground
{
get
{
return threadField.IsBackground;
}
set
{
threadField.IsBackground = value;
}
}
///
/// Blocks the calling thread until a thread terminates
///
public void Join()
{
threadField.Join();
}
///
/// Blocks the calling thread until a thread terminates or the specified time elapses
///
/// Time of wait in milliseconds
public void Join( long MiliSeconds )
{
lock ( this )
{
threadField.Join( new TimeSpan( MiliSeconds * 10000 ) );
}
}
///
/// Blocks the calling thread until a thread terminates or the specified time elapses
///
/// Time of wait in milliseconds
/// Time of wait in nanoseconds
public void Join( long MiliSeconds, int NanoSeconds )
{
lock ( this )
{
threadField.Join( new TimeSpan( MiliSeconds * 10000 + NanoSeconds * 100 ) );
}
}
///
/// Resumes a thread that has been suspended
///
public void Resume()
{
threadField.Resume();
}
///
/// Raises a ThreadAbortException in the thread on which it is invoked,
/// to begin the process of terminating the thread. Calling this method
/// usually terminates the thread
///
public void Abort()
{
threadField.Abort();
}
///
/// Raises a ThreadAbortException in the thread on which it is invoked,
/// to begin the process of terminating the thread while also providing
/// exception information about the thread termination.
/// Calling this method usually terminates the thread.
///
/// An object that contains application-specific information, such as state, which can be used by the thread being aborted
public void Abort( Object stateInfo )
{
lock ( this )
{
threadField.Abort( stateInfo );
}
}
///
/// Suspends the thread, if the thread is already suspended it has no effect
///
public void Suspend()
{
threadField.Suspend();
}
///
/// Obtain a String that represents the current Object
///
/// A String that represents the current Object
public override string ToString()
{
return "Thread[" + Name + "," + Priority.ToString() + "," + "" + "]";
}
///
/// Gets the currently running thread
///
/// The currently running thread
public static ThreadClass Current()
{
ThreadClass CurrentThread = new ThreadClass();
CurrentThread.Instance = Thread.CurrentThread;
return CurrentThread;
}
}
/*******************************/
///
/// Removes the first occurrence of an specific object from an ArrayList instance.
///
/// The ArrayList instance
/// The element to remove
/// True if item is found in the ArrayList; otherwise, false
public static Boolean VectorRemoveElement( ArrayList arrayList, Object element )
{
Boolean containsItem = arrayList.Contains( element );
arrayList.Remove( element );
return containsItem;
}
/*******************************/
///
/// Converts an array of sbytes to an array of bytes
///
/// The array of sbytes to be converted
/// The new array of bytes
public static byte[] ToByteArray( sbyte[] sbyteArray )
{
byte[] byteArray = new byte[sbyteArray.Length];
for ( int index = 0; index < sbyteArray.Length; index++ )
byteArray[index] = (byte)sbyteArray[index];
return byteArray;
}
///
/// Converts a string to an array of bytes
///
/// The string to be converted
/// The new array of bytes
public static byte[] ToByteArray( string sourceString )
{
byte[] byteArray = new byte[sourceString.Length];
for ( int index = 0; index < sourceString.Length; index++ )
byteArray[index] = (byte)sourceString[index];
return byteArray;
}
///
/// Converts a array of object-type instances to a byte-type array.
///
/// Array to convert.
/// An array of byte type elements.
public static byte[] ToByteArray( object[] tempObjectArray )
{
byte[] byteArray = new byte[tempObjectArray.Length];
for ( int index = 0; index < tempObjectArray.Length; index++ )
byteArray[index] = (byte)tempObjectArray[index];
return byteArray;
}
/*******************************/
///
/// This method returns the literal value received
///
/// The literal to return
/// The received value
public static long Identity( long literal )
{
return literal;
}
///
/// This method returns the literal value received
///
/// The literal to return
/// The received value
public static ulong Identity( ulong literal )
{
return literal;
}
///
/// This method returns the literal value received
///
/// The literal to return
/// The received value
public static float Identity( float literal )
{
return literal;
}
///
/// This method returns the literal value received
///
/// The literal to return
/// The received value
public static double Identity( double literal )
{
return literal;
}
/*******************************/
///
/// Copies an array of chars obtained from a String into a specified array of chars
///
/// The String to get the chars from
/// Position of the String to start getting the chars
/// Position of the String to end getting the chars
/// Array to return the chars
/// Position of the destination array of chars to start storing the chars
/// An array of chars
public static void GetCharsFromString( string sourceString, int sourceStart, int sourceEnd, ref char[] destinationArray, int destinationStart )
{
int sourceCounter;
int destinationCounter;
sourceCounter = sourceStart;
destinationCounter = destinationStart;
while ( sourceCounter < sourceEnd )
{
destinationArray[destinationCounter] = (char)sourceString[sourceCounter];
sourceCounter++;
destinationCounter++;
}
}
/*******************************/
///
/// This class manages different issues for calendars.
/// The different calendars are internally managed using a hash table structure.
///
public class CalendarManager
{
///
/// Field number for get and set indicating the year.
///
public const int YEAR = 0;
///
/// Field number for get and set indicating the month.
///
public const int MONTH = 1;
///
/// Field number for get and set indicating the day of the month.
///
public const int DATE = 2;
///
/// Field number for get and set indicating the hour of the morning or afternoon.
///
public const int HOUR = 3;
///
/// Field number for get and set indicating the minute within the hour.
///
public const int MINUTE = 4;
///
/// Field number for get and set indicating the second within the minute.
///
public const int SECOND = 5;
///
/// Field number for get and set indicating the millisecond within the second.
///
public const int MILLISECOND = 6;
///
/// Field number for get and set indicating the day of the month.
///
public const int DAY_OF_MONTH = 7;
///
/// Field used to get or set the day of the week.
///
public const int DAY_OF_WEEK = 8;
///
/// Field number for get and set indicating the hour of the day.
///
public const int HOUR_OF_DAY = 9;
///
/// Field number for get and set indicating whether the HOUR is before or after noon.
///
public const int AM_PM = 10;
///
/// Value of the AM_PM field indicating the period of the day from midnight to just
/// before noon.
///
public const int AM = 11;
///
/// Value of the AM_PM field indicating the period of the day from noon to just before midnight.
///
public const int PM = 12;
///
/// The hash table that contains the type of calendars and its properties.
///
static public CalendarHashTable manager = new CalendarHashTable();
///
/// Internal class that inherits from HashTable to manage the different calendars.
/// This structure will contain an instance of Calendar that represents
/// a type of calendar and its properties (represented by an instance of CalendarProperties
/// class).
///
public class CalendarHashTable : Hashtable
{
///
/// Gets the calendar current date and time.
///
/// The calendar to get its current date and time.
/// A DateTime value that indicates the current date and time for the
/// calendar given.
public DateTime GetDateTime( Calendar calendar )
{
if ( this[calendar] != null )
return ( (CalendarProperties)this[calendar] ).dateTime;
else
{
CalendarProperties tempProps = new CalendarProperties();
tempProps.dateTime = DateTime.Now;
this.Add( calendar, tempProps );
return this.GetDateTime( calendar );
}
}
///
/// Sets the specified DateTime value to the specified calendar.
///
/// The calendar to set its date.
/// The DateTime value to set to the calendar.
public void SetDateTime( Calendar calendar, DateTime date )
{
if ( this[calendar] != null )
{
( (CalendarProperties)this[calendar] ).dateTime = date;
}
else
{
CalendarProperties tempProps = new CalendarProperties();
tempProps.dateTime = date;
this.Add( calendar, tempProps );
}
}
///
/// Sets the corresponding field in an specified calendar with the value given.
/// If the specified calendar does not have exist in the hash table, it creates a
/// new instance of the calendar with the current date and time and then assings it
/// the new specified value.
///
/// The calendar to set its date or time.
/// One of the fields that composes a date/time.
/// The value to be set.
public void Set( Calendar calendar, int field, int fieldValue )
{
if ( this[calendar] != null )
{
DateTime tempDate = ( (CalendarProperties)this[calendar] ).dateTime;
switch ( field )
{
case CalendarManager.DATE:
tempDate = tempDate.AddDays( fieldValue - tempDate.Day );
break;
case CalendarManager.HOUR:
tempDate = tempDate.AddHours( fieldValue - tempDate.Hour );
break;
case CalendarManager.MILLISECOND:
tempDate = tempDate.AddMilliseconds( fieldValue - tempDate.Millisecond );
break;
case CalendarManager.MINUTE:
tempDate = tempDate.AddMinutes( fieldValue - tempDate.Minute );
break;
case CalendarManager.MONTH:
//Month value is 0-based. e.g., 0 for January
tempDate = tempDate.AddMonths( fieldValue - ( tempDate.Month + 1 ) );
break;
case CalendarManager.SECOND:
tempDate = tempDate.AddSeconds( fieldValue - tempDate.Second );
break;
case CalendarManager.YEAR:
tempDate = tempDate.AddYears( fieldValue - tempDate.Year );
break;
case CalendarManager.DAY_OF_MONTH:
tempDate = tempDate.AddDays( fieldValue - tempDate.Day );
break;
case CalendarManager.DAY_OF_WEEK:
;
tempDate = tempDate.AddDays( ( fieldValue - 1 ) - (int)tempDate.DayOfWeek );
break;
case CalendarManager.HOUR_OF_DAY:
tempDate = tempDate.AddHours( fieldValue - tempDate.Hour );
break;
default:
break;
}
( (CalendarProperties)this[calendar] ).dateTime = tempDate;
}
else
{
CalendarProperties tempProps = new CalendarProperties();
tempProps.dateTime = DateTime.Now;
this.Add( calendar, tempProps );
this.Set( calendar, field, fieldValue );
}
}
///
/// Sets the corresponding date (day, month and year) to the calendar specified.
/// If the calendar does not exist in the hash table, it creates a new instance and sets
/// its values.
///
/// The calendar to set its date.
/// Integer value that represent the year.
/// Integer value that represent the month.
/// Integer value that represent the day.
public void Set( Calendar calendar, int year, int month, int day )
{
if ( this[calendar] != null )
{
this.Set( calendar, CalendarManager.YEAR, year );
this.Set( calendar, CalendarManager.MONTH, month );
this.Set( calendar, CalendarManager.DATE, day );
}
else
{
CalendarProperties tempProps = new CalendarProperties();
//Month value is 0-based. e.g., 0 for January
tempProps.dateTime = new DateTime( year, month + 1, day );
this.Add( calendar, tempProps );
}
}
///
/// Sets the corresponding date (day, month and year) and hour (hour and minute)
/// to the calendar specified.
/// If the calendar does not exist in the hash table, it creates a new instance and sets
/// its values.
///
/// The calendar to set its date and time.
/// Integer value that represent the year.
/// Integer value that represent the month.
/// Integer value that represent the day.
/// Integer value that represent the hour.
/// Integer value that represent the minutes.
public void Set( Calendar calendar, int year, int month, int day, int hour, int minute )
{
if ( this[calendar] != null )
{
this.Set( calendar, CalendarManager.YEAR, year );
this.Set( calendar, CalendarManager.MONTH, month );
this.Set( calendar, CalendarManager.DATE, day );
this.Set( calendar, CalendarManager.HOUR, hour );
this.Set( calendar, CalendarManager.MINUTE, minute );
}
else
{
CalendarProperties tempProps = new CalendarProperties();
//Month value is 0-based. e.g., 0 for January
tempProps.dateTime = new DateTime( year, month + 1, day, hour, minute, 0 );
this.Add( calendar, tempProps );
}
}
///
/// Sets the corresponding date (day, month and year) and hour (hour, minute and second)
/// to the calendar specified.
/// If the calendar does not exist in the hash table, it creates a new instance and sets
/// its values.
///
/// The calendar to set its date and time.
/// Integer value that represent the year.
/// Integer value that represent the month.
/// Integer value that represent the day.
/// Integer value that represent the hour.
/// Integer value that represent the minutes.
/// Integer value that represent the seconds.
public void Set( Calendar calendar, int year, int month, int day, int hour, int minute, int second )
{
if ( this[calendar] != null )
{
this.Set( calendar, CalendarManager.YEAR, year );
this.Set( calendar, CalendarManager.MONTH, month );
this.Set( calendar, CalendarManager.DATE, day );
this.Set( calendar, CalendarManager.HOUR, hour );
this.Set( calendar, CalendarManager.MINUTE, minute );
this.Set( calendar, CalendarManager.SECOND, second );
}
else
{
CalendarProperties tempProps = new CalendarProperties();
//Month value is 0-based. e.g., 0 for January
tempProps.dateTime = new DateTime( year, month + 1, day, hour, minute, second );
this.Add( calendar, tempProps );
}
}
///
/// Gets the value represented by the field specified.
///
/// The calendar to get its date or time.
/// One of the field that composes a date/time.
/// The integer value for the field given.
public int Get( Calendar calendar, int field )
{
if ( this[calendar] != null )
{
int tempHour;
switch ( field )
{
case CalendarManager.DATE:
return ( (CalendarProperties)this[calendar] ).dateTime.Day;
case CalendarManager.HOUR:
tempHour = ( (CalendarProperties)this[calendar] ).dateTime.Hour;
return tempHour > 12 ? tempHour - 12 : tempHour;
case CalendarManager.MILLISECOND:
return ( (CalendarProperties)this[calendar] ).dateTime.Millisecond;
case CalendarManager.MINUTE:
return ( (CalendarProperties)this[calendar] ).dateTime.Minute;
case CalendarManager.MONTH:
//Month value is 0-based. e.g., 0 for January
return ( (CalendarProperties)this[calendar] ).dateTime.Month - 1;
case CalendarManager.SECOND:
return ( (CalendarProperties)this[calendar] ).dateTime.Second;
case CalendarManager.YEAR:
return ( (CalendarProperties)this[calendar] ).dateTime.Year;
case CalendarManager.DAY_OF_MONTH:
return ( (CalendarProperties)this[calendar] ).dateTime.Day;
case CalendarManager.DAY_OF_WEEK:
return (int)( ( (CalendarProperties)this[calendar] ).dateTime.DayOfWeek );
case CalendarManager.HOUR_OF_DAY:
return ( (CalendarProperties)this[calendar] ).dateTime.Hour;
case CalendarManager.AM_PM:
tempHour = ( (CalendarProperties)this[calendar] ).dateTime.Hour;
return tempHour > 12 ? CalendarManager.PM : CalendarManager.AM;
default:
return 0;
}
}
else
{
CalendarProperties tempProps = new CalendarProperties();
tempProps.dateTime = DateTime.Now;
this.Add( calendar, tempProps );
return this.Get( calendar, field );
}
}
///
/// Sets the time in the specified calendar with the long value.
///
/// The calendar to set its date and time.
/// A long value that indicates the milliseconds to be set to
/// the hour for the calendar.
public void SetTimeInMilliseconds( Calendar calendar, long milliseconds )
{
if ( this[calendar] != null )
{
( (CalendarProperties)this[calendar] ).dateTime = new DateTime( milliseconds );
}
else
{
CalendarProperties tempProps = new CalendarProperties();
tempProps.dateTime = new DateTime( TimeSpan.TicksPerMillisecond * milliseconds );
this.Add( calendar, tempProps );
}
}
///
/// Gets what the first day of the week is; e.g., Sunday in US, Monday in France.
///
/// The calendar to get its first day of the week.
/// A DayOfWeek value indicating the first day of the week.
public DayOfWeek GetFirstDayOfWeek( Calendar calendar )
{
if ( this[calendar] != null && ( (CalendarProperties)this[calendar] ).dateTimeFormat != null )
{
return ( (CalendarProperties)this[calendar] ).dateTimeFormat.FirstDayOfWeek;
}
else
{
CalendarProperties tempProps = new CalendarProperties();
tempProps.dateTimeFormat = new DateTimeFormatInfo();
tempProps.dateTimeFormat.FirstDayOfWeek = DayOfWeek.Sunday;
this.Add( calendar, tempProps );
return this.GetFirstDayOfWeek( calendar );
}
}
///
/// Sets what the first day of the week is; e.g., Sunday in US, Monday in France.
///
/// The calendar to set its first day of the week.
/// A DayOfWeek value indicating the first day of the week
/// to be set.
public void SetFirstDayOfWeek( Calendar calendar, DayOfWeek firstDayOfWeek )
{
if ( this[calendar] != null && ( (CalendarProperties)this[calendar] ).dateTimeFormat != null )
{
( (CalendarProperties)this[calendar] ).dateTimeFormat.FirstDayOfWeek = firstDayOfWeek;
}
else
{
CalendarProperties tempProps = new CalendarProperties();
tempProps.dateTimeFormat = new DateTimeFormatInfo();
this.Add( calendar, tempProps );
this.SetFirstDayOfWeek( calendar, firstDayOfWeek );
}
}
///
/// Removes the specified calendar from the hash table.
///
/// The calendar to be removed.
public void Clear( Calendar calendar )
{
if ( this[calendar] != null )
this.Remove( calendar );
}
///
/// Removes the specified field from the calendar given.
/// If the field does not exists in the calendar, the calendar is removed from the table.
///
/// The calendar to remove the value from.
/// The field to be removed from the calendar.
public void Clear( Calendar calendar, int field )
{
if ( this[calendar] != null )
this.Remove( calendar );
else
this.Set( calendar, field, 0 );
}
///
/// Internal class that represents the properties of a calendar instance.
///
class CalendarProperties
{
///
/// The date and time of a calendar.
///
public DateTime dateTime;
///
/// The format for the date and time in a calendar.
///
public DateTimeFormatInfo dateTimeFormat;
}
}
}
/*******************************/
///
/// Provides support for DateFormat
///
public class DateTimeFormatManager
{
static public DateTimeFormatHashTable manager = new DateTimeFormatHashTable();
///
/// Hashtable class to provide functionality for dateformat properties
///
public class DateTimeFormatHashTable : Hashtable
{
///
/// Sets the format for datetime.
///
/// DateTimeFormat instance to set the pattern
/// A string with the pattern format
public void SetDateFormatPattern( DateTimeFormatInfo format, string newPattern )
{
if ( this[format] != null )
( (DateTimeFormatProperties)this[format] ).DateFormatPattern = newPattern;
else
{
DateTimeFormatProperties tempProps = new DateTimeFormatProperties();
tempProps.DateFormatPattern = newPattern;
Add( format, tempProps );
}
}
///
/// Gets the current format pattern of the DateTimeFormat instance
///
/// The DateTimeFormat instance which the value will be obtained
/// The string representing the current datetimeformat pattern
public string GetDateFormatPattern( DateTimeFormatInfo format )
{
if ( this[format] == null )
return "d-MMM-yy";
else
return ( (DateTimeFormatProperties)this[format] ).DateFormatPattern;
}
///
/// Sets the datetimeformat pattern to the giving format
///
/// The datetimeformat instance to set
/// The new datetimeformat pattern
public void SetTimeFormatPattern( DateTimeFormatInfo format, string newPattern )
{
if ( this[format] != null )
( (DateTimeFormatProperties)this[format] ).TimeFormatPattern = newPattern;
else
{
DateTimeFormatProperties tempProps = new DateTimeFormatProperties();
tempProps.TimeFormatPattern = newPattern;
Add( format, tempProps );
}
}
///
/// Gets the current format pattern of the DateTimeFormat instance
///
/// The DateTimeFormat instance which the value will be obtained
/// The string representing the current datetimeformat pattern
public string GetTimeFormatPattern( DateTimeFormatInfo format )
{
if ( this[format] == null )
return "h:mm:ss tt";
else
return ( (DateTimeFormatProperties)this[format] ).TimeFormatPattern;
}
///
/// Internal class to provides the DateFormat and TimeFormat pattern properties on .NET
///
class DateTimeFormatProperties
{
public string DateFormatPattern = "d-MMM-yy";
public string TimeFormatPattern = "h:mm:ss tt";
}
}
}
/*******************************/
///
/// Gets the DateTimeFormat instance using the culture passed as parameter and sets the pattern to the time or date depending of the value
///
/// The desired date style.
/// The desired time style
/// The CultureInfo instance used to obtain the DateTimeFormat
/// The DateTimeFomatInfo of the culture and with the desired date or time style
public static DateTimeFormatInfo GetDateTimeFormatInstance( int dateStyle, int timeStyle, CultureInfo culture )
{
DateTimeFormatInfo format = culture.DateTimeFormat;
switch ( timeStyle )
{
case -1:
DateTimeFormatManager.manager.SetTimeFormatPattern( format, "" );
break;
case 0:
DateTimeFormatManager.manager.SetTimeFormatPattern( format, "h:mm:ss 'o clock' tt zzz" );
break;
case 1:
DateTimeFormatManager.manager.SetTimeFormatPattern( format, "h:mm:ss tt zzz" );
break;
case 2:
DateTimeFormatManager.manager.SetTimeFormatPattern( format, "h:mm:ss tt" );
break;
case 3:
DateTimeFormatManager.manager.SetTimeFormatPattern( format, "h:mm tt" );
break;
}
switch ( dateStyle )
{
case -1:
DateTimeFormatManager.manager.SetDateFormatPattern( format, "" );
break;
case 0:
DateTimeFormatManager.manager.SetDateFormatPattern( format, "dddd, MMMM dd%, yyy" );
break;
case 1:
DateTimeFormatManager.manager.SetDateFormatPattern( format, "MMMM dd%, yyy" );
break;
case 2:
DateTimeFormatManager.manager.SetDateFormatPattern( format, "d-MMM-yy" );
break;
case 3:
DateTimeFormatManager.manager.SetDateFormatPattern( format, "M/dd/yy" );
break;
}
return format;
}
/*******************************/
///
/// Gets the DateTimeFormat instance and date instance to obtain the date with the format passed
///
/// The DateTimeFormat to obtain the time and date pattern
/// The date instance used to get the date
/// A string representing the date with the time and date patterns
public static string FormatDateTime( DateTimeFormatInfo format, DateTime date )
{
string timePattern = DateTimeFormatManager.manager.GetTimeFormatPattern( format );
string datePattern = DateTimeFormatManager.manager.GetDateFormatPattern( format );
return date.ToString( datePattern + " " + timePattern, format );
}
/*******************************/
///
/// Adds a new key-and-value pair into the hash table
///
/// The collection to work with
/// Key used to obtain the value
/// Value asociated with the key
/// The old element associated with the key
public static Object PutElement( IDictionary collection, Object key, Object newValue )
{
Object element = collection[key];
collection[key] = newValue;
return element;
}
/*******************************/
///
/// Provides support functions to create read-write random acces files and write functions
///
public class RandomAccessFileSupport
{
///
/// Creates a new random acces stream with read-write or read rights
///
/// A relative or absolute path for the file to open
/// Mode to open the file in
/// The new FileStream
public static FileStream CreateRandomAccessFile( string fileName, string mode )
{
FileStream newFile = null;
if ( mode.CompareTo( "rw" ) == 0 )
newFile = new FileStream( fileName, FileMode.OpenOrCreate, FileAccess.ReadWrite );
else if ( mode.CompareTo( "r" ) == 0 )
newFile = new FileStream( fileName, FileMode.Open, FileAccess.Read );
else
throw new ArgumentException();
return newFile;
}
///
/// Creates a new random acces stream with read-write or read rights
///
/// File infomation for the file to open
/// Mode to open the file in
/// The new FileStream
public static FileStream CreateRandomAccessFile( FileInfo fileName, string mode )
{
return CreateRandomAccessFile( fileName.FullName, mode );
}
///
/// Writes the data to the specified file stream
///
/// Data to write
/// File to write to
public static void WriteBytes( string data, FileStream fileStream )
{
int index = 0;
int length = data.Length;
while ( index < length )
fileStream.WriteByte( (byte)data[index++] );
}
///
/// Writes the received string to the file stream
///
/// String of information to write
/// File to write to
public static void WriteChars( string data, FileStream fileStream )
{
WriteBytes( data, fileStream );
}
///
/// Writes the received data to the file stream
///
/// Data to write
/// File to write to
public static void WriteRandomFile( sbyte[] sByteArray, FileStream fileStream )
{
byte[] byteArray = ToByteArray( sByteArray );
fileStream.Write( byteArray, 0, byteArray.Length );
}
}
/*******************************/
///
/// Checks if a file have write permissions
///
/// The file instance to check
/// True if have write permissions otherwise false
public static bool FileCanWrite( FileInfo file )
{
return ( File.GetAttributes( file.FullName ) & FileAttributes.ReadOnly ) != FileAttributes.ReadOnly;
}
/*******************************/
///
/// Checks if the giving File instance is a directory or file, and returns his Length
///
/// The File instance to check
/// The length of the file
public static long FileLength( FileInfo file )
{
if ( Directory.Exists( file.FullName ) )
return 0;
else
return file.Length;
}
/*******************************/
/// Reads a number of characters from the current source Stream and writes the data to the target array at the specified index.
/// The source Stream to read from.
/// Contains the array of characteres read from the source Stream.
/// The starting index of the target array.
/// The maximum number of characters to read from the source Stream.
/// 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.
public static Int32 ReadInput( Stream sourceStream, ref byte[] target, int start, int count )
{
// Returns 0 bytes if not enough space in target
if ( target.Length == 0 )
return 0;
byte[] receiver = new byte[target.Length];
int bytesRead = sourceStream.Read( receiver, start, count );
// Returns -1 if EOF
if ( bytesRead == 0 )
return -1;
for ( int i = start; i < start + bytesRead; i++ )
target[i] = (byte)receiver[i];
return bytesRead;
}
/// Reads a number of characters from the current source TextReader and writes the data to the target array at the specified index.
/// The source TextReader to read from
/// Contains the array of characteres read from the source TextReader.
/// The starting index of the target array.
/// The maximum number of characters to read from the source TextReader.
/// 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.
public static Int32 ReadInput( TextReader sourceTextReader, ref sbyte[] target, int start, int count )
{
// Returns 0 bytes if not enough space in target
if ( target.Length == 0 )
return 0;
char[] charArray = new char[target.Length];
int bytesRead = sourceTextReader.Read( charArray, start, count );
// Returns -1 if EOF
if ( bytesRead == 0 )
return -1;
for ( int index = start; index < start + bytesRead; index++ )
target[index] = (sbyte)charArray[index];
return bytesRead;
}
/*******************************/
///
/// Performs an unsigned bitwise right shift with the specified number
///
/// Number to operate on
/// Ammount of bits to shift
/// The resulting number from the shift operation
public static int URShift( int number, int bits )
{
if ( number >= 0 )
return number >> bits;
else
return ( number >> bits ) + ( 2 << ~bits );
}
///
/// Performs an unsigned bitwise right shift with the specified number
///
/// Number to operate on
/// Ammount of bits to shift
/// The resulting number from the shift operation
public static int URShift( int number, long bits )
{
return URShift( number, (int)bits );
}
///
/// Performs an unsigned bitwise right shift with the specified number
///
/// Number to operate on
/// Ammount of bits to shift
/// The resulting number from the shift operation
public static long URShift( long number, int bits )
{
if ( number >= 0 )
return number >> bits;
else
return ( number >> bits ) + ( 2L << ~bits );
}
///
/// Performs an unsigned bitwise right shift with the specified number
///
/// Number to operate on
/// Ammount of bits to shift
/// The resulting number from the shift operation
public static long URShift( long number, long bits )
{
return URShift( number, (int)bits );
}
/*******************************/
///
/// Writes the exception stack trace to the received stream
///
/// Exception to obtain information from
/// Output sream used to write to
public static void WriteStackTrace( Exception throwable, TextWriter stream )
{
stream.Write( throwable.StackTrace );
stream.Flush();
}
/*******************************/
///
/// Removes the element with the specified key from a Hashtable instance.
///
/// The Hashtable instance
/// The key of the element to remove
/// The element removed
public static Object HashtableRemove( Hashtable hashtable, Object key )
{
Object element = hashtable[key];
hashtable.Remove( key );
return element;
}
/*******************************/
///
/// Converts an array of sbytes to an array of chars
///
/// The array of sbytes to convert
/// The new array of chars
public static char[] ToCharArray( sbyte[] sByteArray )
{
char[] charArray = new char[sByteArray.Length];
sByteArray.CopyTo( charArray, 0 );
return charArray;
}
///
/// Converts an array of bytes to an array of chars
///
/// The array of bytes to convert
/// The new array of chars
public static char[] ToCharArray( byte[] byteArray )
{
char[] charArray = new char[byteArray.Length];
byteArray.CopyTo( charArray, 0 );
return charArray;
}
/*******************************/
///
/// Receives a byte array and returns it transformed in an sbyte array
///
/// Byte array to process
/// The transformed array
public static sbyte[] ToSByteArray( byte[] byteArray )
{
sbyte[] sbyteArray = new sbyte[byteArray.Length];
for ( int index = 0; index < byteArray.Length; index++ )
sbyteArray[index] = (sbyte)byteArray[index];
return sbyteArray;
}
/*******************************/
///
/// Returns the last element of an ArrayList instance.
///
/// The ArrayList instance
/// The last element of the ArrayList
public static Object VectorLastElement( ArrayList arrayList )
{
return arrayList[arrayList.Count - 1];
}
///
/// Returns the last element of a Stack instance.
///
/// The Stack instance
/// The last element of the Stack
public static Object VectorLastElement( Stack stack )
{
return stack.ToArray()[0];
}
/*******************************/
///
/// Adds an element to the top end of a Stack instance.
///
/// The Stack instance
/// The element to add
/// The element added
public static Object StackPush( Stack stack, Object element )
{
stack.Push( element );
return element;
}
/*******************************/
///
/// Creates an instance of a received Type.
///
/// The Type of the new class instance to return.
/// An Object containing the new instance.
public static Object CreateNewInstance( Type classType )
{
Object instance = null;
Type[] constructor = new Type[] { };
ConstructorInfo[] constructors = null;
constructors = classType.GetConstructors();
if ( constructors.Length == 0 )
throw new UnauthorizedAccessException();
else
{
for ( int i = 0; i < constructors.Length; i++ )
{
ParameterInfo[] parameters = constructors[i].GetParameters();
if ( parameters.Length == 0 )
{
instance = classType.GetConstructor( constructor ).Invoke( new Object[] { } );
break;
}
else if ( i == constructors.Length - 1 )
throw new MethodAccessException();
}
}
return instance;
}
/*******************************/
///
/// Obtains the int value depending of the type of modifiers that the constructor have
///
/// The ConstructorInfo used to obtain the int value
/// The int value of the modifier present in the constructor. 1 if it's public, 2 if it's private, otherwise 4
public static int GetConstructorModifiers( ConstructorInfo constructor )
{
int temp;
if ( constructor.IsPublic )
temp = 1;
else if ( constructor.IsPrivate )
temp = 2;
else
temp = 4;
return temp;
}
/*******************************/
///
/// Write an array of bytes int the FileStream specified.
///
/// FileStream that must be updated.
/// Array of bytes that must be written in the FileStream.
public static void WriteOutput( FileStream FileStreamWrite, sbyte[] Source )
{
FileStreamWrite.Write( ToByteArray( Source ), 0, Source.Length );
}
}