iphonelazext: plistfile test project - plistread. Added an ability to add more string values to plist file

git-svn-id: https://svn.code.sf.net/p/lazarus-ccr/svn@4415 8e941d3f-bd1b-0410-a28a-d453659cc2b4
This commit is contained in:
skalogryz 2016-01-06 14:21:36 +00:00
parent f3a55dedd4
commit d6a99df706
4 changed files with 216 additions and 3 deletions

View File

@ -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.

View File

@ -0,0 +1,20 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>CFBundleDevelopmentRegion</key><string>English</string>
<key>CFBundleDisplayName</key><string>%s</string>
<key>CFBundleExecutable</key><string>%s</string>
<key>CFBundleIdentifier</key><string>%s</string>
<key>CFBundleInfoDictionaryVersion</key><string>6.0</string>
<key>CFBundleName</key><string>%s</string>
<key>CFBundlePackageType</key><string>APPL</string>
<key>CFBundleSignature</key><string>????</string>
<key>CFBundleSupportedPlatforms</key>
<array><string>%s</string></array>
<key>CFBundleVersion</key><string>1.0</string>
<key>DTPlatformName</key><string>%s</string>
<key>DTSDKName</key><string>%s</string>
<key>LSRequiresIPhoneOS</key><true/>
</dict>
</plist>

View File

@ -0,0 +1,77 @@
<?xml version="1.0" encoding="UTF-8"?>
<CONFIG>
<ProjectOptions>
<Version Value="9"/>
<PathDelim Value="\"/>
<General>
<Flags>
<MainUnitHasCreateFormStatements Value="False"/>
<MainUnitHasTitleStatement Value="False"/>
</Flags>
<SessionStorage Value="InProjectDir"/>
<MainUnit Value="0"/>
<Title Value="plistread"/>
<UseAppBundle Value="False"/>
<ResourceType Value="res"/>
</General>
<i18n>
<EnableI18N LFM="False"/>
</i18n>
<VersionInfo>
<StringTable ProductVersion=""/>
</VersionInfo>
<MacroValues Count="1">
<Macro1 Name="LCLWidgetType" Value="nogui"/>
</MacroValues>
<BuildModes Count="1">
<Item1 Name="Default" Default="True"/>
<SharedMatrixOptions Count="1">
<Item1 ID="322992283170" Modes="Default" Type="IDEMacro" MacroName="LCLWidgetType" Value="nogui"/>
</SharedMatrixOptions>
</BuildModes>
<PublishOptions>
<Version Value="2"/>
</PublishOptions>
<RunParams>
<local>
<FormatVersion Value="1"/>
</local>
</RunParams>
<Units Count="1">
<Unit0>
<Filename Value="plistread.lpr"/>
<IsPartOfProject Value="True"/>
</Unit0>
</Units>
</ProjectOptions>
<CompilerOptions>
<Version Value="11"/>
<PathDelim Value="\"/>
<Target>
<Filename Value="plistread"/>
</Target>
<SearchPaths>
<IncludeFiles Value="$(ProjOutDir)"/>
<OtherUnitFiles Value="..\.."/>
<UnitOutputDirectory Value="lib\$(TargetCPU)-$(TargetOS)"/>
</SearchPaths>
<Linking>
<Debugging>
<UseHeaptrc Value="True"/>
</Debugging>
</Linking>
</CompilerOptions>
<Debugging>
<Exceptions Count="3">
<Item1>
<Name Value="EAbort"/>
</Item1>
<Item2>
<Name Value="ECodetoolError"/>
</Item2>
<Item3>
<Name Value="EFOpenError"/>
</Item3>
</Exceptions>
</Debugging>
</CONFIG>

View File

@ -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.