-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathKmarDataStructure.pas
222 lines (186 loc) · 5.86 KB
/
KmarDataStructure.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
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
unit KmarDataStructure;
interface
uses SysUtils;
type TDataType=(Default,MyInteger,MyFloat,MyString);
type TFilterType=(MinLength,MaxLength,LessThen,MoreThen, ExcludeChar, IncludeChar);
type TFilter= class
FilterType:TFilterType;
FilterValue:String;
end;
type TDataItem=class
Name:string;
DataType:TDataType;
Filters:array of TFilter;
function incfilterlength: integer;
function decfilterlength: integer;
end;
type TFormStructure=class
structure : array of TDataItem;
function incdatalength:integer;
function decdatalength:integer;
function removedatafield(index:integer):integer;
procedure writedata(handle:Thandle);
procedure readdata(handle:THandle);
end;
implementation
procedure TFormstructure.writedata(handle:THandle);
var
i,o,u:integer;
begin
//
{Handle:=filecreate('kmarstructdata.ksd', fmopenwrite);
o:=length(self.structure);
filewrite(Handle, o, sizeof(integer));
for i:=0 to length(self.structure)-1 do
begin
// self.structure[i].Name, datatype, filter;
o:=length(self.structure[i].Name); //name
filewrite(Handle, o,sizeof(integer));
filewrite(Handle, PChar(self.structure[i].name)^, o*sizeof(char));
case self.structure[i].DataType of
default:
begin
o:=0;
end;
MyInteger:
begin
o:=1;
end;
MyFloat:
begin
o:=2;
end;
Mystring:
begin
o:=3;
end;
else o:=0;
end; //end of case
filewrite(handle, o, sizeof(integer)); //datatype
o:=length(self.structure[i].Filters)-1;
filewrite(Handle, o, sizeof(integer)); //how many filters
for u:=0 to length(self.structure[i].Filters)-1-1 do
begin
//self.structure[i].Filters[u].FilterType, value
case self.structure[i].Filters[u].FilterType of
MinLength: o:=0;
MaxLength: o:=1;
LessThen: o:=2;
MoreThen: o:=3;
ExcludeChar: o:=4;
IncludeChar: o:=5;
else o:=0;
end; //end of case
filewrite(Handle, o, sizeof(integer)); //filtertype
o:=length(self.structure[i].Filters[u].FilterValue);
filewrite(Handle, o, sizeof(integer));
filewrite(Handle, PChar(self.structure[i].Filters[u].FilterValue)^, o*sizeof(char)); //filtervalue
end; //end of for(u);
end; //end of for(i)
fileclose(Handle); }
end;
procedure TFormStructure.readdata(handle:THandle);
begin
{
//load last form from file if it exists
if fileexists('kmarstructdata.ksd') then
begin
//load lines into the structure
filehandle:=fileopen-( 'kmarstructdata.ksd', fmopenread);
fileread(filehandle, o, sizeof(integer)); //got field count
i:=0;
while (o>i) do i:=formstructure.incdatalength+1;
for i:=0 to length(formstructure.structure)-1 do
begin //for every field
fileread(filehandle, o, sizeof(integer));
setlength(formstructure.structure[i].Name, o);
fileread(filehandle, PChar(formstructure.structure[i].Name)^, o*sizeof(Char)); //got name
fileread(filehandle, o, sizeof(integer));
case o of
0: FormStructure.Structure[i].DataType:=Default;
1: FormStructure.Structure[i].DataType:=MyInteger;
2: FormStructure.Structure[i].DataType:=MyFloat;
3: FormStructure.Structure[i].DataType:=MyString;
end; //end of case
fileread(filehandle, o, sizeof(integer)); //filter count
u:=0;
while (u<o) do u:=formstructure.structure[i].incfilterlength+1;
for u:=0 to length(FormStructure.structure[i].Filters)-1 do
begin
fileread(filehandle, o, sizeof(integer));
case o of
0: FormStructure.Structure[i].Filters[u].FilterType:=MinLength;
1: FormStructure.Structure[i].Filters[u].FilterType:=MaxLength;
2: FormStructure.Structure[i].Filters[u].FilterType:=LessThen;
3: FormStructure.Structure[i].Filters[u].FilterType:=MoreThen;
4: FormStructure.Structure[i].Filters[u].FilterType:=ExcludeChar;
5: FormStructure.Structure[i].Filters[u].FilterType:=IncludeChar;
end; //end of case
fileread(filehandle, o, sizeof(integer));
setlength(FormStructure.structure[i].Filters[u].FilterValue, o);
fileread(filehandle, PChar(FormStructure.structure[i].Filters[u].FilterValue)^, o*sizeof(char)); //got filter value!
end; //end of for(u)
end; //end of for(i)
end; //end of fileexists }
end;
function TFormStructure.incdatalength:integer;
var o:integer;
begin
setlength(self.structure, length(self.structure)+1);
o:=length(self.structure)-1;
self.structure[o]:=TDataItem.Create;
result:=length(self.structure)-1;
end;
function TFormStructure.decdatalength:integer;
var o:integer;
begin
if length(self.structure)>0 then
begin
o:=length(self.structure)-1;
self.structure[o].Free;
setlength(self.structure, length(self.structure)-1);
result:=length(self.structure)-1;
end else result:= -1;
end;
function TFormStructure.removedatafield(index:integer):integer;
var
i:integer;
temp:TDataItem;
function last:integer;
begin
result:=length(self.structure)-1;
end;
begin
if (index>=0) and (index<=last) then
begin
//self.structure[index].Free;
temp:=self.structure[index];
for i:=index to last-1 do
begin
self.structure[i]:=self.structure[i+1];
end;
self.structure[last]:=temp;
self.decdatalength;
result:=last;
end else result:=-1;
end;
function TDataItem.incfilterlength:integer;
var o:integer;
begin
setlength(self.Filters, length(self.Filters)+1);
o:=length(self.Filters)-1;
self.Filters[o]:=TFilter.Create;
result:=length(self.Filters)-1;
end;
function TDataItem.decfilterlength:integer;
var o:integer;
begin
if length(self.Filters)>0 then
begin
o:=length(self.Filters)-1;
self.Filters[o].Free;
setlength(self.Filters, length(self.Filters)+1);
result:=length(self.Filters)-1;
end else result:=-1;
end;
end.