PageRenderTime 24ms CodeModel.GetById 16ms app.highlight 3ms RepoModel.GetById 1ms 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
 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
26unit FormatUtils;
27
28{$IFDEF FPC}
29  {$MODE DELPHI}
30{$ENDIF}
31
32interface
33
34uses Classes;
35
36{ Converts a file-size in bytes into a string with either decimal or binary
37  conversion and prefix, maximum is TB or TiB }
38function  SizeToStr(const Size : Int64; const Binary : Boolean = false) : String;
39
40{ Split a string into multiple parts at the specified delimiters }
41procedure Tokenize(const S : String; const Delimiter : String; var List : TStrings);
42
43implementation
44
45uses SysUtils, StrUtils, Math;
46
47function SizeToStr(const Size : Int64; const Binary : Boolean = false) : String;
48const
49    OVERHANG : Real = 0.10;
50    SYMBOLSDEC : Array [1..5] of String = ('Byte','kB','MB','GB','TB');
51    SYMBOLSBIN : Array [1..5] of String = ('Byte','KiB','MiB','GiB','TiB');
52var
53    Factor : Integer;
54    Count : Integer;
55    Temp : Extended;
56begin
57    if Binary then
58        Factor := 1024
59    else
60        Factor := 1000;
61    Count := 1;
62    Temp := Size;
63
64    while (Temp > Factor * (1 - OVERHANG)) AND (Count <= High(SYMBOLSDEC)) do
65    begin
66        Temp := Temp / Factor;
67        Inc(Count);
68    end;
69    
70    if Count = 1 then
71       Result := IntToStr(Size)
72    else
73        Result := Format('%.2f',[Temp]);
74    if Binary then
75        Result := Result + ' ' + SYMBOLSBIN[Count]
76    else
77        Result := Result + ' ' + SYMBOLSDEC[Count];
78end;
79
80procedure Tokenize(const S : String; const Delimiter : String; var List : TStrings);
81var
82    pre, post : Integer;
83begin
84    pre := 1;
85    post := Pos(Delimiter,S);
86    while post > 0 do
87    begin
88        List.Add(MidStr(S,pre,post-pre));
89        pre := post+1;
90        post := PosEx(Delimiter,S,pre);
91    end;
92    List.Add(MidStr(S,pre,Length(S)-pre+1));
93end;
94
95end.