[97] | 1 | unit Functions;
|
---|
| 2 |
|
---|
| 3 | interface
|
---|
| 4 |
|
---|
| 5 | uses TypeDefs, Classes;
|
---|
| 6 |
|
---|
| 7 | function BoolToStr(bool: Boolean): String;
|
---|
| 8 | function Decode_Int(buffer: TByteData): LongWord;
|
---|
| 9 | function Encode_Int(input: LongWord): TByteData;
|
---|
| 10 | function Decode_Float(buffer: TByteData): Single;
|
---|
| 11 | function Encode_Float(input: Single): TByteData;
|
---|
[105] | 12 | function IntToBin(Value: Byte): String;
|
---|
[97] | 13 | function DataToBin(Data: TByteData): String;
|
---|
| 14 | function BinToInt(bin: String): Byte;
|
---|
[113] | 15 | function MakeDatLink(FileID: Integer): Integer;
|
---|
[97] | 16 |
|
---|
[245] | 17 | function ReadString(Stream: TStream; Offset: Integer): String;
|
---|
| 18 |
|
---|
[97] | 19 | function StringSmaller(string1, string2: String): Boolean;
|
---|
| 20 |
|
---|
| 21 | function FormatNumber(Value: LongWord; Width: Byte; leadingzeros: Char): String;
|
---|
| 22 | function FormatFileSize(size: LongWord): String;
|
---|
| 23 | function CreateHexString(Data: TByteData; HexOnly: Boolean): String;
|
---|
| 24 | function DecodeHexString(hex: String): TByteData;
|
---|
| 25 | function GetWinFileName(Name: String): String;
|
---|
| 26 | //function GetExtractPath: String;
|
---|
| 27 |
|
---|
| 28 | function Explode(_string: String; delimiter: Char): TStrings;
|
---|
| 29 |
|
---|
| 30 |
|
---|
| 31 | implementation
|
---|
| 32 |
|
---|
| 33 | uses SysUtils, Math, StrUtils;
|
---|
| 34 |
|
---|
| 35 | type
|
---|
| 36 | TValueSwitcher = record
|
---|
| 37 | case IsFloat: Boolean of
|
---|
| 38 | True: (ValueFloat: Single);
|
---|
| 39 | False: (ValueInt: LongWord);
|
---|
| 40 | end;
|
---|
| 41 |
|
---|
| 42 |
|
---|
| 43 |
|
---|
| 44 | function BoolToStr(bool: Boolean): String;
|
---|
| 45 | begin
|
---|
| 46 | if bool then
|
---|
| 47 | Result := 'true'
|
---|
| 48 | else
|
---|
| 49 | Result := 'false';
|
---|
| 50 | end;
|
---|
| 51 |
|
---|
| 52 |
|
---|
| 53 | function Decode_Int(buffer: TByteData): LongWord;
|
---|
| 54 | begin
|
---|
| 55 | Result := buffer[0] + buffer[1] * 256 + buffer[2] * 256 * 256 + buffer[3] * 256 * 256 * 256;
|
---|
| 56 | end;
|
---|
| 57 |
|
---|
| 58 |
|
---|
| 59 | function Encode_Int(input: LongWord): TByteData;
|
---|
| 60 | begin
|
---|
| 61 | SetLength(Result, 4);
|
---|
| 62 | Result[0] := input mod 256;
|
---|
| 63 | input := input div 256;
|
---|
| 64 | Result[1] := input mod 256;
|
---|
| 65 | input := input div 256;
|
---|
| 66 | Result[2] := input mod 256;
|
---|
| 67 | input := input div 256;
|
---|
| 68 | Result[3] := input mod 256;
|
---|
| 69 | end;
|
---|
| 70 |
|
---|
| 71 |
|
---|
| 72 | function Decode_Float(buffer: TByteData): Single;
|
---|
| 73 | var
|
---|
| 74 | _valueswitcher: TValueSwitcher;
|
---|
| 75 | begin
|
---|
| 76 | _valueswitcher.ValueInt := Decode_Int(buffer);
|
---|
| 77 | Result := _valueswitcher.ValueFloat;
|
---|
| 78 | if IsNAN(Result) then
|
---|
| 79 | Result := 0.0;
|
---|
| 80 | end;
|
---|
| 81 |
|
---|
| 82 |
|
---|
| 83 | function Encode_Float(input: Single): TByteData;
|
---|
| 84 | var
|
---|
| 85 | _valueswitcher: TValueSwitcher;
|
---|
| 86 | begin
|
---|
| 87 | _valueswitcher.ValueFloat := input;
|
---|
| 88 | Result := Encode_Int(_valueswitcher.ValueInt);
|
---|
| 89 | end;
|
---|
| 90 |
|
---|
| 91 |
|
---|
[105] | 92 | function IntToBin(Value: Byte): String;
|
---|
| 93 | var
|
---|
| 94 | i: Byte;
|
---|
| 95 | begin
|
---|
| 96 | Result := '';
|
---|
| 97 | for i := 7 downto 0 do
|
---|
| 98 | Result := Result + IntToStr((Value shr i) and $01);
|
---|
| 99 | end;
|
---|
[97] | 100 |
|
---|
| 101 |
|
---|
| 102 | function DataToBin(Data: TByteData): String;
|
---|
| 103 | var
|
---|
| 104 | i, j: Byte;
|
---|
| 105 | singlebyte: Byte;
|
---|
| 106 | bytepart: String;
|
---|
| 107 | begin
|
---|
| 108 | SetLength(bytepart, 8);
|
---|
| 109 | Result := '';
|
---|
| 110 | for i := 0 to High(Data) do
|
---|
| 111 | begin
|
---|
| 112 | singlebyte := Data[i];
|
---|
| 113 | for j := 7 downto 0 do
|
---|
| 114 | begin
|
---|
| 115 | bytepart[j + 1] := Char((singlebyte and $01) + 48);
|
---|
| 116 | singlebyte := singlebyte shr 1;
|
---|
| 117 | end;
|
---|
| 118 | Result := Result + bytepart + ' ';
|
---|
| 119 | end;
|
---|
| 120 | end;
|
---|
| 121 |
|
---|
| 122 |
|
---|
| 123 | function BinToInt(bin: String): Byte;
|
---|
| 124 | var
|
---|
| 125 | Add: Integer;
|
---|
| 126 | i: Byte;
|
---|
| 127 | begin
|
---|
| 128 | Result := 0;
|
---|
| 129 | if Length(bin) <> 8 then
|
---|
| 130 | Exit;
|
---|
| 131 | Add := 1;
|
---|
| 132 | for i := 8 downto 1 do
|
---|
| 133 | begin
|
---|
| 134 | if not (bin[i] in ['0', '1']) then
|
---|
| 135 | Exit;
|
---|
| 136 | if bin[i] = '1' then
|
---|
| 137 | Inc(Result, Add);
|
---|
| 138 | Add := Add shl 1;
|
---|
| 139 | end;
|
---|
| 140 | end;
|
---|
| 141 |
|
---|
| 142 |
|
---|
[113] | 143 | function MakeDatLink(FileID: Integer): Integer;
|
---|
| 144 | begin
|
---|
| 145 | Result := FileID * 256 + 1;
|
---|
| 146 | end;
|
---|
[97] | 147 |
|
---|
[113] | 148 |
|
---|
| 149 |
|
---|
[245] | 150 |
|
---|
| 151 | function ReadString(Stream: TStream; Offset: Integer): String;
|
---|
| 152 | var
|
---|
| 153 | i: Integer;
|
---|
| 154 | c: Char;
|
---|
| 155 | begin
|
---|
| 156 | if Assigned(Stream) then
|
---|
| 157 | begin
|
---|
| 158 | if Offset >= 0 then
|
---|
| 159 | begin
|
---|
| 160 | Result := '';
|
---|
| 161 | Stream.Seek(Offset, soFromBeginning);
|
---|
| 162 | repeat
|
---|
| 163 | Stream.Read(c, 1);
|
---|
| 164 | if Ord(c) > 0 then
|
---|
| 165 | Result := Result + c;
|
---|
| 166 | until Ord(c) = 0;
|
---|
| 167 | end;
|
---|
| 168 | end;
|
---|
| 169 | end;
|
---|
| 170 |
|
---|
| 171 |
|
---|
| 172 |
|
---|
[97] | 173 | function FormatNumber(Value: LongWord; Width: Byte; leadingzeros: Char): String;
|
---|
| 174 | begin
|
---|
| 175 | Result := AnsiReplaceStr(Format('%' + IntToStr(Width) + 'u', [Value]), ' ', leadingzeros);
|
---|
| 176 | end;
|
---|
| 177 |
|
---|
| 178 |
|
---|
| 179 |
|
---|
| 180 |
|
---|
| 181 | function FormatFileSize(size: LongWord): String;
|
---|
| 182 | var
|
---|
| 183 | floatformat: TFormatSettings;
|
---|
| 184 | begin
|
---|
| 185 | floatformat.DecimalSeparator := '.';
|
---|
| 186 | if size >= 1000 * 1024 * 1024 then
|
---|
| 187 | Result := FloatToStrF(size / 1024 / 1024 / 1024, ffFixed, 5, 1, floatformat) + ' GB'
|
---|
| 188 | else
|
---|
| 189 | if size >= 1000 * 1024 then
|
---|
| 190 | Result := FloatToStrF(size / 1024 / 1024, ffFixed, 5, 1, floatformat) + ' MB'
|
---|
| 191 | else
|
---|
| 192 | if size >= 1000 then
|
---|
| 193 | Result := FloatToStrF(size / 1024, ffFixed, 5, 1, floatformat) + ' KB'
|
---|
| 194 | else
|
---|
| 195 | Result := IntToStr(size) + ' B';
|
---|
| 196 | end;
|
---|
| 197 |
|
---|
| 198 |
|
---|
| 199 |
|
---|
| 200 |
|
---|
| 201 | function CreateHexString(Data: TByteData; HexOnly: Boolean): String;
|
---|
| 202 | var
|
---|
| 203 | string_build, ascii_version: String;
|
---|
[111] | 204 | i: Integer;
|
---|
[97] | 205 | begin
|
---|
| 206 | string_build := '';
|
---|
| 207 | ascii_version := '';
|
---|
| 208 | for i := 0 to High(Data) do
|
---|
| 209 | begin
|
---|
| 210 | if not HexOnly then
|
---|
| 211 | if (i mod 16) = 0 then
|
---|
| 212 | string_build := string_build + '0x' + IntToHex(i, 6) + ' ';
|
---|
| 213 | string_build := string_build + IntToHex(Data[i], 2);
|
---|
| 214 | if not HexOnly then
|
---|
| 215 | begin
|
---|
| 216 | if Data[i] >= 32 then
|
---|
| 217 | ascii_version := ascii_version + Chr(Data[i])
|
---|
| 218 | else
|
---|
| 219 | ascii_version := ascii_version + '.';
|
---|
| 220 | if ((i + 1) mod 2) = 0 then
|
---|
| 221 | string_build := string_build + #32;
|
---|
| 222 | if ((i + 1) mod 16) = 0 then
|
---|
| 223 | begin
|
---|
| 224 | string_build := string_build + #32 + ascii_version + CrLf;
|
---|
| 225 | ascii_version := '';
|
---|
| 226 | end;
|
---|
| 227 | end;
|
---|
| 228 | end;
|
---|
| 229 | Result := string_build;
|
---|
| 230 | end;
|
---|
| 231 |
|
---|
| 232 |
|
---|
| 233 |
|
---|
| 234 |
|
---|
| 235 | function DecodeHexString(hex: String): TByteData;
|
---|
| 236 | var
|
---|
[111] | 237 | i: Integer;
|
---|
[97] | 238 | begin
|
---|
| 239 | SetLength(Result, Length(hex) div 2);
|
---|
| 240 | for i := 0 to Length(Result) do
|
---|
| 241 | begin
|
---|
| 242 | Result[i] := 0;
|
---|
| 243 | case UpCase(hex[1 + i * 2]) of
|
---|
| 244 | '0'..'9':
|
---|
| 245 | Result[i] := (Ord(UpCase(hex[1 + i * 2])) - 48) * 16;
|
---|
| 246 | 'A'..'F':
|
---|
| 247 | Result[i] := (Ord(UpCase(hex[1 + i * 2])) - 55) * 16;
|
---|
| 248 | end;
|
---|
| 249 | case UpCase(hex[1 + i * 2 + 1]) of
|
---|
| 250 | '0'..'9':
|
---|
| 251 | Result[i] := Result[i] + (Ord(UpCase(hex[1 + i * 2 + 1])) - 48);
|
---|
| 252 | 'A'..'F':
|
---|
| 253 | Result[i] := Result[i] + (Ord(UpCase(hex[1 + i * 2 + 1])) - 55);
|
---|
| 254 | end;
|
---|
| 255 | end;
|
---|
| 256 | end;
|
---|
| 257 |
|
---|
| 258 |
|
---|
| 259 |
|
---|
| 260 |
|
---|
| 261 | function StringSmaller(string1, string2: String): Boolean;
|
---|
| 262 | var
|
---|
| 263 | i: Integer;
|
---|
| 264 | len: Integer;
|
---|
| 265 | begin
|
---|
| 266 | len := Min(Length(string1), Length(string2));
|
---|
| 267 | for i := 1 to len do
|
---|
| 268 | if Ord(string1[i]) <> Ord(string2[i]) then
|
---|
| 269 | begin
|
---|
| 270 | Result := Ord(string1[i]) < Ord(string2[i]);
|
---|
| 271 | Exit;
|
---|
| 272 | end;
|
---|
| 273 | Result := Length(string1) < Length(string2);
|
---|
| 274 | end;
|
---|
| 275 |
|
---|
| 276 |
|
---|
| 277 |
|
---|
| 278 | function Explode(_string: String; delimiter: Char): TStrings;
|
---|
| 279 | var
|
---|
| 280 | start, len: Word;
|
---|
| 281 | begin
|
---|
| 282 | Result := TStringList.Create;
|
---|
| 283 | start := 1;
|
---|
| 284 | while PosEx(delimiter, _string, start) > 0 do
|
---|
| 285 | begin
|
---|
| 286 | len := PosEx(delimiter, _string, start) - start;
|
---|
| 287 | Result.Add(MidStr(_string, start, len));
|
---|
| 288 | start := start + len + 1;
|
---|
| 289 | end;
|
---|
| 290 | Result.Add(MidStr(_string, start, Length(_string) - start + 1));
|
---|
| 291 | end;
|
---|
| 292 |
|
---|
| 293 |
|
---|
| 294 | function GetWinFileName(Name: String): String;
|
---|
| 295 | begin
|
---|
| 296 | Result := Name;
|
---|
| 297 | Result := AnsiReplaceStr(Result, '\', '__');
|
---|
| 298 | Result := AnsiReplaceStr(Result, '/', '__');
|
---|
| 299 | Result := AnsiReplaceStr(Result, '>', '__');
|
---|
| 300 | Result := AnsiReplaceStr(Result, '<', '__');
|
---|
| 301 | end;
|
---|
| 302 |
|
---|
| 303 |
|
---|
| 304 | end.
|
---|