This repository has been archived by the owner on Sep 14, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
nososettings.pas
144 lines (125 loc) · 3.39 KB
/
nososettings.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
unit nososettings;
{$mode ObjFPC}{$H+}
INTERFACE
uses
Classes, SysUtils, strutils;
Type
TSetting = Record
name : string;
value : string;
Comment : string;
end;
Procedure SetSettingsFilename(Fname:string);
Procedure InitSetting(Name,value,comment:string);
Function GetSetInt(name:string):int64;
Function GetSetStr(name:string):string;
Function GetSetBool(name:string):boolean;
Procedure SaveSettings();
Procedure LoadSettings();
var
ArraySettings : Array of TSetting;
SettingsFilename : string = 'config.conf';
SetsFile : TextFile;
CS_Settings : TRTLCriticalSection;
IMPLEMENTATION
Procedure SetSettingsFilename(Fname:string);
Begin
SettingsFilename := Fname;
AssignFile(SetsFile, SettingsFilename);
End;
Procedure InitSetting(Name,value,comment:string);
Begin
EnterCriticalSection(CS_Settings);
SetLength(ArraySettings,Length(ArraySettings)+1);
ArraySettings[Length(ArraySettings)-1].name := name;
ArraySettings[Length(ArraySettings)-1].value := value;
ArraySettings[Length(ArraySettings)-1].Comment := comment;
LeaveCriticalSection(CS_Settings);
End;
Function GetSetInt(name:string):int64;
var
counter : integer;
Begin
result := 0;
EnterCriticalSection(CS_Settings);
for counter := 0 to high(ArraySettings) do
if UpperCase(ArraySettings[counter].name) = Uppercase(name) then
begin
result := StrToInt64Def(ArraySettings[counter].value,0);
break;
end;
LeaveCriticalSection(CS_Settings);
End;
Function GetSetStr(name:string):string;
var
counter : integer;
Begin
result := '';
EnterCriticalSection(CS_Settings);
for counter := 0 to high(ArraySettings) do
if UpperCase(ArraySettings[counter].name) = Uppercase(name) then
begin
result := ArraySettings[counter].value;
break;
end;
LeaveCriticalSection(CS_Settings);
End;
Function GetSetBool(name:string):boolean;
var
counter : integer;
Begin
result := false;
EnterCriticalSection(CS_Settings);
for counter := 0 to high(ArraySettings) do
if UpperCase(ArraySettings[counter].name) = Uppercase(name) then
begin
result := StrTOBoolDef(ArraySettings[counter].value,False);
break;
end;
LeaveCriticalSection(CS_Settings);
End;
Procedure SaveSettings();
var
counter : integer;
Begin
EnterCriticalSection(CS_Settings);
Rewrite(SetsFile);
for counter := 0 to high(ArraySettings) do
begin
writeln(SetsFile,format('# %s',[ArraySettings[counter].Comment]));
writeln(SetsFile,format('%s %s',[ArraySettings[counter].name,ArraySettings[counter].value]));
writeln(Setsfile,'');
end;
CloseFile(SetsFile);
EnterCriticalSection(CS_Settings);
End;
Procedure LoadSettings();
var
LLine : string;
DataArr : array of string;
Counter : integer;
Begin
EnterCriticalSection(CS_Settings);
reset(SetsFile);
While not eof(SetsFile) do
begin
Readln(SetsFile,LLine);
DataArr := SplitString(LLine,' ');
for counter := 0 to high(ArraySettings) do
begin
if Uppercase(DataArr[0]) = Uppercase(ArraySettings[counter].name) then
begin
ArraySettings[counter].value:= DataArr[1];
end;
end;
end;
CloseFile(SetsFile);
EnterCriticalSection(CS_Settings);
End;
INITIALIZATION
SetLength(ArraySettings,0);
AssignFile(SetsFile, SettingsFilename);
InitCriticalSection(CS_Settings);
FINALIZATION
DoneCriticalSection(CS_Settings);
END.{End unit}