[futurebasic] Re: [FB] FB^3 Memory and Temp file questions

Message: < previous - next > : Reply : Subscribe : Cleanse
Home   : October 2000 : Group Archive : Group : All Groups

From: Heather Donahue <HeatherDonahue@...>
Date: Mon, 30 Oct 2000 10:13:52 -0800
At 4:53 AM -0400 on 10/30/00, Robert Covington wrote:

>* Item 2
>
>Anyone know how to find the Temporary Items folder, and  create and then
>delete a file there? I am in the need of writing temporary PICT files, and
>then deleting them when done. I want to be sure I am fine across OS
>versions and international too (folder finding/using). Creating just
>involves a Temp name and the vRefnum I imagine, and standard PICT write.

This might help, it's from the WASTE demo I've ported.  I've removed some of the ToolBox calls that aren't standard.  I've also generalized it a bit but it should give you the general idea.

---
Begin Globals
_kFileNotOpened = -1
_sigCreator = _"????"//your creator
_kTypeText = _"TEXT"//your file type
End Globals

LOCAL FN WriteTextFile ( pFileSpec AS ^FSSpec )

DIM targetSpec AS ^FSSpec: targetSpec = pFileSpec
DIM fileInfo AS FInfo
DIM replacing AS Boolean: replacing = _false
DIM dataForkRefNum AS SInt16: dataForkRefNum = _kFileNotOpened
DIM resForkRefNum AS SInt16: resForkRefNum = _kFileNotOpened
DIM theTime AS UNSIGNED LONG
DIM tempFileName AS Str255
DIM tempFileSpec AS FSSpec
DIM tempVRef AS SInt16// volume reference # for the temp file
DIM tempDirID AS LONG// directory ID of the temp file
DIM err AS LONG


// will we be replacing an existing file?
err = FN FSpGetFInfo ( #pFileSpec, fileInfo )

LONG if ( err == _noErr )
replacing = _true
XELSE
if ( err != _fnfErr ) THEN GOTO "WTxtFile"
END IF

LONG if ( replacing = _true )

// make up a temporary file name -- the name doesn't have
// to make sense, just be unique
CALL GetDateTime ( @theTime )
CALL NumToString ( theTime, tempFileName )

// find the temporary items folder on the file's volume; create it if necessary
err = FN FindFolder ( pFileSpec.vRefNum, _kTemporaryFolderType, _kCreateFolder, @tempVRef, @tempDirID )
if ( err != _noErr ) THEN GOTO "WTxtFile"

// make an FSSpec for the temp file
err = FN FSMakeFSSpec ( tempVRef, tempDirID, @tempFileName, @tempFileSpec )
if ( ( err != _noErr) AND ( err != _fnfErr ) ) THEN EXIT "WTxtFile"

targetSpec = @tempFileSpec
END IF

// create a new file.  if we're replacing, make a temp file.  if it's a
//  new file from the onset, just create the file
CALL FSpCreateResFile ( #targetSpec, _sigCreator, _kTypeText, _smSystemScript )
err = FN ResError
if ( err != _noErr ) THEN GOTO "WTxtFile"


// open the data fork for writing
// <your code here>

if ( err != _noErr ) THEN GOTO "WTxtFile"

// open the resource file for writing
resForkRefNum = FN FSpOpenResFile ( #targetSpec, _fsRdWrPerm )
err = FN ResError
if ( err != _noErr ) THEN GOTO "WTxtFile"

// write resources
//<your code here>

err = _noErr

"WTxtFile"
// display an alert box if anything went wrong
//if ( err != _noErr ) THEN FN ErrorAlert ( err )

// close the data fork, if we opened it
LONG if ( dataForkRefNum != _kFileNotOpened )
// <close datafork>
dataForkRefNum = _kFileNotOpened
END IF

// close the resource fork, if we opened it
LONG if ( resForkRefNum != _kFileNotOpened )
CALL CloseResFile ( resForkRefNum )
resForkRefNum = _kFileNotOpened
END IF

LONG if ( replacing = _true )

// since we were replacing an existing file, let's now swap the original
// and the temp file.
err = FN FSpExchangeFiles ( tempFileSpec, #pFileSpec )
LONG if ( err != _noErr )
// handle the error
EXIT FN
END IF

// FSpExchangeFiles doesn't change the finder info, if we are replacing
// a unicode with text we need to set the type info for this file
Dim tempFinfo as FInfo
err = FN FSpGetFInfo ( #pFileSpec, tempFinfo )
tempFinfo.fdType& = _kTypeText
err = FN FSpSetFInfo ( #pFileSpec, tempFinfo )

// can the temp file since we don't need it anymore
err = FN FSpDelete ( tempFileSpec )
LONG if ( err != _noErr )
EXIT FN
END IF
END IF

// and update the disk with any unwritten data
//err = FN FlushVol ( _nil, pFileSpec.vRefNum )

END FN = err

---
-- 
Heather Donahue
PGP Public Key at: http://pgpkeys.mit.edu:11371/pks/lookup?op=get&search=HeatherDonahue@...
See X-PGP-fingerprint in headers.