1 | unit lbc_common;
|
---|
2 |
|
---|
3 | {$mode objfpc}{$H+}
|
---|
4 |
|
---|
5 | interface
|
---|
6 |
|
---|
7 | uses
|
---|
8 | SysUtils, Types, zint;
|
---|
9 |
|
---|
10 | function ustrlen(const AData: TByteDynArray): NativeInt;
|
---|
11 | procedure ustrcpy(var ATarget: TByteDynArray; const ASource: TByteDynArray);
|
---|
12 | procedure ustrcpy(var ATarget: TByteDynArray; const ASource: String);
|
---|
13 | procedure uconcat(var ADest: TByteDynArray; const ASource: TByteDynArray);
|
---|
14 | procedure uconcat(var ADest: TByteDynArray; const ASource: TCharDynArray);
|
---|
15 | procedure uconcat(var ADest: TByteDynArray; const ASource: String);
|
---|
16 |
|
---|
17 |
|
---|
18 | implementation
|
---|
19 |
|
---|
20 | { Local replacement for strlen() with uint8_t strings }
|
---|
21 | function ustrlen(const AData: TByteDynArray): NativeInt;
|
---|
22 | var
|
---|
23 | i: NativeInt;
|
---|
24 | begin
|
---|
25 | Result := High(AData) - Low(AData) + 1;
|
---|
26 | for i := Low(AData) to High(AData) do
|
---|
27 | if AData[i] = 0 then
|
---|
28 | begin
|
---|
29 | Result := i - Low(AData);
|
---|
30 | break;
|
---|
31 | end;
|
---|
32 | end;
|
---|
33 |
|
---|
34 | { Local replacement for strcpy() with uint8_t strings }
|
---|
35 | procedure ustrcpy(var ATarget: TByteDynArray; const ASource: TByteDynArray);
|
---|
36 | var
|
---|
37 | len: NativeInt;
|
---|
38 | begin
|
---|
39 | len := ustrlen(ASource);
|
---|
40 | if len > 0 then
|
---|
41 | begin
|
---|
42 | Move(ASource[0], ATarget[0], Len+1);
|
---|
43 | ATarget[len] := 0; // Be sure we have zero terminal
|
---|
44 | end;
|
---|
45 | end;
|
---|
46 |
|
---|
47 | procedure ustrcpy(var ATarget: TByteDynArray; const ASource: String);
|
---|
48 | var
|
---|
49 | len: NativeInt;
|
---|
50 | begin
|
---|
51 | len := Length(ASource);
|
---|
52 | if len > 0 then
|
---|
53 | begin
|
---|
54 | Move(ASource[1], ATarget[0], Len+1);
|
---|
55 | ATarget[len] := 0;
|
---|
56 | end;
|
---|
57 | end;
|
---|
58 |
|
---|
59 | { Concatinates dest[] with the contents of source[], copying /0 as well }
|
---|
60 | procedure uconcat(var ADest: TByteDynArray; const ASource: TByteDynArray);
|
---|
61 | var
|
---|
62 | j, n: NativeInt;
|
---|
63 | begin
|
---|
64 | j := ustrlen(ADest);
|
---|
65 | n := ustrlen(ASource);
|
---|
66 | Move(ASource[0], ADest[j], n);
|
---|
67 | ADest[j+n] := 0;
|
---|
68 | end;
|
---|
69 |
|
---|
70 | procedure uconcat(var ADest: TByteDynArray; const ASource: TCharDynArray);
|
---|
71 | var
|
---|
72 | j, n: NativeInt;
|
---|
73 | begin
|
---|
74 | j := ustrlen(ADest);
|
---|
75 | n := System.strlen(PChar(ASource));
|
---|
76 | Move(ASource[0], ADest[j], n);
|
---|
77 | ADest[j+n] := 0;
|
---|
78 | end;
|
---|
79 |
|
---|
80 | procedure uconcat(var ADest: TByteDynArray; const ASource: String);
|
---|
81 | begin
|
---|
82 | uconcat(ADest, PChar(ASource));
|
---|
83 | end;
|
---|
84 |
|
---|
85 | end.
|
---|
86 |
|
---|