QB64.org Forum

Active Forums => QB64 Discussion => Topic started by: RhoSigma on September 29, 2021, 05:33:56 am

Title: Testers (cross-platform) wanted for a bit of code
Post by: RhoSigma on September 29, 2021, 05:33:56 am
I'm currently fixing stuff, also taking the opportunity to improve things.

Need to know if the following code works on all QB64 supported platforms, but I've only Windows systems to test on, so this is a call on all Mac/Linux users.

The following code does expand a relative path into a fully qualified absolut path, it works on Windows 32- and 64-bit, but what about Mac/Linux?

Code: QB64: [Select]
  1.     FUNCTION ExpandPath%& ALIAS _fullpath (buffer$, filespec$, BYVAL bufSize&)
  2.  
  3. PRINT "First test should work and print full path to qb64.exe"
  4. buff$ = STRING$(1056, CHR$(0))
  5. file$ = "qb64.exe"
  6. succ%& = ExpandPath%&(buff$, file$ + CHR$(0), 1056)
  7. IF succ%& > 0 THEN
  8.     PRINT "Result: success"
  9.     PRINT "Buffer: "; LEFT$(buff$, INSTR(buff$, CHR$(0)) - 1)
  10.     PRINT "Length:"; INSTR(buff$, CHR$(0)) - 1
  11.     PRINT "Result: error"
  12.     PRINT "Buffer: "; LEFT$(buff$, INSTR(buff$, CHR$(0)) - 1)
  13.     PRINT "Length:"; INSTR(buff$, CHR$(0)) - 1
  14.  
  15. PRINT "Second test should fail and print empty buffer and zero length"
  16. buff$ = STRING$(3, CHR$(0))
  17. file$ = "qb64.exe"
  18. succ%& = ExpandPath%&(buff$, file$ + CHR$(0), 3)
  19. IF succ%& > 0 THEN
  20.     PRINT "Result: success"
  21.     PRINT "Buffer: "; LEFT$(buff$, INSTR(buff$, CHR$(0)) - 1)
  22.     PRINT "Length:"; INSTR(buff$, CHR$(0)) - 1
  23.     PRINT "Result: error"
  24.     PRINT "Buffer: "; LEFT$(buff$, INSTR(buff$, CHR$(0)) - 1)
  25.     PRINT "Length:"; INSTR(buff$, CHR$(0)) - 1
  26.  
  27.  
  28.  
Title: Re: Testers (cross-platform) wanted for a bit of code
Post by: luke on September 29, 2021, 09:46:06 am
There's no _fullpath function in Unix, but there's the very similar realpath:
Code: [Select]
Declare Library
    Function realpath%& (path$, buf$)
End Declare

Print expandpath$("untitled.bas")


Function expandpath$ (relpath$)
    buf$ = String$(4096, Chr$(0)) 'PATH_MAX
    r%& = realpath%&(relpath$ + Chr$(0), buf$)
    If r%& = 0 Then
        expandpath$ = ""
    Else
        expandpath$ = Left$(buf$, InStr(buf$, Chr$(0)) - 1)
    End If
End Function

I've hardcoded the output buffer size as 4096 which is PATH_MAX on my current Linux, but that's pretty dodgy. A better way to do things would be to pass NULL for the second argument, then realpath returns a pointer to a malloc'd buffer that you can call free on.

I didn't do that because copying the data from the buffer into a QB64 string turns out to be rather annoying, so you get the hacky version for now.
Title: Re: Testers (cross-platform) wanted for a bit of code
Post by: George McGinn on September 29, 2021, 02:27:53 pm
In Ubuntu 20.04, and either compiling in v1.5 or the development build 2.0, I get an internal compiler error:

Quote
In file included from qbx.cpp:2173:
../temp/main.txt: In function ‘void QBMAIN(void*)’:
../temp/main.txt:29:30: error: ‘_fullpath’ was not declared in this scope
   29 | *__OFFSET_SUCC=(  ptrszint  )_fullpath((char*)(__STRING_BUFF)->chr,(char*)(qbs_add(__STRING_FILE,func_chr( 0 )))->chr, 1056 );
      |                                                      ^~~~~~~~~
../temp/main.txt:153:30: error: ‘_fullpath’ was not declared in this scope
  153 | *__OFFSET_SUCC=(  ptrszint  )_fullpath((char*)(__STRING_BUFF)->chr,(char*)(qbs_add(__STRING_FILE,func_chr( 0 )))->chr, 3 );
      |                                                        ^~~~~~~~~
Title: Re: Testers (cross-platform) wanted for a bit of code
Post by: RhoSigma on September 29, 2021, 02:36:37 pm
Thank you @luke and @GeorgeMcGinn for your replies.

George, could you please check, if Luke's code is working for you?
Title: Re: Testers (cross-platform) wanted for a bit of code
Post by: Dav on September 29, 2021, 04:02:41 pm
Just a confirmation on my 32-bit windows for you.  I get the results you got and expected.

- Dav
Title: Re: Testers (cross-platform) wanted for a bit of code
Post by: George McGinn on October 04, 2021, 11:42:51 pm
@RhoSigma  - I just tried to compile it with the development build that is out there now, and I get the same internal compiler errors.

I'm running Linux Ubuntu 20.04 and 64-bit processing, it that means anything,

Thank you @luke and @GeorgeMcGinn for your replies.

George, could you please check, if Luke's code is working for you?
Title: Re: Testers (cross-platform) wanted for a bit of code
Post by: RhoSigma on October 05, 2021, 04:02:17 am
Thanks for testing @Dav and @George McGinn, the problem is already resolved now to my satisfaction.
Title: Re: Testers (cross-platform) wanted for a bit of code
Post by: George McGinn on October 05, 2021, 12:49:15 pm
Sorry, I missed Luke's code. It works fine now.



Thanks for testing @Dav and @George McGinn, the problem is already resolved now to my satisfaction.