#INCLUDE "PROTHEUS.CH"
#INCLUDE "TRYEXCEPTION.CH"
#INCLUDE "FILEIO.CH"
/*/
	CLASS:		TOTVSFt
	Autor:		Marinaldo de Jesus
	Data:		01/05/2011
	Descricao:	Alternativa aas funcoes tipo FT_F* devido as limitacoes apontadas em (http://tdn.totvs.com.br/kbm#9734)
	Sintaxe:	TOTVSFt():New() : Objeto do Tipo TOTVSFt
/*/
CLASS TOTVSFt FROM LongClassName

	DATA aLines

	DATA cCRLF
	DATA cFile
	DATA cLine

	DATA cClassName

	DATA nRecno
	DATA nfHandle
	DATA nFileSize
	DATA nLastRecno
	DATA nBufferSize

	METHOD New()		CONSTRUCTOR
	METHOD ClassName()

	METHOD ft_fUse( cFile )
	METHOD ft_fOpen( cFile )
	METHOD ft_fClose()

	METHOD ft_fAlias()

	METHOD ft_fExists( cFile )

	METHOD ft_fRecno()
	METHOD ft_fSkip( nSkipper )
	METHOD ft_fGoTo( nGoTo )
	METHOD ft_fGoTop()
	METHOD ft_fGoBottom()
	METHOD ft_fLastRec()
	METHOD ft_fRecCount()

	METHOD ft_fEof()
	METHOD ft_fBof()

	METHOD ft_fReadLn()
	METHOD ft_fReadLine()

	METHOD ft_fError( cError )

	METHOD ft_fSetCRLF( cCRLF )
	METHOD ft_fSetBufferSize( nBufferSize )

END CLASS

User Function TOTVSFt()
Return( NIL )

/*/
	METHOD:		New
	Autor:		Marinaldo de Jesus
	Data:		01/05/2011
	Descricao:	CONSTRUCTOR
	Sintaxe:	TOTVSFt():New() : Object do Tipo fT
/*/
METHOD New() Class TOTVSFt

	Self:aLines			:= Array(0)

	Self:cFile			:= ""
	Self:cLine			:= ""

	Self:cClassName		:= "FT"

	Self:nRecno			:= 0
	Self:nLastRecno		:= 0
	Self:nfHandle		:= -1
	Self:nFileSize		:= 0

	Self:ft_fSetCRLF()
	Self:ft_fSetBufferSize()

Return( Self )

/*/
	METHOD:		ClassName
	Autor:		Marinaldo de Jesus
	Data:		01/05/2011
	Descricao:	Retornar o Nome da Classe
	Sintaxe:	TOTVSFt():ClassName() : Retorna o Nome da Classe
/*/
METHOD ClassName() Class TOTVSFt
Return( Self:cClassName )

/*/
	METHOD:		ft_fUse
	Autor:		Marinaldo de Jesus
	Data:		01/05/2011
	Descricao:	Abrir o Arquivo Passado como Parametro
	Sintaxe:	TOTVSFt():ft_fUse( cFile ) : nfHandle ( nfHandle > 0 True, False)
/*/
METHOD ft_fUse( cFile ) Class TOTVSFt

	TRYEXCEPTION

		IF !( Self:ft_fExists( cFile ) )
			BREAK
		EndIF

		Self:ft_fOpen( cFile )

	CATCHEXCEPTION

		Self:ft_fClose()

	ENDEXCEPTION

Return( Self:nfHandle )

/*/
	METHOD:		ft_fOpen
	Autor:		Marinaldo de Jesus
	Data:		01/05/2011
	Descricao:	Abrir o Arquivo Passado como Parametro
	Sintaxe:	TOTVSFt():ft_fOpen( cFile ) : nfHandle ( nfHandle > 0 True, False)
/*/
METHOD ft_fOpen( cFile ) Class TOTVSFt

	TRYEXCEPTION

		IF !( Self:ft_fExists( cFile ) )
			BREAK
		EndIF

		Self:cFile		:= cFile
		Self:nfHandle	:= fOpen( Self:cFile , FO_READ )

		IF ( Self:nfHandle <= 0 )
			BREAK
		EndIF

		Self:nFileSize	:= fSeek( Self:nfHandle , 0 , FS_END )

		fSeek( Self:nfHandle , 0 , FS_SET )

		Self:nFileSize	:= ReadFile( @Self:aLines , @Self:nfHandle , @Self:nBufferSize , @Self:nFileSize , @Self:cCRLF )

		Self:ft_fGoTop()

	CATCHEXCEPTION

		Self:nfHandle := -1

	ENDEXCEPTION

Return( Self:nfHandle )

