Recent

Author Topic: Is there a special "ExtractFileExt" for Linux?  (Read 3920 times)

QEnnay

  • Jr. Member
  • **
  • Posts: 93
Is there a special "ExtractFileExt" for Linux?
« on: August 20, 2021, 12:12:19 am »
Hi, I need to check if some file extensions are in a select group.

Code: Pascal  [Select][+][-]
  1.   anExt:=ExtractFileExt(aFile);
  2.  

With a Linux ".folder" in the middle of a path it returns what looks to be a single "dot", the text in debugger is too small to see exactly.

"/home/user/booth/bill.com" works as expected

"/home/user/booth/.config/bill/privatefile" does not

Sooo,is there a special ExtractFileExt that can handle dot-folders such as the above ".config"?
Linux-Mint 20.1 x64 + Cinnamon; Lenovo Flex 5 Ryzen 5 4500, 16GB memory
FPC: 3.2.0-1, Lazarus 2.0.12-0, all 64bit

Warfley

  • Hero Member
  • *****
  • Posts: 630
Re: Is there a special "ExtractFileExt" for Linux?
« Reply #1 on: August 20, 2021, 12:39:04 am »
You could first get the file name using ExtractFileName and call ExtractFileExt on that:
Code: Pascal  [Select][+][-]
  1. ext := ExtractFileExt(ExtractFileName(Path));

El Salvador

  • Jr. Member
  • **
  • Posts: 68
Re: Is there a special "ExtractFileExt" for Linux?
« Reply #2 on: August 20, 2021, 09:10:48 am »
Quote
"/home/user/booth/.config/bill/privatefile" does not
But this file has no extension. In fact, I just tried and the method returns an empty string.

If I use this path "/home/user/booth/.config/bill/privatefile.ext'" instead, it returns ".ext".

marcov

  • Administrator
  • Hero Member
  • *
  • Posts: 9713
  • FPC developer.
Re: Is there a special "ExtractFileExt" for Linux?
« Reply #3 on: August 20, 2021, 03:15:50 pm »
It is worth reporting it as a bug to have it looked at. No guarantees, but it might be an oversight.


winni

  • Hero Member
  • *****
  • Posts: 2807
Re: Is there a special "ExtractFileExt" for Linux?
« Reply #4 on: August 20, 2021, 04:07:47 pm »
It is worth reporting it as a bug to have it looked at. No guarantees, but it might be an oversight.

If there is no extension then no extension is the result.

What is wrong with this? What do you think should be the result?

Switch your logic on.

Winni

marcov

  • Administrator
  • Hero Member
  • *
  • Posts: 9713
  • FPC developer.
Re: Is there a special "ExtractFileExt" for Linux?
« Reply #5 on: August 20, 2021, 04:10:12 pm »
What is wrong with this? What do you think should be the result?

I might have misread, I thought he meant splitting on the dot in .config. Not returning an empty (or single dot) extension, which is expected.

PascalDragon

  • Hero Member
  • *****
  • Posts: 3525
  • Compiler Developer
Re: Is there a special "ExtractFileExt" for Linux?
« Reply #6 on: August 26, 2021, 03:22:32 pm »
What is wrong with this? What do you think should be the result?

The question is whether ExtractFileExt is supposed to take a path or merely a filename. If the later then there is indeed nothing wrong with it, but if it's supposed to take a path then the result for the path /home/user/booth/.config/bill/privatefile as posted by QEnnay is wrong as ExtractFileExt will return .config/bill/privatefile instead of an empty string.

Bart

  • Hero Member
  • *****
  • Posts: 4516
    • Bart en Mariska's Webstek
Re: Is there a special "ExtractFileExt" for Linux?
« Reply #7 on: August 26, 2021, 09:44:00 pm »
ExtractFileExt is defined in rtl/objpas/sysutils/fina.inc and is OS independant.
The algorithm will stop when it finds either a dot or a directoryseparator, starting the search from the end.

So ExtractFileExt('/home/user/booth/.config/bill/privatefile') wiil return an empty string on all platforms.
Which IMO is by design as .config/bill/privatefile in no way can be assumed to be a file extenstion.

Bart

marcov

  • Administrator
  • Hero Member
  • *
  • Posts: 9713
  • FPC developer.
Re: Is there a special "ExtractFileExt" for Linux?
« Reply #8 on: August 26, 2021, 09:48:57 pm »
What is wrong with this? What do you think should be the result?

The question is whether ExtractFileExt is supposed to take a path or merely a filename. If the later then there is indeed nothing wrong with it, but if it's supposed to take a path then the result for the path /home/user/booth/.config/bill/privatefile as posted by QEnnay is wrong as ExtractFileExt will return .config/bill/privatefile instead of an empty string.

As far as I know, extractfileext should not need info from the filesystem. So it can't know if the last bit is a file or a dir. But it does not seems logical to cross directory separators ("/") bounderies to synthesize the extension

PascalDragon

  • Hero Member
  • *****
  • Posts: 3525
  • Compiler Developer
Re: Is there a special "ExtractFileExt" for Linux?
« Reply #9 on: August 28, 2021, 12:17:47 pm »
What is wrong with this? What do you think should be the result?

The question is whether ExtractFileExt is supposed to take a path or merely a filename. If the later then there is indeed nothing wrong with it, but if it's supposed to take a path then the result for the path /home/user/booth/.config/bill/privatefile as posted by QEnnay is wrong as ExtractFileExt will return .config/bill/privatefile instead of an empty string.

As far as I know, extractfileext should not need info from the filesystem. So it can't know if the last bit is a file or a dir.

That's not what I meant.

But it does not seems logical to cross directory separators ("/") bounderies to synthesize the extension

That's what I meant and what ExtractFileExt already seems to do correctly, so the question is what QEnnay expected the function to return when they passed in /home/user/booth/.config/bill/privatefile, cause the expected result and what is currently indeed returned is an empty string.

munair

  • Hero Member
  • *****
  • Posts: 742
  • compiler developer @SharpBASIC
    • SharpBASIC
Re: Is there a special "ExtractFileExt" for Linux?
« Reply #10 on: September 03, 2021, 11:37:51 pm »
With a Linux ".folder" in the middle of a path it returns what looks to be a single "dot", the text in debugger is too small to see exactly.

No it doesn't. In your particular case ExtractFileExt() returns an empty string, not a single dot. The function is even clever enough to figure out that "/home/user/booth/.config" is not an extension either (it still returns an empty string, as it should).
keep it simple

 

TinyPortal © 2005-2018