sysstrh.inc 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  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. Var TrueBoolStrs,
  50. FalseBoolStrs : Array of String;
  51. function NewStr(const S: string): PString;
  52. procedure DisposeStr(S: PString);
  53. procedure AssignStr(var P: PString; const S: string);
  54. procedure AppendStr(var Dest: String; const S: string);
  55. function UpperCase(const s: string): string;
  56. function LowerCase(const s: string): string; overload;
  57. { the compiler can't decide else if it should use the char or the ansistring
  58. version for a variant }
  59. function LowerCase(const V: variant): string; overload;{$ifdef SYSUTILSINLINE}inline;{$endif}
  60. function CompareStr(const S1, S2: string): Integer;
  61. function CompareMemRange(P1, P2: Pointer; Length: cardinal): integer;
  62. function CompareMem(P1, P2: Pointer; Length: cardinal): Boolean;
  63. function CompareText(const S1, S2: string): integer;
  64. function SameText(const s1,s2:String):Boolean;
  65. function AnsiUpperCase(const s: string): string;{$ifdef SYSUTILSINLINE}inline;{$endif}
  66. function AnsiLowerCase(const s: string): string;{$ifdef SYSUTILSINLINE}inline;{$endif}
  67. function AnsiCompareStr(const S1, S2: string): integer;{$ifdef SYSUTILSINLINE}inline;{$endif}
  68. function AnsiCompareText(const S1, S2: string): integer;{$ifdef SYSUTILSINLINE}inline;{$endif}
  69. function AnsiSameText(const s1,s2:String):Boolean;{$ifdef SYSUTILSINLINE}inline;{$endif}
  70. function AnsiSameStr(const s1,s2:String):Boolean;{$ifdef SYSUTILSINLINE}inline;{$endif}
  71. function AnsiStrComp(S1, S2: PChar): integer;{$ifdef SYSUTILSINLINE}inline;{$endif}
  72. function AnsiStrIComp(S1, S2: PChar): integer;{$ifdef SYSUTILSINLINE}inline;{$endif}
  73. function AnsiStrLComp(S1, S2: PChar; MaxLen: cardinal): integer;{$ifdef SYSUTILSINLINE}inline;{$endif}
  74. function AnsiStrLIComp(S1, S2: PChar; MaxLen: cardinal): integer;{$ifdef SYSUTILSINLINE}inline;{$endif}
  75. function AnsiStrLower(Str: PChar): PChar;{$ifdef SYSUTILSINLINE}inline;{$endif}
  76. function AnsiStrUpper(Str: PChar): PChar;{$ifdef SYSUTILSINLINE}inline;{$endif}
  77. function AnsiLastChar(const S: string): PChar;
  78. function AnsiStrLastChar(Str: PChar): PChar;
  79. function Trim(const S: string): string;
  80. function TrimLeft(const S: string): string;
  81. function TrimRight(const S: string): string;
  82. function QuotedStr(const S: string): string;
  83. function AnsiQuotedStr(const S: string; Quote: char): string;
  84. function AnsiDequotedStr(const S: string; AQuote: Char): string;
  85. function AnsiExtractQuotedStr(var Src: PChar; Quote: Char): string;
  86. function AdjustLineBreaks(const S: string): string;
  87. function AdjustLineBreaks(const S: string; Style: TTextLineBreakStyle): string;
  88. function IsValidIdent(const Ident: string): boolean;
  89. function IntToStr(Value: integer): string;
  90. function IntToStr(Value: Int64): string;
  91. function IntToStr(Value: QWord): string;
  92. function IntToHex(Value: integer; Digits: integer): string;
  93. function IntToHex(Value: Int64; Digits: integer): string;
  94. function StrToInt(const s: string): integer;
  95. function TryStrToInt(const s: string; var i : integer) : boolean;
  96. function StrToInt64(const s: string): int64;
  97. function TryStrToInt64(const s: string; var i : int64) : boolean;
  98. function StrToQWord(const s: string): QWord;
  99. function TryStrToQWord(const s: string; var Q : QWord) : boolean;
  100. function StrToIntDef(const S: string; Default: integer): integer;
  101. function StrToInt64Def(const S: string; Default: int64): int64;
  102. function StrToQWordDef(const S: string; Default: QWord): QWord;
  103. function LoadStr(Ident: integer): string;
  104. // function FmtLoadStr(Ident: integer; const Args: array of const): string;
  105. Function Format (Const Fmt : String; const Args : Array of const) : String;
  106. Function Format (Const Fmt: string; const Args: array of const; const FormatSettings: TFormatSettings): string;
  107. Function FormatBuf (Var Buffer; BufLen : Cardinal; Const Fmt; fmtLen : Cardinal; Const Args : Array of const) : Cardinal;
  108. Function FormatBuf (Var Buffer; BufLen : Cardinal; Const Fmt; fmtLen : Cardinal; Const Args : Array of const; Const FormatSettings: TFormatSettings) : Cardinal;
  109. Function StrFmt(Buffer,Fmt : PChar; Const args: Array of const) : Pchar;
  110. Function StrFmt(Buffer,Fmt : PChar; Const Args: Array of const; Const FormatSettings: TFormatSettings): PChar;
  111. Function StrLFmt(Buffer : PCHar; Maxlen : Cardinal;Fmt : PChar; Const args: Array of const) : Pchar;
  112. Function StrLFmt(Buffer : PCHar; Maxlen : Cardinal;Fmt : PChar; Const args: Array of const; Const FormatSettings: TFormatSettings) : Pchar;
  113. Procedure FmtStr(Var Res: String; Const Fmt : String; Const args: Array of const);
  114. Procedure FmtStr(Var Res: string; const Fmt : string; Const args: Array of const; Const FormatSettings: TFormatSettings);
  115. {$ifdef FPC_HAS_TYPE_EXTENDED}
  116. Function FloatToStrF(Value: Extended; format: TFloatFormat; Precision, Digits: Integer): String;
  117. Function FloatToStrF(Value: Extended; format: TFloatFormat; Precision, Digits: Integer; Const FormatSettings: TFormatSettings): String;
  118. {$endif FPC_HAS_TYPE_EXTENDED}
  119. Function FloatToStrF(Value: Double; format: TFloatFormat; Precision, Digits: Integer): String;
  120. Function FloatToStrF(Value: Double; format: TFloatFormat; Precision, Digits: Integer; Const FormatSettings: TFormatSettings): String;
  121. Function FloatToStrF(Value: Single; format: TFloatFormat; Precision, Digits: Integer): String;
  122. Function FloatToStrF(Value: Single; format: TFloatFormat; Precision, Digits: Integer; Const FormatSettings: TFormatSettings): String;
  123. Function FloatToStrF(Value: Comp; format: TFloatFormat; Precision, Digits: Integer): String;
  124. Function FloatToStrF(Value: Comp; format: TFloatFormat; Precision, Digits: Integer; Const FormatSettings: TFormatSettings): String;
  125. Function FloatToStrF(Value: Currency; format: TFloatFormat; Precision, Digits: Integer): String;
  126. Function FloatToStrF(Value: Currency; format: TFloatFormat; Precision, Digits: Integer; Const FormatSettings: TFormatSettings): String;
  127. {$ifndef FPC_COMP_IS_INT64}
  128. Function FloatToStrF(Value: Int64; format: TFloatFormat; Precision, Digits: Integer): String;
  129. Function FloatToStrF(Value: Int64; format: TFloatFormat; Precision, Digits: Integer; Const FormatSettings: TFormatSettings): String;
  130. {$endif FPC_COMP_IS_INT64}
  131. Function CurrToStrF(Value: Currency; Format: TFloatFormat; Digits: Integer): string;
  132. Function CurrToStrF(Value: Currency; Format: TFloatFormat; Digits: Integer; Const FormatSettings: TFormatSettings): string;
  133. {$ifdef FPC_HAS_TYPE_EXTENDED}
  134. Function FloatToStr(Value: Extended): String;
  135. Function FloatToStr(Value: Extended; Const FormatSettings: TFormatSettings): String;
  136. {$endif FPC_HAS_TYPE_EXTENDED}
  137. Function FloatToStr(Value: Double): String;
  138. Function FloatToStr(Value: Double; Const FormatSettings: TFormatSettings): String;
  139. Function FloatToStr(Value: Single): String;
  140. Function FloatToStr(Value: Single; Const FormatSettings: TFormatSettings): String;
  141. Function FloatToStr(Value: Currency): String;
  142. Function FloatToStr(Value: Currency; Const FormatSettings: TFormatSettings): String;
  143. Function FloatToStr(Value: Comp): String;
  144. Function FloatToStr(Value: Comp; Const FormatSettings: TFormatSettings): String;
  145. {$ifndef FPC_COMP_IS_INT64}
  146. Function FloatToStr(Value: Int64): String;
  147. Function FloatToStr(Value: Int64; Const FormatSettings: TFormatSettings): String;
  148. {$endif FPC_COMP_IS_INT64}
  149. Function StrToFloat(Const S : String) : Extended;
  150. Function StrToFloat(Const S : String; Const FormatSettings: TFormatSettings) : Extended;
  151. Function StrToFloatDef(Const S: String; Const Default: Extended): Extended;
  152. Function StrToFloatDef(Const S: String; Const Default: Extended; Const FormatSettings: TFormatSettings): Extended;
  153. Function TryStrToFloat(Const S : String; Var Value: Single): Boolean;
  154. Function TryStrToFloat(Const S : String; Var Value: Single; Const FormatSettings: TFormatSettings): Boolean;
  155. Function TryStrToFloat(Const S : String; Var Value: Double): Boolean;
  156. Function TryStrToFloat(Const S : String; Var Value: Double; Const FormatSettings: TFormatSettings): Boolean;
  157. {$ifdef FPC_HAS_TYPE_EXTENDED}
  158. Function TryStrToFloat(Const S : String; Var Value: Extended): Boolean;
  159. Function TryStrToFloat(Const S : String; Var Value: Extended; Const FormatSettings: TFormatSettings): Boolean;
  160. {$endif FPC_HAS_TYPE_EXTENDED}
  161. Function TextToFloat(Buffer: PChar; Var Value: Extended): Boolean;
  162. Function TextToFloat(Buffer: PChar; Var Value: Extended; Const FormatSettings: TFormatSettings): Boolean;
  163. Function TextToFloat(Buffer: PChar; Var Value; ValueType: TFloatValue): Boolean;
  164. Function TextToFloat(Buffer: PChar; Var Value; ValueType: TFloatValue; Const FormatSettings: TFormatSettings): Boolean;
  165. Function FloatToText(Buffer: PChar; Value: Extended; format: TFloatFormat; Precision, Digits: Integer): Longint;
  166. Function FloatToText(Buffer: PChar; Value: Extended; format: TFloatFormat; Precision, Digits: Integer; Const FormatSettings: TFormatSettings): Longint;
  167. Function FloatToDateTime (Const Value : Extended) : TDateTime;
  168. Function FloattoCurr (Const Value : Extended) : Currency;
  169. function TryFloatToCurr(const Value: Extended; var AResult: Currency): Boolean;
  170. Function CurrToStr(Value: Currency): string;
  171. function StrToCurr(const S: string): Currency;
  172. function TryStrToCurr(const S: string;Var Value : Currency): Boolean;
  173. function StrToCurrDef(const S: string; Default : Currency): Currency;
  174. function StrToBool(const S: string): Boolean;
  175. function BoolToStr(B: Boolean;UseBoolStrs:Boolean=False): string;
  176. function StrToBoolDef(const S: string; Default: Boolean): Boolean;
  177. function TryStrToBool(const S: string; out Value: Boolean): Boolean;
  178. function LastDelimiter(const Delimiters, S: string): Integer;
  179. function StringReplace(const S, OldPattern, NewPattern: string; Flags: TReplaceFlags): string;
  180. Function FloatToTextFmt(Buffer: PChar; Value: Extended; format: PChar; FormatSettings : TFormatSettings): Integer;
  181. Function FloatToTextFmt(Buffer: PChar; Value: Extended; format: PChar): Integer;
  182. Procedure FloatToDecimal(Out Result: TFloatRec; const Value; ValueType: TFloatValue; Precision, Decimals : integer);
  183. Procedure FloatToDecimal(Out Result: TFloatRec; Value: Extended; Precision, Decimals : integer);
  184. Function FormatFloat(Const Format : String; Value : Extended) : String;
  185. Function FormatFloat(Const Format : String; Value : Extended; Const FormatSettings: TFormatSettings) : String;
  186. Function IsDelimiter(const Delimiters, S: string; Index: Integer): Boolean;
  187. function FormatCurr(const Format: string; Value: Currency): string;
  188. Function FormatCurr(const Format: string; Value: Currency; Const FormatSettings: TFormatSettings): string;
  189. function SScanf(const s: string; const fmt : string;const Pointers : array of Pointer) : Integer;
  190. {// MBCS Functions. No MBCS yet, so mostly these are calls to the regular counterparts.}
  191. Type
  192. TMbcsByteType = (mbSingleByte, mbLeadByte, mbTrailByte);
  193. Function ByteType(const S: string; Index: Integer): TMbcsByteType;
  194. Function StrByteType(Str: PChar; Index: Cardinal): TMbcsByteType;
  195. Function ByteToCharLen(const S: string; MaxLen: Integer): Integer;
  196. Function CharToByteLen(const S: string; MaxLen: Integer): Integer;
  197. Function ByteToCharIndex(const S: string; Index: Integer): Integer;
  198. Function StrCharLength(const Str: PChar): Integer;
  199. const
  200. {$ifndef unix}
  201. SwitchChars = ['/','-'];
  202. {$else}
  203. SwitchChars = ['-'];
  204. {$endif}
  205. Type
  206. TSysCharSet = Set of char;
  207. PSysCharSet = ^TSysCharSet;
  208. Function FindCmdLineSwitch(const Switch: string; const Chars: TSysCharSet;IgnoreCase: Boolean): Boolean;
  209. Function FindCmdLineSwitch(const Switch: string; IgnoreCase: Boolean): Boolean;
  210. Function FindCmdLineSwitch(const Switch: string): Boolean;
  211. function WrapText(const Line, BreakStr: string; const BreakChars: TSysCharSet; MaxCol: Integer): string;
  212. function WrapText(const Line: string; MaxCol: Integer): string;
  213. {==============================================================================}
  214. { extra functions }
  215. {==============================================================================}
  216. function LeftStr(const S: string; Count: integer): string;
  217. function RightStr(const S: string; Count: integer): string;
  218. function BCDToInt(Value: integer): integer;