{------------------------------------------------------------------------------} { Unit Name: IniName Purpose : Solve ini-file name Author : Vesa Lappalainen Date : 6.1.2001 Changed : 13.4.2001 - .\ replaced with GetCurrentDir() because the name is used in the moment the ini-file is read, so even the file is opened at the begin, the result can go to wrong directory Changed : 15.9.2001/vl + VCL/CLX compiling (define const CLX) ToDo : function GetIniName(const n:string='';const extension:string='.'ini'):string; - tries to solve the ini-name from n. If n='' then n is taken from current program name. If local file exits the tha is used, othervise the file in program directory function IniReadString(ini:TCustomIniFile;const sec,item,def:string; dofree:boolean=false) : string; - read string from ready opened ini-file. Can tolerate also epty sec/item-values (=> def). ini can also be nil. if dofree := true then frees ini after reading procedure IniWriteString(ini:TTCustomIniFile;const sec,item,s:string; dofree:boolean=false); - as previous but for write function CanWriteToFile(const FileName:string):boolean; - checks if it is possible to write to file. } {------------------------------------------------------------------------------} unit IniName; interface uses IniFiles; function GetIniName(const ns:string=''; const extension:string='.ini'):string; function IniReadString(ini:TCustomIniFile;const sec,item:string; const def:string='';dofree:boolean=false) : string; function IniReadInteger(ini:TCustomIniFile;const sec,item:string; const def:integer=0;dofree:boolean=false) : integer; procedure IniWriteString(ini:TCustomIniFile;const sec,item,s:string; dofree:boolean=false); procedure IniWriteInteger(ini:TCustomIniFile;const sec,item:string; s:integer; dofree:boolean=false); function CanWriteToFile(const FileName:string):boolean; function CanCreateFile(const FileName:string):boolean; implementation uses Sysutils; {------------------------------------------------------------------------------} { GetIniName - finds IniFile name from n. } {------------------------------------------------------------------------------} function GetIniName(const ns:string; const extension:string):string; { if n includes any \ => n if n = '-' => '' if no path in n => ProgramPath+n if n = '' => ProgramNamePart+'.ini' if no .ini in n => n+'.ini' if n is .ext => ProgramNamePart+'.ext' } var n,ext,name,path : string; i:integer; begin n := ns; ext := extension; if ( Pos('.',n) = 1 ) and ( Pos('\',n) = 0 ) and ( Pos('/',n) = 0 ) then begin ext := n; n := ''; end; Result := n; if ( Pos('\',n) <> 0 ) or ( Pos('/',n) <> 0 ) then begin // Result := ChangeAll(Result,'\',PathDelim); // Result := ChangeAll(Result,'/',PathDelim); exit; end; Result := ''; if ( n = '-' ) then exit; path := ExtractFilePath(ParamStr(0)); Result := n; if ( n = '' ) then begin name := ExtractFileName(ParamStr(0)); i := Pos('.',name); if ( i > 0 ) then Delete(name,i,10); Result := name; end; if ( ExtractFileExt(Result) = '' ) then Result := Result + ext; if ( FileExists(GetCurrentDir()+Result) ) then begin Result := GetCurrentDir()+Result; exit; end; Result := path + Result; end; {------------------------------------------------------------------------------} function IniReadString(ini:TCustomIniFile;const sec,item:string;const def:string='';dofree:boolean=false) : string; begin Result := def; if ( ini = NIL ) then exit; try if ( sec = '' ) or ( item = '' ) then exit; Result := Ini.ReadString(sec,item,def); finally if ( dofree ) then ini.Free; end; end; function IniReadInteger(ini:TCustomIniFile;const sec,item:string; const def:integer=0;dofree:boolean=false) : integer; begin Result := StrToIntDef(IniReadString(ini,sec,item,'',dofree),def); end; {------------------------------------------------------------------------------} procedure IniWriteString(ini:TCustomIniFile;const sec,item,s:string;dofree:boolean=false); begin if ( ini = NIL ) then exit; try if ( sec = '' ) or ( item = '' ) then exit; Ini.WriteString(sec,item,s); finally if ( dofree ) then ini.Free; end; end; procedure IniWriteInteger(ini:TCustomIniFile;const sec,item:string; s:integer; dofree:boolean=false); begin IniWriteString(ini,sec,item,IntToStr(s),dofree); end; function CanWriteToFile(const FileName:string):boolean; {$ifdef DELPHI5} var f:TextFile; begin {$i-} AssignFile(f,FileName); Append(f); Result := IOResult = 0; CloseFile(f); {$i-} end; {$else} begin Result := not FileIsReadOnly(FileName); end; {$endif} function CanCreateFile(const FileName:string):boolean; var {$ifdef DELPHI5} f:TextFile; begin AssignFile(f,FileName); Rewrite(f); Result := IOResult = 0; CloseFile(f); DeleteFile(FileName); {$i-} end; {$else} path : string; begin Result := false; path := ExtractFilePath(FileName); try if ( path <> '' ) and ( not ForceDirectories(path) ) then exit; except exit; end; end; {$endif} end.