Author Topic: File.extractCompleteSuffix() fails when the directories contain dots  (Read 4167 times)

Offline Andres.Pozo

  • PTeam Member
  • PixInsight Padawan
  • ****
  • Posts: 927
The method File.extractCompleteSuffix() of PJSR seems to fail when there is a directory in the path which name contains dots.

For example:
Code: [Select]
var path = "/dir1/dir2.1/test.fit";
var extension = File.extractCompleteSuffix(path);
console.writeln("Extension:'",extension,"'");

This should return '.fit' but instead it returns an empty string.

The tests have been done in Win7 x64 with PI 1.8.1.1092

Offline FunTomas

  • PixInsight Addict
  • ***
  • Posts: 135
    • Astrofoto.sk
Thank you Andrés for report.

Offline Juan Conejero

  • PTeam Member
  • PixInsight Jedi Grand Master
  • ********
  • Posts: 7111
    • http://pixinsight.com/
Hi Andrés,

There's no bug here. File.extractCompleteSuffix() returns the whole file suffix (aka whole extension) of a file path specification, which can include several dot separators. For example:

console.writeln( File.extractCompleteSuffix( "/dir1/dir2.1test.fit" ) );

outputs ".1test.fit". A typical case where this method is useful is to extract compound file suffixes such as "file.tar.gz". This is very common on UNIX/Linux platforms.

In your example:

var path = "/dir1/dir2.1/test.fit";

the problem is that a slash character is illegal in a file suffix. Hence, the File.extractCompleteSuffix() function returns an empty string.

For your script, instead of File.extractCompleteSuffix() you should use File.extractSuffix(), which will extract just the last component of a compound file suffix:

console.writeln( File.extractSuffix( "/dir1/dir2.1/test.fit" ) );

outputs ".fit", as expected.
Juan Conejero
PixInsight Development Team
http://pixinsight.com/

Offline Andres.Pozo

  • PTeam Member
  • PixInsight Padawan
  • ****
  • Posts: 927
I agree that my script should use extractSuffix() instead extractCompleteSuffix(). However I don't agree with you explanation:

In "/dir1/dir2.1/test.fit" the operative system interprets that "/dir1/dir2.1/" is the directory and "test.fit" is the filename. The complete suffix of this filename is ".fit".

What happens if you need to use extractCompleteSuffix() with "/dir1/dir2.1/test.tar.gz"? The current implementation returns "" instead the correct value ".tar.gz".

Offline Juan Conejero

  • PTeam Member
  • PixInsight Jedi Grand Master
  • ********
  • Posts: 7111
    • http://pixinsight.com/
Unfortunately, the definition of complete suffix that we have currently implemented in PCL and PJSR is much simpler than that: All characters in the file path after the last dot character. See for example:

http://qt-project.org/doc/qt-4.8/qfileinfo.html#completeSuffix

Unlike Qt, in our implementation file suffixes always include leading dot characters, but the basic definitions are the same. I'll try to reimplement the relevant routines in PCL and PJSR to include a better separator detection logic in the next version (I *hope* that these changes won't break anything). With the current implementation, one has to use two steps to detect a complete file suffix in cases like these:

console.writeln( File.extractCompleteSuffix( File.extractNameAndSuffix( "/dir1/dir2.1/test.tar.gz" ) ) );

works correctly.

Thank you for pointing out this flaw.
Juan Conejero
PixInsight Development Team
http://pixinsight.com/

Offline Andres.Pozo

  • PTeam Member
  • PixInsight Padawan
  • ****
  • Posts: 927
Thanks Juan!

Offline Juan Conejero

  • PTeam Member
  • PixInsight Jedi Grand Master
  • ********
  • Posts: 7111
    • http://pixinsight.com/
This bug is now fixed in PCL 2.0.12 and PixInsight Core 1.8.2. Thanks again for detecting it.
Juan Conejero
PixInsight Development Team
http://pixinsight.com/