-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmiscfunctions.pas
More file actions
272 lines (236 loc) · 7.82 KB
/
miscfunctions.pas
File metadata and controls
272 lines (236 loc) · 7.82 KB
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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
(*
@AI:unit-summary: This Pascal unit provides utility functions for string manipulation, object-integer conversions, and form resizing, primarily aimed at enhancing user interface elements and managing object references in a list box context.
*)
unit MiscFunctions;
{$mode ObjFPC}{$H+}
interface
uses
Classes, SysUtils, StdCtrls, Forms, LCLIntf, LCLType, Controls, Math, Imaging, ImagingTypes, RegExpr;
type
TQRType = (QRCode, QRURL, QRPhoneNumber);
TQRTypes = set of TQRType;
function NoSpaces (stIn: string): string;
function NoDoubleSpace (stIn: string): string;
function O2I (obj: TObject): integer;
function I2O (Value: integer): TObject;
function ListObject (lb: TListBox): integer;
procedure SizeForm (frm, relativeTo: TForm; Scale: byte);
function SafeComponentName (Raw: string): string;
function CalcCRC32Hex (Stream: TStream): string;
procedure ResizeImageMax (var Img: TImageData; MaxWidth, MaxHeight: integer);
function FindAnyComponent (Root: TComponent; const Name: string): TComponent;
function ValidateQRCode (QRType: TQRType; QRString: string): boolean;
implementation
const
CRLF = chr(13) + chr(10);
function FindAnyComponent (Root: TComponent; const Name: string): TComponent;
(*
@AI:summary: This function likely searches for a component by its name within a specified root component.
@AI:params: Root: The root component from which the search for the named component begins.
@AI:params: Name: The name of the component to be searched for within the root component.
@AI:returns: The function is expected to return the found component or nil if not found.
*)
var
i: integer;
Found: TComponent;
begin
if Root.Name = Name then begin
Result := Root;
Exit;
end;
// Search recursively through subcomponents
for i := 0 to Root.ComponentCount - 1 do begin
Found := FindAnyComponent(Root.Components[i], Name);
if Assigned(Found) then begin
Result := Found;
Exit;
end;
end;
// Final fallback: use Root.FindComponent (only if supported)
if Assigned(Root) then begin
Found := Root.FindComponent(Name);
if Assigned(Found) then begin
Result := Found;
Exit;
end;
end;
Result := nil;
end;
procedure ResizeImageMax (var Img: TImageData; MaxWidth, MaxHeight: integer);
var
Scale: single;
NewWidth, NewHeight: integer;
begin
if (Img.Width <= MaxWidth) and (Img.Height <= MaxHeight) then begin
Exit;
end;
Scale := Min(MaxWidth / Img.Width, MaxHeight / Img.Height);
NewWidth := Round(Img.Width * Scale);
NewHeight := Round(Img.Height * Scale);
ResizeImage(Img, NewWidth, NewHeight, rfLanczos);
end;
function CalcCRC32Hex (Stream: TStream): string;
const
CRC32_POLY = $EDB88320;
var
Table: array[0..255] of longword;
CRC: longword;
I, J: integer;
Buf: array[0..4095] of byte;
Count: integer;
begin
// Generate table once (static cache optional)
for I := 0 to 255 do begin
CRC := I;
for J := 0 to 7 do begin
if (CRC and 1) <> 0 then begin
CRC := (CRC shr 1) xor CRC32_POLY;
end else begin
CRC := CRC shr 1;
end;
end;
Table[I] := CRC;
end;
CRC := $FFFFFFFF;
Stream.Position := 0;
repeat
Count := Stream.Read(Buf, SizeOf(Buf));
for I := 0 to Count - 1 do begin
CRC := (CRC shr 8) xor Table[(CRC xor Buf[I]) and $FF];
end;
until Count = 0;
CRC := CRC xor $FFFFFFFF;
Result := IntToHex(CRC, 8);
end;
procedure SizeForm (frm, relativeTo: TForm; Scale: byte);
(*
@AI:summary: Adjusts the size of a form based on a specified scaling factor relative to another form.
@AI:params: frm: The form whose size will be adjusted.
@AI:params: relativeTo: The form that serves as the reference for scaling.
@AI:params: Scale: The scaling factor to apply to the size of the form.
@AI:returns:
*)
var
TargetMonitor: TMonitor;
ScaledWidth, ScaledHeight: integer;
begin
// Clamp Scale between 50% and 100%
Scale := Min(100, Max(50, Scale));
if Assigned(relativeTo) then begin
TargetMonitor := Screen.MonitorFromWindow(HWND(relativeTo.Handle));
end else begin
TargetMonitor := Screen.Monitors[0];
end;
ScaledWidth := (TargetMonitor.Width * Scale) div 100;
ScaledHeight := (TargetMonitor.Height * Scale) div 100;
frm.Width := ScaledWidth;
frm.Height := ScaledHeight;
frm.Left := TargetMonitor.Left + ((TargetMonitor.Width - frm.Width) div 2);
frm.Top := TargetMonitor.Top + ((TargetMonitor.Height - frm.Height) div 2);
end;
function SafeComponentName (Raw: string): string;
(*
@AI:summary: This function likely sanitizes or processes a raw string to ensure it is safe for use as a component name.
@AI:params: Raw: The input string that needs to be sanitized or processed.
@AI:returns: A sanitized string that is safe for use as a component name.
*)
var
Cleaned: string;
i: integer;
begin
Cleaned := '';
for i := 1 to Length(Raw) do begin
if Raw[i] in ['A'..'Z', 'a'..'z', '0'..'9', '_'] then begin
Cleaned := Cleaned + Raw[i];
end;
end;
Cleaned := Trim(Cleaned);
if Cleaned = '' then begin
Cleaned := 'X';
end;
if Cleaned[1] in ['0'..'9'] then begin
Cleaned := 'C_' + Cleaned;
end;
Result := Cleaned;
end;
function NoSpaces (stIn: string): string;
(*
@AI:summary: This function removes all spaces from the input string.
@AI:params: stIn: The input string from which spaces will be removed.
@AI:returns: A string that contains the input string without any spaces.
*)
begin
Result := StringReplace(stIn, ' ', '', [rfReplaceAll]);
end;
(*
@AI:summary: This function appears to remove any double spaces from the input string, as in {space}{space} is converted to {space}.
@AI:params: stIn: The input string that may contain double spaces which need to be eliminated.
@AI:returns: A string with all double spaces removed.
*)
function NoDoubleSpace (stIn: string): string;
begin
while Pos(crlf + crlf, stin) <> 0 do begin
stIn := StringReplace(stIn, crlf + crlf, crlf, [rfReplaceAll]);
end;
Result := trim(stIn);
end;
(*
@AI:action O2I: Converts a TObject pointer into an integer. Used to store object references (e.g., ListBox/ComboBox items) as Integer keys.
@AI:params: obj: The object whose memory address will be cast to an integer.
@AI:returns: Integer representation of the object's pointer.
*)
function O2I (obj: TObject): integer;
var
ptr: Pointer;
begin
ptr := Pointer(obj);
Result := integer(ptr);
end;
{*
@AI:action I2O: Converts an integer (originally created from a TObject) back into a TObject pointer.
@AI:params: id: An integer representing a stored object pointer.
@AI:returns: The original TObject corresponding to that integer.
*}
function I2O (Value: integer): TObject;
var
ptr: Pointer;
begin
ptr := Pointer(Value);
Result := TObject(ptr);
end;
function ListObject (lb: TListBox): integer;
(*
@AI:summary: This function uses the o2i function to take the tObject value stored on a tStringList item and returns its integer value. Defaults to -1 if no item is selected in the listbox as a "safety net".
@AI:params: lb: The list box from which the item count is to be obtained.
@AI:returns: An integer representing the tObject value as an integer.
*)
begin
Result := -1;
if lb.ItemIndex <> -1 then begin
Result := o2i(lb.Items.Objects[lb.ItemIndex]);
end;
end;
function ValidateQRCode (QRType: TQRType; QRString: string): boolean;
(*
@AI:summary: Validates a QR code based on its type and content.
@AI:params: QRType: Specifies the type of QR code to validate.
@AI:params: QRString: The actual string content of the QR code to be validated.
@AI:returns: Returns true if the QR code is valid, otherwise false.
*)
begin
Result := False;
case QRType of
QRCode: begin
Result := ExecRegExpr('^[0-9]{4}-[0-9]{3}$', QRString);
end;
QRURL: begin
// TODO: Implement URL validation
Result := True;
end;
QRPhoneNumber: begin
// TODO: Implement phone number validation
Result := True;
end;
end;
end;
end.