Hi,

I create a little class to access big text files with very long lines.
So, after parsing the file, i can get very quickly a line in the text file.
Now, i want to fclose() the text file when the object is destroyed.
How can i do this with harbour ?

--
Guy Roussin

#!/usr/bin/hbrun
#include "hbclass.ch"

// Note: BUFLEN must be >= len(::cSep)
#define BUFLEN 1024

function main()
 local oObjet
 oObjet := FLaccess():New("clLine.prg")
 ? oObjet:numberOfLines()
 ? oObjet:getLine()
 ? oObjet:getLine()
 ? oObjet:getLine(16)
 ? oObjet:getLine()
 ? oObjet:nCurrentLine
 ? oObjet:nHandle
return NIL 

class FLaccess
 method new()
 method readLines()
 method numberOfLines()
 method getLine()
 var nHandle READONLY
 var cSep READONLY
 var nCurrentLine READONLY
 var aPtLines READONLY
endclass

method new(cF,cS) class FLaccess
 ::nHandle:=fopen(cF,0)
 ::cSep:=if(cS==NIL,hb_OSNewLine(),cS)
 ::nCurrentLine:=0
 ::aPtLines:={}
 ::readLines()
return Self

method readLines() class FLaccess
 local cBuffer:=space(BUFLEN)
 local nAtcSep:=0
 local nOctetsLus:=0
 local nLencSep:=len(::cSep)
 aadd(::aPtLines,-nLencSep)
 while (nOctetsLus:=fread(::nHandle,@cBuffer,BUFLEN))>0
  nAtcSep:=at(::cSep,cBuffer) 
  if nAtcSep==0 .or. nAtcSep+nLencSep-1>nOctetsLus   
   if nOctetsLus<BUFLEN
    aadd(::aPtLines,fseek(::nHandle,0,1)) 
    exit
   endif
   fseek(::nHandle,1-nLencSep,1)            
  else                                    
   fseek(::nHandle,nAtcSep-nOctetsLus+nLencSep-1,1)
   aadd(::aPtLines,fseek(::nHandle,0,1)-nLencSep) 
   if nOctetsLus<BUFLEN .and. nAtcSep+nLencSep-1==nOctetsLus        
    exit
   endif
  endif
 end
return NIL

method numberOfLines() class FLaccess
return(len(::aPtLines))

method getLine(nLine) class FLaccess
 local cLine
 if nLine==NIL
  nLine:=::nCurrentLine+1
 endif
 ::nCurrentLine:=nLine
 cLine:=space(::aPtLines[nLine+1]-::aPtLines[nLine]+::aPtLines[1]) 
 fseek(::nHandle,::aPtLines[nLine]-::aPtLines[1],0)
 fread(::nHandle,@cLine,::aPtLines[nLine+1]-::aPtLines[nLine]+::aPtLines[1])
return(cLine)
_______________________________________________
Harbour mailing list
[email protected]
http://lists.harbour-project.org/mailman/listinfo/harbour

Reply via email to