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
Descobrir Sessao em INI dinamico
Moderador: Moderadores
Descobrir Sessao em INI dinamico
fui...
e-mail:janioaguiar@yahoo.com.br
msn: janio_aguiar@hotmail.com
xHarbour1.2.1/Harbour3.2 + wvg + hwgui + Mediator + MySql
e-mail:janioaguiar@yahoo.com.br
msn: janio_aguiar@hotmail.com
xHarbour1.2.1/Harbour3.2 + wvg + hwgui + Mediator + MySql
Descobrir Sessao em INI dinamico
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
e-mail:janioaguiar@yahoo.com.br
msn: janio_aguiar@hotmail.com
xHarbour1.2.1/Harbour3.2 + wvg + hwgui + Mediator + MySql
Descobrir Sessao em INI dinamico
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
Este fonte foi compilado com harbour release 17974.
Espero ter ajudado,
Rossine.
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
Espero ter ajudado,
Rossine.
Rossine.
Harbour 3.4, MingW / Msvc, QT, Qt5xhb, GtQtc, DbfCdx, MySql/MariaDB.
Harbour 3.4, MingW / Msvc, QT, Qt5xhb, GtQtc, DbfCdx, MySql/MariaDB.

