From d6a99df706e5c391cb55732b2bf61e1a851a5ffc Mon Sep 17 00:00:00 2001 From: skalogryz Date: Wed, 6 Jan 2016 14:21:36 +0000 Subject: [PATCH] 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 --- components/iphonelazext/plistfile.pas | 85 ++++++++++++++++++- components/iphonelazext/tests/plist/plist.xml | 20 +++++ .../iphonelazext/tests/plist/plistread.lpi | 77 +++++++++++++++++ .../iphonelazext/tests/plist/plistread.lpr | 37 ++++++++ 4 files changed, 216 insertions(+), 3 deletions(-) create mode 100644 components/iphonelazext/tests/plist/plist.xml create mode 100644 components/iphonelazext/tests/plist/plistread.lpi create mode 100644 components/iphonelazext/tests/plist/plistread.lpr 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 @@ + + + + + + + + + + + + + + <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> 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. +