[Lazarus] need suggestion about ParamStrUTF8(0)

FreeMan freeman35 at delphiturkiye.com
Tue Feb 17 19:03:14 CET 2015


This is my solution, this function's job is: If parameter AFilename is 
symbolic link, get it's target path, and "../" symbols remove and result 
set to link's target path.
this function useful for unix based system, I tested just osx. Can it 
add in main source utils of lazarus ?

function GetTargetIfFileIsSymlink(const AFilename: string): string;
var FileAttr: LongInt;
     I, J : Integer;
begin
   Result := '';
   if not FileExistsUTF8(AFilename) then begin
     raise Exception.Create(Format(lrsFileDoesNotExist, [AFilename]));
   end;
   FileAttr := FileGetAttr(AFilename);
   if(FileAttr and faSymLink)=0then Result := ExtractFilePath(AFilename)
   else begin
    Result := fpReadLink(AFilename);
     with TStringList.Create do
     begin
       Delimiter := DirectorySeparator;
       DelimitedText := Result;
       J := 0;
       for I:=0 to Count -1 do
        if Strings[I]='..'then inc(J);

       if J>0then begin
         DelimitedText := ExtractFilePath(AFilename);
         while (J>0) and (Count>0) do begin
           Delete(Count-1);
           Dec(J);
         end;
       end;
       Result := DelimitedText;
       Free;
     end;
   end;
end;

On 17.02.2015 18:12, FreeMan wrote:
>
>   {$IFDEF UNIX}
>     S := ParamStrUTF8(0);
>    //CheckIfFileIsSymlink(S); I wish, this be a function and result be 
> is fpReadLink, 'cos its in :)
>     FileAttr := FileGetAttr(S);
>     if(FileAttr and faSymLink)>0then S := fpReadLink(S)
>     else s:= 'normal file';
>   {$ENDIF}





More information about the Lazarus mailing list