[futurebasic] Re: [FB] The file name returns zero part 2

Message: < previous - next > : Reply : Subscribe : Cleanse
Home   : February 2011 : Group Archive : Group : All Groups

From: Robert Purves <listrp@...>
Date: Tue, 8 Feb 2011 14:57:14 +1300
Yoshiyuki Hasegawa wrote:

> It is necessary to do the character of CFString in the finding, delete and to add it.
> It is the following in the syntax of FutureBasic.
> 
> '-------------------------------
> dim as short   found
> dim as Str255  fileName : fileName = "myFileName.csv"
> 
> found = instr( 1, fileName, ".csv" )// Check the CSV file
> 
> long if found > 0
> fileName = left$( fileName, found-1 )// Delete extension
> fileName = fileName + ".abcd"// Add my extension
> end if
> '-------------------------------
> 
> Or, if the function to replace the extension exists !
> 
> I do not understand use well because there are no examples though it seems to provide those functions in Tlbx CFString.incl.
> 
> I think needing in that to be the following functions etc.
> fn CFStringFind(),   CFStringAppend(),   CFStringDelete(),   CFStringReplace()   etc.


See the example
FB_5_5_Examples/CoreFoundation/CFString left, right, mid
which has functions CFCreateLeftString(), CFCreateRightString() and CFCreateMidString().


Alternatively, consider CFStringCopyPathReplacingExtension() below.

Robert P.

'--------------------
/*
The existing extension is replaced (by newExtension) if 
it is a case-insensitive match with oldExtension.
The caller owns the returned CFStringRef and must release it.
*/
local fn CFStringCopyPathReplacingExtension( path as CFStringRef, oldExtension as CFStringRef, newExtension as CFStringRef ) as CFStringRef
'~'1
dim as CFStringRef originalExtension, newPath
dim as CFUrlRef    originalUrl, truncatedUrl, finalUrl

newPath = fn CFStringCreateCopy( _kCFAllocatorDefault, path ) // default
originalUrl = fn CFURLCreateWithString( _kCFAllocatorDefault, path, 0 )
long if ( originalUrl )
originalExtension = fn CFURLCopyPathExtension( originalUrl )
long if ( originalExtension )
long if ( _kCFCompareEqualTo == fn CFStringCompare( originalExtension, oldExtension, _kCFCompareCaseInsensitive ) )
truncatedUrl = fn CFURLCreateCopyDeletingPathExtension( _kCFAllocatorDefault, originalUrl )
finalUrl = fn CFURLCreateCopyAppendingPathExtension( _kCFAllocatorDefault, truncatedUrl, newExtension )
CFRelease( newPath )
newPath = fn CFURLGetString( finalUrl )
fn CFRetain( newPath )
CFRelease( truncatedUrl )
CFRelease( finalUrl )
end if
CFRelease( originalExtension )
end if
CFRelease( originalUrl )
end if
end fn = newPath



// demo

include "ConsoleWindow"

local fn PrintCFString( string as CFStringRef )
'~'1
dim as Str255   s

if ( string == 0 ) then exit fn
fn CFStringGetPascalString( string, @s, 256, _kCFStringEncodingMacRoman )
print s
end fn

// demo main

dim as CFStringRef name
name = fn CFStringCopyPathReplacingExtension( @"file0.csv", @"csv", @"abcd" ) // replaces
fn PrintCFString( name )
CFRelease( name )

name = fn CFStringCopyPathReplacingExtension( @"file1.CSV", @"csv", @"abcd" ) // replaces
fn PrintCFString( name )
CFRelease( name )

name = fn CFStringCopyPathReplacingExtension( @"someDir/file2.csv", @"csv", @"abcd" ) // replaces
fn PrintCFString( name )
CFRelease( name )

name = fn CFStringCopyPathReplacingExtension( @"file3.txt", @"csv", @"abcd" ) // no replacement
fn PrintCFString( name )
CFRelease( name )

name = fn CFStringCopyPathReplacingExtension( @"file4", @"csv", @"abcd" ) // no replacement
fn PrintCFString( name )
CFRelease( name )
'--------------------