[futurebasic] 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: Bowerbird@...
Date: Thu, 21 Feb 2002 20:46:19 EST
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


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

LOCAL MODE
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
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
      osErr% = FN HLOCK(h&)                       ' try to lock handle
      LONG IF osErr% = _noErr                     ' did we lock it OK?
        WRITE FILE #fNum,[h&]+hOs&,fLen&          ' dump to disk
        osErr% = FN HUNLOCK(h&)                   ' let float for better mem 
usage
      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
LOCAL FN file2Hndl(fName$,fRef%,os&,max&,@hPtr&)
  ON ERROR GOSUB 65535                            ' enable local error 
handling
  
  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
      osErr% = FN HLOCK(h&)                       ' lock it down
      RECORD #fNum%,os&                           ' position to offset
      READ FILE #fNum%,[h&],sz&                   ' put data into block
      osErr% = FN HUNLOCK(h&)                     ' let block float now
    XELSE
      osErr% = _mFulErr                           ' mem full error
    END IF                                        ' { END IF handle valid }
  XELSE
    osErr% = ERROR
  END IF                                          ' { END IF file open error }
  POKE LONG hPtr&,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
  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