sysstrh.inc 14 KB

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