sysstrh.inc 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  1. {
  2. *********************************************************************
  3. Copyright (C) 1997, 1998 Gertjan Schouten
  4. This program is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 2 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program; if not, write to the Free Software
  14. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  15. *********************************************************************
  16. System Utilities For Free Pascal
  17. }
  18. {==============================================================================}
  19. { standard functions }
  20. {==============================================================================}
  21. type
  22. PString = ^String;
  23. { For FloatToText }
  24. TFloatFormat = (ffGeneral, ffExponent, ffFixed, ffNumber, ffCurrency);
  25. TFloatValue = (fvExtended, fvCurrency, fvSingle, fvReal, fvDouble, fvComp);
  26. TReplaceFlags = set of (rfReplaceAll, rfIgnoreCase);
  27. TFloatRec = Record
  28. Exponent: Integer;
  29. Negative: Boolean;
  30. Digits: Array[0..18] Of Char;
  31. End;
  32. const
  33. { For floattodatetime }
  34. MinDateTime: TDateTime = -657434.0; { 01/01/0100 12:00:00.000 AM }
  35. MaxDateTime: TDateTime = 2958465.99999; { 12/31/9999 11:59:59.999 PM }
  36. {$if defined(FPC_HAS_TYPE_EXTENDED) or defined(FPC_HAS_TYPE_FLOAT128)}
  37. MinCurrency: Currency = -922337203685477.5807;
  38. MaxCurrency: Currency = 922337203685477.5807;
  39. {$else}
  40. MinCurrency: Currency = -922337203685477.0000;
  41. MaxCurrency: Currency = 922337203685477.0000;
  42. {$endif}
  43. Const
  44. LeadBytes: set of Char = [];
  45. EmptyStr : string = '';
  46. NullStr : PString = @EmptyStr;
  47. EmptyWideStr : WideString = '';
  48. // NullWideStr : PWideString = @EmptyWideStr;
  49. function NewStr(const S: string): PString;
  50. procedure DisposeStr(S: PString);
  51. procedure AssignStr(var P: PString; const S: string);
  52. procedure AppendStr(var Dest: String; const S: string);
  53. function UpperCase(const s: string): string;
  54. function LowerCase(const s: string): string; overload;
  55. { the compiler can't decide else if it should use the char or the ansistring
  56. version for a variant }
  57. function LowerCase(const V: variant): string; overload;{$ifdef SYSUTILSINLINE}inline;{$endif}
  58. function CompareStr(const S1, S2: string): Integer;
  59. function CompareMemRange(P1, P2: Pointer; Length: cardinal): integer;
  60. function CompareMem(P1, P2: Pointer; Length: cardinal): Boolean;
  61. function CompareText(const S1, S2: string): integer;
  62. function SameText(const s1,s2:String):Boolean;
  63. function AnsiUpperCase(const s: string): string;{$ifdef SYSUTILSINLINE}inline;{$endif}
  64. function AnsiLowerCase(const s: string): string;{$ifdef SYSUTILSINLINE}inline;{$endif}
  65. function AnsiCompareStr(const S1, S2: string): integer;{$ifdef SYSUTILSINLINE}inline;{$endif}
  66. function AnsiCompareText(const S1, S2: string): integer;{$ifdef SYSUTILSINLINE}inline;{$endif}
  67. function AnsiSameText(const s1,s2:String):Boolean;{$ifdef SYSUTILSINLINE}inline;{$endif}
  68. function AnsiSameStr(const s1,s2:String):Boolean;{$ifdef SYSUTILSINLINE}inline;{$endif}
  69. function AnsiStrComp(S1, S2: PChar): integer;{$ifdef SYSUTILSINLINE}inline;{$endif}
  70. function AnsiStrIComp(S1, S2: PChar): integer;{$ifdef SYSUTILSINLINE}inline;{$endif}
  71. function AnsiStrLComp(S1, S2: PChar; MaxLen: cardinal): integer;{$ifdef SYSUTILSINLINE}inline;{$endif}
  72. function AnsiStrLIComp(S1, S2: PChar; MaxLen: cardinal): integer;{$ifdef SYSUTILSINLINE}inline;{$endif}
  73. function AnsiStrLower(Str: PChar): PChar;{$ifdef SYSUTILSINLINE}inline;{$endif}
  74. function AnsiStrUpper(Str: PChar): PChar;{$ifdef SYSUTILSINLINE}inline;{$endif}
  75. function AnsiLastChar(const S: string): PChar;
  76. function AnsiStrLastChar(Str: PChar): PChar;
  77. function Trim(const S: string): string;
  78. function TrimLeft(const S: string): string;
  79. function TrimRight(const S: string): string;
  80. function QuotedStr(const S: string): string;
  81. function AnsiQuotedStr(const S: string; Quote: char): string;
  82. function AnsiExtractQuotedStr(var Src: PChar; Quote: Char): string;
  83. function AdjustLineBreaks(const S: string): string;
  84. function AdjustLineBreaks(const S: string; Style: TTextLineBreakStyle): string;
  85. function IsValidIdent(const Ident: string): boolean;
  86. function IntToStr(Value: integer): string;
  87. function IntToStr(Value: Int64): string;
  88. function IntToStr(Value: QWord): string;
  89. function IntToHex(Value: integer; Digits: integer): string;
  90. function IntToHex(Value: Int64; Digits: integer): string;
  91. function StrToInt(const s: string): integer;
  92. function TryStrToInt(const s: string; var i : integer) : boolean;
  93. function StrToInt64(const s: string): int64;
  94. function TryStrToInt64(const s: string; var i : int64) : boolean;
  95. function StrToQWord(const s: string): QWord;
  96. function TryStrQWord(const s: string; var i : QWord) : boolean;
  97. function StrToIntDef(const S: string; Default: integer): integer;
  98. function StrToInt64Def(const S: string; Default: int64): int64;
  99. function StrToQWordDef(const S: string; Default: QWord): QWord;
  100. function LoadStr(Ident: integer): string;
  101. // function FmtLoadStr(Ident: integer; const Args: array of const): string;
  102. Function Format (Const Fmt : String; const Args : Array of const) : String;
  103. Function FormatBuf (Var Buffer; BufLen : Cardinal; Const Fmt; fmtLen : Cardinal; Const Args : Array of const) : Cardinal;
  104. Function StrFmt(Buffer,Fmt : PChar; Const args: Array of const) : Pchar;
  105. Function StrLFmt(Buffer : PCHar; Maxlen : Cardinal;Fmt : PChar; Const args: Array of const) : Pchar;
  106. Procedure FmtStr(Var Res: String; Const Fmt : String; Const args: Array of const);
  107. {$ifdef FPC_HAS_TYPE_EXTENDED}
  108. Function FloatToStrF(Value: Extended; format: TFloatFormat; Precision, Digits: Integer): String;
  109. {$endif FPC_HAS_TYPE_EXTENDED}
  110. Function FloatToStrF(Value: Double; format: TFloatFormat; Precision, Digits: Integer): String;
  111. Function FloatToStrF(Value: Single; format: TFloatFormat; Precision, Digits: Integer): String;
  112. Function FloatToStrF(Value: Comp; format: TFloatFormat; Precision, Digits: Integer): String;
  113. Function FloatToStrF(Value: Currency; format: TFloatFormat; Precision, Digits: Integer): String;
  114. {$ifndef FPC_COMP_IS_INT64}
  115. Function FloatToStrF(Value: Int64; format: TFloatFormat; Precision, Digits: Integer): String;
  116. {$endif FPC_COMP_IS_INT64}
  117. Function CurrToStrF(Value: Currency; Format: TFloatFormat; Digits: Integer): string;
  118. {$ifdef FPC_HAS_TYPE_EXTENDED}
  119. Function FloatToStr(Value: Extended): String;
  120. {$endif FPC_HAS_TYPE_EXTENDED}
  121. Function FloatToStr(Value: Double): String;
  122. Function FloatToStr(Value: Single): String;
  123. Function FloatToStr(Value: Currency): String;
  124. Function FloatToStr(Value: Comp): String;
  125. {$ifndef FPC_COMP_IS_INT64}
  126. Function FloatToStr(Value: Int64): String;
  127. {$endif FPC_COMP_IS_INT64}
  128. Function StrToFloat(Const S : String) : Extended;
  129. Function StrToFloatDef(Const S: String; Const Default: Extended): Extended;
  130. Function TryStrToFloat(Const S : String; Var Value: Single): Boolean;
  131. Function TryStrToFloat(Const S : String; Var Value: Double): Boolean;
  132. {$ifdef FPC_HAS_TYPE_EXTENDED}
  133. Function TryStrToFloat(Const S : String; Var Value: Extended): Boolean;
  134. {$endif FPC_HAS_TYPE_EXTENDED}
  135. Function TextToFloat(Buffer: PChar; Var Value: Extended): Boolean;
  136. Function TextToFloat(Buffer: PChar; Var Value; ValueType: TFloatValue): Boolean;
  137. Function FloatToText(Buffer: PChar; Value: Extended; format: TFloatFormat; Precision, Digits: Integer): Longint;
  138. Function FloatToDateTime (Const Value : Extended) : TDateTime;
  139. Function FloattoCurr (Const Value : Extended) : Currency;
  140. function TryFloatToCurr(const Value: Extended; var AResult: Currency): Boolean;
  141. Function CurrToStr(Value: Currency): string;
  142. function StrToCurr(const S: string): Currency;
  143. function TryStrToCurr(const S: string;Var Value : Currency): Boolean;
  144. function StrToCurrDef(const S: string; Default : Currency): Currency;
  145. function StrToBool(const S: string): Boolean;
  146. function BoolToStr(B: Boolean): string;
  147. function LastDelimiter(const Delimiters, S: string): Integer;
  148. function StringReplace(const S, OldPattern, NewPattern: string; Flags: TReplaceFlags): string;
  149. Function FloatToTextFmt(Buffer: PChar; Value: Extended; format: PChar): Integer;
  150. Procedure FloatToDecimal(Var Result: TFloatRec; Value: Extended; Precision, Decimals : integer);
  151. Function FormatFloat(Const Format : String; Value : Extended) : String;
  152. Function IsDelimiter(const Delimiters, S: string; Index: Integer): Boolean;
  153. function FormatCurr(const Format: string; Value: Currency): string;
  154. function SScanf(const s: string; const fmt : string;const Pointers : array of Pointer) : Integer;
  155. {// MBCS Functions. No MBCS yet, so mostly these are calls to the regular counterparts.}
  156. Type
  157. TMbcsByteType = (mbSingleByte, mbLeadByte, mbTrailByte);
  158. Function ByteType(const S: string; Index: Integer): TMbcsByteType;
  159. Function StrByteType(Str: PChar; Index: Cardinal): TMbcsByteType;
  160. Function ByteToCharLen(const S: string; MaxLen: Integer): Integer;
  161. Function CharToByteLen(const S: string; MaxLen: Integer): Integer;
  162. Function ByteToCharIndex(const S: string; Index: Integer): Integer;
  163. Function StrCharLength(const Str: PChar): Integer;
  164. const
  165. {$ifndef unix}
  166. SwitchChars = ['/','-'];
  167. {$else}
  168. SwitchChars = ['-'];
  169. {$endif}
  170. Type
  171. TSysCharSet = Set of char;
  172. PSysCharSet = ^TSysCharSet;
  173. Function FindCmdLineSwitch(const Switch: string; const Chars: TSysCharSet;IgnoreCase: Boolean): Boolean;
  174. Function FindCmdLineSwitch(const Switch: string; IgnoreCase: Boolean): Boolean;
  175. Function FindCmdLineSwitch(const Switch: string): Boolean;
  176. function WrapText(const Line, BreakStr: string; const BreakChars: TSysCharSet; MaxCol: Integer): string;
  177. function WrapText(const Line: string; MaxCol: Integer): string;
  178. {==============================================================================}
  179. { extra functions }
  180. {==============================================================================}
  181. function LeftStr(const S: string; Count: integer): string;
  182. function RightStr(const S: string; Count: integer): string;
  183. function BCDToInt(Value: integer): integer;