syshelpers.pp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450
  1. {
  2. This file is part of the Free Pascal run time library.
  3. Copyright (c) 2021 by Zeljko Avramovic (user avra in Lazarus forum)
  4. syshelpers - Type helpers for customizable boolean, binary and hexadecimal data internationalized string representation
  5. See the file COPYING.FPC, included in this distribution,
  6. for details about the copyright.
  7. This program is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  10. **********************************************************************}
  11. {$IFNDEF FPC_DOTTEDUNITS}
  12. unit syshelpers;
  13. {$ENDIF FPC_DOTTEDUNITS}
  14. {$mode objfpc}
  15. {$H+}
  16. {$modeswitch typehelpers}
  17. {$modeswitch advancedrecords}
  18. {$modeswitch allowinline}
  19. {$macro on}
  20. interface
  21. {$IFDEF FPC_DOTTEDUNITS}
  22. uses
  23. System.Classes, System.SysUtils;
  24. {$ELSE FPC_DOTTEDUNITS}
  25. uses
  26. classes, sysutils;
  27. {$ENDIF FPC_DOTTEDUNITS}
  28. ///////////////////////
  29. // //
  30. // Format settings //
  31. // //
  32. ///////////////////////
  33. type
  34. TStringCaseFormat = (scfUnchangedCase, scfLowerCase, scfUpperCase);
  35. TBitFormatSettings = record // for boolean to string conversion
  36. BitTrueString: string;
  37. BitFalseString: string;
  38. //
  39. BitOnString: string;
  40. BitOffString: string;
  41. //
  42. BitOneString: string;
  43. BitZeroString: string;
  44. //
  45. class operator Initialize(var aNewSettings: TBitFormatSettings);
  46. //
  47. procedure CopyToDefaultBoolStrings;
  48. procedure CopyToDefaultBitFormatSettings; inline;
  49. end;
  50. TBinFormatSettings = record // for number to binary conversion
  51. BinPrefixString: string;
  52. BinSufixString: string;
  53. BinNibbleSeparator: string;
  54. BinByteSeparator: string;
  55. BinWordSeparator: string;
  56. BinDwordSeparator: string;
  57. //
  58. class operator Initialize(var aNewSettings: TBinFormatSettings);
  59. //
  60. procedure CopyToDefaultBinFormatSettings; inline;
  61. end;
  62. THexFormatSettings = record // for number to hex conversion
  63. HexPrefixString: string; // $
  64. HexSufixString: string;
  65. HexNibbleSeparator: string; // between hex digits
  66. HexByteSeparator: string; // between byte pairs of hex digits
  67. HexWordSeparator: string; // between word quads of hex digits
  68. HexDwordSeparator: string; // between dword octets of hex digits
  69. //
  70. class operator Initialize(var aNewSettings: THexFormatSettings);
  71. //
  72. procedure CopyToDefaultHexFormatSettings; inline;
  73. end;
  74. var
  75. DefaultBitFormatSettings: TBitFormatSettings; // global boolean to string conversion defaults
  76. DefaultBinFormatSettings: TBinFormatSettings; // global number to binary conversion defaults
  77. DefaultHexFormatSettings: THexFormatSettings; // global number to hex conversion defaults
  78. // global easy access boolean to string conversion defaults
  79. BitOnString: string absolute DefaultBitFormatSettings.BitOnString;
  80. BitOffString: string absolute DefaultBitFormatSettings.BitOffString;
  81. //
  82. BitTrueString: string absolute DefaultBitFormatSettings.BitTrueString;
  83. BitFalseString: string absolute DefaultBitFormatSettings.BitFalseString;
  84. //
  85. BitOneString: string absolute DefaultBitFormatSettings.BitOneString;
  86. BitZeroString: string absolute DefaultBitFormatSettings.BitZeroString;
  87. // global easy access number to binary conversion defaults
  88. BinPrefixString: string absolute DefaultBinFormatSettings.BinPrefixString;
  89. BinSufixString: string absolute DefaultBinFormatSettings.BinSufixString;
  90. BinNibbleSeparator: string absolute DefaultBinFormatSettings.BinNibbleSeparator;
  91. BinByteSeparator: string absolute DefaultBinFormatSettings.BinByteSeparator;
  92. BinWordSeparator: string absolute DefaultBinFormatSettings.BinWordSeparator;
  93. BinDwordSeparator: string absolute DefaultBinFormatSettings.BinDwordSeparator;
  94. // global easy access number to hex conversion defaults
  95. HexPrefixString: string absolute DefaultHexFormatSettings.HexPrefixString;
  96. HexSufixString: string absolute DefaultHexFormatSettings.HexSufixString;
  97. HexNibbleSeparator: string absolute DefaultHexFormatSettings.HexNibbleSeparator;
  98. HexByteSeparator: string absolute DefaultHexFormatSettings.HexByteSeparator;
  99. HexWordSeparator: string absolute DefaultHexFormatSettings.HexWordSeparator;
  100. HexDwordSeparator: string absolute DefaultHexFormatSettings.HexDwordSeparator;
  101. //////////////////////
  102. // //
  103. // System helpers //
  104. // //
  105. //////////////////////
  106. type
  107. TByteSysHelper = type helper(TByteHelper) for Byte
  108. {$i syshelpersoh.inc}
  109. end;
  110. TShortIntSysHelper = type helper(TShortIntHelper) for ShortInt
  111. {$i syshelpersoh.inc}
  112. end;
  113. TWordSysHelper = type helper(TWordHelper) for Word
  114. {$i syshelpersoh.inc}
  115. end;
  116. TSmallIntSysHelper = type helper(TSmallIntHelper) for SmallInt
  117. {$i syshelpersoh.inc}
  118. end;
  119. TCardinalSysHelper = type helper(TCardinalHelper) for Cardinal
  120. {$i syshelpersoh.inc}
  121. end;
  122. TIntegerSysHelper = type helper(TIntegerHelper) for Integer
  123. {$i syshelpersoh.inc}
  124. end;
  125. TQwordSysHelper = type helper(TQwordHelper) for Qword
  126. {$i syshelpersoh.inc}
  127. end;
  128. TInt64SysHelper = type helper(TInt64Helper) for Int64
  129. {$i syshelpersoh.inc}
  130. end;
  131. TNativeIntSysHelper = type helper(TNativeIntHelper) for NativeInt
  132. {$i syshelpersoh.inc}
  133. end;
  134. TNativeUIntSysHelper = type helper(TNativeUIntHelper) for NativeUInt
  135. {$i syshelpersoh.inc}
  136. end;
  137. TBooleanSysHelper = type helper(TBooleanHelper) for Boolean
  138. {$i syshelpersbh.inc}
  139. end;
  140. TBoolean8SysHelper = type helper(TBoolean8Helper) for Boolean8
  141. {$i syshelpersbh.inc}
  142. end;
  143. TBoolean16SysHelper = type helper(TBoolean16Helper) for Boolean16
  144. {$i syshelpersbh.inc}
  145. end;
  146. TBoolean32SysHelper = type helper(TBoolean32Helper) for Boolean32
  147. {$i syshelpersbh.inc}
  148. end;
  149. TBoolean64SysHelper = type helper(TBoolean64Helper) for Boolean64
  150. {$i syshelpersbh.inc}
  151. end;
  152. TByteBoolSysHelper = type helper(TByteBoolHelper) for ByteBool
  153. {$i syshelpersbh.inc}
  154. end;
  155. TWordBoolSysHelper = type helper(TWordBoolHelper) for WordBool
  156. {$i syshelpersbh.inc}
  157. end;
  158. TLongBoolSysHelper = type helper(TLongBoolHelper) for LongBool
  159. {$i syshelpersbh.inc}
  160. end;
  161. TQWordBoolSysHelper = type helper(TQWordBoolHelper) for QWordBool
  162. {$i syshelpersbh.inc}
  163. end;
  164. implementation
  165. {$IFDEF FPC_DOTTEDUNITS}
  166. uses System.SysConst;
  167. {$ELSE FPC_DOTTEDUNITS}
  168. uses sysconst;
  169. {$ENDIF FPC_DOTTEDUNITS}
  170. ///////////////////////
  171. // //
  172. // Format settings //
  173. // //
  174. ///////////////////////
  175. class operator TBitFormatSettings.Initialize(var aNewSettings: TBitFormatSettings);
  176. begin
  177. aNewSettings.BitTrueString := 'True';
  178. aNewSettings.BitFalseString := 'False';
  179. //
  180. aNewSettings.BitOnString := 'On';
  181. aNewSettings.BitOffString := 'Off';
  182. //
  183. aNewSettings.BitOneString := '1';
  184. aNewSettings.BitZeroString := '0';
  185. end;
  186. procedure TBitFormatSettings.CopyToDefaultBoolStrings;
  187. begin // without this call, new bit strings will not be used in BoolToStr() and TryStrToBool()
  188. if Length(TrueBoolStrs) < 3 then
  189. SetLength(TrueBoolStrs, 3);
  190. TrueBoolStrs[0] := Self.BitTrueString; // used in BoolToStr() and TryStrToBool()
  191. TrueBoolStrs[1] := Self.BitOnString; // used in TryStrToBool()
  192. TrueBoolStrs[2] := Self.BitOneString; // used in TryStrToBool()
  193. If Length(FalseBoolStrs) < 3 then
  194. SetLength(FalseBoolStrs, 3);
  195. FalseBoolStrs[0] := Self.BitFalseString; // used in BoolToStr() and TryStrToBool()
  196. FalseBoolStrs[1] := Self.BitOffString; // used in TryStrToBool()
  197. FalseBoolStrs[2] := Self.BitZeroString; // used in TryStrToBool()
  198. end;
  199. procedure TBitFormatSettings.CopyToDefaultBitFormatSettings;
  200. begin
  201. DefaultBitFormatSettings := Self;
  202. end;
  203. class operator TBinFormatSettings.Initialize(var aNewSettings: TBinFormatSettings);
  204. begin
  205. aNewSettings := Default(TBinFormatSettings);
  206. end;
  207. procedure TBinFormatSettings.CopyToDefaultBinFormatSettings;
  208. begin
  209. DefaultBinFormatSettings := Self;
  210. end;
  211. class operator THexFormatSettings.Initialize(var aNewSettings: THexFormatSettings);
  212. begin
  213. aNewSettings := Default(THexFormatSettings);
  214. end;
  215. procedure THexFormatSettings.CopyToDefaultHexFormatSettings;
  216. begin
  217. DefaultHexFormatSettings := Self;
  218. end;
  219. //////////////////////
  220. // //
  221. // System helpers //
  222. // //
  223. //////////////////////
  224. { ---------------------------------------------------------------------
  225. TBooleanSysHelper
  226. ---------------------------------------------------------------------}
  227. {$define TBOOLHELPER:=TBooleanSysHelper}
  228. {$define TBOOLTYPE:=Boolean}
  229. {$i syshelpersb.inc}
  230. { ---------------------------------------------------------------------
  231. TBoolean8SysHelper
  232. ---------------------------------------------------------------------}
  233. {$define TBOOLHELPER:=TBoolean8SysHelper}
  234. {$define TBOOLTYPE:=Boolean8}
  235. {$i syshelpersb.inc}
  236. { ---------------------------------------------------------------------
  237. TBoolean16SysHelper
  238. ---------------------------------------------------------------------}
  239. {$define TBOOLHELPER:=TBoolean16SysHelper}
  240. {$define TBOOLTYPE:=Boolean16}
  241. {$i syshelpersb.inc}
  242. { ---------------------------------------------------------------------
  243. TBoolean32SysHelper
  244. ---------------------------------------------------------------------}
  245. {$define TBOOLHELPER:=TBoolean32SysHelper}
  246. {$define TBOOLTYPE:=Boolean32}
  247. {$i syshelpersb.inc}
  248. { ---------------------------------------------------------------------
  249. TBoolean64SysHelper
  250. ---------------------------------------------------------------------}
  251. {$define TBOOLHELPER:=TBoolean64SysHelper}
  252. {$define TBOOLTYPE:=Boolean64}
  253. {$i syshelpersb.inc}
  254. { ---------------------------------------------------------------------
  255. TByteBoolSysHelper
  256. ---------------------------------------------------------------------}
  257. {$define TBOOLHELPER:=TByteBoolSysHelper}
  258. {$define TBOOLTYPE:=ByteBool}
  259. {$i syshelpersb.inc}
  260. { ---------------------------------------------------------------------
  261. TWordBoolSysHelper
  262. ---------------------------------------------------------------------}
  263. {$define TBOOLHELPER:=TWordBoolSysHelper}
  264. {$define TBOOLTYPE:=WordBool}
  265. {$i syshelpersb.inc}
  266. { ---------------------------------------------------------------------
  267. TLongBoolSysHelper
  268. ---------------------------------------------------------------------}
  269. {$define TBOOLHELPER:=TLongBoolSysHelper}
  270. {$define TBOOLTYPE:=LongBool}
  271. {$i syshelpersb.inc}
  272. { ---------------------------------------------------------------------
  273. TLongBoolSysHelper
  274. ---------------------------------------------------------------------}
  275. {$define TBOOLHELPER:=TQWordBoolSysHelper}
  276. {$define TBOOLTYPE:=QWordBool}
  277. {$i syshelpersb.inc}
  278. { ---------------------------------------------------------------------
  279. TByteSysHelper
  280. ---------------------------------------------------------------------}
  281. {$define TORDINALHELPER:=TByteSysHelper}
  282. {$define TORDINALBITINDEX:=TByteBitIndex}
  283. {$define TORDINALNIBBLEINDEX:=TByteNibbleIndex}
  284. {$i syshelperso.inc}
  285. { ---------------------------------------------------------------------
  286. TShortintSysHelper
  287. ---------------------------------------------------------------------}
  288. {$define TORDINALHELPER:=TShortIntSysHelper}
  289. {$define TORDINALBITINDEX:=TShortIntBitIndex}
  290. {$define TORDINALNIBBLEINDEX:=TShortIntNibbleIndex}
  291. {$i syshelperso.inc}
  292. { ---------------------------------------------------------------------
  293. TSmallintSysHelper
  294. ---------------------------------------------------------------------}
  295. {$define TORDINALHELPER:=TSmallIntSysHelper}
  296. {$define TORDINALBITINDEX:=TSmallIntBitIndex}
  297. {$define TORDINALNIBBLEINDEX:=TSmallIntNibbleIndex}
  298. {$i syshelperso.inc}
  299. { ---------------------------------------------------------------------
  300. TWordSysHelper
  301. ---------------------------------------------------------------------}
  302. {$define TORDINALHELPER:=TWordSysHelper}
  303. {$define TORDINALBITINDEX:=TWordBitIndex}
  304. {$define TORDINALNIBBLEINDEX:=TWordNibbleIndex}
  305. {$i syshelperso.inc}
  306. { ---------------------------------------------------------------------
  307. TCardinalSysHelper
  308. ---------------------------------------------------------------------}
  309. {$define TORDINALHELPER:=TCardinalSysHelper}
  310. {$define TORDINALBITINDEX:=TCardinalBitIndex}
  311. {$define TORDINALNIBBLEINDEX:=TCardinalNibbleIndex}
  312. {$i syshelperso.inc}
  313. { ---------------------------------------------------------------------
  314. TIntegerSysHelper
  315. ---------------------------------------------------------------------}
  316. {$define TORDINALHELPER:=TIntegerSysHelper}
  317. {$define TORDINALBITINDEX:=TIntegerBitIndex}
  318. {$define TORDINALNIBBLEINDEX:=TIntegerNibbleIndex}
  319. {$i syshelperso.inc}
  320. { ---------------------------------------------------------------------
  321. TInt64SysHelper
  322. ---------------------------------------------------------------------}
  323. {$define TORDINALHELPER:=TInt64SysHelper}
  324. {$define TORDINALBITINDEX:=TInt64BitIndex}
  325. {$define TORDINALNIBBLEINDEX:=TInt64NibbleIndex}
  326. {$i syshelperso.inc}
  327. { ---------------------------------------------------------------------
  328. TQWordSysHelper
  329. ---------------------------------------------------------------------}
  330. {$define TORDINALHELPER:=TQWordSysHelper}
  331. {$define TORDINALBITINDEX:=TQwordBitIndex}
  332. {$define TORDINALNIBBLEINDEX:=TQwordNibbleIndex}
  333. {$i syshelperso.inc}
  334. { ---------------------------------------------------------------------
  335. TNativeIntSysHelper
  336. ---------------------------------------------------------------------}
  337. {$define TORDINALHELPER:=TNativeIntSysHelper}
  338. {$define TORDINALBITINDEX:=TNativeIntBitIndex}
  339. {$define TORDINALNIBBLEINDEX:=TNativeIntNibbleIndex}
  340. {$i syshelperso.inc}
  341. { ---------------------------------------------------------------------
  342. TNativeUIntSysHelper
  343. ---------------------------------------------------------------------}
  344. {$define TORDINALHELPER:=TNativeUIntSysHelper}
  345. {$define TORDINALBITINDEX:=TNativeUIntBitIndex}
  346. {$define TORDINALNIBBLEINDEX:=TNativeUIntNibbleIndex}
  347. {$i syshelperso.inc}
  348. end.