Skip to content

Commit

Permalink
INI file exports refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
ollydev committed Jul 20, 2023
1 parent bf29a16 commit 748b8d0
Show file tree
Hide file tree
Showing 2 changed files with 144 additions and 18 deletions.
58 changes: 40 additions & 18 deletions Source/script/imports/simba/simba.import_file.pas
Original file line number Diff line number Diff line change
Expand Up @@ -25,33 +25,53 @@ implementation
*)

(*
WriteINI
~~~~~~~~
procedure WriteINI(Section, KeyName, NewString, FileName: String);
INIFileWrite
~~~~~~~~~~~~~
function INIFileWrite(FileName: String; Section, Key, Value: String): Boolean
*)
procedure _LapeWriteINI(const Params: PParamArray); LAPE_WRAPPER_CALLING_CONV
procedure _LapeINIFileWrite(const Params: PParamArray; const Result: Pointer); LAPE_WRAPPER_CALLING_CONV
begin
WriteINI(PString(Params^[0])^, PString(Params^[1])^, PString(Params^[2])^, PString(Params^[3])^);
PBoolean(Result)^ := INIFileWrite(PString(Params^[0])^, PString(Params^[1])^, PString(Params^[2])^, PString(Params^[3])^);
end;

(*
ReadINI
~~~~~~~
function ReadINI(Section, KeyName, FileName: String): String;
INIFileRead
~~~~~~~~~~~~~
function INIFileRead(FileName: String; Section, Key, Value: String): String
*)
procedure _LapeReadINI(const Params: PParamArray; const Result: Pointer); LAPE_WRAPPER_CALLING_CONV
procedure _LapeINIFileRead(const Params: PParamArray; const Result: Pointer); LAPE_WRAPPER_CALLING_CONV
begin
PString(Result)^ := ReadINI(PString(Params^[0])^, PString(Params^[1])^, PString(Params^[2])^);
PString(Result)^ := INIFileRead(PString(Params^[0])^, PString(Params^[1])^, PString(Params^[2])^, PString(Params^[3])^);
end;

(*
DeleteINI
~~~~~~~~~
procedure DeleteINI(Section, KeyName, FileName: String);
INIFileDelete
~~~~~~~~~~~~~
function INIFileDelete(FileName: String; Section, Key: String): Boolean
*)
procedure _LapeINIFileDelete(const Params: PParamArray; const Result: Pointer); LAPE_WRAPPER_CALLING_CONV
begin
PBoolean(Result)^ := INIFileDelete(PString(Params^[0])^, PString(Params^[1])^, PString(Params^[2])^);
end;

(*
INIFileKeys
~~~~~~~~~~~
function INIFileKeys(FileName: String; Section: String): TStringArray
*)
procedure _LapeINIFileKeys(const Params: PParamArray; const Result: Pointer); LAPE_WRAPPER_CALLING_CONV
begin
PStringArray(Result)^ := INIFileKeys(PString(Params^[0])^, PString(Params^[1])^);
end;

(*
INIFileSections
~~~~~~~~~~~~~~~
function INIFileSections(FileName: String): TStringArray
*)
procedure _LapeDeleteINI(const Params: PParamArray); LAPE_WRAPPER_CALLING_CONV
procedure _LapeINIFileSections(const Params: PParamArray; const Result: Pointer); LAPE_WRAPPER_CALLING_CONV
begin
DeleteINI(PString(Params^[0])^, PString(Params^[1])^, PString(Params^[1])^);
PStringArray(Result)^ := INIFileSections(PString(Params^[0])^);
end;

(*
Expand Down Expand Up @@ -570,9 +590,11 @@ procedure ImportFile(Compiler: TSimbaScript_Compiler);
addGlobalVar(SimbaEnv.DataPath, 'SIMBA_DATA_PATH').isConstant := True;
addGlobalVar(SimbaEnv.ScreenshotsPath, 'SCREENSHOTS_PATH').isConstant := True;

addGlobalFunc('procedure WriteINI(Section, KeyName, NewString, FileName: String)', @_LapeWriteINI);
addGlobalFunc('function ReadINI(Section, KeyName, FileName: String): String', @_LapeReadINI);
addGlobalFunc('procedure DeleteINI(Section, KeyName, FileName: String)', @_LapeDeleteINI);
addGlobalFunc('function INIFileWrite(FileName: String; Section, Key, Value: String): Boolean', @_LapeINIFileWrite);
addGlobalFunc('function INIFileRead(FileName: String; Section, Key, Value: String): String', @_LapeINIFileRead);
addGlobalFunc('function INIFileDelete(FileName: String; Section, Key: String): Boolean', @_LapeINIFileDelete);
addGlobalFunc('function INIFileKeys(FileName: String; Section: String): TStringArray', @_LapeINIFileKeys);
addGlobalFunc('function INIFileSections(FileName: String): TStringArray', @_LapeINIFileSections);

addGlobalFunc('function ZipExtractAll(ZipFileName, OutputDir: String): Boolean', @_LapeZipExtractAll);
addGlobalFunc('function ZipExtractOne(ZipFileName, FileName, OutputDir: String): Boolean', @_LapeZipExtractOne);
Expand Down
104 changes: 104 additions & 0 deletions Source/simba.files.pas
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,12 @@ TSimbaDir = class
procedure DeleteINI(const Section, KeyName : string; FileName : string);
procedure WriteINI(const Section, KeyName, NewString : string; FileName : string);

function INIFileWrite(FileName: String; Section, Key, Value: String): Boolean;
function INIFileRead(FileName: String; Section, Key, Value: String): String;
function INIFileDelete(FileName: String; Section, Key: String): Boolean;
function INIFileKeys(FileName: String; Section: String): TStringArray;
function INIFileSections(FileName: String): TStringArray;

implementation

uses
Expand Down Expand Up @@ -629,5 +635,103 @@ procedure WriteINI(const Section, KeyName, NewString : string; FileName : string
end;
end;

function INIFileWrite(FileName: String; Section, Key, Value: String): Boolean;
begin
Result := True;

try
with TINIFile.Create(FileName) do
try
WriteString(Section, Key, Value);
finally
Free();
end;
except
Result := False;
end;
end;

function INIFileRead(FileName: String; Section, Key, Value: String): String;
begin
Result := '';

try
with TINIFile.Create(FileName) do
try
Result := ReadString(Section, Key, Value);
finally
Free();
end;
except
end;
end;

function INIFileDelete(FileName: String; Section, Key: String): Boolean;
begin
Result := True;

try
with TIniFile.Create(FileName) do
try
if (Key = '') then
EraseSection(Section)
else
DeleteKey(Section, Key);
finally
Free();
end;
except
Result := False;
end;
end;

function INIFileKeys(FileName: String; Section: String): TStringArray;
var
List: TStringList;
begin
Result := [];

List := nil;
try
with TIniFile.Create(FileName) do
try
List := TStringList.Create();
ReadSection(Section, List);

Result := List.ToStringArray();
finally
Free();
end;
except

end;
if Assigned(List) then
List.Free();
end;

function INIFileSections(FileName: String): TStringArray;
var
List: TStringList;
begin
Result := [];

List := nil;
try
with TIniFile.Create(FileName) do
try
List := TStringList.Create();
ReadSections(List);

Result := List.ToStringArray();
finally
Free();
end;
except
end;

if Assigned(List) then
List.Free();
end;

end.

0 comments on commit 748b8d0

Please sign in to comment.