Descobrir Sessao em INI dinamico

Projeto [x]Harbour - Compilador de código aberto compatível com o Clipper.

Moderador: Moderadores

Avatar do usuário
janio
Colaborador
Colaborador
Mensagens: 1846
Registrado em: 06 Jul 2004 07:43
Localização: UBAJARA - CE

Descobrir Sessao em INI dinamico

Mensagem por janio »

Olá a todos,

Preciso manipular um arquivo .ini dinamico que uma sessão (ou chave) pode ou nao existir. Como saber? Exemplo: No arquivo ini a chave [Produto002] pode ou nao existir. Como saber disso?

Janio
fui...
e-mail:janioaguiar@yahoo.com.br
msn: janio_aguiar@hotmail.com
xHarbour1.2.1/Harbour3.2 + wvg + hwgui + Mediator + MySql
Avatar do usuário
janio
Colaborador
Colaborador
Mensagens: 1846
Registrado em: 06 Jul 2004 07:43
Localização: UBAJARA - CE

Descobrir Sessao em INI dinamico

Mensagem por janio »

Encontrei uma solução paleativa

Código: Selecionar todos

	hIniFile  := Hb_ReadIni( arquivoINI )
	
	aKeys := HGetKeys( hIniFile ) 
	For i = 1 to Len(aKeys)
		If aKeys[i] = chaveProcurada
		    Exit
                Endif
	next
fui...
e-mail:janioaguiar@yahoo.com.br
msn: janio_aguiar@hotmail.com
xHarbour1.2.1/Harbour3.2 + wvg + hwgui + Mediator + MySql
rossine
Usuário Nível 3
Usuário Nível 3
Mensagens: 325
Registrado em: 06 Ago 2007 09:57
Localização: Divinópolis-MG

Descobrir Sessao em INI dinamico

Mensagem por rossine »

Olá Janio,

O código abaixo foi extraido da mingiui, fonte, h_init.prg e c_ini.c. Extrai trechos destes fontes e montei o sample abaixo

Código: Selecionar todos


function main

local aRet

cls

hb_memowrit( "pdv.ini", "[ALIGUOTAS]" + chr(13) + chr(10) + ;
                        "ALIGUOTAS_1800=18.00" + chr(13) + chr(10) + ;
                        "ALIGUOTAS_1200=12.00" + chr(13) + chr(10) + ;
                        "ALIGUOTAS_0700=07.00" + chr(13) + chr(10) + ;
                        "ALIGUOTAS_0500=05.00" + chr(13) + chr(10) )

aRet := _GetSection( "ALIGUOTAS", ".\pdv.ini" )

? hb_valtoexp( aRet )

return NIL

*-----------------------------------------------------------------------------*
Function _GetSectionNames(cIniFile)
*-----------------------------------------------------------------------------*
// return 1-dimmensional array with section list in cIniFile
// or empty array if no sections are present
   Local aSectionList := {}, cLista, aLista

   If FILE( cIniFile )
      cLista := _GetPrivateProfileSectionNames( cIniFile )
      aLista := _SPLIT( cLista, CHR(0) )
      if !EMPTY( aLista )
         AEVAL( aLista, { |cVal| iif( EMPTY(cVal), , AADD( aSectionList, cVal ) ) } ,, LEN( aLista ) - 1 )
      endif
   Else
      ? "Error - Can`t open " + cIniFile
   EndIf

Return ( aSectionList )

*-----------------------------------------------------------------------------*
Function _GetSection( cSection, cIniFile )
*-----------------------------------------------------------------------------*
// return 2-dimmensional array with {key,value} pairs from section <cSection> in <cIniFile>
   Local aKeyValueList := {}, cLista, aLista, i, n

   If FILE( cIniFile )
      cLista := _GetPrivateProfileSection( cSection, cIniFile )
      aLista := _SPLIT( cLista, CHR(0) )
      If !EMPTY( aLista )
         for i := 1 to LEN( aLista ) - 1
            If ( n := AT( "=", aLista[i] ) ) > 0
               AADD( aKeyValueList, { LEFT( aLista[i], n - 1 ), SUBSTR( aLista[i], n + 1 ) } )
            EndIf
         next i
      EndIf
   Else
      ? "Error - Can`t open " + cIniFile
   Endif

Return ( aKeyValueList )

*-----------------------------------------------------------------------------*
Static Function _SPLIT( elstr, _separator )
*-----------------------------------------------------------------------------*
   Local aElems := {}, Elem, _sep := iif( EMPTY( _separator ), " ", _separator )
   Local nSepPos

   If !EMPTY( elstr )
      elstr := ALLTRIM( elstr )
      do while AT( _sep, elstr ) > 0
         If ( nSepPos := AT( _sep, elstr ) ) > 0
            Elem  := LEFT( elstr, nSepPos - 1 )
            elstr := SUBSTR( elstr, LEN( Elem ) + 2 )
            AADD( aElems, Elem )
         EndIf
      enddo
      AADD( aElems, elstr )
   Endif

Return ( aElems )

#pragma begindump

#include "windows.h"
#include "hbapi.h"

HB_FUNC( _GETPRIVATEPROFILESECTIONNAMES )
{
   TCHAR bBuffer[ 32767 ];
   DWORD dwLen;
   char * lpFileName = ( char * ) hb_parc( 1 );

   dwLen = GetPrivateProfileSectionNames( bBuffer, sizeof( bBuffer ), lpFileName );
   hb_retclen( ( char * ) bBuffer, dwLen );
}

// Used to retrieve all key/value pairs of a given section.

HB_FUNC( _GETPRIVATEPROFILESECTION )
{
   TCHAR bBuffer[ 32767 ];
   DWORD dwLen;
   char * lpFileName = ( char * ) hb_parc( 2 );
   char * lpSectionName = ( char * ) hb_parc( 1 );

   dwLen = GetPrivateProfileSectionA( lpSectionName, bBuffer, sizeof( bBuffer ), lpFileName );
   hb_retclen( ( char * ) bBuffer, dwLen );
}

#pragma enddump

Este fonte foi compilado com harbour release 17974.

Espero ter ajudado,

Rossine.
Rossine.

Harbour 3.4, MingW / Msvc, QT, Qt5xhb, GtQtc, DbfCdx, MySql/MariaDB.
Responder