syshelpers.pp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378
  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. unit syshelpers;
  12. {$mode objfpc}
  13. {$H+}
  14. {$modeswitch typehelpers}
  15. {$modeswitch advancedrecords}
  16. {$modeswitch allowinline}
  17. {$macro on}
  18. interface
  19. uses
  20. classes, sysutils;
  21. ///////////////////////
  22. // //
  23. // Format settings //
  24. // //
  25. ///////////////////////
  26. type
  27. TStringCaseFormat = (scfUnchangedCase, scfLowerCase, scfUpperCase);
  28. TBitFormatSettings = record // for boolean to string conversion
  29. BitTrueString: string;
  30. BitFalseString: string;
  31. //
  32. BitOnString: string;
  33. BitOffString: string;
  34. //
  35. BitOneString: string;
  36. BitZeroString: string;
  37. //
  38. class operator Initialize(var aNewSettings: TBitFormatSettings);
  39. //
  40. procedure CopyToDefaultBoolStrings;
  41. procedure CopyToDefaultBitFormatSettings; inline;
  42. end;
  43. TBinFormatSettings = record // for number to binary conversion
  44. BinPrefixString: string;
  45. BinSufixString: string;
  46. BinNibbleSeparator: string;
  47. BinByteSeparator: string;
  48. BinWordSeparator: string;
  49. BinDwordSeparator: string;
  50. //
  51. class operator Initialize(var aNewSettings: TBinFormatSettings);
  52. //
  53. procedure CopyToDefaultBinFormatSettings; inline;
  54. end;
  55. THexFormatSettings = record // for number to hex conversion
  56. HexPrefixString: string; // $
  57. HexSufixString: string;
  58. HexNibbleSeparator: string; // between hex digits
  59. HexByteSeparator: string; // between byte pairs of hex digits
  60. HexWordSeparator: string; // between word quads of hex digits
  61. HexDwordSeparator: string; // between dword octets of hex digits
  62. //
  63. class operator Initialize(var aNewSettings: THexFormatSettings);
  64. //
  65. procedure CopyToDefaultHexFormatSettings; inline;
  66. end;
  67. var
  68. DefaultBitFormatSettings: TBitFormatSettings; // global boolean to string conversion defaults
  69. DefaultBinFormatSettings: TBinFormatSettings; // global number to binary conversion defaults
  70. DefaultHexFormatSettings: THexFormatSettings; // global number to hex conversion defaults
  71. // global easy access boolean to string conversion defaults
  72. BitOnString: string absolute DefaultBitFormatSettings.BitOnString;
  73. BitOffString: string absolute DefaultBitFormatSettings.BitOffString;
  74. //
  75. BitTrueString: string absolute DefaultBitFormatSettings.BitTrueString;
  76. BitFalseString: string absolute DefaultBitFormatSettings.BitFalseString;
  77. //
  78. BitOneString: string absolute DefaultBitFormatSettings.BitOneString;
  79. BitZeroString: string absolute DefaultBitFormatSettings.BitZeroString;
  80. // global easy access number to binary conversion defaults
  81. BinPrefixString: string absolute DefaultBinFormatSettings.BinPrefixString;
  82. BinSufixString: string absolute DefaultBinFormatSettings.BinSufixString;
  83. BinNibbleSeparator: string absolute DefaultBinFormatSettings.BinNibbleSeparator;
  84. BinByteSeparator: string absolute DefaultBinFormatSettings.BinByteSeparator;
  85. BinWordSeparator: string absolute DefaultBinFormatSettings.BinWordSeparator;
  86. BinDwordSeparator: string absolute DefaultBinFormatSettings.BinDwordSeparator;
  87. // global easy access number to hex conversion defaults
  88. HexPrefixString: string absolute DefaultHexFormatSettings.HexPrefixString;
  89. HexSufixString: string absolute DefaultHexFormatSettings.HexSufixString;
  90. HexNibbleSeparator: string absolute DefaultHexFormatSettings.HexNibbleSeparator;
  91. HexByteSeparator: string absolute DefaultHexFormatSettings.HexByteSeparator;
  92. HexWordSeparator: string absolute DefaultHexFormatSettings.HexWordSeparator;
  93. HexDwordSeparator: string absolute DefaultHexFormatSettings.HexDwordSeparator;
  94. //////////////////////
  95. // //
  96. // System helpers //
  97. // //
  98. //////////////////////
  99. type
  100. TByteSysHelper = type helper(TByteHelper) for Byte
  101. {$i syshelpersoh.inc}
  102. end;
  103. TShortIntSysHelper = type helper(TShortIntHelper) for ShortInt
  104. {$i syshelpersoh.inc}
  105. end;
  106. TWordSysHelper = type helper(TWordHelper) for Word
  107. {$i syshelpersoh.inc}
  108. end;
  109. TSmallIntSysHelper = type helper(TSmallIntHelper) for SmallInt
  110. {$i syshelpersoh.inc}
  111. end;
  112. TCardinalSysHelper = type helper(TCardinalHelper) for Cardinal
  113. {$i syshelpersoh.inc}
  114. end;
  115. TIntegerSysHelper = type helper(TIntegerHelper) for Integer
  116. {$i syshelpersoh.inc}
  117. end;
  118. TQwordSysHelper = type helper(TQwordHelper) for Qword
  119. {$i syshelpersoh.inc}
  120. end;
  121. TInt64SysHelper = type helper(TInt64Helper) for Int64
  122. {$i syshelpersoh.inc}
  123. end;
  124. TNativeIntSysHelper = type helper(TNativeIntHelper) for NativeInt
  125. {$i syshelpersoh.inc}
  126. end;
  127. TNativeUIntSysHelper = type helper(TNativeUIntHelper) for NativeUInt
  128. {$i syshelpersoh.inc}
  129. end;
  130. TBooleanSysHelper = type helper(TBooleanHelper) for Boolean
  131. {$i syshelpersbh.inc}
  132. end;
  133. TByteBoolSysHelper = type helper(TByteBoolHelper) for ByteBool
  134. {$i syshelpersbh.inc}
  135. end;
  136. TWordBoolSysHelper = type helper(TWordBoolHelper) for WordBool
  137. {$i syshelpersbh.inc}
  138. end;
  139. TLongBoolSysHelper = type helper(TLongBoolHelper) for LongBool
  140. {$i syshelpersbh.inc}
  141. end;
  142. implementation
  143. uses sysconst;
  144. ///////////////////////
  145. // //
  146. // Format settings //
  147. // //
  148. ///////////////////////
  149. class operator TBitFormatSettings.Initialize(var aNewSettings: TBitFormatSettings);
  150. begin
  151. aNewSettings.BitTrueString := 'True';
  152. aNewSettings.BitFalseString := 'False';
  153. //
  154. aNewSettings.BitOnString := 'On';
  155. aNewSettings.BitOffString := 'Off';
  156. //
  157. aNewSettings.BitOneString := '1';
  158. aNewSettings.BitZeroString := '0';
  159. end;
  160. procedure TBitFormatSettings.CopyToDefaultBoolStrings;
  161. begin // without this call, new bit strings will not be used in BoolToStr() and TryStrToBool()
  162. if Length(TrueBoolStrs) < 3 then
  163. SetLength(TrueBoolStrs, 3);
  164. TrueBoolStrs[0] := Self.BitTrueString; // used in BoolToStr() and TryStrToBool()
  165. TrueBoolStrs[1] := Self.BitOnString; // used in TryStrToBool()
  166. TrueBoolStrs[2] := Self.BitOneString; // used in TryStrToBool()
  167. If Length(FalseBoolStrs) < 3 then
  168. SetLength(FalseBoolStrs, 3);
  169. FalseBoolStrs[0] := Self.BitFalseString; // used in BoolToStr() and TryStrToBool()
  170. FalseBoolStrs[1] := Self.BitOffString; // used in TryStrToBool()
  171. FalseBoolStrs[2] := Self.BitZeroString; // used in TryStrToBool()
  172. end;
  173. procedure TBitFormatSettings.CopyToDefaultBitFormatSettings;
  174. begin
  175. DefaultBitFormatSettings := Self;
  176. end;
  177. class operator TBinFormatSettings.Initialize(var aNewSettings: TBinFormatSettings);
  178. begin
  179. aNewSettings := Default(TBinFormatSettings);
  180. end;
  181. procedure TBinFormatSettings.CopyToDefaultBinFormatSettings;
  182. begin
  183. DefaultBinFormatSettings := Self;
  184. end;
  185. class operator THexFormatSettings.Initialize(var aNewSettings: THexFormatSettings);
  186. begin
  187. aNewSettings := Default(THexFormatSettings);
  188. end;
  189. procedure THexFormatSettings.CopyToDefaultHexFormatSettings;
  190. begin
  191. DefaultHexFormatSettings := Self;
  192. end;
  193. //////////////////////
  194. // //
  195. // System helpers //
  196. // //
  197. //////////////////////
  198. { ---------------------------------------------------------------------
  199. TByteSysHelper
  200. ---------------------------------------------------------------------}
  201. {$define TORDINALHELPER:=TByteSysHelper}
  202. {$define TORDINALBITINDEX:=TByteBitIndex}
  203. {$define TORDINALNIBBLEINDEX:=TByteNibbleIndex}
  204. {$i syshelperso.inc}
  205. { ---------------------------------------------------------------------
  206. TShortintSysHelper
  207. ---------------------------------------------------------------------}
  208. {$define TORDINALHELPER:=TShortIntSysHelper}
  209. {$define TORDINALBITINDEX:=TShortIntBitIndex}
  210. {$define TORDINALNIBBLEINDEX:=TShortIntNibbleIndex}
  211. {$i syshelperso.inc}
  212. { ---------------------------------------------------------------------
  213. TSmallintSysHelper
  214. ---------------------------------------------------------------------}
  215. {$define TORDINALHELPER:=TSmallIntSysHelper}
  216. {$define TORDINALBITINDEX:=TSmallIntBitIndex}
  217. {$define TORDINALNIBBLEINDEX:=TSmallIntNibbleIndex}
  218. {$i syshelperso.inc}
  219. { ---------------------------------------------------------------------
  220. TWordSysHelper
  221. ---------------------------------------------------------------------}
  222. {$define TORDINALHELPER:=TWordSysHelper}
  223. {$define TORDINALBITINDEX:=TWordBitIndex}
  224. {$define TORDINALNIBBLEINDEX:=TWordNibbleIndex}
  225. {$i syshelperso.inc}
  226. { ---------------------------------------------------------------------
  227. TCardinalSysHelper
  228. ---------------------------------------------------------------------}
  229. {$define TORDINALHELPER:=TCardinalSysHelper}
  230. {$define TORDINALBITINDEX:=TCardinalBitIndex}
  231. {$define TORDINALNIBBLEINDEX:=TCardinalNibbleIndex}
  232. {$i syshelperso.inc}
  233. { ---------------------------------------------------------------------
  234. TIntegerSysHelper
  235. ---------------------------------------------------------------------}
  236. {$define TORDINALHELPER:=TIntegerSysHelper}
  237. {$define TORDINALBITINDEX:=TIntegerBitIndex}
  238. {$define TORDINALNIBBLEINDEX:=TIntegerNibbleIndex}
  239. {$i syshelperso.inc}
  240. { ---------------------------------------------------------------------
  241. TInt64SysHelper
  242. ---------------------------------------------------------------------}
  243. {$define TORDINALHELPER:=TInt64SysHelper}
  244. {$define TORDINALBITINDEX:=TInt64BitIndex}
  245. {$define TORDINALNIBBLEINDEX:=TInt64NibbleIndex}
  246. {$i syshelperso.inc}
  247. { ---------------------------------------------------------------------
  248. TQWordSysHelper
  249. ---------------------------------------------------------------------}
  250. {$define TORDINALHELPER:=TQWordSysHelper}
  251. {$define TORDINALBITINDEX:=TQwordBitIndex}
  252. {$define TORDINALNIBBLEINDEX:=TQwordNibbleIndex}
  253. {$i syshelperso.inc}
  254. { ---------------------------------------------------------------------
  255. TNativeIntSysHelper
  256. ---------------------------------------------------------------------}
  257. {$define TORDINALHELPER:=TNativeIntSysHelper}
  258. {$define TORDINALBITINDEX:=TNativeIntBitIndex}
  259. {$define TORDINALNIBBLEINDEX:=TNativeIntNibbleIndex}
  260. {$i syshelperso.inc}
  261. { ---------------------------------------------------------------------
  262. TNativeUIntSysHelper
  263. ---------------------------------------------------------------------}
  264. {$define TORDINALHELPER:=TNativeUIntSysHelper}
  265. {$define TORDINALBITINDEX:=TNativeUIntBitIndex}
  266. {$define TORDINALNIBBLEINDEX:=TNativeUIntNibbleIndex}
  267. {$i syshelperso.inc}
  268. { ---------------------------------------------------------------------
  269. TBooleanSysHelper
  270. ---------------------------------------------------------------------}
  271. {$define TBOOLHELPER:=TBooleanSysHelper}
  272. {$define TBOOLTYPE:=Boolean}
  273. {$i syshelpersb.inc}
  274. { ---------------------------------------------------------------------
  275. TByteBoolSysHelper
  276. ---------------------------------------------------------------------}
  277. {$define TBOOLHELPER:=TByteBoolSysHelper}
  278. {$define TBOOLTYPE:=ByteBool}
  279. {$i syshelpersb.inc}
  280. { ---------------------------------------------------------------------
  281. TWordBoolSysHelper
  282. ---------------------------------------------------------------------}
  283. {$define TBOOLHELPER:=TWordBoolSysHelper}
  284. {$define TBOOLTYPE:=WordBool}
  285. {$i syshelpersb.inc}
  286. { ---------------------------------------------------------------------
  287. TLongBoolSysHelper
  288. ---------------------------------------------------------------------}
  289. {$define TBOOLHELPER:=TLongBoolSysHelper}
  290. {$define TBOOLTYPE:=LongBool}
  291. {$i syshelpersb.inc}
  292. end.