diff --git a/components/iphonelazext/plistfile.pas b/components/iphonelazext/plistfile.pas
index 1008bed16..aee41d853 100644
--- a/components/iphonelazext/plistfile.pas
+++ b/components/iphonelazext/plistfile.pas
@@ -14,12 +14,12 @@
}
unit PlistFile;
-{$mode delphi}
+{$mode delphi}{$h+}
interface
uses
- Classes, SysUtils, DOM, XMLRead, XMLWrite, LazFilesUtils;
+ Classes, SysUtils, DOM, XMLRead;
type
TPlistType = (ltString, ltArray, ltDict, ltData, ltDate, ltBoolean, ltNumber);
@@ -40,23 +40,29 @@ type
items : array of TPListValue;
names : array of string;
constructor Create(AType: TPlistType);
+ destructor Destroy; override;
function AddValue: Integer;
property ValueType: TPListType read fType;
+ function FindValue(const nm: string): Integer;
end;
{ TPListFile }
TPListFile = class(TObject)
+ protected
+ function GetValueIdx(const aname: string; force: Boolean): Integer;
public
root : TPListValue;
constructor Create;
destructor Destroy; override;
function GetStrValue(const valname: string): string;
+ procedure SetStrValue(const AName: string; const AValue: WideString; AValType: TPlistType = ltString);
end;
function LoadFromXML(const fn: string; plist: TPListFile): Boolean; overload;
function LoadFromXML(doc: TXMLDocument; plist: TPListFile): Boolean; overload;
function WriteXML(const plist: TPlistFile): string;
+function SaveToXMLFile(plist: TPlistFile; const fn: string): Boolean;
procedure DebugPlistFile(const fl: TPListFile);
@@ -231,6 +237,31 @@ begin
end;
end;
+function SaveToXMLFile(plist: TPlistFile; const fn: string): Boolean;
+var
+ fs : TFileStream;
+ s : string;
+begin
+ if not Assigned(plist) then begin
+ Result:=false;
+ Exit;
+ end;
+ try
+ fs:=TfileStream.Create(fn, fmCreate);
+ try
+ s:=WriteXML(plist);
+ if length(s)>0 then
+ fs.Write(s[1], length(s));
+ Result:=true;
+ finally
+ fs.Free;
+ end;
+ except
+ Result:=false;
+ end;
+end;
+
+
function LoadFromXML(const fn: string; plist: TPListFile): Boolean; overload;
var
doc : TXMLDocument;
@@ -330,10 +361,24 @@ begin
Result:=true;
end;
+function TPListFile.GetValueIdx(const aname: string; force: Boolean): Integer;
+var
+ idx: integer;
+begin
+ idx:=root.FindValue(aname);
+ if (idx<0) and (force) then begin
+ idx:=root.AddValue;
+ if not Assigned(root.Items[idx]) then
+ root.items[idx]:=TPListValue.Create(ltString);
+ root.names[idx]:=aname;
+ end;
+ Result:=idx;
+end;
+
constructor TPListFile.Create;
begin
inherited Create;
- root:=TPListValue.Create(ltDict)
+ root:=TPListValue.Create(ltDict);
end;
destructor TPListFile.Destroy;
@@ -360,6 +405,18 @@ begin
end;
+procedure TPListFile.SetStrValue(const AName: string; const AValue: WideString; AValType: TPlistType);
+var
+ idx: integer;
+begin
+ idx:=GetValueIdx(aname, true);
+ writeln('idx= ', idx,' ',length(root.items),' ',root.count,' ',Assigned(root.items[idx]));
+ root.items[idx].str:=avalue;
+ writeln('222');
+ root.items[idx].fType:=AValType;
+ writeln('333');
+end;
+
{ TPListValue }
constructor TPListValue.Create(AType: TPlistType);
@@ -368,6 +425,16 @@ begin
fType:=AType;
end;
+destructor TPListValue.Destroy;
+var
+ i : integer;
+begin
+ for i:=0 to length(items)-1 do
+ if Assigned(items[i]) then
+ items[i].Free;
+ inherited Destroy;
+end;
+
function TPListValue.AddValue: Integer;
begin
if not (fType in [ltArray, ltDict]) then begin
@@ -383,5 +450,17 @@ begin
inc(count);
end;
+function TPListValue.FindValue(const nm: string): Integer;
+var
+ i : integer;
+begin
+ for i:=0 to count-1 do
+ if names[i]=nm then begin
+ Result:=i;
+ Exit;
+ end;
+ Result:=-1;
+end;
+
end.
diff --git a/components/iphonelazext/tests/plist/plist.xml b/components/iphonelazext/tests/plist/plist.xml
new file mode 100644
index 000000000..ba5125385
--- /dev/null
+++ b/components/iphonelazext/tests/plist/plist.xml
@@ -0,0 +1,20 @@
+
+
+
+
+ CFBundleDevelopmentRegionEnglish
+ CFBundleDisplayName%s
+ CFBundleExecutable%s
+ CFBundleIdentifier%s
+ CFBundleInfoDictionaryVersion6.0
+ CFBundleName%s
+ CFBundlePackageTypeAPPL
+ CFBundleSignature????
+ CFBundleSupportedPlatforms
+ %s
+ CFBundleVersion1.0
+ DTPlatformName%s
+ DTSDKName%s
+ LSRequiresIPhoneOS
+
+
diff --git a/components/iphonelazext/tests/plist/plistread.lpi b/components/iphonelazext/tests/plist/plistread.lpi
new file mode 100644
index 000000000..af8a42424
--- /dev/null
+++ b/components/iphonelazext/tests/plist/plistread.lpi
@@ -0,0 +1,77 @@
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
diff --git a/components/iphonelazext/tests/plist/plistread.lpr b/components/iphonelazext/tests/plist/plistread.lpr
new file mode 100644
index 000000000..9cc358de7
--- /dev/null
+++ b/components/iphonelazext/tests/plist/plistread.lpr
@@ -0,0 +1,37 @@
+program plistread;
+
+{$mode objfpc}{$H+}
+
+uses
+ {$IFDEF UNIX}{$IFDEF UseCThreads}
+ cthreads,
+ {$ENDIF}{$ENDIF}
+ Classes, SysUtils, plistfile
+ { you can add units after this };
+
+procedure TestRead(const fn: string);
+var
+ pl : TPListFile;
+begin
+ pl:=TPlistFile.Create;
+ try
+ LoadFromXML(fn, pl);
+ writeln(pl.GetStrValue('CFBundleDevelopmentRegion'));
+ pl.SetStrValue('DTPlatformName','TESTPLATFORMNAME');
+ SaveToXMLFile(pl, ChangeFileExt(fn, '.xml.out'));
+ finally
+ pl.Free;
+ end;
+end;
+
+var
+ fn : string;
+begin
+ if ParamCount=0 then begin
+ writeln('please specify the .info file');
+ Exit;
+ end;
+ fn := ParamStr(1);
+ TestRead(fn);
+end.
+