1
1
Fork 0
mirror of https://github.com/QB64-Phoenix-Edition/QB64pe.git synced 2024-07-25 11:46:15 +00:00
QB64-PE/source/utilities/file.bas
Matthew Kilgore df70f7e708 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
2023-02-18 14:50:31 -05:00

102 lines
2.4 KiB
QBasic

'
' Duplicates the contents of one file into another
'
' Returns: 0 on success, 1 on error
FUNCTION CopyFile& (sourceFile$, destFile$)
DIM sourcefileNo, destFileNo
DIM fileLength AS _INTEGER64
E = 0
sourceFileNo = FREEFILE
OPEN sourceFile$ FOR BINARY as #sourceFileNo
if E = 1 THEN GOTO errorCleanup
fileLength = LOF(sourceFileNo)
destFileNo = FREEFILE
OPEN destFile$ FOR BINARY as #destFileNo
if E = 1 THEN GOTO errorCleanup
' Read the file in one go
buffer$ = SPACE$(fileLength)
GET #sourceFileNo, , buffer$
PUT #destFileNo, , buffer$
errorCleanup:
IF sourceFileNo <> 0 THEN CLOSE #sourceFileNo
IF destFileNo <> 0 THEN CLOSE #destFileNo
CopyFile& = E
END FUNCTION
'
' Splits the filename from its path, and returns the path
'
' Returns: The path, or empty if no path
FUNCTION getfilepath$ (f$)
FOR i = LEN(f$) TO 1 STEP -1
a$ = MID$(f$, i, 1)
IF a$ = "/" OR a$ = "\" THEN
getfilepath$ = LEFT$(f$, i)
EXIT FUNCTION
END IF
NEXT
getfilepath$ = ""
END FUNCTION
'
' Checks if a filename has an extension on the end
'
' Returns: True if provided filename has an extension
FUNCTION FileHasExtension (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 FileHasExtension = -1: EXIT FUNCTION
NEXT
END FUNCTION
'
' Strips the extension off of a filename
'
' Returns: Provided filename without extension on the end
FUNCTION RemoveFileExtension$ (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 RemoveFileExtension$ = LEFT$(f$, i - 1): EXIT FUNCTION
NEXT
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
'
SUB PATH_SLASH_CORRECT (a$)
IF os$ = "WIN" THEN
FOR x = 1 TO LEN(a$)
IF ASC(a$, x) = 47 THEN ASC(a$, x) = 92
NEXT
ELSE
FOR x = 1 TO LEN(a$)
IF ASC(a$, x) = 92 THEN ASC(a$, x) = 47
NEXT
END IF
END SUB