PageRenderTime 47ms CodeModel.GetById 28ms RepoModel.GetById 0ms app.codeStats 0ms

/FormatUtils.pas

http://github.com/foxblock/PNDTools
Pascal | 95 lines | 52 code | 13 blank | 30 comment | 5 complexity | 79de982f6d210cfaa2eaf3e3002d894a MD5 | raw file
Possible License(s): GPL-3.0, CC-BY-SA-3.0
  1. {******************************************************************************}
  2. { }
  3. { Various utility functions regarding string formatting }
  4. { }
  5. { -------------------------------------------------------------------------- }
  6. { }
  7. { PNDTools is Copyright ©2011-2013 Janek Schäfer }
  8. { }
  9. { This file is part of PNDTools }
  10. { }
  11. { PNDTools is free software: you can redistribute it and/or modify }
  12. { it under the terms of the GNU General Public License as published by }
  13. { the Free Software Foundation, either version 3 of the License, or }
  14. { (at your option) any later version. }
  15. { }
  16. { PNDTools is distributed in the hope that it will be useful, }
  17. { but WITHOUT ANY WARRANTY; without even the implied warranty of }
  18. { MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the }
  19. { GNU General Public License for more details. }
  20. { }
  21. { You should have received a copy of the GNU General Public License }
  22. { along with this program. If not, see <http://www.gnu.org/licenses/>. }
  23. { }
  24. {******************************************************************************}
  25. unit FormatUtils;
  26. {$IFDEF FPC}
  27. {$MODE DELPHI}
  28. {$ENDIF}
  29. interface
  30. uses Classes;
  31. { Converts a file-size in bytes into a string with either decimal or binary
  32. conversion and prefix, maximum is TB or TiB }
  33. function SizeToStr(const Size : Int64; const Binary : Boolean = false) : String;
  34. { Split a string into multiple parts at the specified delimiters }
  35. procedure Tokenize(const S : String; const Delimiter : String; var List : TStrings);
  36. implementation
  37. uses SysUtils, StrUtils, Math;
  38. function SizeToStr(const Size : Int64; const Binary : Boolean = false) : String;
  39. const
  40. OVERHANG : Real = 0.10;
  41. SYMBOLSDEC : Array [1..5] of String = ('Byte','kB','MB','GB','TB');
  42. SYMBOLSBIN : Array [1..5] of String = ('Byte','KiB','MiB','GiB','TiB');
  43. var
  44. Factor : Integer;
  45. Count : Integer;
  46. Temp : Extended;
  47. begin
  48. if Binary then
  49. Factor := 1024
  50. else
  51. Factor := 1000;
  52. Count := 1;
  53. Temp := Size;
  54. while (Temp > Factor * (1 - OVERHANG)) AND (Count <= High(SYMBOLSDEC)) do
  55. begin
  56. Temp := Temp / Factor;
  57. Inc(Count);
  58. end;
  59. if Count = 1 then
  60. Result := IntToStr(Size)
  61. else
  62. Result := Format('%.2f',[Temp]);
  63. if Binary then
  64. Result := Result + ' ' + SYMBOLSBIN[Count]
  65. else
  66. Result := Result + ' ' + SYMBOLSDEC[Count];
  67. end;
  68. procedure Tokenize(const S : String; const Delimiter : String; var List : TStrings);
  69. var
  70. pre, post : Integer;
  71. begin
  72. pre := 1;
  73. post := Pos(Delimiter,S);
  74. while post > 0 do
  75. begin
  76. List.Add(MidStr(S,pre,post-pre));
  77. pre := post+1;
  78. post := PosEx(Delimiter,S,pre);
  79. end;
  80. List.Add(MidStr(S,pre,Length(S)-pre+1));
  81. end;
  82. end.