home *** CD-ROM | disk | FTP | other *** search
/ Australian Personal Computer 2000 October / tst.iso / programs / borland / RUNIMAGE / DELPHI40 / DOC / SYSUTILS.INT < prev    next >
Encoding:
Text File  |  1998-06-17  |  83.1 KB  |  2,057 lines

  1.  
  2. {*******************************************************}
  3. {                                                       }
  4. {       Borland Delphi Runtime Library                  }
  5. {       System Utilities Unit                           }
  6. {                                                       }
  7. {       Copyright (C) 1995,98 Inprise Corporation       }
  8. {                                                       }
  9. {*******************************************************}
  10.  
  11. unit SysUtils;
  12.  
  13. {$H+}
  14.  
  15. interface
  16.  
  17. uses Windows, SysConst;
  18.  
  19. const
  20.  
  21. { File open modes }
  22.  
  23.   fmOpenRead       = $0000;
  24.   fmOpenWrite      = $0001;
  25.   fmOpenReadWrite  = $0002;
  26.   fmShareCompat    = $0000;
  27.   fmShareExclusive = $0010;
  28.   fmShareDenyWrite = $0020;
  29.   fmShareDenyRead  = $0030;
  30.   fmShareDenyNone  = $0040;
  31.  
  32. { File attribute constants }
  33.  
  34.   faReadOnly  = $00000001;
  35.   faHidden    = $00000002;
  36.   faSysFile   = $00000004;
  37.   faVolumeID  = $00000008;
  38.   faDirectory = $00000010;
  39.   faArchive   = $00000020;
  40.   faAnyFile   = $0000003F;
  41.  
  42. { File mode magic numbers }
  43.  
  44.   fmClosed = $D7B0;
  45.   fmInput  = $D7B1;
  46.   fmOutput = $D7B2;
  47.   fmInOut  = $D7B3;
  48.  
  49. { Seconds and milliseconds per day }
  50.  
  51.   SecsPerDay = 24 * 60 * 60;
  52.   MSecsPerDay = SecsPerDay * 1000;
  53.  
  54. { Days between 1/1/0001 and 12/31/1899 }
  55.  
  56.   DateDelta = 693594;
  57.  
  58. type
  59.  
  60. { Standard Character set type }
  61.  
  62.   TSysCharSet = set of Char;
  63.  
  64. { Type conversion records }
  65.  
  66.   WordRec = packed record
  67.     Lo, Hi: Byte;
  68.   end;
  69.  
  70.   LongRec = packed record
  71.     Lo, Hi: Word;
  72.   end;
  73.  
  74.   Int64Rec = packed record
  75.     Lo, Hi: DWORD;
  76.   end;
  77.  
  78.   TMethod = record
  79.     Code, Data: Pointer;
  80.   end;
  81.  
  82. { General arrays }
  83.  
  84.   PByteArray = ^TByteArray;
  85.   TByteArray = array[0..32767] of Byte;
  86.  
  87.   PWordArray = ^TWordArray;
  88.   TWordArray = array[0..16383] of Word;
  89.  
  90. { Generic procedure pointer }
  91.  
  92.   TProcedure = procedure;
  93.  
  94. { Generic filename type }
  95.  
  96.   TFileName = type string;
  97.  
  98. { Search record used by FindFirst, FindNext, and FindClose }
  99.  
  100.   TSearchRec = record
  101.     Time: Integer;
  102.     Size: Integer;
  103.     Attr: Integer;
  104.     Name: TFileName;
  105.     ExcludeAttr: Integer;
  106.     FindHandle: THandle;
  107.     FindData: TWin32FindData;
  108.   end;
  109.  
  110. { Typed-file and untyped-file record }
  111.  
  112.   TFileRec = record
  113.     Handle: Integer;
  114.     Mode: Integer;
  115.     RecSize: Cardinal;
  116.     Private: array[1..28] of Byte;
  117.     UserData: array[1..32] of Byte;
  118.     Name: array[0..259] of Char;
  119.   end;
  120.  
  121. { Text file record structure used for Text files }
  122.  
  123.   PTextBuf = ^TTextBuf;
  124.   TTextBuf = array[0..127] of Char;
  125.   TTextRec = record
  126.     Handle: Integer;
  127.     Mode: Integer;
  128.     BufSize: Cardinal;
  129.     BufPos: Cardinal;
  130.     BufEnd: Cardinal;
  131.     BufPtr: PChar;
  132.     OpenFunc: Pointer;
  133.     InOutFunc: Pointer;
  134.     FlushFunc: Pointer;
  135.     CloseFunc: Pointer;
  136.     UserData: array[1..32] of Byte;
  137.     Name: array[0..259] of Char;
  138.     Buffer: TTextBuf;
  139.   end;
  140.  
  141. { FloatToText, FloatToTextFmt, TextToFloat, and FloatToDecimal type codes }
  142.  
  143.   TFloatValue = (fvExtended, fvCurrency);
  144.  
  145. { FloatToText format codes }
  146.  
  147.   TFloatFormat = (ffGeneral, ffExponent, ffFixed, ffNumber, ffCurrency);
  148.  
  149. { FloatToDecimal result record }
  150.  
  151.   TFloatRec = packed record
  152.     Exponent: Smallint;
  153.     Negative: Boolean;
  154.     Digits: array[0..20] of Char;
  155.   end;
  156.  
  157. { Date and time record }
  158.  
  159.   TTimeStamp = record
  160.     Time: Integer;      { Number of milliseconds since midnight }
  161.     Date: Integer;      { One plus number of days since 1/1/0001 }
  162.   end;
  163.  
  164. { MultiByte Character Set (MBCS) byte type }
  165.   TMbcsByteType = (mbSingleByte, mbLeadByte, mbTrailByte);
  166.  
  167. { System Locale information record }
  168.   TSysLocale = packed record
  169.     DefaultLCID: LCID;
  170.     PriLangID: LANGID;
  171.     SubLangID: LANGID;
  172.     FarEast: Boolean;
  173.     MiddleEast: Boolean;
  174.   end;
  175.  
  176. { Exceptions }
  177.  
  178.   Exception = class(TObject)
  179.   public
  180.     constructor Create(const Msg: string);
  181.     constructor CreateFmt(const Msg: string; const Args: array of const);
  182.     constructor CreateRes(Ident: Integer; Dummy: Extended = 0);
  183.     constructor CreateResFmt(Ident: Integer; const Args: array of const);
  184.     constructor CreateHelp(const Msg: string; AHelpContext: Integer);
  185.     constructor CreateFmtHelp(const Msg: string; const Args: array of const;
  186.       AHelpContext: Integer);
  187.     constructor CreateResHelp(Ident: Integer; AHelpContext: Integer);
  188.     constructor CreateResFmtHelp(Ident: Integer; const Args: array of const;
  189.       AHelpContext: Integer);
  190.     property HelpContext: Integer;
  191.     property Message: string;
  192.   end;
  193.  
  194.   ExceptClass = class of Exception;
  195.  
  196.   EAbort = class(Exception);
  197.  
  198.   EHeapException = class(Exception)
  199.   public
  200.     procedure FreeInstance; override;
  201.   end;
  202.  
  203.   EOutOfMemory = class(EHeapException);
  204.  
  205.   EInOutError = class(Exception)
  206.   public
  207.     ErrorCode: Integer;
  208.   end;
  209.  
  210.   EExternal = class(Exception)
  211.   public
  212.     ExceptionRecord: PExceptionRecord;
  213.   end;
  214.  
  215.   EExternalException = class(EExternal);
  216.  
  217.   EIntError = class(EExternal);
  218.   EDivByZero = class(EIntError);
  219.   ERangeError = class(EIntError);
  220.   EIntOverflow = class(EIntError);
  221.  
  222.   EMathError = class(EExternal);
  223.   EInvalidOp = class(EMathError);
  224.   EZeroDivide = class(EMathError);
  225.   EOverflow = class(EMathError);
  226.   EUnderflow = class(EMathError);
  227.  
  228.   EInvalidPointer = class(EHeapException);
  229.  
  230.   EInvalidCast = class(Exception);
  231.  
  232.   EConvertError = class(Exception);
  233.  
  234.   EAccessViolation = class(EExternal);
  235.   EPrivilege = class(EExternal);
  236.   EStackOverflow = class(EExternal);
  237.   EControlC = class(EExternal);
  238.  
  239.   EVariantError = class(Exception);
  240.  
  241.   EPropReadOnly = class(Exception);
  242.   EPropWriteOnly = class(Exception);
  243.  
  244.   EAssertionFailed = class(Exception);
  245.  
  246.   EAbstractError = class(Exception);
  247.  
  248.   EIntfCastError = class(Exception);
  249.  
  250.   EInvalidContainer = class(Exception);
  251.   EInvalidInsert = class(Exception);
  252.  
  253.   EPackageError = class(Exception);
  254.  
  255.   EWin32Error = class(Exception)
  256.   public
  257.     ErrorCode: DWORD;
  258.   end;
  259.  
  260. var
  261.  
  262. { Empty string and null string pointer. These constants are provided for
  263.   backwards compatibility only.  }
  264.  
  265.   EmptyStr: string = '';
  266.   NullStr: PString = @EmptyStr;
  267.  
  268. { Win32 platform identifier.  This will be one of the following values:
  269.  
  270.     VER_PLATFORM_WIN32s
  271.     VER_PLATFORM_WIN32_WINDOWS
  272.     VER_PLATFORM_WIN32_NT
  273.  
  274.   See WINDOWS.PAS for the numerical values. }
  275.  
  276.   Win32Platform: Integer = 0;
  277.  
  278. { Win32 OS version information -
  279.  
  280.   see TOSVersionInfo.dwMajorVersion/dwMinorVersion/dwBuildNumber }
  281.  
  282.   Win32MajorVersion: Integer = 0;
  283.   Win32MinorVersion: Integer = 0;
  284.   Win32BuildNumber: Integer = 0;
  285.  
  286. { Win32 OS extra version info string -
  287.  
  288.   see TOSVersionInfo.szCSDVersion }
  289.  
  290.   Win32CSDVersion: string = '';
  291.  
  292. { Currency and date/time formatting options
  293.  
  294.   The initial values of these variables are fetched from the system registry
  295.   using the GetLocaleInfo function in the Win32 API. The description of each
  296.   variable specifies the LOCALE_XXXX constant used to fetch the initial
  297.   value.
  298.  
  299.   CurrencyString - Defines the currency symbol used in floating-point to
  300.   decimal conversions. The initial value is fetched from LOCALE_SCURRENCY.
  301.  
  302.   CurrencyFormat - Defines the currency symbol placement and separation
  303.   used in floating-point to decimal conversions. Possible values are:
  304.  
  305.     0 = '$1'
  306.     1 = '1$'
  307.     2 = '$ 1'
  308.     3 = '1 $'
  309.  
  310.   The initial value is fetched from LOCALE_ICURRENCY.
  311.  
  312.   NegCurrFormat - Defines the currency format for used in floating-point to
  313.   decimal conversions of negative numbers. Possible values are:
  314.  
  315.     0 = '($1)'      4 = '(1$)'      8 = '-1 $'      12 = '$ -1'
  316.     1 = '-$1'       5 = '-1$'       9 = '-$ 1'      13 = '1- $'
  317.     2 = '$-1'       6 = '1-$'      10 = '1 $-'      14 = '($ 1)'
  318.     3 = '$1-'       7 = '1$-'      11 = '$ 1-'      15 = '(1 $)'
  319.  
  320.   The initial value is fetched from LOCALE_INEGCURR.
  321.  
  322.   ThousandSeparator - The character used to separate thousands in numbers
  323.   with more than three digits to the left of the decimal separator. The
  324.   initial value is fetched from LOCALE_STHOUSAND.
  325.  
  326.   DecimalSeparator - The character used to separate the integer part from
  327.   the fractional part of a number. The initial value is fetched from
  328.   LOCALE_SDECIMAL.
  329.  
  330.   CurrencyDecimals - The number of digits to the right of the decimal point
  331.   in a currency amount. The initial value is fetched from LOCALE_ICURRDIGITS.
  332.  
  333.   DateSeparator - The character used to separate the year, month, and day
  334.   parts of a date value. The initial value is fetched from LOCATE_SDATE.
  335.  
  336.   ShortDateFormat - The format string used to convert a date value to a
  337.   short string suitable for editing. For a complete description of date and
  338.   time format strings, refer to the documentation for the FormatDate
  339.   function. The short date format should only use the date separator
  340.   character and the  m, mm, d, dd, yy, and yyyy format specifiers. The
  341.   initial value is fetched from LOCALE_SSHORTDATE.
  342.  
  343.   LongDateFormat - The format string used to convert a date value to a long
  344.   string suitable for display but not for editing. For a complete description
  345.   of date and time format strings, refer to the documentation for the
  346.   FormatDate function. The initial value is fetched from LOCALE_SLONGDATE.
  347.  
  348.   TimeSeparator - The character used to separate the hour, minute, and
  349.   second parts of a time value. The initial value is fetched from
  350.   LOCALE_STIME.
  351.  
  352.   TimeAMString - The suffix string used for time values between 00:00 and
  353.   11:59 in 12-hour clock format. The initial value is fetched from
  354.   LOCALE_S1159.
  355.  
  356.   TimePMString - The suffix string used for time values between 12:00 and
  357.   23:59 in 12-hour clock format. The initial value is fetched from
  358.   LOCALE_S2359.
  359.  
  360.   ShortTimeFormat - The format string used to convert a time value to a
  361.   short string with only hours and minutes. The default value is computed
  362.   from LOCALE_ITIME and LOCALE_ITLZERO.
  363.  
  364.   LongTimeFormat - The format string used to convert a time value to a long
  365.   string with hours, minutes, and seconds. The default value is computed
  366.   from LOCALE_ITIME and LOCALE_ITLZERO.
  367.  
  368.   ShortMonthNames - Array of strings containing short month names. The mmm
  369.   format specifier in a format string passed to FormatDate causes a short
  370.   month name to be substituted. The default values are fecthed from the
  371.   LOCALE_SABBREVMONTHNAME system locale entries.
  372.  
  373.   LongMonthNames - Array of strings containing long month names. The mmmm
  374.   format specifier in a format string passed to FormatDate causes a long
  375.   month name to be substituted. The default values are fecthed from the
  376.   LOCALE_SMONTHNAME system locale entries.
  377.  
  378.   ShortDayNames - Array of strings containing short day names. The ddd
  379.   format specifier in a format string passed to FormatDate causes a short
  380.   day name to be substituted. The default values are fecthed from the
  381.   LOCALE_SABBREVDAYNAME system locale entries.
  382.  
  383.   LongDayNames - Array of strings containing long day names. The dddd
  384.   format specifier in a format string passed to FormatDate causes a long
  385.   day name to be substituted. The default values are fecthed from the
  386.   LOCALE_SDAYNAME system locale entries.
  387.  
  388.   ListSeparator - The character used to separate items in a list.  The
  389.   initial value is fetched from LOCALE_SLIST.
  390.  
  391.   TwoDigitYearCenturyWindow - Determines what century is added to two
  392.   digit years when converting string dates to numeric dates.  This value
  393.   is subtracted from the current year before extracting the century.
  394.   This can be used to extend the lifetime of existing applications that
  395.   are inextricably tied to 2 digit year data entry.  The best solution
  396.   to Year 2000 (Y2k) issues is not to accept 2 digit years at all - require
  397.   4 digit years in data entry to eliminate century ambiguities.
  398.  
  399.   Examples:
  400.  
  401.   Current TwoDigitCenturyWindow  Century  StrToDate() of:
  402.   Year    Value                  Pivot    '01/01/03' '01/01/68' '01/01/50'
  403.   -------------------------------------------------------------------------
  404.   1998    0 (default)            1900     1903       1968       1950
  405.   2002    0 (default)            2000     2003       2068       2050
  406.   1998    50                     1948     2003       1968       1950
  407.   2002    50                     1952     2003       1968       2050
  408.   2020    50                     1970     2003       2068       2050
  409.  }
  410.  
  411. var
  412.   CurrencyString: string;
  413.   CurrencyFormat: Byte;
  414.   NegCurrFormat: Byte;
  415.   ThousandSeparator: Char;
  416.   DecimalSeparator: Char;
  417.   CurrencyDecimals: Byte;
  418.   DateSeparator: Char;
  419.   ShortDateFormat: string;
  420.   LongDateFormat: string;
  421.   TimeSeparator: Char;
  422.   TimeAMString: string;
  423.   TimePMString: string;
  424.   ShortTimeFormat: string;
  425.   LongTimeFormat: string;
  426.   ShortMonthNames: array[1..12] of string;
  427.   LongMonthNames: array[1..12] of string;
  428.   ShortDayNames: array[1..7] of string;
  429.   LongDayNames: array[1..7] of string;
  430.   SysLocale: TSysLocale;
  431.   EraNames: array[1..7] of string;
  432.   EraYearOffsets: array[1..7] of Integer;
  433.   TwoDigitYearCenturyWindow: Word = 0;
  434.   ListSeparator: Char;
  435.  
  436. { Memory management routines }
  437.  
  438. { AllocMem allocates a block of the given size on the heap. Each byte in
  439.   the allocated buffer is set to zero. To dispose the buffer, use the
  440.   FreeMem standard procedure. }
  441.  
  442. function AllocMem(Size: Cardinal): Pointer;
  443.  
  444. { Exit procedure handling }
  445.  
  446. { AddExitProc adds the given procedure to the run-time library's exit
  447.   procedure list. When an application terminates, its exit procedures are
  448.   executed in reverse order of definition, i.e. the last procedure passed
  449.   to AddExitProc is the first one to get executed upon termination. }
  450.  
  451. procedure AddExitProc(Proc: TProcedure);
  452.  
  453. { String handling routines }
  454.  
  455. { NewStr allocates a string on the heap. NewStr is provided for backwards
  456.   compatibility only. }
  457.  
  458. function NewStr(const S: string): PString;
  459.  
  460. { DisposeStr disposes a string pointer that was previously allocated using
  461.   NewStr. DisposeStr is provided for backwards compatibility only. }
  462.  
  463. procedure DisposeStr(P: PString);
  464.  
  465. { AssignStr assigns a new dynamically allocated string to the given string
  466.   pointer. AssignStr is provided for backwards compatibility only. }
  467.  
  468. procedure AssignStr(var P: PString; const S: string);
  469.  
  470. { AppendStr appends S to the end of Dest. AppendStr is provided for
  471.   backwards compatibility only. Use "Dest := Dest + S" instead. }
  472.  
  473. procedure AppendStr(var Dest: string; const S: string);
  474.  
  475. { UpperCase converts all ASCII characters in the given string to upper case.
  476.   The conversion affects only 7-bit ASCII characters between 'a' and 'z'. To
  477.   convert 8-bit international characters, use AnsiUpperCase. }
  478.  
  479. function UpperCase(const S: string): string;
  480.  
  481. { LowerCase converts all ASCII characters in the given string to lower case.
  482.   The conversion affects only 7-bit ASCII characters between 'A' and 'Z'. To
  483.   convert 8-bit international characters, use AnsiLowerCase. }
  484.  
  485. function LowerCase(const S: string): string;
  486.  
  487. { CompareStr compares S1 to S2, with case-sensitivity. The return value is
  488.   less than 0 if S1 < S2, 0 if S1 = S2, or greater than 0 if S1 > S2. The
  489.   compare operation is based on the 8-bit ordinal value of each character
  490.   and is not affected by the current Windows locale. }
  491.  
  492. function CompareStr(const S1, S2: string): Integer;
  493.  
  494. { CompareMem performs a binary compare of Length bytes of memory referenced
  495.   by P1 to that of P2.  CompareMem returns True if the memory referenced by
  496.   P1 is identical to that of P2. }
  497.  
  498. function CompareMem(P1, P2: Pointer; Length: Integer): Boolean; assembler;
  499.  
  500. { CompareText compares S1 to S2, without case-sensitivity. The return value
  501.   is the same as for CompareStr. The compare operation is based on the 8-bit
  502.   ordinal value of each character, after converting 'a'..'z' to 'A'..'Z',
  503.   and is not affected by the current Windows locale. }
  504.  
  505. function CompareText(const S1, S2: string): Integer;
  506.  
  507. { AnsiUpperCase converts all characters in the given string to upper case.
  508.   The conversion uses the current Windows locale. }
  509.  
  510. function AnsiUpperCase(const S: string): string;
  511.  
  512. { AnsiLowerCase converts all characters in the given string to lower case.
  513.   The conversion uses the current Windows locale. }
  514.  
  515. function AnsiLowerCase(const S: string): string;
  516.  
  517. { AnsiCompareStr compares S1 to S2, with case-sensitivity. The compare
  518.   operation is controlled by the current Windows locale. The return value
  519.   is the same as for CompareStr. }
  520.  
  521. function AnsiCompareStr(const S1, S2: string): Integer;
  522.  
  523. { AnsiCompareText compares S1 to S2, without case-sensitivity. The compare
  524.   operation is controlled by the current Windows locale. The return value
  525.   is the same as for CompareStr. }
  526.  
  527. function AnsiCompareText(const S1, S2: string): Integer;
  528.  
  529. { AnsiStrComp compares S1 to S2, with case-sensitivity. The compare
  530.   operation is controlled by the current Windows locale. The return value
  531.   is the same as for CompareStr. }
  532.  
  533. function AnsiStrComp(S1, S2: PChar): Integer;
  534.  
  535. { AnsiStrIComp compares S1 to S2, without case-sensitivity. The compare
  536.   operation is controlled by the current Windows locale. The return value
  537.   is the same as for CompareStr. }
  538.  
  539. function AnsiStrIComp(S1, S2: PChar): Integer;
  540.  
  541. { AnsiStrLComp compares S1 to S2, with case-sensitivity, up to a maximum
  542.   length of MaxLen bytes. The compare operation is controlled by the
  543.   current Windows locale. The return value is the same as for CompareStr. }
  544.  
  545. function AnsiStrLComp(S1, S2: PChar; MaxLen: Cardinal): Integer;
  546.  
  547. { AnsiStrLIComp compares S1 to S2, without case-sensitivity, up to a maximum
  548.   length of MaxLen bytes. The compare operation is controlled by the
  549.   current Windows locale. The return value is the same as for CompareStr. }
  550.  
  551. function AnsiStrLIComp(S1, S2: PChar; MaxLen: Cardinal): Integer;
  552.  
  553. { AnsiStrLower converts all characters in the given string to lower case.
  554.   The conversion uses the current Windows locale. }
  555.  
  556. function AnsiStrLower(Str: PChar): PChar;
  557.  
  558. { AnsiStrUpper converts all characters in the given string to upper case.
  559.   The conversion uses the current Windows locale. }
  560.  
  561. function AnsiStrUpper(Str: PChar): PChar;
  562.  
  563. { AnsiLastChar returns a pointer to the last full character in the string.
  564.   This function supports multibyte characters  }
  565.  
  566. function AnsiLastChar(const S: string): PChar;
  567.  
  568. { AnsiStrLastChar returns a pointer to the last full character in the string.
  569.   This function supports multibyte characters.  }
  570.  
  571. function AnsiStrLastChar(P: PChar): PChar;
  572.  
  573. { Trim trims leading and trailing spaces and control characters from the
  574.   given string. }
  575.  
  576. function Trim(const S: string): string;
  577.  
  578. { TrimLeft trims leading spaces and control characters from the given
  579.   string. }
  580.  
  581. function TrimLeft(const S: string): string;
  582.  
  583. { TrimRight trims trailing spaces and control characters from the given
  584.   string. }
  585.  
  586. function TrimRight(const S: string): string;
  587.  
  588. { QuotedStr returns the given string as a quoted string. A single quote
  589.   character is inserted at the beginning and the end of the string, and
  590.   for each single quote character in the string, another one is added. }
  591.  
  592. function QuotedStr(const S: string): string;
  593.  
  594. { AnsiQuotedStr returns the given string as a quoted string, using the
  595.   provided Quote character.  A Quote character is inserted at the beginning
  596.   and end of thestring, and each Quote character in the string is doubled.
  597.   This function supports multibyte character strings (MBCS). }
  598.  
  599. function AnsiQuotedStr(const S: string; Quote: Char): string;
  600.  
  601. { AnsiExtractQuotedStr removes the Quote characters from the beginning and end
  602.   of a quoted string, and reduces pairs of Quote characters within the quoted
  603.   string to a single character. If the first character in Src is not the Quote
  604.   character, the function returns an empty string.  The function copies
  605.   characters from the Src to the result string until the second solitary
  606.   Quote character or the first null character in Src. The Src parameter is
  607.   updated to point to the first character following the quoted string.  If
  608.   the Src string does not contain a matching end Quote character, the Src
  609.   parameter is updated to point to the terminating null character in Src.
  610.   This function supports multibyte character strings (MBCS).  }
  611.  
  612. function AnsiExtractQuotedStr(var Src: PChar; Quote: Char): string;
  613.  
  614. { AdjustLineBreaks adjusts all line breaks in the given string to be true
  615.   CR/LF sequences. The function changes any CR characters not followed by
  616.   a LF and any LF characters not preceded by a CR into CR/LF pairs. }
  617.  
  618. function AdjustLineBreaks(const S: string): string;
  619.  
  620. { IsValidIdent returns true if the given string is a valid identifier. An
  621.   identifier is defined as a character from the set ['A'..'Z', 'a'..'z', '_']
  622.   followed by zero or more characters from the set ['A'..'Z', 'a'..'z',
  623.   '0..'9', '_']. }
  624.  
  625. function IsValidIdent(const Ident: string): Boolean;
  626.  
  627. { IntToStr converts the given value to its decimal string representation. }
  628.  
  629. function IntToStr(Value: Integer): string; overload;
  630. function IntToStr(Value: Int64): string; overload;
  631.  
  632. { IntToHex converts the given value to a hexadecimal string representation
  633.   with the minimum number of digits specified. }
  634.  
  635. function IntToHex(Value: Integer; Digits: Integer): string; overload;
  636. function IntToHex(Value: Int64; Digits: Integer): string; overload;
  637.  
  638. { StrToInt converts the given string to an integer value. If the string
  639.   doesn't contain a valid value, an EConvertError exception is raised. }
  640.  
  641. function StrToInt(const S: string): Integer;
  642. function StrToInt64(const S: string): Int64;
  643.  
  644. { StrToIntDef converts the given string to an integer value. If the string
  645.   doesn't contain a valid value, the value given by Default is returned. }
  646.  
  647. function StrToIntDef(const S: string; Default: Integer): Integer;
  648. function StrToInt64Def(const S: string; Default: Int64): Int64;
  649.  
  650. { LoadStr loads the string resource given by Ident from the application's
  651.   executable file. If the string resource does not exist, an empty string
  652.   is returned. }
  653.  
  654. function LoadStr(Ident: Integer): string;
  655.  
  656. { LoadStr loads the string resource given by Ident from the application's
  657.   executable file, and uses it as the format string in a call to the
  658.   Format function with the given arguments. }
  659.  
  660. function FmtLoadStr(Ident: Integer; const Args: array of const): string;
  661.  
  662. { File management routines }
  663.  
  664. { FileOpen opens the specified file using the specified access mode. The
  665.   access mode value is constructed by OR-ing one of the fmOpenXXXX constants
  666.   with one of the fmShareXXXX constants. If the return value is positive,
  667.   the function was successful and the value is the file handle of the opened
  668.   file. A return value of -1 indicates that an error occurred. }
  669.  
  670. function FileOpen(const FileName: string; Mode: LongWord): Integer;
  671.  
  672. { FileCreate creates a new file by the specified name. If the return value
  673.   is positive, the function was successful and the value is the file handle
  674.   of the new file. A return value of -1 indicates that an error occurred. }
  675.  
  676. function FileCreate(const FileName: string): Integer;
  677.  
  678. { FileRead reads Count bytes from the file given by Handle into the buffer
  679.   specified by Buffer. The return value is the number of bytes actually
  680.   read; it is less than Count if the end of the file was reached. The return
  681.   value is -1 if an error occurred. }
  682.  
  683. function FileRead(Handle: Integer; var Buffer; Count: LongWord): Integer;
  684.  
  685. { FileWrite writes Count bytes to the file given by Handle from the buffer
  686.   specified by Buffer. The return value is the number of bytes actually
  687.   written, or -1 if an error occurred. }
  688.  
  689. function FileWrite(Handle: Integer; const Buffer; Count: LongWord): Integer;
  690.  
  691. { FileSeek changes the current position of the file given by Handle to be
  692.   Offset bytes relative to the point given by Origin. Origin = 0 means that
  693.   Offset is relative to the beginning of the file, Origin = 1 means that
  694.   Offset is relative to the current position, and Origin = 2 means that
  695.   Offset is relative to the end of the file. The return value is the new
  696.   current position, relative to the beginning of the file, or -1 if an error
  697.   occurred. }
  698.  
  699. function FileSeek(Handle, Offset, Origin: Integer): Integer; overload;
  700. function FileSeek(Handle: Integer; const Offset: Int64; Origin: Integer): Int64; overload;
  701.  
  702. { FileClose closes the specified file. }
  703.  
  704. procedure FileClose(Handle: Integer);
  705.  
  706. { FileAge returns the date-and-time stamp of the specified file. The return
  707.   value can be converted to a TDateTime value using the FileDateToDateTime
  708.   function. The return value is -1 if the file does not exist. }
  709.  
  710. function FileAge(const FileName: string): Integer;
  711.  
  712. { FileExists returns a boolean value that indicates whether the specified
  713.   file exists. }
  714.  
  715. function FileExists(const FileName: string): Boolean;
  716.  
  717. { FindFirst searches the directory given by Path for the first entry that
  718.   matches the filename given by Path and the attributes given by Attr. The
  719.   result is returned in the search record given by SearchRec. The return
  720.   value is zero if the function was successful. Otherwise the return value
  721.   is a Windows error code. FindFirst is typically used in conjunction with
  722.   FindNext and FindClose as follows:
  723.  
  724.     Result := FindFirst(Path, Attr, SearchRec);
  725.     while Result = 0 do
  726.     begin
  727.       ProcessSearchRec(SearchRec);
  728.       Result := FindNext(SearchRec);
  729.     end;
  730.     FindClose(SearchRec);
  731.  
  732.   where ProcessSearchRec represents user-defined code that processes the
  733.   information in a search record. }
  734.  
  735. function FindFirst(const Path: string; Attr: Integer;
  736.   var F: TSearchRec): Integer;
  737.  
  738. { FindNext returs the next entry that matches the name and attributes
  739.   specified in a previous call to FindFirst. The search record must be one
  740.   that was passed to FindFirst. The return value is zero if the function was
  741.   successful. Otherwise the return value is a Windows error code. }
  742.  
  743. function FindNext(var F: TSearchRec): Integer;
  744.  
  745. { FindClose terminates a FindFirst/FindNext sequence. FindClose does nothing
  746.   in the 16-bit version of Windows, but is required in the 32-bit version,
  747.   so for maximum portability every FindFirst/FindNext sequence should end
  748.   with a call to FindClose. }
  749.  
  750. procedure FindClose(var F: TSearchRec);
  751.  
  752. { FileGetDate returns the DOS date-and-time stamp of the file given by
  753.   Handle. The return value is -1 if the handle is invalid. The
  754.   FileDateToDateTime function can be used to convert the returned value to
  755.   a TDateTime value. }
  756.  
  757. function FileGetDate(Handle: Integer): Integer;
  758.  
  759. { FileSetDate sets the DOS date-and-time stamp of the file given by Handle
  760.   to the value given by Age. The DateTimeToFileDate function can be used to
  761.   convert a TDateTime value to a DOS date-and-time stamp. The return value
  762.   is zero if the function was successful. Otherwise the return value is a
  763.   Windows error code. }
  764.  
  765. function FileSetDate(Handle: Integer; Age: Integer): Integer;
  766.  
  767. { FileGetAttr returns the file attributes of the file given by FileName. The
  768.   attributes can be examined by AND-ing with the faXXXX constants defined
  769.   above. A return value of -1 indicates that an error occurred. }
  770.  
  771. function FileGetAttr(const FileName: string): Integer;
  772.  
  773. { FileSetAttr sets the file attributes of the file given by FileName to the
  774.   value given by Attr. The attribute value is formed by OR-ing the
  775.   appropriate faXXXX constants. The return value is zero if the function was
  776.   successful. Otherwise the return value is a Windows error code. }
  777.  
  778. function FileSetAttr(const FileName: string; Attr: Integer): Integer;
  779.  
  780. { DeleteFile deletes the file given by FileName. The return value is True if
  781.   the file was successfully deleted, or False if an error occurred. }
  782.  
  783. function DeleteFile(const FileName: string): Boolean;
  784.  
  785. { RenameFile renames the file given by OldName to the name given by NewName.
  786.   The return value is True if the file was successfully renamed, or False if
  787.   an error occurred. }
  788.  
  789. function RenameFile(const OldName, NewName: string): Boolean;
  790.  
  791. { ChangeFileExt changes the extension of a filename. FileName specifies a
  792.   filename with or without an extension, and Extension specifies the new
  793.   extension for the filename. The new extension can be a an empty string or
  794.   a period followed by up to three characters. }
  795.  
  796. function ChangeFileExt(const FileName, Extension: string): string;
  797.  
  798. { ExtractFilePath extracts the drive and directory parts of the given
  799.   filename. The resulting string is the leftmost characters of FileName,
  800.   up to and including the colon or backslash that separates the path
  801.   information from the name and extension. The resulting string is empty
  802.   if FileName contains no drive and directory parts. }
  803.  
  804. function ExtractFilePath(const FileName: string): string;
  805.  
  806. { ExtractFileDir extracts the drive and directory parts of the given
  807.   filename. The resulting string is a directory name suitable for passing
  808.   to SetCurrentDir, CreateDir, etc. The resulting string is empty if
  809.   FileName contains no drive and directory parts. }
  810.  
  811. function ExtractFileDir(const FileName: string): string;
  812.  
  813. { ExtractFileDrive extracts the drive part of the given filename.  For
  814.   filenames with drive letters, the resulting string is '<drive>:'.
  815.   For filenames with a UNC path, the resulting string is in the form
  816.   '\\<servername>\<sharename>'.  If the given path contains neither
  817.   style of filename, the result is an empty string. }
  818.  
  819. function ExtractFileDrive(const FileName: string): string;
  820.  
  821. { ExtractFileName extracts the name and extension parts of the given
  822.   filename. The resulting string is the leftmost characters of FileName,
  823.   starting with the first character after the colon or backslash that
  824.   separates the path information from the name and extension. The resulting
  825.   string is equal to FileName if FileName contains no drive and directory
  826.   parts. }
  827.  
  828. function ExtractFileName(const FileName: string): string;
  829.  
  830. { ExtractFileExt extracts the extension part of the given filename. The
  831.   resulting string includes the period character that separates the name
  832.   and extension parts. The resulting string is empty if the given filename
  833.   has no extension. }
  834.  
  835. function ExtractFileExt(const FileName: string): string;
  836.  
  837. { ExpandFileName expands the given filename to a fully qualified filename.
  838.   The resulting string consists of a drive letter, a colon, a root relative
  839.   directory path, and a filename. Embedded '.' and '..' directory references
  840.   are removed. }
  841.  
  842. function ExpandFileName(const FileName: string): string;
  843.  
  844. { ExpandUNCFileName expands the given filename to a fully qualified filename.
  845.   This function is the same as ExpandFileName except that it will return the
  846.   drive portion of the filename in the format '\\<servername>\<sharename> if
  847.   that drive is actually a network resource instead of a local resource.
  848.   Like ExpandFileName, embedded '.' and '..' directory references are
  849.   removed. }
  850.  
  851. function ExpandUNCFileName(const FileName: string): string;
  852.  
  853. { ExtractRelativePath will return a file path name relative to the given
  854.   BaseName.  It strips the common path dirs and adds '..\' for each level
  855.   up from the BaseName path. }
  856.  
  857. function ExtractRelativePath(const BaseName, DestName: string): string;
  858.  
  859. { ExtractShortPathName will convert the given filename to the short form
  860.   by calling the GetShortPathName API.  Will return an empty string if
  861.   the file or directory specified does not exist }
  862.  
  863. function ExtractShortPathName(const FileName: string): string;
  864.  
  865. { FileSearch searches for the file given by Name in the list of directories
  866.   given by DirList. The directory paths in DirList must be separated by
  867.   semicolons. The search always starts with the current directory of the
  868.   current drive. The returned value is a concatenation of one of the
  869.   directory paths and the filename, or an empty string if the file could not
  870.   be located. }
  871.  
  872. function FileSearch(const Name, DirList: string): string;
  873.  
  874. { DiskFree returns the number of free bytes on the specified drive number,
  875.   where 0 = Current, 1 = A, 2 = B, etc. DiskFree returns -1 if the drive
  876.   number is invalid. }
  877.  
  878. function DiskFree(Drive: Byte): Int64;
  879.  
  880. { DiskSize returns the size in bytes of the specified drive number, where
  881.   0 = Current, 1 = A, 2 = B, etc. DiskSize returns -1 if the drive number
  882.   is invalid. }
  883.  
  884. function DiskSize(Drive: Byte): Int64;
  885.  
  886. { FileDateToDateTime converts a DOS date-and-time value to a TDateTime
  887.   value. The FileAge, FileGetDate, and FileSetDate routines operate on DOS
  888.   date-and-time values, and the Time field of a TSearchRec used by the
  889.   FindFirst and FindNext functions contains a DOS date-and-time value. }
  890.  
  891. function FileDateToDateTime(FileDate: Integer): TDateTime;
  892.  
  893. { DateTimeToFileDate converts a TDateTime value to a DOS date-and-time
  894.   value. The FileAge, FileGetDate, and FileSetDate routines operate on DOS
  895.   date-and-time values, and the Time field of a TSearchRec used by the
  896.   FindFirst and FindNext functions contains a DOS date-and-time value. }
  897.  
  898. function DateTimeToFileDate(DateTime: TDateTime): Integer;
  899.  
  900. { GetCurrentDir returns the current directory. }
  901.  
  902. function GetCurrentDir: string;
  903.  
  904. { SetCurrentDir sets the current directory. The return value is True if
  905.   the current directory was successfully changed, or False if an error
  906.   occurred. }
  907.  
  908. function SetCurrentDir(const Dir: string): Boolean;
  909.  
  910. { CreateDir creates a new directory. The return value is True if a new
  911.   directory was successfully created, or False if an error occurred. }
  912.  
  913. function CreateDir(const Dir: string): Boolean;
  914.  
  915. { RemoveDir deletes an existing empty directory. The return value is
  916.   True if the directory was successfully deleted, or False if an error
  917.   occurred. }
  918.  
  919. function RemoveDir(const Dir: string): Boolean;
  920.  
  921. { PChar routines }
  922.  
  923. { StrLen returns the number of characters in Str, not counting the null
  924.   terminator. }
  925.  
  926. function StrLen(Str: PChar): Cardinal;
  927.  
  928. { StrEnd returns a pointer to the null character that terminates Str. }
  929.  
  930. function StrEnd(Str: PChar): PChar;
  931.  
  932. { StrMove copies exactly Count characters from Source to Dest and returns
  933.   Dest. Source and Dest may overlap. }
  934.  
  935. function StrMove(Dest, Source: PChar; Count: Cardinal): PChar;
  936.  
  937. { StrCopy copies Source to Dest and returns Dest. }
  938.  
  939. function StrCopy(Dest, Source: PChar): PChar;
  940.  
  941. { StrECopy copies Source to Dest and returns StrEnd(Dest). }
  942.  
  943. function StrECopy(Dest, Source: PChar): PChar;
  944.  
  945. { StrLCopy copies at most MaxLen characters from Source to Dest and
  946.   returns Dest. }
  947.  
  948. function StrLCopy(Dest, Source: PChar; MaxLen: Cardinal): PChar;
  949.  
  950. { StrPCopy copies the Pascal style string Source into Dest and
  951.   returns Dest. }
  952.  
  953. function StrPCopy(Dest: PChar; const Source: string): PChar;
  954.  
  955. { StrPLCopy copies at most MaxLen characters from the Pascal style string
  956.   Source into Dest and returns Dest. }
  957.  
  958. function StrPLCopy(Dest: PChar; const Source: string;
  959.   MaxLen: Cardinal): PChar;
  960.  
  961. { StrCat appends a copy of Source to the end of Dest and returns Dest. }
  962.  
  963. function StrCat(Dest, Source: PChar): PChar;
  964.  
  965. { StrLCat appends at most MaxLen - StrLen(Dest) characters from Source to
  966.   the end of Dest, and returns Dest. }
  967.  
  968. function StrLCat(Dest, Source: PChar; MaxLen: Cardinal): PChar;
  969.  
  970. { StrComp compares Str1 to Str2. The return value is less than 0 if
  971.   Str1 < Str2, 0 if Str1 = Str2, or greater than 0 if Str1 > Str2. }
  972.  
  973. function StrComp(Str1, Str2: PChar): Integer;
  974.  
  975. { StrIComp compares Str1 to Str2, without case sensitivity. The return
  976.   value is the same as StrComp. }
  977.  
  978. function StrIComp(Str1, Str2: PChar): Integer;
  979.  
  980. { StrLComp compares Str1 to Str2, for a maximum length of MaxLen
  981.   characters. The return value is the same as StrComp. }
  982.  
  983. function StrLComp(Str1, Str2: PChar; MaxLen: Cardinal): Integer;
  984.  
  985. { StrLIComp compares Str1 to Str2, for a maximum length of MaxLen
  986.   characters, without case sensitivity. The return value is the same
  987.   as StrComp. }
  988.  
  989. function StrLIComp(Str1, Str2: PChar; MaxLen: Cardinal): Integer;
  990.  
  991. { StrScan returns a pointer to the first occurrence of Chr in Str. If Chr
  992.   does not occur in Str, StrScan returns NIL. The null terminator is
  993.   considered to be part of the string. }
  994.  
  995. function StrScan(Str: PChar; Chr: Char): PChar;
  996.  
  997. { StrRScan returns a pointer to the last occurrence of Chr in Str. If Chr
  998.   does not occur in Str, StrRScan returns NIL. The null terminator is
  999.   considered to be part of the string. }
  1000.  
  1001. function StrRScan(Str: PChar; Chr: Char): PChar;
  1002.  
  1003. { StrPos returns a pointer to the first occurrence of Str2 in Str1. If
  1004.   Str2 does not occur in Str1, StrPos returns NIL. }
  1005.  
  1006. function StrPos(Str1, Str2: PChar): PChar;
  1007.  
  1008. { StrUpper converts Str to upper case and returns Str. }
  1009.  
  1010. function StrUpper(Str: PChar): PChar;
  1011.  
  1012. { StrLower converts Str to lower case and returns Str. }
  1013.  
  1014. function StrLower(Str: PChar): PChar;
  1015.  
  1016. { StrPas converts Str to a Pascal style string. This function is provided
  1017.   for backwards compatibility only. To convert a null terminated string to
  1018.   a Pascal style string, use a type cast or an assignment. }
  1019.  
  1020. function StrPas(Str: PChar): string;
  1021.  
  1022. { StrAlloc allocates a buffer of the given size on the heap. The size of
  1023.   the allocated buffer is encoded in a four byte header that immediately
  1024.   preceeds the buffer. To dispose the buffer, use StrDispose. }
  1025.  
  1026. function StrAlloc(Size: Cardinal): PChar;
  1027.  
  1028. { StrBufSize returns the allocated size of the given buffer, not including
  1029.   the two byte header. }
  1030.  
  1031. function StrBufSize(Str: PChar): Cardinal;
  1032.  
  1033. { StrNew allocates a copy of Str on the heap. If Str is NIL, StrNew returns
  1034.   NIL and doesn't allocate any heap space. Otherwise, StrNew makes a
  1035.   duplicate of Str, obtaining space with a call to the StrAlloc function,
  1036.   and returns a pointer to the duplicated string. To dispose the string,
  1037.   use StrDispose. }
  1038.  
  1039. function StrNew(Str: PChar): PChar;
  1040.  
  1041. { StrDispose disposes a string that was previously allocated with StrAlloc
  1042.   or StrNew. If Str is NIL, StrDispose does nothing. }
  1043.  
  1044. procedure StrDispose(Str: PChar);
  1045.  
  1046. { String formatting routines }
  1047.  
  1048. { The Format routine formats the argument list given by the Args parameter
  1049.   using the format string given by the Format parameter.
  1050.  
  1051.   Format strings contain two types of objects--plain characters and format
  1052.   specifiers. Plain characters are copied verbatim to the resulting string.
  1053.   Format specifiers fetch arguments from the argument list and apply
  1054.   formatting to them.
  1055.  
  1056.   Format specifiers have the following form:
  1057.  
  1058.     "%" [index ":"] ["-"] [width] ["." prec] type
  1059.  
  1060.   A format specifier begins with a % character. After the % come the
  1061.   following, in this order:
  1062.  
  1063.   -  an optional argument index specifier, [index ":"]
  1064.   -  an optional left-justification indicator, ["-"]
  1065.   -  an optional width specifier, [width]
  1066.   -  an optional precision specifier, ["." prec]
  1067.   -  the conversion type character, type
  1068.  
  1069.   The following conversion characters are supported:
  1070.  
  1071.   d  Decimal. The argument must be an integer value. The value is converted
  1072.      to a string of decimal digits. If the format string contains a precision
  1073.      specifier, it indicates that the resulting string must contain at least
  1074.      the specified number of digits; if the value has less digits, the
  1075.      resulting string is left-padded with zeros.
  1076.  
  1077.   u  Unsigned decimal.  Similar to 'd' but no sign is output.
  1078.  
  1079.   e  Scientific. The argument must be a floating-point value. The value is
  1080.      converted to a string of the form "-d.ddd...E+ddd". The resulting
  1081.      string starts with a minus sign if the number is negative, and one digit
  1082.      always precedes the decimal point. The total number of digits in the
  1083.      resulting string (including the one before the decimal point) is given
  1084.      by the precision specifer in the format string--a default precision of
  1085.      15 is assumed if no precision specifer is present. The "E" exponent
  1086.      character in the resulting string is always followed by a plus or minus
  1087.      sign and at least three digits.
  1088.  
  1089.   f  Fixed. The argument must be a floating-point value. The value is
  1090.      converted to a string of the form "-ddd.ddd...". The resulting string
  1091.      starts with a minus sign if the number is negative. The number of digits
  1092.      after the decimal point is given by the precision specifier in the
  1093.      format string--a default of 2 decimal digits is assumed if no precision
  1094.      specifier is present.
  1095.  
  1096.   g  General. The argument must be a floating-point value. The value is
  1097.      converted to the shortest possible decimal string using fixed or
  1098.      scientific format. The number of significant digits in the resulting
  1099.      string is given by the precision specifier in the format string--a
  1100.      default precision of 15 is assumed if no precision specifier is present.
  1101.      Trailing zeros are removed from the resulting string, and a decimal
  1102.      point appears only if necessary. The resulting string uses fixed point
  1103.      format if the number of digits to the left of the decimal point in the
  1104.      value is less than or equal to the specified precision, and if the
  1105.      value is greater than or equal to 0.00001. Otherwise the resulting
  1106.      string uses scientific format.
  1107.  
  1108.   n  Number. The argument must be a floating-point value. The value is
  1109.      converted to a string of the form "-d,ddd,ddd.ddd...". The "n" format
  1110.      corresponds to the "f" format, except that the resulting string
  1111.      contains thousand separators.
  1112.  
  1113.   m  Money. The argument must be a floating-point value. The value is
  1114.      converted to a string that represents a currency amount. The conversion
  1115.      is controlled by the CurrencyString, CurrencyFormat, NegCurrFormat,
  1116.      ThousandSeparator, DecimalSeparator, and CurrencyDecimals global
  1117.      variables, all of which are initialized from the Currency Format in
  1118.      the International section of the Windows Control Panel. If the format
  1119.      string contains a precision specifier, it overrides the value given
  1120.      by the CurrencyDecimals global variable.
  1121.  
  1122.   p  Pointer. The argument must be a pointer value. The value is converted
  1123.      to a string of the form "XXXX:YYYY" where XXXX and YYYY are the
  1124.      segment and offset parts of the pointer expressed as four hexadecimal
  1125.      digits.
  1126.  
  1127.   s  String. The argument must be a character, a string, or a PChar value.
  1128.      The string or character is inserted in place of the format specifier.
  1129.      The precision specifier, if present in the format string, specifies the
  1130.      maximum length of the resulting string. If the argument is a string
  1131.      that is longer than this maximum, the string is truncated.
  1132.  
  1133.   x  Hexadecimal. The argument must be an integer value. The value is
  1134.      converted to a string of hexadecimal digits. If the format string
  1135.      contains a precision specifier, it indicates that the resulting string
  1136.      must contain at least the specified number of digits; if the value has
  1137.      less digits, the resulting string is left-padded with zeros.
  1138.  
  1139.   Conversion characters may be specified in upper case as well as in lower
  1140.   case--both produce the same results.
  1141.  
  1142.   For all floating-point formats, the actual characters used as decimal and
  1143.   thousand separators are obtained from the DecimalSeparator and
  1144.   ThousandSeparator global variables.
  1145.  
  1146.   Index, width, and precision specifiers can be specified directly using
  1147.   decimal digit string (for example "%10d"), or indirectly using an asterisk
  1148.   charcater (for example "%*.*f"). When using an asterisk, the next argument
  1149.   in the argument list (which must be an integer value) becomes the value
  1150.   that is actually used. For example "Format('%*.*f', [8, 2, 123.456])" is
  1151.   the same as "Format('%8.2f', [123.456])".
  1152.  
  1153.   A width specifier sets the minimum field width for a conversion. If the
  1154.   resulting string is shorter than the minimum field width, it is padded
  1155.   with blanks to increase the field width. The default is to right-justify
  1156.   the result by adding blanks in front of the value, but if the format
  1157.   specifier contains a left-justification indicator (a "-" character
  1158.   preceding the width specifier), the result is left-justified by adding
  1159.   blanks after the value.
  1160.  
  1161.   An index specifier sets the current argument list index to the specified
  1162.   value. The index of the first argument in the argument list is 0. Using
  1163.   index specifiers, it is possible to format the same argument multiple
  1164.   times. For example "Format('%d %d %0:d %d', [10, 20])" produces the string
  1165.   '10 20 10 20'.
  1166.  
  1167.   The Format function can be combined with other formatting functions. For
  1168.   example
  1169.  
  1170.     S := Format('Your total was %s on %s', [
  1171.       FormatFloat('$#,##0.00;;zero', Total),
  1172.       FormatDateTime('mm/dd/yy', Date)]);
  1173.  
  1174.   which uses the FormatFloat and FormatDateTime functions to customize the
  1175.   format beyond what is possible with Format. }
  1176.  
  1177. function Format(const Format: string; const Args: array of const): string;
  1178.  
  1179. { FmtStr formats the argument list given by Args using the format string
  1180.   given by Format into the string variable given by Result. For further
  1181.   details, see the description of the Format function. }
  1182.  
  1183. procedure FmtStr(var Result: string; const Format: string;
  1184.   const Args: array of const);
  1185.  
  1186. { StrFmt formats the argument list given by Args using the format string
  1187.   given by Format into the buffer given by Buffer. It is up to the caller to
  1188.   ensure that Buffer is large enough for the resulting string. The returned
  1189.   value is Buffer. For further details, see the description of the Format
  1190.   function. }
  1191.  
  1192. function StrFmt(Buffer, Format: PChar; const Args: array of const): PChar;
  1193.  
  1194. { StrFmt formats the argument list given by Args using the format string
  1195.   given by Format into the buffer given by Buffer. The resulting string will
  1196.   contain no more than MaxLen characters, not including the null terminator.
  1197.   The returned value is Buffer. For further details, see the description of
  1198.   the Format function. }
  1199.  
  1200. function StrLFmt(Buffer: PChar; MaxLen: Cardinal; Format: PChar;
  1201.   const Args: array of const): PChar;
  1202.  
  1203. { FormatBuf formats the argument list given by Args using the format string
  1204.   given by Format and FmtLen into the buffer given by Buffer and BufLen.
  1205.   The Format parameter is a reference to a buffer containing FmtLen
  1206.   characters, and the Buffer parameter is a reference to a buffer of BufLen
  1207.   characters. The returned value is the number of characters actually stored
  1208.   in Buffer. The returned value is always less than or equal to BufLen. For
  1209.   further details, see the description of the Format function. }
  1210.  
  1211. function FormatBuf(var Buffer; BufLen: Cardinal; const Format;
  1212.   FmtLen: Cardinal; const Args: array of const): Cardinal;
  1213.  
  1214. { Floating point conversion routines }
  1215.  
  1216. { FloatToStr converts the floating-point value given by Value to its string
  1217.   representation. The conversion uses general number format with 15
  1218.   significant digits. For further details, see the description of the
  1219.   FloatToStrF function. }
  1220.  
  1221. function FloatToStr(Value: Extended): string;
  1222.  
  1223. { CurrToStr converts the currency value given by Value to its string
  1224.   representation. The conversion uses general number format. For further
  1225.   details, see the description of the CurrToStrF function. }
  1226.  
  1227. function CurrToStr(Value: Currency): string;
  1228.  
  1229. { FloatToStrF converts the floating-point value given by Value to its string
  1230.   representation. The Format parameter controls the format of the resulting
  1231.   string. The Precision parameter specifies the precision of the given value.
  1232.   It should be 7 or less for values of type Single, 15 or less for values of
  1233.   type Double, and 18 or less for values of type Extended. The meaning of the
  1234.   Digits parameter depends on the particular format selected.
  1235.  
  1236.   The possible values of the Format parameter, and the meaning of each, are
  1237.   described below.
  1238.  
  1239.   ffGeneral - General number format. The value is converted to the shortest
  1240.   possible decimal string using fixed or scientific format. Trailing zeros
  1241.   are removed from the resulting string, and a decimal point appears only
  1242.   if necessary. The resulting string uses fixed point format if the number
  1243.   of digits to the left of the decimal point in the value is less than or
  1244.   equal to the specified precision, and if the value is greater than or
  1245.   equal to 0.00001. Otherwise the resulting string uses scientific format,
  1246.   and the Digits parameter specifies the minimum number of digits in the
  1247.   exponent (between 0 and 4).
  1248.  
  1249.   ffExponent - Scientific format. The value is converted to a string of the
  1250.   form "-d.ddd...E+dddd". The resulting string starts with a minus sign if
  1251.   the number is negative, and one digit always precedes the decimal point.
  1252.   The total number of digits in the resulting string (including the one
  1253.   before the decimal point) is given by the Precision parameter. The "E"
  1254.   exponent character in the resulting string is always followed by a plus
  1255.   or minus sign and up to four digits. The Digits parameter specifies the
  1256.   minimum number of digits in the exponent (between 0 and 4).
  1257.  
  1258.   ffFixed - Fixed point format. The value is converted to a string of the
  1259.   form "-ddd.ddd...". The resulting string starts with a minus sign if the
  1260.   number is negative, and at least one digit always precedes the decimal
  1261.   point. The number of digits after the decimal point is given by the Digits
  1262.   parameter--it must be between 0 and 18. If the number of digits to the
  1263.   left of the decimal point is greater than the specified precision, the
  1264.   resulting value will use scientific format.
  1265.  
  1266.   ffNumber - Number format. The value is converted to a string of the form
  1267.   "-d,ddd,ddd.ddd...". The ffNumber format corresponds to the ffFixed format,
  1268.   except that the resulting string contains thousand separators.
  1269.  
  1270.   ffCurrency - Currency format. The value is converted to a string that
  1271.   represents a currency amount. The conversion is controlled by the
  1272.   CurrencyString, CurrencyFormat, NegCurrFormat, ThousandSeparator, and
  1273.   DecimalSeparator global variables, all of which are initialized from the
  1274.   Currency Format in the International section of the Windows Control Panel.
  1275.   The number of digits after the decimal point is given by the Digits
  1276.   parameter--it must be between 0 and 18.
  1277.  
  1278.   For all formats, the actual characters used as decimal and thousand
  1279.   separators are obtained from the DecimalSeparator and ThousandSeparator
  1280.   global variables.
  1281.  
  1282.   If the given value is a NAN (not-a-number), the resulting string is 'NAN'.
  1283.   If the given value is positive infinity, the resulting string is 'INF'. If
  1284.   the given value is negative infinity, the resulting string is '-INF'. }
  1285.  
  1286. function FloatToStrF(Value: Extended; Format: TFloatFormat;
  1287.   Precision, Digits: Integer): string;
  1288.  
  1289. { CurrToStrF converts the currency value given by Value to its string
  1290.   representation. A call to CurrToStrF corresponds to a call to
  1291.   FloatToStrF with an implied precision of 19 digits. }
  1292.  
  1293. function CurrToStrF(Value: Currency; Format: TFloatFormat;
  1294.   Digits: Integer): string;
  1295.  
  1296. { FloatToText converts the given floating-point value to its decimal
  1297.   representation using the specified format, precision, and digits. The
  1298.   Value parameter must be a variable of type Extended or Currency, as
  1299.   indicated by the ValueType parameter. The resulting string of characters
  1300.   is stored in the given buffer, and the returned value is the number of
  1301.   characters stored. The resulting string is not null-terminated. For
  1302.   further details, see the description of the FloatToStrF function. }
  1303.  
  1304. function FloatToText(Buffer: PChar; const Value; ValueType: TFloatValue;
  1305.   Format: TFloatFormat; Precision, Digits: Integer): Integer;
  1306.  
  1307. { FormatFloat formats the floating-point value given by Value using the
  1308.   format string given by Format. The following format specifiers are
  1309.   supported in the format string:
  1310.  
  1311.   0     Digit placeholder. If the value being formatted has a digit in the
  1312.         position where the '0' appears in the format string, then that digit
  1313.         is copied to the output string. Otherwise, a '0' is stored in that
  1314.         position in the output string.
  1315.  
  1316.   #     Digit placeholder. If the value being formatted has a digit in the
  1317.         position where the '#' appears in the format string, then that digit
  1318.         is copied to the output string. Otherwise, nothing is stored in that
  1319.         position in the output string.
  1320.  
  1321.   .     Decimal point. The first '.' character in the format string
  1322.         determines the location of the decimal separator in the formatted
  1323.         value; any additional '.' characters are ignored. The actual
  1324.         character used as a the decimal separator in the output string is
  1325.         determined by the DecimalSeparator global variable. The default value
  1326.         of DecimalSeparator is specified in the Number Format of the
  1327.         International section in the Windows Control Panel.
  1328.  
  1329.   ,     Thousand separator. If the format string contains one or more ','
  1330.         characters, the output will have thousand separators inserted between
  1331.         each group of three digits to the left of the decimal point. The
  1332.         placement and number of ',' characters in the format string does not
  1333.         affect the output, except to indicate that thousand separators are
  1334.         wanted. The actual character used as a the thousand separator in the
  1335.         output is determined by the ThousandSeparator global variable. The
  1336.         default value of ThousandSeparator is specified in the Number Format
  1337.         of the International section in the Windows Control Panel.
  1338.  
  1339.   E+    Scientific notation. If any of the strings 'E+', 'E-', 'e+', or 'e-'
  1340.   E-    are contained in the format string, the number is formatted using
  1341.   e+    scientific notation. A group of up to four '0' characters can
  1342.   e-    immediately follow the 'E+', 'E-', 'e+', or 'e-' to determine the
  1343.         minimum number of digits in the exponent. The 'E+' and 'e+' formats
  1344.         cause a plus sign to be output for positive exponents and a minus
  1345.         sign to be output for negative exponents. The 'E-' and 'e-' formats
  1346.         output a sign character only for negative exponents.
  1347.  
  1348.   'xx'  Characters enclosed in single or double quotes are output as-is, and
  1349.   "xx"  do not affect formatting.
  1350.  
  1351.   ;     Separates sections for positive, negative, and zero numbers in the
  1352.         format string.
  1353.  
  1354.   The locations of the leftmost '0' before the decimal point in the format
  1355.   string and the rightmost '0' after the decimal point in the format string
  1356.   determine the range of digits that are always present in the output string.
  1357.  
  1358.   The number being formatted is always rounded to as many decimal places as
  1359.   there are digit placeholders ('0' or '#') to the right of the decimal
  1360.   point. If the format string contains no decimal point, the value being
  1361.   formatted is rounded to the nearest whole number.
  1362.  
  1363.   If the number being formatted has more digits to the left of the decimal
  1364.   separator than there are digit placeholders to the left of the '.'
  1365.   character in the format string, the extra digits are output before the
  1366.   first digit placeholder.
  1367.  
  1368.   To allow different formats for positive, negative, and zero values, the
  1369.   format string can contain between one and three sections separated by
  1370.   semicolons.
  1371.  
  1372.   One section - The format string applies to all values.
  1373.  
  1374.   Two sections - The first section applies to positive values and zeros, and
  1375.   the second section applies to negative values.
  1376.  
  1377.   Three sections - The first section applies to positive values, the second
  1378.   applies to negative values, and the third applies to zeros.
  1379.  
  1380.   If the section for negative values or the section for zero values is empty,
  1381.   that is if there is nothing between the semicolons that delimit the
  1382.   section, the section for positive values is used instead.
  1383.  
  1384.   If the section for positive values is empty, or if the entire format string
  1385.   is empty, the value is formatted using general floating-point formatting
  1386.   with 15 significant digits, corresponding to a call to FloatToStrF with
  1387.   the ffGeneral format. General floating-point formatting is also used if
  1388.   the value has more than 18 digits to the left of the decimal point and
  1389.   the format string does not specify scientific notation.
  1390.  
  1391.   The table below shows some sample formats and the results produced when
  1392.   the formats are applied to different values:
  1393.  
  1394.   Format string          1234        -1234       0.5         0
  1395.   -----------------------------------------------------------------------
  1396.                          1234        -1234       0.5         0
  1397.   0                      1234        -1234       1           0
  1398.   0.00                   1234.00     -1234.00    0.50        0.00
  1399.   #.##                   1234        -1234       .5
  1400.   #,##0.00               1,234.00    -1,234.00   0.50        0.00
  1401.   #,##0.00;(#,##0.00)    1,234.00    (1,234.00)  0.50        0.00
  1402.   #,##0.00;;Zero         1,234.00    -1,234.00   0.50        Zero
  1403.   0.000E+00              1.234E+03   -1.234E+03  5.000E-01   0.000E+00
  1404.   #.###E-0               1.234E3     -1.234E3    5E-1        0E0
  1405.   ----------------------------------------------------------------------- }
  1406.  
  1407. function FormatFloat(const Format: string; Value: Extended): string;
  1408.  
  1409. { FormatCurr formats the currency value given by Value using the format
  1410.   string given by Format. For further details, see the description of the
  1411.   FormatFloat function. }
  1412.  
  1413. function FormatCurr(const Format: string; Value: Currency): string;
  1414.  
  1415. { FloatToTextFmt converts the given floating-point value to its decimal
  1416.   representation using the specified format. The Value parameter must be a
  1417.   variable of type Extended or Currency, as indicated by the ValueType
  1418.   parameter. The resulting string of characters is stored in the given
  1419.   buffer, and the returned value is the number of characters stored. The
  1420.   resulting string is not null-terminated. For further details, see the
  1421.   description of the FormatFloat function. }
  1422.  
  1423. function FloatToTextFmt(Buffer: PChar; const Value; ValueType: TFloatValue;
  1424.   Format: PChar): Integer;
  1425.  
  1426. { StrToFloat converts the given string to a floating-point value. The string
  1427.   must consist of an optional sign (+ or -), a string of digits with an
  1428.   optional decimal point, and an optional 'E' or 'e' followed by a signed
  1429.   integer. Leading and trailing blanks in the string are ignored. The
  1430.   DecimalSeparator global variable defines the character that must be used
  1431.   as a decimal point. Thousand separators and currency symbols are not
  1432.   allowed in the string. If the string doesn't contain a valid value, an
  1433.   EConvertError exception is raised. }
  1434.  
  1435. function StrToFloat(const S: string): Extended;
  1436.  
  1437. { StrToCurr converts the given string to a currency value. For further
  1438.   details, see the description of the StrToFloat function. }
  1439.  
  1440. function StrToCurr(const S: string): Currency;
  1441.  
  1442. { TextToFloat converts the null-terminated string given by Buffer to a
  1443.   floating-point value which is returned in the variable given by Value.
  1444.   The Value parameter must be a variable of type Extended or Currency, as
  1445.   indicated by the ValueType parameter. The return value is True if the
  1446.   conversion was successful, or False if the string is not a valid
  1447.   floating-point value. For further details, see the description of the
  1448.   StrToFloat function. }
  1449.  
  1450. function TextToFloat(Buffer: PChar; var Value;
  1451.   ValueType: TFloatValue): Boolean;
  1452.  
  1453. { FloatToDecimal converts a floating-point value to a decimal representation
  1454.   that is suited for further formatting. The Value parameter must be a
  1455.   variable of type Extended or Currency, as indicated by the ValueType
  1456.   parameter. For values of type Extended, the Precision parameter specifies
  1457.   the requested number of significant digits in the result--the allowed range
  1458.   is 1..18. For values of type Currency, the Precision parameter is ignored,
  1459.   and the implied precision of the conversion is 19 digits. The Decimals
  1460.   parameter specifies the requested maximum number of digits to the left of
  1461.   the decimal point in the result. Precision and Decimals together control
  1462.   how the result is rounded. To produce a result that always has a given
  1463.   number of significant digits regardless of the magnitude of the number,
  1464.   specify 9999 for the Decimals parameter. The result of the conversion is
  1465.   stored in the specified TFloatRec record as follows:
  1466.  
  1467.   Exponent - Contains the magnitude of the number, i.e. the number of
  1468.   significant digits to the right of the decimal point. The Exponent field
  1469.   is negative if the absolute value of the number is less than one. If the
  1470.   number is a NAN (not-a-number), Exponent is set to -32768. If the number
  1471.   is INF or -INF (positive or negative infinity), Exponent is set to 32767.
  1472.  
  1473.   Negative - True if the number is negative, False if the number is zero
  1474.   or positive.
  1475.  
  1476.   Digits - Contains up to 18 (for type Extended) or 19 (for type Currency)
  1477.   significant digits followed by a null terminator. The implied decimal
  1478.   point (if any) is not stored in Digits. Trailing zeros are removed, and
  1479.   if the resulting number is zero, NAN, or INF, Digits contains nothing but
  1480.   the null terminator. }
  1481.  
  1482. procedure FloatToDecimal(var Result: TFloatRec; const Value;
  1483.   ValueType: TFloatValue; Precision, Decimals: Integer);
  1484.  
  1485. { Date/time support routines }
  1486.  
  1487. function DateTimeToTimeStamp(DateTime: TDateTime): TTimeStamp;
  1488.  
  1489. function TimeStampToDateTime(const TimeStamp: TTimeStamp): TDateTime;
  1490. function MSecsToTimeStamp(MSecs: Comp): TTimeStamp;
  1491. function TimeStampToMSecs(const TimeStamp: TTimeStamp): Comp;
  1492.  
  1493. { EncodeDate encodes the given year, month, and day into a TDateTime value.
  1494.   The year must be between 1 and 9999, the month must be between 1 and 12,
  1495.   and the day must be between 1 and N, where N is the number of days in the
  1496.   specified month. If the specified values are not within range, an
  1497.   EConvertError exception is raised. The resulting value is the number of
  1498.   days between 12/30/1899 and the given date. }
  1499.  
  1500. function EncodeDate(Year, Month, Day: Word): TDateTime;
  1501.  
  1502. { EncodeTime encodes the given hour, minute, second, and millisecond into a
  1503.   TDateTime value. The hour must be between 0 and 23, the minute must be
  1504.   between 0 and 59, the second must be between 0 and 59, and the millisecond
  1505.   must be between 0 and 999. If the specified values are not within range, an
  1506.   EConvertError exception is raised. The resulting value is a number between
  1507.   0 (inclusive) and 1 (not inclusive) that indicates the fractional part of
  1508.   a day given by the specified time. The value 0 corresponds to midnight,
  1509.   0.5 corresponds to noon, 0.75 corresponds to 6:00 pm, etc. }
  1510.  
  1511. function EncodeTime(Hour, Min, Sec, MSec: Word): TDateTime;
  1512.  
  1513. { DecodeDate decodes the integral (date) part of the given TDateTime value
  1514.   into its corresponding year, month, and day. If the given TDateTime value
  1515.   is less than or equal to zero, the year, month, and day return parameters
  1516.   are all set to zero. }
  1517.  
  1518. procedure DecodeDate(Date: TDateTime; var Year, Month, Day: Word);
  1519.  
  1520. { DecodeTime decodes the fractional (time) part of the given TDateTime value
  1521.   into its corresponding hour, minute, second, and millisecond. }
  1522.  
  1523. procedure DecodeTime(Time: TDateTime; var Hour, Min, Sec, MSec: Word);
  1524.  
  1525. { DateTimeToSystemTime converts a date and time from Delphi's TDateTime
  1526.   format into the Win32 API's TSystemTime format. }
  1527.  
  1528. procedure DateTimeToSystemTime(DateTime: TDateTime; var SystemTime: TSystemTime);
  1529.  
  1530. { SystemTimeToDateTime converts a date and time from the Win32 API's
  1531.   TSystemTime format into Delphi's TDateTime format. }
  1532.  
  1533. function SystemTimeToDateTime(const SystemTime: TSystemTime): TDateTime;
  1534.  
  1535. { DayOfWeek returns the day of the week of the given date. The result is an
  1536.   integer between 1 and 7, corresponding to Sunday through Saturday. }
  1537.  
  1538. function DayOfWeek(Date: TDateTime): Integer;
  1539.  
  1540. { Date returns the current date. }
  1541.  
  1542. function Date: TDateTime;
  1543.  
  1544. { Time returns the current time. }
  1545.  
  1546. function Time: TDateTime;
  1547.  
  1548. { Now returns the current date and time, corresponding to Date + Time. }
  1549.  
  1550. function Now: TDateTime;
  1551.  
  1552. { IncMonth returns Date shifted by the specified number of months.
  1553.   NumberOfMonths parameter can be negative, to return a date N months ago.
  1554.   If the input day of month is greater than the last day of the resulting
  1555.   month, the day is set to the last day of the resulting month.
  1556.   Input time of day is copied to the DateTime result.  }
  1557.  
  1558. function IncMonth(const Date: TDateTime; NumberOfMonths: Integer): TDateTime;
  1559.  
  1560. { IsLeapYear determines whether the given year is a leap year. }
  1561.  
  1562. function IsLeapYear(Year: Word): Boolean;
  1563.  
  1564. type
  1565.   PDayTable = ^TDayTable;
  1566.   TDayTable = array[1..12] of Word;
  1567.  
  1568. { The MonthDays array can be used to quickly find the number of
  1569.   days in a month:  MonthDays[IsLeapYear(Y), M]      }
  1570.  
  1571. const
  1572.   MonthDays: array [Boolean] of TDayTable =
  1573.     ((31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31),
  1574.      (31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31));
  1575.  
  1576. { DateToStr converts the date part of the given TDateTime value to a string.
  1577.   The conversion uses the format specified by the ShortDateFormat global
  1578.   variable. }
  1579.  
  1580. function DateToStr(Date: TDateTime): string;
  1581.  
  1582. { TimeToStr converts the time part of the given TDateTime value to a string.
  1583.   The conversion uses the format specified by the LongTimeFormat global
  1584.   variable. }
  1585.  
  1586. function TimeToStr(Time: TDateTime): string;
  1587.  
  1588. { DateTimeToStr converts the given date and time to a string. The resulting
  1589.   string consists of a date and time formatted using the ShortDateFormat and
  1590.   LongTimeFormat global variables. Time information is included in the
  1591.   resulting string only if the fractional part of the given date and time
  1592.   value is non-zero. }
  1593.  
  1594. function DateTimeToStr(DateTime: TDateTime): string;
  1595.  
  1596. { StrToDate converts the given string to a date value. The string must
  1597.   consist of two or three numbers, separated by the character defined by
  1598.   the DateSeparator global variable. The order for month, day, and year is
  1599.   determined by the ShortDateFormat global variable--possible combinations
  1600.   are m/d/y, d/m/y, and y/m/d. If the string contains only two numbers, it
  1601.   is interpreted as a date (m/d or d/m) in the current year. Year values
  1602.   between 0 and 99 are assumed to be in the current century. If the given
  1603.   string does not contain a valid date, an EConvertError exception is
  1604.   raised. }
  1605.  
  1606. function StrToDate(const S: string): TDateTime;
  1607.  
  1608. { StrToTime converts the given string to a time value. The string must
  1609.   consist of two or three numbers, separated by the character defined by
  1610.   the TimeSeparator global variable, optionally followed by an AM or PM
  1611.   indicator. The numbers represent hour, minute, and (optionally) second,
  1612.   in that order. If the time is followed by AM or PM, it is assumed to be
  1613.   in 12-hour clock format. If no AM or PM indicator is included, the time
  1614.   is assumed to be in 24-hour clock format. If the given string does not
  1615.   contain a valid time, an EConvertError exception is raised. }
  1616.  
  1617. function StrToTime(const S: string): TDateTime;
  1618.  
  1619. { StrToDateTime converts the given string to a date and time value. The
  1620.   string must contain a date optionally followed by a time. The date and
  1621.   time parts of the string must follow the formats described for the
  1622.   StrToDate and StrToTime functions. }
  1623.  
  1624. function StrToDateTime(const S: string): TDateTime;
  1625.  
  1626. { FormatDateTime formats the date-and-time value given by DateTime using the
  1627.   format given by Format. The following format specifiers are supported:
  1628.  
  1629.   c       Displays the date using the format given by the ShortDateFormat
  1630.           global variable, followed by the time using the format given by
  1631.           the LongTimeFormat global variable. The time is not displayed if
  1632.           the fractional part of the DateTime value is zero.
  1633.  
  1634.   d       Displays the day as a number without a leading zero (1-31).
  1635.  
  1636.   dd      Displays the day as a number with a leading zero (01-31).
  1637.  
  1638.   ddd     Displays the day as an abbreviation (Sun-Sat) using the strings
  1639.           given by the ShortDayNames global variable.
  1640.  
  1641.   dddd    Displays the day as a full name (Sunday-Saturday) using the strings
  1642.           given by the LongDayNames global variable.
  1643.  
  1644.   ddddd   Displays the date using the format given by the ShortDateFormat
  1645.           global variable.
  1646.  
  1647.   dddddd  Displays the date using the format given by the LongDateFormat
  1648.           global variable.
  1649.  
  1650.   g       Displays the period/era as an abbreviation (Japanese and
  1651.           Taiwanese locales only).
  1652.  
  1653.   gg      Displays the period/era as a full name.
  1654.  
  1655.   e       Displays the year in the current period/era as a number without
  1656.           a leading zero (Japanese, Korean and Taiwanese locales only).
  1657.  
  1658.   ee      Displays the year in the current period/era as a number with
  1659.           a leading zero (Japanese, Korean and Taiwanese locales only).
  1660.  
  1661.   m       Displays the month as a number without a leading zero (1-12). If
  1662.           the m specifier immediately follows an h or hh specifier, the
  1663.           minute rather than the month is displayed.
  1664.  
  1665.   mm      Displays the month as a number with a leading zero (01-12). If
  1666.           the mm specifier immediately follows an h or hh specifier, the
  1667.           minute rather than the month is displayed.
  1668.  
  1669.   mmm     Displays the month as an abbreviation (Jan-Dec) using the strings
  1670.           given by the ShortMonthNames global variable.
  1671.  
  1672.   mmmm    Displays the month as a full name (January-December) using the
  1673.           strings given by the LongMonthNames global variable.
  1674.  
  1675.   yy      Displays the year as a two-digit number (00-99).
  1676.  
  1677.   yyyy    Displays the year as a four-digit number (0000-9999).
  1678.  
  1679.   h       Displays the hour without a leading zero (0-23).
  1680.  
  1681.   hh      Displays the hour with a leading zero (00-23).
  1682.  
  1683.   n       Displays the minute without a leading zero (0-59).
  1684.  
  1685.   nn      Displays the minute with a leading zero (00-59).
  1686.  
  1687.   s       Displays the second without a leading zero (0-59).
  1688.  
  1689.   ss      Displays the second with a leading zero (00-59).
  1690.  
  1691.   t       Displays the time using the format given by the ShortTimeFormat
  1692.           global variable.
  1693.  
  1694.   tt      Displays the time using the format given by the LongTimeFormat
  1695.           global variable.
  1696.  
  1697.   am/pm   Uses the 12-hour clock for the preceding h or hh specifier, and
  1698.           displays 'am' for any hour before noon, and 'pm' for any hour
  1699.           after noon. The am/pm specifier can use lower, upper, or mixed
  1700.           case, and the result is displayed accordingly.
  1701.  
  1702.   a/p     Uses the 12-hour clock for the preceding h or hh specifier, and
  1703.           displays 'a' for any hour before noon, and 'p' for any hour after
  1704.           noon. The a/p specifier can use lower, upper, or mixed case, and
  1705.           the result is displayed accordingly.
  1706.  
  1707.   ampm    Uses the 12-hour clock for the preceding h or hh specifier, and
  1708.           displays the contents of the TimeAMString global variable for any
  1709.           hour before noon, and the contents of the TimePMString global
  1710.           variable for any hour after noon.
  1711.  
  1712.   /       Displays the date separator character given by the DateSeparator
  1713.           global variable.
  1714.  
  1715.   :       Displays the time separator character given by the TimeSeparator
  1716.           global variable.
  1717.  
  1718.   'xx'    Characters enclosed in single or double quotes are displayed as-is,
  1719.   "xx"    and do not affect formatting.
  1720.  
  1721.   Format specifiers may be written in upper case as well as in lower case
  1722.   letters--both produce the same result.
  1723.  
  1724.   If the string given by the Format parameter is empty, the date and time
  1725.   value is formatted as if a 'c' format specifier had been given.
  1726.  
  1727.   The following example:
  1728.  
  1729.     S := FormatDateTime('"The meeting is on" dddd, mmmm d, yyyy, ' +
  1730.       '"at" hh:mm AM/PM', StrToDateTime('2/15/95 10:30am'));
  1731.  
  1732.   assigns 'The meeting is on Wednesday, February 15, 1995 at 10:30 AM' to
  1733.   the string variable S. }
  1734.  
  1735. function FormatDateTime(const Format: string; DateTime: TDateTime): string;
  1736.  
  1737. { DateTimeToString converts the date and time value given by DateTime using
  1738.   the format string given by Format into the string variable given by Result.
  1739.   For further details, see the description of the FormatDateTime function. }
  1740.  
  1741. procedure DateTimeToString(var Result: string; const Format: string;
  1742.   DateTime: TDateTime);
  1743.  
  1744. { System error messages }
  1745.  
  1746. function SysErrorMessage(ErrorCode: Integer): string;
  1747.  
  1748. { Initialization file support }
  1749.  
  1750. function GetLocaleStr(Locale, LocaleType: Integer; const Default: string): string;
  1751. function GetLocaleChar(Locale, LocaleType: Integer; Default: Char): Char;
  1752.  
  1753. { GetFormatSettings resets all date and number format variables to their
  1754.   default values. }
  1755.  
  1756. procedure GetFormatSettings;
  1757.  
  1758. { Exception handling routines }
  1759.  
  1760. function ExceptObject: TObject;
  1761. function ExceptAddr: Pointer;
  1762.  
  1763. function ExceptionErrorMessage(ExceptObject: TObject; ExceptAddr: Pointer;
  1764.   Buffer: PChar; Size: Integer): Integer;
  1765.  
  1766. procedure ShowException(ExceptObject: TObject; ExceptAddr: Pointer);
  1767.  
  1768. procedure Abort;
  1769.  
  1770. procedure OutOfMemoryError;
  1771.  
  1772. procedure Beep;
  1773.  
  1774. { MBCS functions }
  1775.  
  1776. { LeadBytes is a char set that indicates which char values are lead bytes
  1777.   in multibyte character sets (Japanese, Chinese, etc).
  1778.   This set is always empty for western locales. }
  1779. var
  1780.   LeadBytes: set of Char = [];
  1781.  
  1782. { ByteType indicates what kind of byte exists at the Index'th byte in S.
  1783.   Western locales always return mbSingleByte.  Far East multibyte locales
  1784.   may also return mbLeadByte, indicating the byte is the first in a multibyte
  1785.   character sequence, and mbTrailByte, indicating that the byte is the second
  1786.   in a multibyte character sequence.  Parameters are assumed to be valid. }
  1787.  
  1788. function ByteType(const S: string; Index: Integer): TMbcsByteType;
  1789.  
  1790. { StrByteType works the same as ByteType, but on null-terminated PChar strings }
  1791.  
  1792. function StrByteType(Str: PChar; Index: Cardinal): TMbcsByteType;
  1793.  
  1794. { ByteToCharLen returns the character length of a MBCS string, scanning the
  1795.   string for up to MaxLen bytes.  In multibyte character sets, the number of
  1796.   characters in a string may be less than the number of bytes.  }
  1797.  
  1798. function ByteToCharLen(const S: string; MaxLen: Integer): Integer;
  1799.  
  1800. { CharToByteLen returns the byte length of a MBCS string, scanning the string
  1801.   for up to MaxLen characters. }
  1802.  
  1803. function CharToByteLen(const S: string; MaxLen: Integer): Integer;
  1804.  
  1805. { ByteToCharIndex returns the 1-based character index of the Index'th byte in
  1806.   a MBCS string.  Returns zero if Index is out of range:
  1807.   (Index <= 0) or (Index > Length(S)) }
  1808.  
  1809. function ByteToCharIndex(const S: string; Index: Integer): Integer;
  1810.  
  1811. { CharToByteIndex returns the 1-based byte index of the Index'th character
  1812.   in a MBCS string.  Returns zero if Index or Result are out of range:
  1813.   (Index <= 0) or (Index > Length(S)) or (Result would be > Length(S)) }
  1814.  
  1815. function CharToByteIndex(const S: string; Index: Integer): Integer;
  1816.  
  1817. { IsPathDelimiter returns True if the character at byte S[Index]
  1818.   is '\', and it is not a MBCS lead or trail byte. }
  1819.  
  1820. function IsPathDelimiter(const S: string; Index: Integer): Boolean;
  1821.  
  1822. { IsDelimiter returns True if the character at byte S[Index] matches any
  1823.   character in the Delimiters string, and the character is not a MBCS lead or
  1824.   trail byte.  S may contain multibyte characters; Delimiters must contain
  1825.   only single byte characters. }
  1826.  
  1827. function IsDelimiter(const Delimiters, S: string; Index: Integer): Boolean;
  1828.  
  1829. { LastDelimiter returns the byte index in S of the rightmost whole
  1830.   character that matches any character in Delimiters (except null (#0)).
  1831.   S may contain multibyte characters; Delimiters must contain only single
  1832.   byte non-null characters.
  1833.   Example: LastDelimiter('\.:', 'c:\filename.ext') returns 12. }
  1834.  
  1835. function LastDelimiter(const Delimiters, S: string): Integer;
  1836.  
  1837. { AnsiCompareFileName supports DOS file name comparison idiosyncracies
  1838.   in Far East locales (Zenkaku).  In non-MBCS locales, AnsiCompareFileName
  1839.   is identical to AnsiCompareText.  For general purpose file name comparisions,
  1840.   you should use this function instead of AnsiCompareText. }
  1841.  
  1842. function AnsiCompareFileName(const S1, S2: string): Integer;
  1843.  
  1844. { AnsiLowerCaseFileName supports lowercase conversion idiosyncracies of
  1845.   DOS file names in Far East locales (Zenkaku).  In non-MBCS locales,
  1846.   AnsiLowerCaseFileName is identical to AnsiLowerCase. }
  1847.  
  1848. function AnsiLowerCaseFileName(const S: string): string;
  1849.  
  1850. { AnsiUpperCaseFileName supports uppercase conversion idiosyncracies of
  1851.   DOS file names in Far East locales (Zenkaku).  In non-MBCS locales,
  1852.   AnsiUpperCaseFileName is identical to AnsiUpperCase. }
  1853.  
  1854. function AnsiUpperCaseFileName(const S: string): string;
  1855.  
  1856. { AnsiPos:  Same as Pos but supports MBCS strings }
  1857.  
  1858. function AnsiPos(const Substr, S: string): Integer;
  1859.  
  1860. { AnsiStrPos: Same as StrPos but supports MBCS strings }
  1861.  
  1862. function AnsiStrPos(Str, SubStr: PChar): PChar;
  1863.  
  1864. { AnsiStrRScan: Same as StrRScan but supports MBCS strings }
  1865.  
  1866. function AnsiStrRScan(Str: PChar; Chr: Char): PChar;
  1867.  
  1868. { AnsiStrScan: Same as StrScan but supports MBCS strings }
  1869.  
  1870. function AnsiStrScan(Str: PChar; Chr: Char): PChar;
  1871.  
  1872. { StringReplace replaces occurances of <oldpattern> with <newpattern> in a
  1873.   given string.  Assumes the string may contain Multibyte characters }
  1874.  
  1875. type
  1876.   TReplaceFlags = set of (rfReplaceAll, rfIgnoreCase);
  1877.  
  1878. function StringReplace(const S, OldPattern, NewPattern: string;
  1879.   Flags: TReplaceFlags): string;
  1880.  
  1881. { WrapText will scan a string for BreakChars and insert the BreakStr at the
  1882.   last BreakChar position before MaxCol.  Will not insert a break into an
  1883.   embedded quoted string (both ''' and '"' supported) }
  1884.  
  1885. function WrapText(const Line, BreakStr: string; BreakChars: TSysCharSet;
  1886.   MaxCol: Integer): string;
  1887.  
  1888. { FindCmdLineSwitch determines whether the string in the Switch parameter
  1889.   was passed as a command line argument to the application.  SwitchChars
  1890.   identifies valid argument-delimiter characters (i.e., "-" and "/" are
  1891.   common delimiters). The IgnoreCase paramter controls whether a
  1892.   case-sensistive or case-insensitive search is performed. }
  1893.  
  1894. function FindCmdLineSwitch(const Switch: string; SwitchChars: TSysCharSet;
  1895.   IgnoreCase: Boolean): Boolean;
  1896.  
  1897. { Package support routines }
  1898.  
  1899. { Package Info flags }
  1900.  
  1901. const
  1902.   pfNeverBuild = $00000001;
  1903.   pfDesignOnly = $00000002;
  1904.   pfRunOnly = $00000004;
  1905.   pfIgnoreDupUnits = $00000008;
  1906.   pfModuleTypeMask = $C0000000;
  1907.   pfExeModule = $00000000;
  1908.   pfPackageModule = $40000000;
  1909.   pfProducerMask = $0C000000;
  1910.   pfV3Produced =  $00000000;
  1911.   pfProducerUndefined = $04000000;
  1912.   pfBCB4Produced = $08000000;
  1913.   pfDelphi4Produced = $0C000000;
  1914.   pfLibraryModule = $80000000;
  1915.  
  1916. { Unit info flags }
  1917.  
  1918. const
  1919.   ufMainUnit = $01;
  1920.   ufPackageUnit = $02;
  1921.   ufWeakUnit = $04;
  1922.   ufOrgWeakUnit = $08;
  1923.   ufImplicitUnit = $10;
  1924.  
  1925.   ufWeakPackageUnit = ufPackageUnit or ufWeakUnit;
  1926.  
  1927. { Procedure type of the callback given to GetPackageInfo.  Name is the actual
  1928.   name of the package element.  If IsUnit is True then Name is the name of
  1929.   a contained unit; a required package if False.  Param is the value passed
  1930.   to GetPackageInfo }
  1931.  
  1932. type
  1933.   TNameType = (ntContainsUnit, ntRequiresPackage);
  1934.  
  1935.   TPackageInfoProc = procedure (const Name: string; NameType: TNameType; Flags: Byte; Param: Pointer);
  1936.  
  1937. { LoadPackage loads a given package DLL, checks for duplicate units and
  1938.   calls the initialization blocks of all the contained units }
  1939.  
  1940. function LoadPackage(const Name: string): HMODULE;
  1941.  
  1942. { UnloadPackage does the opposite of LoadPackage by calling the finalization
  1943.   blocks of all contained units, then unloading the package DLL }
  1944.  
  1945. procedure UnloadPackage(Module: HMODULE);
  1946.  
  1947. { GetPackageInfo accesses the given package's info table and enumerates
  1948.   all the contained units and required packages }
  1949.  
  1950. procedure GetPackageInfo(Module: HMODULE; Param: Pointer; var Flags: Integer;
  1951.   InfoProc: TPackageInfoProc);
  1952.  
  1953. { GetPackageDescription loads the description resource from the package
  1954.   library. If the description resource does not exist,
  1955.   an empty string is returned. }
  1956. function GetPackageDescription(ModuleName: PChar): string;
  1957.  
  1958. { InitializePackage Validates and initializes the given package DLL }
  1959.  
  1960. procedure InitializePackage(Module: HMODULE);
  1961.  
  1962. { FinalizePackage finalizes the given package DLL }
  1963.  
  1964. procedure FinalizePackage(Module: HMODULE);
  1965.  
  1966. { RaiseLastWin32Error calls the GetLastError API to retrieve the code for }
  1967. { the last occuring Win32 error.  If GetLastError returns an error code,  }
  1968. { RaiseLastWin32Error then raises an exception with the error code and    }
  1969. { message associated with with error. }
  1970.  
  1971. procedure RaiseLastWin32Error;
  1972.  
  1973. { Win32Check is used to check the return value of a Win32 API function     }
  1974. { which returns a BOOL to indicate success.  If the Win32 API function     }
  1975. { returns False (indicating failure), Win32Check calls RaiseLastWin32Error }
  1976. { to raise an exception.  If the Win32 API function returns True,          }
  1977. { Win32Check returns True. }
  1978.  
  1979. function Win32Check(RetVal: BOOL): BOOL;
  1980.  
  1981. { Termination procedure support }
  1982.  
  1983. type
  1984.   TTerminateProc = function: Boolean;
  1985.  
  1986. { Call AddTerminateProc to add a terminate procedure to the system list of }
  1987. { termination procedures.  Delphi will call all of the function in the     }
  1988. { termination procedure list before an application terminates.  The user-  }
  1989. { defined TermProc function should return True if the application can      }
  1990. { safely terminate or False if the application cannot safely terminate.    }
  1991. { If one of the functions in the termination procedure list returns False, }
  1992. { the application will not terminate. }
  1993.  
  1994. procedure AddTerminateProc(TermProc: TTerminateProc);
  1995.  
  1996. { CallTerminateProcs is called by VCL when an application is about to }
  1997. { terminate.  It returns True only if all of the functions in the     }
  1998. { system's terminate procedure list return True.  This function is    }
  1999. { intended only to be called by Delphi, and it should not be called   }
  2000. { directly. }
  2001.  
  2002. function CallTerminateProcs: Boolean;
  2003.  
  2004. function GDAL: LongWord;
  2005. procedure RCS;
  2006. procedure RPR;
  2007.  
  2008.  
  2009. { HexDisplayPrefix contains the prefix to display on hexadecimal
  2010.   values - '$' for Pascal syntax, '0x' for C++ syntax.  This is
  2011.   for display only - this does not affect the string-to-integer
  2012.   conversion routines. }
  2013. var
  2014.   HexDisplayPrefix: string = '$';
  2015.  
  2016. { The GetDiskFreeSpace Win32 API does not support partitions larger than 2GB
  2017.   under Win95.  A new Win32 function, GetDiskFreeSpaceEx, supports partitions
  2018.   larger than 2GB but only exists on Win NT 4.0 and Win95 OSR2.
  2019.   The GetDiskFreeSpaceEx function pointer variable below will be initialized
  2020.   at startup to point to either the actual OS API function if it exists on
  2021.   the system, or to an internal Delphi function if it does not.  When running
  2022.   on Win95 pre-OSR2, the output of this function will still be limited to
  2023.   the 2GB range reported by Win95, but at least you don't have to worry
  2024.   about which API function to call in code you write.  }
  2025.  
  2026. var
  2027.   GetDiskFreeSpaceEx: function (Directory: PChar; var FreeAvailable,
  2028.     TotalSpace: TLargeInteger; TotalFree: PLargeInteger): Bool stdcall = nil;
  2029.  
  2030. { Thread synchronization }
  2031.  
  2032. { TMultiReadExclusiveWriteSynchronizer minimizes thread serialization to gain
  2033.   read access to a resource shared among threads while still providing complete
  2034.   exclusivity to callers needing write access to the shared resource.
  2035.   (multithread shared reads, single thread exclusive write)
  2036.   Reading is allowed while owning a write lock.
  2037.   Read locks can be promoted to write locks.}
  2038.  
  2039. type
  2040.   TActiveThreadRecord = record
  2041.     ThreadID: Integer;
  2042.     RecursionCount: Integer;
  2043.   end;
  2044.   TActiveThreadArray = array of TActiveThreadRecord;
  2045.  
  2046.   TMultiReadExclusiveWriteSynchronizer = class
  2047.   public
  2048.     constructor Create;
  2049.     destructor Destroy; override;
  2050.     procedure BeginRead;
  2051.     procedure EndRead;
  2052.     procedure BeginWrite;
  2053.     procedure EndWrite;
  2054.   end;
  2055.  
  2056. implementation
  2057.