-
Notifications
You must be signed in to change notification settings - Fork 26
/
Copy pathNsfwBox.Utils.pas
177 lines (154 loc) · 4.19 KB
/
NsfwBox.Utils.pas
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
unit NsfwBox.Utils;
interface
uses
Classes, SysUtils, System.Generics.Collections, System.IOUtils,
NsfwBox.Logging, XSuperObject;
type
PInterface = ^IInterface;
TJsonHelper = Class
public
class function ReadIntArray(const ASource: ISuperObject; AKeyName: string): TArray<Int64>; static;
class function ReadNativeIntArray(const ASource: ISuperObject; AKeyName: string): TArray<NativeInt>; static;
end;
TArrayHelper = Class
public
///<summary>Returns new array with values by given indexes.</summary>
class function PickValues<T>(const Ar: TArray<T>; AIndexes: TArray<integer>): TArray<T>; static;
End;
function GetFirstStr(Ar: TArray<string>): string;
function StrIn(const Ar: TArray<string>; AStr: string; AIgnoreCase: boolean = True): boolean;
function BytesCountToSizeStr(ABytesCount: int64): string;
function GetPercents(AFull, APiece: Real): integer;
function GetThumbByFileExt(const AFilename: string): string;
///<summary>FreeAndNil for interfaced objects without reference counting.</summary>
procedure FreeInterfaced(const [ref] AObject: IInterface);
implementation
uses Unit1, NsfwBox.Styling;
procedure FreeInterfaced(const [ref] AObject: IInterface);
var
LTmp: TObject;
begin
try
LTmp := AObject As TObject;
FreeAndNil(LTmp);
TObject(Pointer(@AObject)^) := nil;
except
On E: Exception do Log('Utils.FreeInterfaced', E);
end;
end;
function BytesCountToSizeStr(ABytesCount: int64): string;
const
UNITS: Tarray<string> = ['Kb', 'Mb', 'Gb'];
var
I: integer;
LValue: int64;
begin
LValue := ABytesCount;
for I := 0 to High(UNITS) do begin
LValue := Round(LValue / 1024);
if LValue < 1024 then
Break;
end;
Result := LValue.ToString + ' ' + UNITS[I];
end;
function GetPercents(AFull, APiece: Real): integer;
var
X: Real;
begin
Result := 0;
try
if AFull > 0 then begin
X := AFull / 100;
Result := Round(APiece / X);
if Result > 100 then Result := 100;
end;
except
On E: Exception do begin
Log('GetPercents', E);
end;
end;
end;
function GetFirstStr(Ar: TArray<string>): string;
begin
if Length(Ar) > 0 then
Result := Ar[0]
else
Result := '';
end;
function StrIn(const Ar: TArray<string>; AStr: string; AIgnoreCase: boolean): boolean;
var
I: integer;
begin
Result := False;
for I := 0 to High(Ar) do begin
if AIgnoreCase then
Result := (UpperCase(Ar[I]) = UpperCase(AStr))
else
Result := (Ar[I] = AStr);
if Result then Exit;
end;
end;
function GetThumbByFileExt(const AFilename: string): string;
const
VIDEO: TArray<string> = ['.m4v', '.mp4', '.webm'];
AUDIO: TArray<string> = ['.mp3', '.m4a', '.ogg', '.wav'];
var
LExt: string;
begin
LExt := TPath.GetExtension(AFilename);
if StrIn(VIDEO, LExt, True) then
Result := Form1.AppStyle.GetImagePath(IMAGE_DUMMY_VIDEO)
else if StrIn(AUDIO, LExt, True) then
Result := Form1.AppStyle.GetImagePath(IMAGE_DUMMY_AUDIO)
else
Result := Form1.AppStyle.GetImagePath(IMAGE_LOADING);
end;
{ TJsonHelper }
class function TJsonHelper.ReadIntArray(const ASource: ISuperObject;
AKeyName: string): TArray<Int64>;
var
I: integer;
begin
if ASource.Null[AKeyName] = jAssigned then
begin
var Ar: ISuperArray := ASource.A[AKeyName];
SetLength(Result, Ar.Length);
for I := 0 to High(Result) do
Result[I] := Ar.I[I];
end else
Result := Nil;
end;
class function TJsonHelper.ReadNativeIntArray(const ASource: ISuperObject;
AKeyName: string): TArray<NativeInt>;
var
I: integer;
begin
if ASource.Null[AKeyName] = jAssigned then
begin
var Ar: ISuperArray := ASource.A[AKeyName];
SetLength(Result, Ar.Length);
for I := 0 to High(Result) do
Result[I] := Ar.I[I];
end else
Result := Nil;
end;
{ TArrayHelper }
class function TArrayHelper.PickValues<T>(const Ar: TArray<T>;
AIndexes: TArray<integer>): TArray<T>;
var
I, Len, Pos: integer;
begin
Result := [];
Len := Length(Ar);
if Len = 0 then Exit;
for I := Low(AIndexes) to High(AIndexes) do
begin
if (Len > AIndexes[I]) and (AIndexes[I] >= 0) then
begin
Pos := Length(Result);
SetLength(Result, Pos + 1);
Result[Pos] := Ar[AIndexes[I]];
end;
end;
end;
end.