{------------------------------------------------------------------------------} { Unit Name: SetIni Purpose : Set values in ini-files Author : Vesa Lappalainen Date : 13.7.2001 Changed : 28.10.2001/vl + works also in Linux (UpdateFile) + just print the value if 3 parameters + print whole section with 2 parametrs + print all sections with 1 parameters + delete option ToDo : } {------------------------------------------------------------------------------} program setini; {$APPTYPE CONSOLE} uses Classes,SysUtils,IniFiles; const Quiet = {$IFDEF MSWINDOWS} '/q'; {$ELSE} '-q'; {$ENDIF} const Del = {$IFDEF MSWINDOWS} '/d'; {$ELSE} '-d'; {$ENDIF} procedure Help; begin writeln(''); writeln('SetIni Ini-file [Section [Item [Value]]] ['+Quiet+']['+Del+']' ); writeln(''); writeln('Sets the selected Section/Item to new value in ini-file.'); writeln('Options '+ Quiet +' sets quiet, otherwise prints old value.'); writeln('Without Value just prints.'); writeln('Without Item prints whole section.'); writeln('Without Section prints names for every section.'); writeln('With option '+del+' deletes Ini-file, Section or Item.'); writeln(''); writeln('Example:'); writeln(' SetIni ./k2000p.ini User Name vesal'); writeln(''); writeln(' Sets in k2000p.ini:'); writeln(' [User]'); writeln(' Name=vesal'); writeln(''); writeln('Vesa Lappalainen 28.10.2001'); end; function IsOption(const s,opt:string):boolean; overload; begin Result := false; if ( Length(opt) < 2 ) then exit; if ( Length(s) < 2 ) then exit; if ( s[1] <> '-' ) and ( s[1] <> '/' ) then exit; if ( s[2] <> opt[2] ) then exit; Result := true; end; function IsOption(str:TStrings;opt:string):boolean; overload; var i:integer; begin Result := false; i := 0; while ( i < str.Count ) do if ( IsOption(str[i],opt) ) then begin str.Delete(i); Result := true; end else inc(i); end; function GetParams : TStrings; var i:integer; begin Result := TStringList.Create; for i:=0 to ParamCount do Result.Add(ParamStr(i)); end; procedure PrintSections(ini:TIniFile); var str : TStringList; i:integer; begin str := TStringList.Create; ini.ReadSections(str); for i:=0 to str.Count-1 do writeln(str[i]); str.Free; end; procedure PrintThisSection(ini:TIniFile;const sec:string); var str : TStringList; i:integer; begin str := TStringList.Create; ini.ReadSectionValues(sec,str); writeln('['+sec+']'); for i:=0 to str.Count-1 do writeln(str[i]); str.Free; end; procedure PrintItem(ini:TIniFile;const sec,item:string); var oldvalue : string; begin oldvalue := ini.ReadString(sec,item,''); writeln('['+sec+']'); writeln(item+'='+oldvalue); end; const name=1; sec=2; item=3; value=4; var ini : TIniFile; NeedUpdate,IsDel,IsPrint,IsHelp : boolean; Params : TStrings; begin if ( ParamCount < 1 ) then begin Help; exit; end; NeedUpdate := false; Params := GetParams; ini := nil; try IsDel := IsOption(Params,Del); IsPrint := not IsOption(Params,Quiet); IsHelp := IsOption(Params,'/?') or (Params.Count < 1) or (Params[1] ='?'); if IsHelp then begin if IsPrint then Help; exit; end; ini := TIniFile.Create(Params[name]); if ( Params.Count <= sec) then begin // Just file if ( IsPrint ) then PrintSections(ini); if ( IsDel ) then DeleteFile(Params[name]); exit; end; if ( Params.Count <= item ) then begin // Just file and section if ( IsPrint ) then PrintThisSection(ini,Params[sec]); if ( IsDel ) then begin ini.EraseSection(Params[sec]); NeedUpdate := true; end; exit; end; if ( IsPrint ) then PrintItem(ini,Params[sec],Params[item]); if ( Params.Count <= value ) then begin // file section item if ( IsDel ) then begin ini.DeleteKey(Params[sec],Params[item]); NeedUpdate := true; end; exit; end; ini.WriteString(Params[sec],Params[item],Params[value]); NeedUpdate := true; finally if ( NeedUpdate) then ini.UpdateFile; ini.Free; params.Free; end; end.