Protheus :: ADVPL : Obtendo valores de um arquivo de configuração (*.INI)
ADVPL possui algumas funções padrões para leitura e/ou manipulação de arquivos de configuração (*.INI) [tdn : Manipulação do arquivo de configuração (*.INI) ].
O grande problema dessas funções é: ou manipulam arquivos no Client do Usuário ou arquivos de configuração do Próprio Protheus.
Caso necessitemos trabalhar com arquivos (*.INI) customizados teremos que escrever nosso próprio código para esse fim.
Caso necessitemos trabalhar com arquivos (*.INI) customizados teremos que escrever nosso próprio código para esse fim.
Para fins didáticos, implementei a função:
INIGetPValue( cFile , cSession , cPropertyName , cDefaultValue , cIgnoreToken )
Para auxiliar no entendimento e aprendizado de Leitura de Arquivos .INI.
Utilizei nela a classe FT [ Protheus :: Alternativa às Funções do Tipo FT_F* ]
Para auxiliar no entendimento e aprendizado de Leitura de Arquivos .INI.
Utilizei nela a classe FT [ Protheus :: Alternativa às Funções do Tipo FT_F* ]
#DEFINE SESSION_POSITION 1 #DEFINE PROPERTY_POSITION 2 #DEFINE PROPERTY_NAME 1 #DEFINE PROPERTY_VALUE 2 #DEFINE PROPERTY_ELEMENTS 2 /*/ Funcao: INIGetPValue Autor: Marinaldo de Jesus Data: 26/05/2011 Uso: Retornar o Valor Atribuido a uma Propriedade de Acordo com a Sessao em um arquivo .INI Sintaxe: StaticCall( INIGetPValue , INIGetPValue , cFile , cSession , cPropertyName , cDefault ) /*/ Static Function INIGetPValue( cFile , cSession , cPropertyName , cDefaultValue , cIgnoreToken ) Local aProperties := {} Local cPropertyValue := "@__PROPERTY_NOT_FOUND__@" Local lExit Local nAT Local nATLine Local nSession Local nProperty Local nProperties Local nATIgnoreTkn Local ofT BEGIN SEQUENCE IF Empty( cFile ) BREAK EndIF IF !File( cFile ) BREAK ENDIF ofT := fT():New() IF ( ofT:ft_fUse( cFile ) <= 0 ) ofT:ft_fUse() BREAK EndIF DEFAULT cSession := Chr(255) DEFAULT cPropertyName := "" DEFAULT cIgnoreToken := ";" cSession := Lower( AllTrim( cSession ) ) cPropertyName := Lower( AllTrim( cPropertyName ) ) While !( ofT:ft_fEof() ) cLine := ofT:ft_fReadLn() BEGIN SEQUENCE IF Empty( cLine ) BREAK EndIF IF ( cIgnoreToken $ cLine ) cLine := AllTrim( cLine ) nATIgnoreTkn := AT( cIgnoreToken , cLine ) IF ( nATIgnoreTkn == 1 ) BREAK EndIF cLine := SubStr( cLine , 1 , nATIgnoreTkn - 1 ) EndIF IF !( "[" $ cLine ) BREAK ENDIF lExit := .F. nATLine := 0 aAdd( aProperties , { Lower( AllTrim( StrTran( StrTran( cLine , "[" , "" ) , "]" , "" ) ) ) , Array( 0 ) } ) nProperties := Len( aProperties ) ofT:ft_fSkip() While !( ofT:ft_fEof() ) cLine := ofT:ft_fReadLn() BEGIN SEQUENCE IF Empty( cLine ) BREAK EndIF IF ( cIgnoreToken $ cLine ) cLine := AllTrim( cLine ) nATIgnoreTkn := AT( cIgnoreToken , cLine ) IF ( nATIgnoreTkn == 1 ) nATLine := 0 lExit := .T. BREAK EndIF cLine := SubStr( cLine , 1 , nATIgnoreTkn - 1 ) EndIF IF ( "[" $ cLine ) lExit := .T. BREAK EndIF aAdd( aProperties[ nProperties ][ PROPERTY_POSITION ] , Array( PROPERTY_ELEMENTS ) ) nProperty := Len( aProperties[ nProperties ][ PROPERTY_POSITION ] ) nAT := AT( "=" , cLine ) aProperties[ nProperties ][ PROPERTY_POSITION ][ nProperty ][ PROPERTY_NAME ] := Lower( AllTrim( SubStr( cLine , 1 , nAT - 1 ) ) ) aProperties[ nProperties ][ PROPERTY_POSITION ][ nProperty ][ PROPERTY_VALUE ] := SubStr( cLine , nAT + 1 ) cLine := "" END SEQUENCE IF ( lExit ) EXIT EndIF nATLine := ofT:ft_fRecno() ofT:ft_fSkip() End While IF ( nATLine > 0 ) ofT:ft_fGoto( nATLine ) EndIF END SEQUENCE ofT:ft_fSkip() End While ofT:ft_fUse() nSession := aScan( aProperties , { |aFindSession| ( aFindSession[ SESSION_POSITION ] == cSession ) } ) IF ( nSession == 0 ) BREAK EndIF nProperty := aScan( aProperties[ nSession ][ PROPERTY_POSITION ] , { |aValues| ( aValues[ PROPERTY_NAME ] == cPropertyName ) } ) IF ( nProperty == 0 ) BREAK EndIF cPropertyValue := aProperties[ nSession ][ PROPERTY_POSITION ][ nProperty ][ PROPERTY_VALUE ] END SEQUENCE IF ( cPropertyValue == "@__PROPERTY_NOT_FOUND__@" ) IF !Empty( cDefaultValue ) cPropertyValue := cDefaultValue Else cPropertyValue := "" EndIF EndIF Return( cPropertyValue ) Static Function __Dummy( lRecursa ) BEGIN SEQUENCE lRecursa := .F. IF !( lRecursa ) BREAK EndIF INIGetPValue() lRecursa := __Dummy( lRecursa ) END SEQUENCE Return( lRecursa )
O Conteúdo do Arquivo INI:
;[session1] PropertyName1=PropertiValue1 [session2] PropertyName1=PropertyValue1;Isso é um comentario PropertyName2=PropertyValue2 ;[session3] PropertyName1=PropertyValue1 PropertyName2=PropertyValue2 PropertyName2=PropertyValue3 [session4] ;Isso é um Comentário PropertyName1=PropertyValue1 PropertyName2=PropertyValue2 PropertyName3=PropertyValue3 PropertyName4=PropertyValue4
O Exemplo de Uso:
Para baixar o código fonte e o arquivo de exemplo, clique aqui
#INCLUDE "PROTHEUS.CH" User Function IniPValue() cPVS1 := StaticCall( INIGetPValue , INIGetPValue , "D:\INIGetPValue\INIGetPValue.INI" , "session1" , "PropertyName1" , "NO_FOUND" , ";" ) cPVS2 := StaticCall( INIGetPValue , INIGetPValue , "D:\INIGetPValue\INIGetPValue.INI" , "session2" , "PropertyName2" , "NO_FOUND" , ";" ) cPVS3 := StaticCall( INIGetPValue , INIGetPValue , "D:\INIGetPValue\INIGetPValue.INI" , "session3" , "PropertyName3" , "NO_FOUND" , ";" ) cPVS4 := StaticCall( INIGetPValue , INIGetPValue , "D:\INIGetPValue\INIGetPValue.INI" , "session4" , "PropertyName3" , "NO_FOUND" , ";" ) Return( NIL )
[]s
иαldσ dj
Olá Marinaldo,
ResponderExcluirFiz o teste com essa rotina, mas só retorna vazio.
Elisandro