/*/
	Funcao:		ReadFile
	Autor:		Marinaldo de Jesus
	Data:		01/05/2011
	Descricao:	Percorre o Arquivo a ser lido e alimento o Array aLines
	Sintaxe:	ReadFile( aLines , nfHandle , nBufferSize , nFileSize , cCRLF ) : nLines Read
/*/
Static Function ReadFile( aLines , nfHandle , nBufferSize , nFileSize , cCRLF )

	Local cLine			:= ""
	Local cBuffer		:= ""

	Local nLines		:= 0
	Local nAtPlus		:= ( Len( cCRLF ) -1 )
	Local nBytesRead	:= 0

	While ( nBytesRead <= nFileSize )
		cBuffer 		+= fReadStr( @nfHandle , @nBufferSize )
		nBytesRead		+= nBufferSize
		While ( cCRLF $ cBuffer )
			++nLines
			cLine 		:= SubStr( cBuffer , 1 , ( AT( cCRLF , cBuffer ) + nAtPlus ) )
			cBuffer		:= SubStr( cBuffer , Len( cLine ) + 1 )
			cLine		:= StrTran( cLine , cCRLF , "" )
			aAdd( aLines , cLine )
			cLine		:= ""
		End While
	End While

	IF !Empty( cBuffer )
		++nLines
		aAdd( aLines , cBuffer )
		cBuffer	:= ""
	EndIF

Return( nLines )

/*/
	METHOD:		ft_fClose
	Autor:		Marinaldo de Jesus
	Data:		01/05/2011
	Descricao:	Fechar o Arquivo aberto pela ft_fOpen ou ft_fUse
	Sintaxe:	TOTVSFt():ft_fClose() : NIL
/*/
METHOD ft_fClose() Class TOTVSFt

	IF ( Self:nfHandle > 0 )
		fClose( Self:nfHandle )
	EndIF

	aSize( Self:aLines , 0 )

	Self:cFile			:= ""
	Self:cLine			:= ""

	Self:nRecno			:= 0
	Self:nfHandle		:= -1
	Self:nFileSize		:= 0
	Self:nLastRecno		:= 0

Return( NIL )

/*/
	METHOD:		ft_fAlias
	Autor:		Marinaldo de Jesus
	Data:		01/05/2011
	Descricao:	Retornar o Nome do Arquivo Atualmente Aberto
	Sintaxe:	TOTVSFt():ft_fAlias() : cFile
/*/
METHOD ft_fAlias() Class TOTVSFt
Return( Self:cFile )

/*/
	METHOD:		ft_fExists
	Autor:		Marinaldo de Jesus
	Data:		01/05/2011
	Descricao:	Verifica se o Arquivo Existe
	Sintaxe:	TOTVSFt():ft_fExists( cFile ) : lExists
/*/
METHOD ft_fExists( cFile ) Class TOTVSFt

	Local lExists

	TRYEXCEPTION

		IF Empty( cFile )
			BREAK
		EndIF

		lExists := File( cFile )

	CATCHEXCEPTION

		lExists := .F.

	ENDEXCEPTION

Return( lExists )

/*/
	METHOD:		ft_fRecno
	Autor:		Marinaldo de Jesus
	Data:		01/05/2011
	Descricao:	Retorna o Recno Atual
	Sintaxe:	TOTVSFt():ft_fRecno() : nRecno
/*/
METHOD ft_fRecno() Class TOTVSFt
Return( Self:nRecno )

/*/
	METHOD:		ft_fSkip
	Autor:		Marinaldo de Jesus
	Data:		01/05/2011
	Descricao:	Salta n Posicoes
	Sintaxe:	TOTVSFt():ft_fSkip( nSkipper ) : nRecno
/*/
METHOD ft_fSkip( nSkipper ) Class TOTVSFt

	DEFAULT nSkipper	:= 1

	Self:nRecno	+= nSkipper

Return( Self:nRecno )

/*/
	METHOD:		ft_fGoTo
	Autor:		Marinaldo de Jesus
	Data:		01/05/2011
	Descricao:	Salta para o Registro informando em nGoto
	Sintaxe:	TOTVSFt():ft_fGoTo( nGoTo ) : nRecno
/*/
METHOD ft_fGoTo( nGoTo ) Class TOTVSFt

	Self:nRecno	:= nGoTo

Return( Self:nRecno )

/*/
	METHOD:		ft_fGoTop
	Autor:		Marinaldo de Jesus
	Data:		01/05/2011
	Descricao:	Salta para o Inicio do Arquivo
	Sintaxe:	TOTVSFt():ft_fGoTo( nGoTo ) : nRecno
/*/
METHOD ft_fGoTop() Class TOTVSFt
Return( Self:ft_fGoTo( 1 ) )

