Recent

Author Topic: Searching for a safe alternative to "GetDirs" function  (Read 1595 times)

dbannon

  • Hero Member
  • *****
  • Posts: 3071
    • tomboy-ng, a rewrite of the classic Tomboy
Re: Searching for a safe alternative to "GetDirs" function
« Reply #15 on: September 13, 2024, 02:41:22 pm »
And it is unix only, since Windows allows both \ and /. So splitting by one character is not enough.

Now, I wondered about that. The OP had a ReadLn; at the end of his/her demo, that is something Windows user use. But the example PATH string is clearly a *nix one.  It appears GetDirs uses only the local PathDelim (as it should) so that code will not work under windows.

Anyway, far better practice to use Path.split(PathDelim....

Davo
Lazarus 3, Linux (and reluctantly Win10/11, OSX Monterey)
My Project - https://github.com/tomboy-notes/tomboy-ng and my github - https://github.com/davidbannon

marcov

  • Administrator
  • Hero Member
  • *
  • Posts: 11764
  • FPC developer.
Re: Searching for a safe alternative to "GetDirs" function
« Reply #16 on: September 13, 2024, 02:51:59 pm »
Now, I wondered about that. The OP had a ReadLn; at the end of his/her demo, that is something Windows user use. But the example PATH string is clearly a *nix one.  It appears GetDirs uses only the local PathDelim (as it should) so that code will not work under windows.

I don't know what code you are looking at (an old dos unit version?), but the sysutils version has a charinset() that checks multiple separators.

Code: Pascal  [Select][+][-]
  1. Function GetDirs (Var DirName : PathStr; Var Dirs : Array of PathPChar) : Longint; {$ifdef FPC_HAS_CPSTRING}rtlproc;{$endif}
  2.  
  3. Var I : Longint;
  4.  
  5. begin
  6.   I:=1;
  7.   Result:=-1;
  8.   While I<=Length(DirName) do
  9.     begin
  10.     If (CharInSet(DirName[i],AllowDirectorySeparators)
  11.     {$ifdef HASAMIGA}
  12.       or (DirName[i] = DriveSeparator)
  13.     {$endif}
  14.       ) and
  15.        { avoid error in case last AnsiChar=pathdelim }
  16.        (length(dirname)>i) then
  17.       begin
  18.         DirName[i]:=#0;
  19.         Inc(Result);
  20.         Dirs[Result]:=@DirName[I+1];
  21.       end;
  22.     Inc(I);
  23.     end;
  24.   If Result>-1 then inc(Result);
  25. end;
  26.  


dbannon

  • Hero Member
  • *****
  • Posts: 3071
    • tomboy-ng, a rewrite of the classic Tomboy
Re: Searching for a safe alternative to "GetDirs" function
« Reply #17 on: September 13, 2024, 02:58:32 pm »
The docs for GetDirs() says -

DirName must contain only OSDirSeparator as Directory separator chars.

https://www.freepascal.org/docs-html/rtl/sysutils/getdirs.html

Davo
Lazarus 3, Linux (and reluctantly Win10/11, OSX Monterey)
My Project - https://github.com/tomboy-notes/tomboy-ng and my github - https://github.com/davidbannon

marcov

  • Administrator
  • Hero Member
  • *
  • Posts: 11764
  • FPC developer.
Re: Searching for a safe alternative to "GetDirs" function
« Reply #18 on: September 13, 2024, 03:12:42 pm »
That's probably outdated then.

dbannon

  • Hero Member
  • *****
  • Posts: 3071
    • tomboy-ng, a rewrite of the classic Tomboy
Re: Searching for a safe alternative to "GetDirs" function
« Reply #19 on: September 14, 2024, 02:07:03 am »
Yes, probably.

AllowDirectorySeparators seems to be defined as both forward and back slash.  That is, in my opinion yet another reason to use the string.split() approach. If I used GetDirs() I would want to know that it is going to mess with both slashes, thats quite wrong, it should only operate on the PathDelim character appropriate for that OS. Yes, I know, 99.9% of cases it will not matter but ...

Davo

Lazarus 3, Linux (and reluctantly Win10/11, OSX Monterey)
My Project - https://github.com/tomboy-notes/tomboy-ng and my github - https://github.com/davidbannon

Thaddy

  • Hero Member
  • *****
  • Posts: 15646
  • Censorship about opinions does not belong here.
Re: Searching for a safe alternative to "GetDirs" function
« Reply #20 on: September 14, 2024, 09:01:01 am »
it should only operate on the PathDelim character appropriate for that OS. Yes, I know, 99.9% of cases it will not matter but ...
characters:
As Marcov pointed out already e.g. Windows allows both / and \ at the OS level.
If I smell bad code it usually is bad code and that includes my own code.

dbannon

  • Hero Member
  • *****
  • Posts: 3071
    • tomboy-ng, a rewrite of the classic Tomboy
Re: Searching for a safe alternative to "GetDirs" function
« Reply #21 on: September 14, 2024, 03:21:53 pm »
As Marcov pointed out already e.g. Windows allows both / and \ at the OS level.

Yes Thaddy, but Windows Users like yourself tend to forget that *nix systems only use one. And the other has its own, predefined use.

(At last, we are even, you called me a Windows User several years ago and never apologized.)

Davo    :P
Lazarus 3, Linux (and reluctantly Win10/11, OSX Monterey)
My Project - https://github.com/tomboy-notes/tomboy-ng and my github - https://github.com/davidbannon

 

TinyPortal © 2005-2018