-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathusaveload.pas
More file actions
212 lines (191 loc) · 5.95 KB
/
usaveload.pas
File metadata and controls
212 lines (191 loc) · 5.95 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
{ Формат сохранениея состоит из 3 строк:
Класс фигуры
Координаты [x]:[y];[x]:[y]...
Параметр:Значение;Параметр:Значение...}
unit USaveLoad;
{$mode objfpc}{$H+}
interface
uses
Classes, SysUtils, UFigures, UAppState, typinfo, strutils;
procedure SaveFile(APath: String; AFigures: array of TFigure);
function GetSaveStr(var AFigures: TFigureArr): String;
function FileLoad(APath: String; var AFigures: TFigureArr): Boolean;
procedure StringLoad(AString: String);
implementation
const
FILE_SIGNATURE = '--VECTORGRAPHICSEDITOR--SAVEDIMAGE--';
TypeKinds: TTypeKinds = tkAny;
function GetAllProps(AInstance: TObject; var APropList: PPropList): Integer;
var
PropCount: Integer;
begin
PropCount := GetPropList(AInstance.ClassInfo, TypeKinds, Nil);
GetMem(APropList, PropCount * SizeOf(PPropInfo));
GetPropList(AInstance.ClassInfo, TypeKinds, APropList);
Result := PropCount;
end;
function CreateFigure(AClassName: String; ACoords, AParams: TTwoDStrArr): TFigure;
var
i: Integer;
Figure: TFigure;
FigureClass: TClass;
PropList: PPropList;
PropCount: Integer;
PropValue: String;
PropKind: TTypeKind;
ParamProp: TObject;
begin
Figure := TFigureClass(FindClass(AClassName)).CreateFromCoords(ACoords);
for i := Low(AParams) to High(AParams) do begin
PropKind := GetPropInfo(Figure, AParams[i, 0])^.PropType^.Kind;
case PropKind of
tkInteger: SetInt64Prop(Figure, AParams[i, 0], StrToInt64(AParams[i, 1]));
tkClass: begin
ParamProp := GetObjectProp(Figure, AParams[i,0]);
SetPropValue(ParamProp, 'Value', AParams[i, 1]);
end;
end;
end;
Result := Figure;
end;
function SplitParams(AStr: String): TTwoDStrArr;
var
SplitedParams: TTwoDStrArr;
i: Integer;
Param: String;
begin
for i := 1 To WordCount(AStr, [';']) do begin
Param := ExtractDelimited(i, AStr, [';']);
SetLength(SplitedParams, Length(SplitedParams) + 1);
SetLength(SplitedParams[i - 1], 2);
SplitedParams[i - 1, 0] := ExtractDelimited(1, Param, [':']);
SplitedParams[i - 1, 1] := ExtractDelimited(2, Param, [':']);
end;
Result := SplitedParams;
end;
procedure SaveFile(APath: String; AFigures: array of TFigure);
var
OutFile: TextFile;
i, j: Integer;
PropList: PPropList;
PropCount: Integer;
PropValue: String;
PropKind: TTypeKind;
PropParampKind: TTypeKind;
ParamProp: TObject;
begin
try
Assign(OutFile, APath);
Rewrite(OutFile);
WriteLn(OutFile, FILE_SIGNATURE);
for i := Low(AFigures) to High(AFigures) do begin
PropCount := GetAllProps(AFigures[i], PropList);
writeln(OutFile, AFigures[i].ClassName);
writeln(OutFile, AFigures[i].GetStrCoord);
for j := 0 to PropCount - 1 do begin
PropKind := PropList^[j]^.PropType^.Kind;
case PropKind of
tkInteger: PropValue := IntToStr(GetInt64Prop(AFigures[i], PropList^[j]^.Name));
tkClass: begin
ParamProp := GetObjectProp(AFigures[i], PropList^[j]^.Name);
PropValue := GetPropValue(ParamProp, 'Value');
end;
end;
write(OutFile, PropList^[j]^.Name +':'+ PropValue + ';');
end;
writeln(OutFile);
end;
finally
Freemem(PropList);
Close(OutFile);
end;
SetFilePath(APath);
SetFileStateSaved;
end;
function GetSaveStr(var AFigures: TFigureArr): String;
var
SaveStr: String;
i, j: Integer;
PropList: PPropList;
PropCount: Integer;
PropValue: String;
PropKind: TTypeKind;
PropParampKind: TTypeKind;
ParamProp: TObject;
begin
SaveStr := '';
for i := Low(AFigures) to High(AFigures) do begin
PropCount := GetAllProps(AFigures[i], PropList);
SaveStr := Concat(SaveStr, AFigures[i].ClassName, ' ');
SaveStr := Concat(SaveStr, AFigures[i].GetStrCoord, ' ');
for j := 0 to PropCount - 1 do begin
PropKind := PropList^[j]^.PropType^.Kind;
case PropKind of
tkInteger: PropValue := IntToStr(GetInt64Prop(AFigures[i], PropList^[j]^.Name));
tkClass: begin
ParamProp := GetObjectProp(AFigures[i], PropList^[j]^.Name);
PropValue := GetPropValue(ParamProp, 'Value');
end;
end;
SaveStr := Concat(SaveStr, PropList^[j]^.Name +':'+ PropValue + ';');
end;
SaveStr := Concat(SaveStr, ' ');
FreeMem(PropList);
end;
Result := SaveStr;
end;
function FileLoad(APath: String; var AFigures: TFigureArr): Boolean;
var
InpFile: TextFile;
Line: String;
FigureClass, FigureCoords, FigureStrParams: String;
SplitedParams: TTwoDStrArr;
SplitedCoords: TTwoDStrArr;
i: Integer;
begin
for i := Low(AFigures) to High(AFigures) do
FreeAndNil(AFigures[i]);
AFigures := nil;
try
Assign(InpFile, APath);
Reset(InpFile);
ReadLn(InpFile, Line);
if Line <> FILE_SIGNATURE then
Exit(False);
while not EOF(InpFile) do begin
Readln(InpFile, FigureClass);
Readln(InpFile, FigureCoords);
Readln(InpFile, FigureStrParams);
SplitedCoords := SplitParams(FigureCoords);
SplitedParams := SplitParams(FigureStrParams);
SetLength(AFigures, Length(AFigures) + 1);
AFigures[High(AFigures)] := CreateFigure(FigureClass, SplitedCoords, SplitedParams);
end;
finally
Close(InpFile);
end;
SetFilePath(APath);
SetFileStateSaved;
Exit(True);
end;
procedure StringLoad(AString: String);
var
FigureClass, FigureCoords, FigureStrParams: String;
SplitedParams: TTwoDStrArr;
SplitedCoords: TTwoDStrArr;
i: Integer;
begin
for i := Low(Figures) to High(Figures) do
FreeAndNil(Figures[i]);
Figures := nil;
while Length(AString) <> 0 do begin
FigureClass := Copy2SpaceDel(AString);
FigureCoords := Copy2SpaceDel(AString);
FigureStrParams := Copy2SpaceDel(AString);
SplitedCoords := SplitParams(FigureCoords);
SplitedParams := SplitParams(FigureStrParams);
SetLength(Figures, Length(Figures) + 1);
Figures[High(Figures)] := CreateFigure(FigureClass, SplitedCoords, SplitedParams);
end;
end;
end.