/*/
	METHOD:		ft_fGoBottom
	Autor:		Marinaldo de Jesus
	Data:		01/05/2011
	Descricao:	Salta para o Final do Arquivo
	Sintaxe:	TOTVSFt():ft_fGoBottom() : nRecno
/*/
METHOD ft_fGoBottom() Class TOTVSFt
Return( Self:ft_fGoTo( Self:nFileSize ) )

/*/
	METHOD:		ft_fLastRec
	Autor:		Marinaldo de Jesus
	Data:		01/05/2011
	Descricao:	Retorna o Numero de Registro do Arquivo
	Sintaxe:	TOTVSFt():ft_fLastRec() : nRecCount
/*/
METHOD ft_fLastRec() Class TOTVSFt
Return( Self:nFileSize )

/*/
	METHOD:		ft_fRecCount
	Autor:		Marinaldo de Jesus
	Data:		01/05/2011
	Descricao:	Retorna o Numero de Registro do Arquivo
	Sintaxe:	TOTVSFt():ft_fRecCount() : nRecCount
/*/
METHOD ft_fRecCount() Class TOTVSFt
Return( Self:nFileSize )

/*/
	METHOD:		ft_fEof
	Autor:		Marinaldo de Jesus
	Data:		01/05/2011
	Descricao:	Verifica se Atingiu o Final do Arquivo
	Sintaxe:	TOTVSFt():ft_fEof() : lEof
/*/
METHOD ft_fEof() Class TOTVSFt
Return( Self:nRecno > Self:nFileSize )

/*/
	METHOD:		ft_fBof
	Autor:		Marinaldo de Jesus
	Data:		01/05/2011
	Descricao:	Verifica se Atingiu o Inicio do Arquivo
	Sintaxe:	TOTVSFt():ft_fBof() : lBof
/*/
METHOD ft_fBof() Class TOTVSFt
Return( Self:nRecno < 1 )

/*/
	METHOD:		ft_fReadLine
	Autor:		Marinaldo de Jesus
	Data:		01/05/2011
	Descricao:	Le a Linha do Registro Atualmente Posicionado
	Sintaxe:	TOTVSFt():ft_fReadLine() : cLine
/*/
METHOD ft_fReadLine() Class TOTVSFt

	TRYEXCEPTION

		Self:nLastRecno	:= Self:nRecno
		Self:cLine		:= Self:aLines[ Self:nRecno ]

	CATCHEXCEPTION

		Self:cLine	:= ""

	ENDEXCEPTION

Return( Self:cLine )

/*/
	METHOD:		ft_fReadLn
	Autor:		Marinaldo de Jesus
	Data:		01/05/2011
	Descricao:	Le a Linha do Registro Atualmente Posicionado
	Sintaxe:	TOTVSFt():ft_fReadLn() : cLine
/*/
METHOD ft_fReadLn() Class TOTVSFt
Return( Self:ft_fReadLine() )

/*/
	METHOD:		ft_fError
	Autor:		Marinaldo de Jesus
	Data:		01/05/2011
	Descricao:	Retorna o Ultimo erro ocorrido
	Sintaxe:	TOTVSFt():ft_fError( @cError ) : nDosError
/*/
METHOD ft_fError( cError ) Class TOTVSFt
	cError	:= CaptureError()
Return( fError() )

/*/
	METHOD:		ft_fSetBufferSize
	Autor:		Marinaldo de Jesus
	Data:		01/05/2011
	Descricao:	Redefine nBufferSize
	Sintaxe:	TOTVSFt():ft_fSetBufferSize( nBufferSize ) : nLastBufferSize
/*/
METHOD ft_fSetBufferSize( nBufferSize ) Class TOTVSFt

	Local nLastBufferSize	:= Self:nBufferSize

	DEFAULT nBufferSize	:= 1024

	Self:nBufferSize	:= nBufferSize
	Self:nBufferSize	:= Max( Self:nBufferSize , 1 )

Return( nLastBufferSize )

/*/
	METHOD:		ft_fSetCRLF
	Autor:		Marinaldo de Jesus
	Data:		01/05/2011
	Descricao:	Redefine cCRLF
	Sintaxe:	TOTVSFt():ft_fSetCRLF( cCRLF ) : nLastCRLF
/*/
METHOD ft_fSetCRLF( cCRLF ) Class TOTVSFt

	Local cLastCRLF	:= Self:cCRLF

	DEFAULT cCRLF	:= CRLF

	Self:cCRLF		:= cCRLF

Return( cLastCRLF )
