From df70f7e708bfafae309a805a2f3025bf0d6c42af Mon Sep 17 00:00:00 2001 From: Matthew Kilgore Date: Tue, 7 Feb 2023 02:15:19 -0500 Subject: [PATCH] The -o flag should not strip extensions except for .exe Current the -o flag will strip any "extension" on the provided filename, which is fairly problimatic on Linux and Mac OS since those executes do not have other extensions and names like "foobar.v1" will get the ".v1" stripped off. This can happen on Windows as well if you leave off the .exe (QB64-PE will add it for you, but also strip off the existing extension). QB64-PE stripping off the ".exe" when provided that on Linux and Mac OS might actually be useful behavior people are relying on (so that they don't need to provide different names when compiling on Linux/Mac OS) so we are preserving that and still removing the extension if it is exactly "EXE", otherwise we now leave it in place. Fixes: #297 --- source/qb64pe.bas | 7 +++- source/utilities/file.bas | 14 +++++++ .../filename/filename.extra.extension.bas | 2 + tests/compile_tests/qb64pe/file.bas | 41 +++++++++++++++++++ tests/compile_tests/qb64pe/file.output | 15 +++++++ 5 files changed, 78 insertions(+), 1 deletion(-) create mode 100644 tests/compile_tests/filename/filename.extra.extension.bas create mode 100644 tests/compile_tests/qb64pe/file.bas create mode 100644 tests/compile_tests/qb64pe/file.output diff --git a/source/qb64pe.bas b/source/qb64pe.bas index a7791aa4b..45fad7b69 100644 --- a/source/qb64pe.bas +++ b/source/qb64pe.bas @@ -12322,7 +12322,12 @@ IF idemode = 0 AND No_C_Compile_Mode = 0 THEN 'resolve relative path for output file path.out$ = getfilepath$(outputfile_cmd$) f$ = MID$(outputfile_cmd$, LEN(path.out$) + 1) - file$ = RemoveFileExtension$(f$) + + If UCASE$(GetFileExtension$(f$)) = "EXE" Then + file$ = RemoveFileExtension$(f$) + Else + file$ = f$ + End If END IF IF LEN(path.out$) OR OutputIsRelativeToStartDir THEN diff --git a/source/utilities/file.bas b/source/utilities/file.bas index ce99277ee..84b10b5bd 100644 --- a/source/utilities/file.bas +++ b/source/utilities/file.bas @@ -71,6 +71,20 @@ FUNCTION RemoveFileExtension$ (f$) RemoveFileExtension$ = f$ END FUNCTION +' +' Returns the extension on the end of a file name +' +' Returns "" if there is no extension +' +FUNCTION GetFileExtension$ (f$) + FOR i = LEN(f$) TO 1 STEP -1 + a = ASC(f$, i) + IF a = 47 OR a = 92 THEN EXIT FOR + IF a = 46 THEN GetFileExtension$ = MID$(f$, i + 1): EXIT FUNCTION + NEXT + GetFileExtension$ = "" +END FUNCTION + ' ' Fixes the provided filename and path to use the correct path separator ' diff --git a/tests/compile_tests/filename/filename.extra.extension.bas b/tests/compile_tests/filename/filename.extra.extension.bas new file mode 100644 index 000000000..77a667b7c --- /dev/null +++ b/tests/compile_tests/filename/filename.extra.extension.bas @@ -0,0 +1,2 @@ +$Console:Only +System diff --git a/tests/compile_tests/qb64pe/file.bas b/tests/compile_tests/qb64pe/file.bas new file mode 100644 index 000000000..f96a7eadc --- /dev/null +++ b/tests/compile_tests/qb64pe/file.bas @@ -0,0 +1,41 @@ +DEFLNG A-Z +$Console:Only + +Type TestCase + file As String + expectedExtension As String +End Type + +Dim tests(5) As TestCase + +tests(1).file = "foobar.exe" +tests(1).expectedExtension = "exe" + +tests(2).file = "foobar.EXE" +tests(2).expectedExtension = "EXE" + +tests(3).file = "foobar." +tests(3).expectedExtension = "" + +tests(4).file = "foobar" +tests(4).expectedExtension = "" + +tests(5).file = "foobar.tar.gz" +tests(5).expectedExtension = "gz" + +For i = 1 To UBOUND(tests) + result$ = GetFileExtension$(tests(i).file) + + Print "Test"; i; ", Filename: "; tests(i).file + Print " Expected: "; tests(i).expectedExtension; ", Actual: "; result$ + + If result$ = tests(i).expectedExtension Then + Print " PASS!" + Else + Print " FAIL!" + End If +Next + +System + +'$include:'../../../source/utilities/file.bas' diff --git a/tests/compile_tests/qb64pe/file.output b/tests/compile_tests/qb64pe/file.output new file mode 100644 index 000000000..ba0236bef --- /dev/null +++ b/tests/compile_tests/qb64pe/file.output @@ -0,0 +1,15 @@ +Test 1 , Filename: foobar.exe + Expected: exe, Actual: exe + PASS! +Test 2 , Filename: foobar.EXE + Expected: EXE, Actual: EXE + PASS! +Test 3 , Filename: foobar. + Expected: , Actual: + PASS! +Test 4 , Filename: foobar + Expected: , Actual: + PASS! +Test 5 , Filename: foobar.tar.gz + Expected: gz, Actual: gz + PASS!