forked from JOGAsoft/EBC-controller
-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathMyIniFile.pas
166 lines (148 loc) · 4.25 KB
/
MyIniFile.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
unit MyIniFile;
{$mode objfpc}
interface
uses
IniFiles, SysUtils, Classes, Forms, Types, strutils;
type
TMyIniFile = class(TIniFile)
private
function StrToHex(s: string): string;
function HexToStr(s: string): string;
public
procedure ReadStrings(const Section, Ident: string; var AStrings: TStringList; ADefault: string = '');
procedure WriteStrings(const Section, Ident: string; const AStrings: TStringList);
procedure WriteHexString(const Section, Ident, Value: string);
function ReadHexString(const Section, Ident: string; const ADefault: string = ''): string;
procedure ReadHexStrings(const Section, Ident: string; var AStrings: TStringList; ADefault: string = '');
procedure WriteHexStrings(const Section, Ident: string; const AStrings: TStringList);
// AD 10/2023: support for Ident=int,int...
procedure ReadIntegers(const Section, Ident: string; var ints: TIntegerDynArray);
procedure WriteIntegers(const Section, Ident: string; const ints:TIntegerDynArray);
end;
function RowIdent(const Ident: string; I: Integer): string;
implementation
{
MBY 2008
Denna unit implementerar extra funktioner hos inifiler.
WriteHexStr och ReadHexStr konverterar en sträng till en hexsträng så
att godtyckliga bytes (t.ex. ascii #0 och #26) kan skrivas/läsas.
}
function RowIdent(const Ident: string; I: Integer): string;
begin
Result := Ident + '_' + IntToStr(I);
end;
{ TMyIniFile }
function TMyIniFile.HexToStr(s: string): string;
var
I, V: Integer;
begin
I := 1;
V := 1;
Result := '';
SetLength(Result, Length(s) div 2);
while I < Length(s) do
begin
Result[V] := Chr(StrToIntDef('$' + Copy(s, I, 2), 0));
Inc(V);
Inc(I, 2);
end;
end;
function TMyIniFile.ReadHexString(const Section, Ident: string; const ADefault: string = ''): string;
begin
Result := HexToStr(ReadString(Section, Ident, ADefault));
end;
procedure TMyIniFile.WriteStrings(const Section, Ident: string; const AStrings: TStringList);
var
I: Integer;
begin
for I := 0 to AStrings.Count - 1 do
begin
WriteString(Section, RowIdent(Ident, I), AStrings.Strings[I]);
end;
end;
procedure TMyIniFile.ReadStrings(const Section, Ident: string; var AStrings: TStringList; ADefault: string = '');
var
I: Integer;
begin
// AStrings.Clear;
I := 0;
while ValueExists(Section, RowIdent(Ident, I)) do
begin
AStrings.Add(ReadString(Section, RowIdent(Ident, I), ADefault));
Inc(I);
end;
end;
function TMyIniFile.StrToHex(s: string): string;
var
I, V: Integer;
h: string;
begin
Result := '';
SetLength(Result, 2 * Length(s));
V := 1;
for I := 1 to Length(s) do
begin
h := IntToHex(Ord(s[I]), 2);
Result[V] := h[1];
Result[V + 1] := h[2];
Inc(V, 2);
end;
end;
procedure TMyIniFile.WriteHexString(const Section, Ident, Value: String);
begin
WriteString(Section, Ident, StrToHex(Value));
end;
procedure TMyIniFile.ReadHexStrings(const Section, Ident: string;
var AStrings: TStringList; ADefault: string);
var
I: Integer;
begin
AStrings.Clear;
I := 0;
while ValueExists(Section, RowIdent(Ident, I)) do
begin
AStrings.Add(ReadHexString(Section, RowIdent(Ident, I), ADefault));
Inc(I);
end;
end;
procedure TMyIniFile.WriteHexStrings(const Section, Ident: string;
const AStrings: TStringList);
var
I: Integer;
begin
for I := 0 to AStrings.Count - 1 do
begin
WriteHexString(Section, RowIdent(Ident, I), AStrings.Strings[I]);
end;
end;
procedure TMyIniFile.ReadIntegers(const Section, Ident: string; var ints:TIntegerDynArray);
var
ss: TStringDynArray;
i,size : Integer;
s : string;
begin
setLength(ints,0);
s := ReadString(Section, Ident, '');
if length(s) < 1 then exit;
ss := SplitString(s,',');
size := 0;
for i := low(ss) to high(ss) do
begin
inc(size); setLength(ints,size);
ints[size-1] := StrToInt(ss[i]);
end;
end;
procedure TMyIniFile.WriteIntegers(const Section, Ident: string; const ints:TIntegerDynArray);
var
s: string;
i : Integer;
begin
s := '';
for i in ints do
begin
if length(s) > 0 then s := s + ',';
s := s + IntToStr(i);
end;
WriteString(Section,Ident,s);
end;
end.