[futurebasic] seeking suggestion, removing multiple extensions

Message: < previous - next > : Reply : Subscribe : Cleanse
Home   : June 2013 : Group Archive : Group : All Groups

From: "Edwards, Waverly" <Waverly.Edwards@...>
Date: Wed, 5 Jun 2013 15:37:40 -0500
I'm hoping someone has an idea they would mind sharing about my current problem.
I'm removing all the extensions from a URL and what I have works, however I can't seem to figure out the logic to release all the URLs elegantly, except for the first one (index 0 ) and the last, which holds the final URL.
My max array is arbitrarily set to 5 because I don't think that I would encounter more than five extension but that's actually bad validation
The first and the last are released by the caller.

Thanks for any ideas,


W.


include "ConsoleWindow"


local fn ConsoleCFPrint( typeRef as CFTypeRef )
'~'1
dim as CFStringRef   oldText, newText

oldText = fn HIViewCopyText( sConsoleHITextView )
newText = fn CFStringCreateWithFormat( _kCFAllocatorDefault, #0, @"%@%@\r", oldText, typeRef )
CFRelease( oldText )
fn HIViewSetText( sConsoleHITextView, newText )
CFRelease( newText )
end fn


clear local
local fn CFURLCopyCleaningAllExtensions( inCFURLRef as CFURLRef ) as CFURLRef
'~'1
dim as CFURLRef    finalURL, urlArray( 5 )
dim as boolean     done
dim as CFIndex     indx, retCount
dim as CFStringRef string, exten

indx = 0
done = _false

/*

     CFURLRef release logic

     index  | Releasable by function?
     ___________________________________________
     indx 0 : NO (Never, caller must release )
     indx 1 : Yes, if current indx = 2
     indx 2 : Yes, if current indx = 3
     indx 3 : .... indx = 4


*/

urlArray( 0 ) = inCFURLRef// released by the caller
indx = 0

while ( ( indx <= 4 ) and ( done == _false ) )
exten = fn CFURLCopyPathExtension( urlArray( indx ) )
long if ( exten )
CFRelease( exten )
finalURL = fn CFURLCreateCopyDeletingPathExtension( _kCFAllocatorDefault, urlArray( indx ) )
urlArray( indx + 1 ) = finalURL

long if ( indx  >= 2 )
CFRelease( urlArray( indx -1) )
urlArray( indx -1) = 0
end if

xelse
done = _true
end if
indx++
wend

print "finalURL "; finalURL
for indx = 0 to 4
long if ( urlArray( indx ) )
print "Action indx :"; indx, "urlArray( indx ) :", urlArray( indx ), "Retain Count :"; fn CFGetRetainCount( urlArray( indx ) )
end if
next


end fn = finalURL


local fn TestCleaner
'~'1
dim as CFURLRef    notRealPathURL, finalURL
dim as CFStringRef testPathStr

testPathStr = @"/Users/wedwards/Desktop/Speech/NotRealFolder.app/fileWithMultipleExtensions.jpg.txt.xml.zip"

notRealPathURL =  fn CFURLCreateWithString( _kCFAllocatorDefault, testPathStr, NULL )

finalURL = fn CFURLCopyCleaningAllExtensions( notRealPathURL )

print :print 
print "Before "
fn ConsoleCFPrint( testPathStr )

print "After"
fn ConsoleCFPrint( finalURL )

end fn

fn TestCleaner