[futurebasic] Re: [FB] Distinguishing Files

Message: < previous - next > : Reply : Subscribe : Cleanse
Home   : April 2010 : Group Archive : Group : All Groups

From: Ken Shmidheiser <kshmidheiser@...>
Date: Tue, 13 Apr 2010 22:31:27 -0400
In this thread Tom asked:

>If I open a file like this:
>
>err = fn FSMakeFSSpec( system( _aplVRefNum ), system( _aplParID ), 
>"HistoricData.txt", @inSpec )
>
>OPEN "I",1, @inSpec
>
>How do I set a string variable to the contents of it so I can use it 
>in a InStr function?

Tom,

I would suggest using the more modern FSRefs which are just as easy 
to use as FSSpecs.

Like Robert suggested, one easy way is to use a Container rather than 
a string which would be limited to 255 characters. FB's Instr works 
fine with containers as well as strings.

Here's one way to read your file into a Container for parsing to test 
for delimiters.

Ken

p.s. The code for loading the text file into a CFStringRef is also 
here but REMed out


include "Util_FileDirectory.incl"

begin globals
dim as Container gC
end globals

local fn ReadAppDirectoryFileIntoContainer( fileName as Str255 )
'~'1
dim as FSRef     appFolderRef, fileRef
dim as Long      fileLen
dim as Handle    h
// dim as CFStringRef  cfStr
dim as OSStatus  err

// Get FSRef of Application Folder
err = fn FD_ApplicationDirectoryGetFSRef( @appFolderRef )
long if ( err == _noErr )
// Get FSRef of file by app path and file name
err = fn FD_PathGetFSRef( fn CFSTR( fileName ), @appFolderRef, @fileRef )
// Check for valid FSRef for file
long if ( err == _noErr )
Open "I", 1, fileRef
// Open the file and check its length
fileLen = lof(1, 1)
// Create and handle to hold file contents
h = fn NewHandleClear( fileLen )
long if ( h )
// Read contents into handle
Read File 1, [h], fileLen
// Close file when complete
Close #1
// Place contents of handle into CFStringRef (user must dispose)
// cfStr = fn CFStringCreateWithBytes( _kCFAllocatorDefault, #[h], fn 
GetHandleSize(h), _kCFStringEncodingMacRoman, _false )
// Place contents of handle into container
gC = &h
// Done with handle, so dispose of it
fn DisposeH( h )
end if
end if
end if
end fn

gC = ""
fn ReadApplicationFileIntoContainer( "HistoricData.txt" )
print gC

do
HandleEvents
until gFBQuit