badidea wrote:'slashFileName' converts backslashes to slashes, which both Windows and Linux accept in FreeBASIC I believe.
Maybe useful for someone else as well.
Well, I'd say not really, because of:
currently, it is to limited to slashes and, 'one way' only (to much statically coded).
(The secret behind flexible procedures is: parameters, instead of literals/globals!)
With a little bit more effort, your function can be made, far more
'general use'.
I've renamed it too, to reflect it's usability:
Code: Select all
' replaces given 'search' char, with a given 'replace' char, in Source String (src_str)
Function ReplaceChar( _
ByRef src_str As String, _ ' the string: to be modified
ByVal search As UByte, _ ' ASCII number of: char (to search for)
ByVal replace As UByte _ ' ASCII number of: char (to replace with)
) ByRef As String ' modified string (as above specified)
For i As UInteger = 0 To Len(src_str) - 1
If src_str[i] = search Then src_str[i] = replace
Next
Return src_str
End Function
And the rewritten test code 'in full':
Code: Select all
function getFilePath(fullFileName as string) as string
Dim As UShort sepPos = instrrev(fullFileName, any "/\")
return mid(fullFileName, 1, sepPos)
end function
function getFileName(fullFileName as string) as string
dim As UShort sepPos = instrrev(fullFileName, any "/\")
return mid(fullFileName, sepPos + 1)
end function
' replaces given 'search' char, with a given 'replace' char (in SourceString)
Function ReplaceChar( _
ByRef src_str As String, _ ' the string: to be modified
ByVal search As UByte, _ ' ASCII number of: char (to search for)
ByVal replace As UByte _ ' ASCII number of: char (to replace with)
) ByRef As String ' modified string (as above specified)
For i As UInteger = 0 To Len(src_str) - 1
If src_str[i] = search Then src_str[i] = replace
Next
Return src_str
End Function
dim as string s1 = "c:\dir\subdir\01_test.txt" 'windows
dim as string s2 = "/dir/main.bas" 'linux
dim as string s3 = "test_123.bas" 'no path
print "s1: '" & getFilePath(s1) & "' + '" & getFileName(s1) & "'"
print "s2: '" & getFilePath(s2) & "' + '" & getFileName(s2) & "'"
print "s3: '" & getFilePath(s3) & "' + '" & getFileName(s3) & "'"
print
Print "modified s1: "; ReplaceChar(s1, Asc("\"), Asc("/")) ' WIN to LIN
Print "modified s2: "; ReplaceChar(s2, Asc("/"), Asc("\")) ' LIN to WIN
Print
Print "now we replace any 'd' with 't' in 's1'"
Print "modified s1: "; ReplaceChar(s1, Asc("d"), Asc("t"))
Sleep