Amiguinhos,
rubensDeve estar faltando alguma procedure, classe ou função.
Veja a
INIFILES.PRG contida no
HarbourBoleto//
// $Id: inifiles.prg,v 1.1 2007/08/02 00:51:33 masturm Exp $
//
#define CRLF (Chr(13) + Chr(10))
function TIniFile()
static oClass
if oClass == nil
oClass := HBClass():New( 'TINIFILE' ) // starts a new class definition
oClass:AddData( 'FileName' ) // define this class objects datas
oClass:AddData( 'Contents' )
oClass:AddMethod( 'New', @New() ) // define this class objects methods
oClass:AddMethod( 'ReadString', @ReadString() )
oClass:AddMethod( 'WriteString', @WriteString() )
oClass:AddMethod( 'ReadNumber', @ReadNumber() )
oClass:AddMethod( 'WriteNumber', @WriteNumber() )
oClass:AddMethod( 'ReadDate', @ReadDate() )
oClass:AddMethod( 'WriteDate', @WriteDate() )
oClass:AddMethod( 'ReadBool', @ReadBool() )
oClass:AddMethod( 'WriteBool', @WriteBool() )
oClass:AddMethod( 'ReadSection', @ReadSection() )
oClass:AddMethod( 'ReadSections', @ReadSections() )
oClass:AddMethod( 'DeleteKey', @DeleteKey() )
oClass:AddMethod( 'EraseSection', @EraseSection() )
oClass:AddMethod( 'UpdateFile', @UpdateFile() )
oClass:Create() // builds this class
endif
return oClass:Instance() // builds an object of this class
static function New(cFileName)
local Self := QSelf()
local Done, hFile, cFile, cLine, cIdent, nPos
local CurrArray
if empty(cFileName)
// raise an error?
outerr('No filename passed to TIniFile():New()')
return nil
else
::FileName := cFilename
::Contents := {}
CurrArray := ::Contents
if File(cFileName)
hFile := fopen(cFilename, 0)
else
hFile := fcreate(cFilename)
endif
cLine := ''
Done := .f.
while !Done
cFile := space(256)
Done := (fread(hFile, @cFile, 256) <= 0)
cFile := strtran(cFile, chr(10), '') // so we can just search for CHR(13)
// prepend last read
cFile := cLine + cFile
while !empty(cFile)
if (nPos := at(chr(13), cFile)) > 0
cLine := left(cFile, nPos - 1)
cFile := substr(cFile, nPos + 1)
if !empty(cLine)
if Left(cLine, 1) == '[' // new section
if (nPos := At(']', cLine)) > 1
cLine := substr(cLine, 2, nPos - 2);
else
cLine := substr(cLine, 2)
endif
AAdd(::Contents, { cLine, { /* this will be CurrArray */ } } )
CurrArray := ::Contents[Len(::Contents)][2]
elseif Left(cLine, 1) == ';' // preserve comments
AAdd( CurrArray, { NIL, cLine } )
else
if (nPos := At('=', cLine)) > 0
cIdent := Left(cLine, nPos - 1)
cLine := SubStr(cLine, nPos + 1)
AAdd( CurrArray, { cIdent, cLine } )
else
AAdd( CurrArray, { cLine, '' } )
endif
endif
cLine := '' // to stop prepend later on
endif
else
cLine := cFile
cFile := ''
endif
end
end
fclose(hFile)
endif
return Self
static function ReadString(cSection, cIdent, cDefault)
local Self := QSelf()
local cResult := cDefault
local i, j, cFind
if Empty(cSection)
cFind := lower(cIdent)
j := AScan( ::Contents, {|x| valtype(x[1]) == 'C' .and. lower(x[1]) == cFind .and. ValType(x[2]) == 'C'} )
if j > 0
cResult := ::Contents[j][2]
endif
else
cFind := lower(cSection)
i := AScan( ::Contents, {|x| valtype(x[1]) == 'C' .and. lower(x[1]) == cFind} )
if i > 0
cFind := lower(cIdent)
j := AScan( ::Contents[i][2], {|x| valtype(x[1]) == 'C' .and. lower(x[1]) == cFind} )
if j > 0
cResult := ::Contents[i][2][j][2]
endif
endif
endif
return cResult
static procedure WriteString(cSection, cIdent, cString)
local Self := QSelf()
local i, j, cFind
if Empty(cIdent)
outerr('Must specify an identifier')
elseif Empty(cSection)
cFind := lower(cIdent)
j := AScan( ::Contents, {|x| valtype(x[1]) == 'C' .and. lower(x[1]) == cFind .and. ValType(x[2]) == 'C'} )
if j > 0
::Contents[j][2] := cString
else
AAdd(::Contents, nil)
AIns(::Contents, 1)
::Contents[1] := {cIdent, cString}
endif
else
cFind := lower(cSection)
if (i := AScan( ::Contents, {|x| valtype(x[1]) == 'C' .and. lower(x[1]) == cFind .and. ValType(x[2]) == 'A'})) > 0
cFind := lower(cIdent)
j := AScan( ::Contents[i][2], {|x| valtype(x[1]) == 'C' .and. lower(x[1]) == cFind} )
if j > 0
::Contents[i][2][j][2] := cString
else
AAdd( ::Contents[i][2], {cIdent, cString} )
endif
else
AAdd( ::Contents, {cSection, {{cIdent, cString}}} )
endif
endif
return
static function ReadNumber(cSection, cIdent, nDefault)
local Self := QSelf()
return Val( ::ReadString(cSection, cIdent, str(nDefault)) )
static procedure WriteNumber(cSection, cIdent, nNumber)
local Self := QSelf()
::WriteString( cSection, cIdent, alltrim(str(nNumber)) )
return
static function ReadDate(cSection, cIdent, dDefault)
local Self := QSelf()
return SToD( ::ReadString(cSection, cIdent, DToS(dDefault)) )
static procedure WriteDate(cSection, cIdent, dDate)
local Self := QSelf()
::WriteString( cSection, cIdent, DToS(dDate) )
return
static function ReadBool(cSection, cIdent, lDefault)
local Self := QSelf()
local cDefault := Iif( lDefault, '.t.', '.f.' )
return ::ReadString(cSection, cIdent, cDefault) == '.t.'
static procedure WriteBool(cSection, cIdent, lBool)
local Self := QSelf()
::WriteString( cSection, cIdent, Iif(lBool, '.t.', '.f.') )
return
static procedure DeleteKey(cSection, cIdent)
local Self := QSelf()
local i, j
cSection := lower(cSection)
i := AScan( ::Contents, {|x| valtype(x[1]) == 'C' .and. lower(x[1]) == cSection} )
if i > 0
cIdent := lower(cIdent)
j := AScan( ::Contents[i][2], {|x| valtype(x[1]) == 'C' .and. lower(x[1]) == cIdent} )
ADel( ::Contents[i][2], j )
ASize( ::Contents[i][2], Len(::Contents[i][2]) - 1 )
endif
return
static procedure EraseSection(cSection)
local Self := QSelf()
local i
if Empty(cSection)
while (i := AScan( ::Contents, {|x| valtype(x[1]) == 'C' .and. ValType(x[2]) == 'C'})) > 0
ADel( ::Contents, i )
ASize( ::Contents, len(::Contents) - 1 )
end
else
cSection := lower(cSection)
if (i := AScan( ::Contents, {|x| valtype(x[1]) == 'C' .and. lower(x[1]) == cSection .and. ValType(x[2]) == 'A'})) > 0
ADel( ::Contents, i )
ASize( ::Contents, Len(::Contents) - 1 )
endif
endif
return
static function ReadSection(cSection)
local Self := QSelf()
local i, j, aSection := {}
if Empty(cSection)
for i := 1 to len(::Contents)
if valtype(::Contents[i][1]) == 'C' .and. valtype(::Contents[i][2]) == 'C'
aadd(aSection, ::Contents[i][1])
endif
next
else
cSection := lower(cSection)
if (i := AScan( ::Contents, {|x| valtype(x[1]) == 'C' .and. x[1] == cSection .and. ValType(x[2]) == 'A'})) > 0
for j := 1 to Len(::Contents[i][2])
if ::Contents[i][2][j][1] <> NIL
AAdd(aSection, ::Contents[i][2][j][1])
endif
next
endif
endif
return aSection
static function ReadSections()
local Self := QSelf()
local i, aSections := {}
for i := 1 to Len(::Contents)
if ValType(::Contents[i][2]) == 'A'
AAdd(aSections, ::Contents[i][1])
endif
next
return aSections
static procedure UpdateFile()
local Self := QSelf()
local i, j, hFile
hFile := fcreate(::Filename)
for i := 1 to Len(::Contents)
if ::Contents[i][1] == NIL
fwrite(hFile, ::Contents[i][2] + Chr(13) + Chr(10))
elseif ValType(::Contents[i][2]) == 'A'
fwrite(hFile, '[' + ::Contents[i][1] + ']' + Chr(13) + Chr(10))
for j := 1 to Len(::Contents[i][2])
if ::Contents[i][2][j][1] == NIL
fwrite(hFile, ::Contents[i][2][j][2] + Chr(13) + Chr(10))
else
fwrite(hFile, ::Contents[i][2][j][1] + '=' + ::Contents[i][2][j][2] + Chr(13) + Chr(10))
endif
next
fwrite(hFile, Chr(13) + Chr(10))
elseif ValType(::Contents[i][2]) == 'C'
fwrite(hFile, ::Contents[i][1] + '=' + ::Contents[i][2] + Chr(13) + Chr(10))
endif
next
fclose(hFile)
return
Veja exemplo de uso no
hBoleto.prg/*
* $Id: hboleto.prg,v 1.3 2007/08/07 14:41:13 masturm Exp $
*/
/*
* Copyright 2006 Mario Simoes Filho mario@argoninformatica.com.br for original demoboleto.prg
* Copyright 2006 Marcelo Sturm <marcelo.sturm@gmail.com> for modifications in the original project
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2, or (at your option)
* any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this software; see the file COPYING. If not, write to
* the Free Software Foundation, Inc., 59 Temple Place, Suite 330,
* Boston, MA 02111-1307 USA (or visit the web site http://www.gnu.org/).
*
* As a special exception, the Harbour Project gives permission for
* additional uses of the text contained in its release of Harbour.
*
* The exception is that, if you link the Harbour libraries with other
* files to produce an executable, this does not by itself cause the
* resulting executable to be covered by the GNU General Public License.
* Your use of that executable is in no way restricted on account of
* linking the Harbour library code into it.
*
* This exception does not however invalidate any other reasons why
* the executable file might be covered by the GNU General Public License.
*
* This exception applies only to the code released by the Harbour
* Project under the name Harbour. If you copy code from other
* Harbour Project or Free Software Foundation releases into a copy of
* Harbour, as the General Public License permits, the exception does
* not apply to the code that you add in this way. To avoid misleading
* anyone as to the status of such modified files, you must delete
* this exception notice from them.
*
* If you write modifications of your own for Harbour, it is your choice
* whether to permit this exception to apply to your modifications.
* If you do not wish that, delete this exception notice.
*
*/
//#include "common.ch"
#define CRLF CHR(13)+CHR(10)
#xtranslate Default( <x>, <y> ) => IIF( <x> == NIL, <y>, <x> )
// Esse programa lê um arquivo .ini de configurações de boletos e gera os boletos a partir dele.
// Gostaria de ter feito com xml, ao invés de ini, mas não conhecia nenhuma classe pronta p/ xml ... e fiz com ini mesmo.
// Se alguém quiser/puder mudar p/ xml, fique à vontade!
// Marcelo Sturm - 01/08/2007
/* -------------------------------------------------------------------------- */
FUNCTION Main( cFileName )
LOCAL oIni, oRetIni, oBol
LOCAL cDir, cDirRemessa, lPrint, lPreview, lPromptPrint, cBol, nI := 0
SET DATE BRIT
// CriaIni(cFileName) // Descomentando esta linha, um arquivo de exemplo é gerado.
oIni = TIniFile():New(Default(cFilename, 'bol.ini'))
oBol := oBoleto(oIni:ReadString("CAB", "Banco"))
oBol:lBoleto := oIni:ReadBool("CAB", "lBoleto", .T.)
oBol:lRemessa := oIni:ReadBool("CAB", "lRemessa", .F.)
oBol:lAnsi := oIni:ReadBool("CAB", "lAnsi", .F.)
lPrint := oIni:ReadBool("CAB", "lPrint", .F.)
lPreview := oIni:ReadBool("CAB", "lPreview", .F.)
lPromptPrint := oIni:ReadBool("CAB", "lPromptPrint", .F.)
oBol:nBolsPag := oIni:ReadNumber("CAB", "nBolsPag", 2)
oBol:cImageLnk := oIni:ReadString("CAB", "cImageLnk")
oBol:Cedente := oIni:ReadString("CAB", "Cedente")
oBol:CedenteCNPJ := oIni:ReadString("CAB", "CedenteCNPJ")
oBol:cNumCC := oIni:ReadString("CAB", "cNumCC")
oBol:cNumAgencia := oIni:ReadString("CAB", "cNumAgencia")
oBol:cCarteira := oIni:ReadString("CAB", "cCarteira")
oBol:EspecieTit := oIni:ReadString("CAB", "EspecieTit")
oBol:cTipoCob := oIni:ReadString("CAB", "cTipoCob")
oBol:nMora := oIni:ReadNumber("CAB", "nMora", 0)
oBol:nMulta := oIni:ReadNumber("CAB", "nMulta", 0)
oBol:nDiasProt := oIni:ReadNumber("CAB", "nDiasProt", 0)
cDir := oIni:ReadString("CAB", "cDir")
cDirRemessa := oIni:ReadString("CAB", "cDirRemessa")
oBol:Open("boleto") //, cDir, cDirRemessa, cDir) // Cria html - Sempre colocar após a definição completa do Cedente, pois
// isso influencia na criação do Arquivo Remessa.
DO WHILE .T.
cBol := "BOL" + LTRIM(STR(++nI))
IF EMPTY(oIni:ReadNumber(cBol, "nValor", 0))
EXIT
ENDIF
oBol:Sacado := oIni:ReadString(cBol, "Sacado")
oBol:Endereco := oIni:ReadString(cBol, "Endereco")
oBol:Bairro := oIni:ReadString(cBol, "Bairro")
oBol:Cidade := oIni:ReadString(cBol, "Cidade")
oBol:Estado := oIni:ReadString(cBol, "Estado")
oBol:CEP := oIni:ReadString(cBol, "CEP")
oBol:CNPJ := oIni:ReadString(cBol, "CNPJ")
oBol:Instrucoes := oIni:ReadString(cBol, "Instrucoes", "")
oBol:cNumDoc := oIni:ReadString(cBol, "cNumDoc", "") // seu numero do documento
oBol:cNossoNumero := oIni:ReadString(cBol, "cNossoNumero", "") // numero do banco
oBol:nValor := oIni:ReadNumber(cBol, "nValor", 0) // valor do boleto
oBol:DtEmis := oIni:ReadDate(cBol, "DtEmis", DATE())
oBol:DtVenc := oIni:ReadDate(cBol, "DtVenc", DATE())
oBol:Execute() // monta html
ENDDO
oBol:Close()
IF lPrint
oBol:Print(lPreview, lPromptPrint) // Imprime o boleto */
ENDIF
IF oBol:lRemessa .AND. !EMPTY(oBol:oRem:NomeRem)
oRetIni = TIniFile():New(Default(cFilename, 'bol.ini') + '.ret')
oRetIni:WriteString("RET", "NomeRem", oBol:oRem:NomeRem)
oRetIni:WriteString("RET", "Destino", oBol:oRem:Destino)
oRetIni:WriteString("RET", "cNumSequencial", oBol:oRem:cNumSequencial)
oRetIni:WriteNumber("RET", "nTitLote", oBol:oRem:nTitLote)
oRetIni:UpdateFile()
ENDIF
RETURN NIL
/* -------------------------------------------------------------------------- */
STATIC FUNCTION CriaIni( cFileName )
LOCAL oIni
oIni = TIniFile():New(Default(cFilename, 'bol.ini'))
oIni:WriteString("CAB", "Banco", "409")
oIni:WriteString("CAB", "cImageLnk", "")
oIni:WriteBool("CAB", "lBoleto", .T.)
oIni:WriteBool("CAB", "lRemessa", .T.)
oIni:WriteBool("CAB", "lAnsi", .T.)
oIni:WriteBool("CAB", "lPrint", .T.)
oIni:WriteBool("CAB", "lPreview", .T.)
oIni:WriteBool("CAB", "lPromptPrint", .T.)
oIni:WriteNumber("CAB", "nBolsPag", 2)
oIni:WriteString("CAB", "Cedente", "Teste de Cedente")
oIni:WriteString("CAB", "CedenteCNPJ", "11111111111180")
oIni:WriteString("CAB", "cNumCC", "100778-3")
oIni:WriteString("CAB", "cNumAgencia", "1748-5")
oIni:WriteString("CAB", "cCarteira", "1")
oIni:WriteString("CAB", "EspecieTit", "DM")
oIni:WriteString("CAB", "cTipoCob", "5")
oIni:WriteNumber("CAB", "nMora", 0)
oIni:WriteNumber("CAB", "nMulta", 0)
oIni:WriteNumber("CAB", "nDiasProt", 0)
oIni:WriteString("CAB", "cDir", "")
oIni:WriteString("CAB", "cDirRemessa", "")
oIni:WriteString("BOL1", "Sacado", "Sacado")
oIni:WriteString("BOL1", "Endereco", "Endereço")
oIni:WriteString("BOL1", "Bairro", "Bairro")
oIni:WriteString("BOL1", "Cidade", "Cidade")
oIni:WriteString("BOL1", "Estado", "Estado")
oIni:WriteString("BOL1", "CEP", "20000000")
oIni:WriteString("BOL1", "CNPJ", "0000000")
oIni:WriteString("BOL1", "Instrucoes", "Observação")
oIni:WriteString("BOL1", "cNumDoc", "001396") // seu numero do documento
oIni:WriteString("BOL1", "cNossoNumero", "7410114733") // numero do banco
oIni:WriteNumber("BOL1", "nValor", 1051.32) // valor do boleto
oIni:WriteDate("BOL1", "DtVenc", CTOD("26/09/2006"))
oIni:WriteString("BOL2", "Sacado", "Cedente 2 - áéíóúàãõâêôüçÁÉÍÓÚÀÃÕÂÊÔÜǪº°§")
oIni:WriteString("BOL2", "Endereco", "Endereço")
oIni:WriteString("BOL2", "Bairro", "Bairro")
oIni:WriteString("BOL2", "Cidade", "Cidade")
oIni:WriteString("BOL2", "Estado", "Estado")
oIni:WriteString("BOL2", "CEP", "20000000")
oIni:WriteString("BOL2", "CNPJ", "0000000")
oIni:WriteString("BOL2", "cNumDoc", "001397") // seu numero do documento
oIni:WriteString("BOL2", "cNossoNumero", "5682521917") // numero do banco
oIni:WriteNumber("BOL2", "nValor", 193.68) // valor do boleto
oIni:WriteDate("BOL2", "DtVenc", CTOD("19/07/2006"))
RETURN oIni:UpdateFile()
Exemplo de uso simplificado:
LOCAL oIni
oIni = TIniFile():New( 'teste.ini' )
...
oIni:WriteString("CAB", "Banco", "409")
...
oIni:WriteBool("CAB", "lBoleto", .T.)
...
oIni:WriteNumber("CAB", "nBolsPag", 2)
...
oIni:WriteDate("BOL1", "DtVenc", CTOD("26/09/2006"))
...
oIni:WriteString("BOL2", "Sacado", "Cedente 2 - áéíóúàãõâêôüçÁÉÍÓÚÀÃÕÂÊÔÜǪº°§")
...
oIni:UpdateFile()
Para cada tipo da linguagem você tem uma função que trata e salva o conteúdo.