@@ -44,25 +44,32 @@ interface
4444
4545type
4646
47- TYamlOptionsSerializer = class (TOptionsSerializer )
47+ TYamlOptionsSerializer = class (TOptionsFileSerializer )
4848 private
4949 fSerializer : TRTTIYaml;
50- function ParseFile (const aFilename : string; out aYamlObj : TYamlObject) : Boolean;
50+ function ParseFile (out aYamlObj : TYamlObject) : Boolean;
5151 public
52- constructor Create;
52+ constructor Create( const aFilename : string) ;
5353 destructor Destroy; override;
54- function Load (const aFilename : string; aSections : TSectionList; aFailOnSectionNotExists : Boolean) : Boolean; override;
55- function LoadSection (const aFilename : string; aSections : TSectionList; aOptions: TOptions) : Boolean; override;
56- procedure Save (const aFilename : string; aSections : TSectionList); override;
57- function GetFileSectionNames (const aFilename : string; out oSections : TArray<string>) : Boolean; override;
54+ function Load (aSections : TSectionList; aFailOnSectionNotExists : Boolean) : Boolean; override;
55+ function LoadSection (aSections : TSectionList; aOptions: TOptions) : Boolean; override;
56+ procedure Save (aSections : TSectionList); override;
57+ function GetFileSectionNames (out oSections : TArray<string>) : Boolean; override;
58+ function ConfigExists : Boolean; override;
5859 end ;
5960
6061implementation
6162
6263{ TYamlOptionsSerializer }
6364
64- constructor TYamlOptionsSerializer.Create ;
65+ function TYamlOptionsSerializer.ConfigExists : Boolean ;
6566begin
67+ Result := FileExists(Filename);
68+ end ;
69+
70+ constructor TYamlOptionsSerializer.Create(const aFilename : string);
71+ begin
72+ Filename := aFilename;
6673 fSerializer := TRTTIYaml.Create(TSerializeLevel.slPublishedProperty,True);
6774end ;
6875
@@ -72,14 +79,14 @@ destructor TYamlOptionsSerializer.Destroy;
7279 inherited ;
7380end ;
7481
75- function TYamlOptionsSerializer.GetFileSectionNames (const aFilename : string; out oSections : TArray<string>) : Boolean;
82+ function TYamlOptionsSerializer.GetFileSectionNames (out oSections : TArray<string>) : Boolean;
7683var
7784 yaml : TYamlObject;
7885 i : Integer;
7986begin
8087 Result := False;
8188 yaml := nil ;
82- if ParseFile(aFilename, yaml) then
89+ if ParseFile(yaml) then
8390 begin
8491 try
8592 for i := 0 to yaml.Count - 1 do
@@ -93,23 +100,23 @@ function TYamlOptionsSerializer.GetFileSectionNames(const aFilename : string; ou
93100 end ;
94101end ;
95102
96- function TYamlOptionsSerializer.ParseFile (const aFilename : string; out aYamlObj : TYamlObject) : Boolean;
103+ function TYamlOptionsSerializer.ParseFile (out aYamlObj : TYamlObject) : Boolean;
97104var
98105 fileoptions : string;
99106begin
100107 aYamlObj := nil ;
101- if FileExists(aFilename ) then
108+ if FileExists(Filename ) then
102109 begin
103- fileoptions := TFile.ReadAllText(aFilename ,TEncoding.UTF8);
104- if fileoptions.IsEmpty then EOptionLoadError.CreateFmt(' Config file "%s" is empty!' ,[ExtractFileName(aFilename )]);
110+ fileoptions := TFile.ReadAllText(Filename ,TEncoding.UTF8);
111+ if fileoptions.IsEmpty then EOptionLoadError.CreateFmt(' Config file "%s" is empty!' ,[ExtractFileName(Filename )]);
105112
106113 aYamlObj := TYamlObject.ParseYAMLValue(fileoptions) as TYamlObject;
107- if aYamlObj = nil then raise EOptionLoadError.CreateFmt(' Config file "%s" is damaged or not well-formed Yaml format!' ,[ExtractFileName(aFilename )]);
114+ if aYamlObj = nil then raise EOptionLoadError.CreateFmt(' Config file "%s" is damaged or not well-formed Yaml format!' ,[ExtractFileName(Filename )]);
108115 end ;
109116 Result := aYamlObj <> nil ;
110117end ;
111118
112- function TYamlOptionsSerializer.Load (const aFilename : string; aSections : TSectionList; aFailOnSectionNotExists : Boolean) : Boolean;
119+ function TYamlOptionsSerializer.Load (aSections : TSectionList; aFailOnSectionNotExists : Boolean) : Boolean;
113120var
114121 option : TOptions;
115122 yaml : TYamlObject;
@@ -118,7 +125,7 @@ function TYamlOptionsSerializer.Load(const aFilename : string; aSections : TSect
118125begin
119126 Result := False;
120127 // read option file
121- if ParseFile(aFilename, yaml) then
128+ if ParseFile(yaml) then
122129 begin
123130 found := 0 ;
124131 try
@@ -152,14 +159,14 @@ function TYamlOptionsSerializer.Load(const aFilename : string; aSections : TSect
152159 end ;
153160end ;
154161
155- function TYamlOptionsSerializer.LoadSection (const aFilename : string; aSections : TSectionList; aOptions: TOptions) : Boolean;
162+ function TYamlOptionsSerializer.LoadSection (aSections : TSectionList; aOptions: TOptions) : Boolean;
156163var
157164 yaml : TYamlObject;
158165 ypair : TYamlPair;
159166begin
160167 Result := False;
161168 // read option file
162- if ParseFile(aFilename, yaml) then
169+ if ParseFile(yaml) then
163170 begin
164171 try
165172 ypair := fSerializer.GetYamlPairByName(yaml,aOptions.Name );
@@ -177,7 +184,7 @@ function TYamlOptionsSerializer.LoadSection(const aFilename : string; aSections
177184 end ;
178185end ;
179186
180- procedure TYamlOptionsSerializer.Save (const aFilename : string; aSections : TSectionList);
187+ procedure TYamlOptionsSerializer.Save (aSections : TSectionList);
181188var
182189 option : TOptions;
183190 fileoptions : string;
@@ -198,7 +205,7 @@ procedure TYamlOptionsSerializer.Save(const aFilename : string; aSections : TSec
198205 end ;
199206 end ;
200207 fileoptions := yaml.ToYaml;
201- if not fileoptions.IsEmpty then TFile.WriteAllText(aFilename ,fileoptions);
208+ if not fileoptions.IsEmpty then TFile.WriteAllText(Filename ,fileoptions);
202209 finally
203210 yaml.Free;
204211 end ;
0 commit comments