[futurebasic] Re: [FB] some functions to read in a text-file in chunks

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

From: Jay <jktr@...>
Date: Thu, 21 Feb 02 23:54:07 -0600
>here's an fb2 program i made for jim henson that will
>read a text file in chucks, for files greater than 32k
>
>it won't run in fb3.  could one of you find upstanding
>fb3 programmers make the necessary modifications,
>please?, and re-post it and i'll send it back to jim,
>as he can't make posts to this list...  thank you...
>
>-bowerbird
>
Okay, I needed a bit of exercise. I dimmed your vars and allocated them 
properly to regs or RAM, changed the HLOCK and HUNLOCK calls, and 
eliminated the ON ERROR lines. I didn't do a lot of the prettification 
that Alain would do, but maybe he'll be sending his version, too.

This compiles in FB^3 and should run, but no guarantees, as I didn't 
fully analyze the original code.

HTH,
 e-e
 =J= a  y
  "


//COMPILE 0,_caseinsensitive
//WINDOW OFF
% _ACount,0
WINDOW 1,"",(0,0)-(1000,700),_dialogshadow
END GLOBALS

LOCAL MODE
dim nxtRef%
LOCAL FN nxtFileRef
nxtRef% = 0
DO
INC(nxtRef%) ' start w/file #1
UNTIL USR 4(nxtRef%) = 0 ' inc until USR rtn returns 0
END FN = nxtRef%

LOCAL MODE
dim dErr%,fNum%,fLen&,osErr%,state%
LOCAL FN hndl2File(fNam$,vRef%,fOs&,type$,h&,hOs&)
REM IF hOs&=0 THEN hOs&=500
dErr% = _noErr ' no error yet
LONG IF h& <> _nil ' be paranoid
//ON ERROR GOSUB 65535 ' we want local error handling
DEF OPEN type$ ' set file type
fNum% = FN nxtFileRef
OPEN "O",#fNum%,fNam$,1,vRef% ' creates file if it doesn't exist
RECORD #fNum%,fOs& ' position to offset

LONG IF ERROR = _noErr
fLen& = FN GETHANDLESIZE(h&)-hOs& ' get data length in bytes
state% = FN HGetState (h&)
FN HLOCK(h&) ' try to lock handle
LONG IF fn memerr = _noErr ' did we lock it OK?
WRITE FILE #fNum,[h&]+hOs&,fLen& ' dump to disk
FN hsetstate(h&,state%) ' return to original state
XELSE
dErr% = -1 ' our handle error
END IF ' { END IF hlock error }
XELSE ' disk error dErr%    ' and clear its internal flag
END IF ' { END IF file error }

CLOSE #fNum% ' close file and exit
END IF ' { END IF handle not nil }
END FN = dErr%

LOCAL MODE
dim osErr%,fNum%,sz&,h&
LOCAL FN file2Hndl(fName$,fRef%,os&,max&,@hPtr&)

osErr% = _noErr ' default
fNum% = FN nxtFileRef ' find next available file num
OPEN "I",fNum%,fName$,1,fRef% ' open for input

LONG IF ERROR = _noErr ' ck for open error
sz& = LOF(fNum%,1)-os& ' calc file sz less offset into file
IF max& AND sz& > max& THEN sz& = max& ' adjust size if req'd
h& = FN NEWHANDLE(sz&) ' ask for RAM

LONG IF h& <> _nil ' ck for nil handle
HLOCK(h&) ' lock it down
RECORD #fNum%,os& ' position to offset
READ FILE #fNum%,[h&],sz& ' put data into block
HUNLOCK(h&) ' let block float now
XELSE
osErr% = _mFulErr ' mem full error
END IF ' { END IF handle valid }

XELSE
osErr% = fn memerr
END IF ' { END IF file open error }

hPtr.0& = h& ' return handle in VAR
CLOSE #fNum% ' close the file
ERROR = _noErr ' always clear ERROR
END FN = osErr% ' return error, if any

CLEAR LOCAL FN readchunks
dim filename$,outputname$,@volRefNum%,@thehandle&
dim howlong&,howmuch&,startat&,osErr%
DEF OPEN "TEXTTBB6" : REM jim, tex-edit is a wonderful program, try it...
filename$ = FILES$(_fOpen,"TEXT",,volRefNum%)
IF filename$="" THEN EXIT FN
OPEN "I",#1,filename$,,volRefNum%
howlong&=LOF(1,1)
WINDOW FILL
LOCATE 0,0 : CLS LINE : PRINT "length=";howlong&
howmuch&=20000
FOR startat&=0 TO howlong& STEP howmuch&
REM FN file2Hndl(fName$,fRef%,os&,max&,@hPtr&)
osErr%=FN file2Hndl(filename$,0,startat&,howmuch&,thehandle&)
outputname$="startat& = "+STR$(startat&)
PRINT outputname$;" was output..."
REM FN hndl2File(fNam$,vRef%,fOs&,type$,h&,hOs&)
FN hndl2File(outputname$,volRefNum%,0,"TEXT",thehandle&,0)
NEXT startat&
END FN

FN readchunks
DO
HANDLEEVENTS
UNTIL 0