Read record from a inifile

Read record from a inifile

Postby Silvio.Falconi » Mon Nov 24, 2014 9:45 am

I have this file ini

inical.ini
Code: Select all  Expand view
[Cambi]
1=Eur|Lit|1936.27|1Eur = 1936.27
2=Eur|Us$|0.9872|1Eur = 0.9872
3=Eur|Chf|1.452|1Eur = 1.452
4=Eur|Gbp|0.634|1Eur = 0.634
5=Eur|Jpy|117.452|1Eur = 117.452
 


and I must make a array type

aCambi:= {{'Eur', 'Lit', '1936.27'," 1Eur = 1936.27"}, ;
{'Eur', 'Us$', '0.9872', " 1Eur = 0.9872" }, ;
{'Eur', 'Chf', '1.452' , " 1Eur = 1.452" }, ;
{'Eur', 'Gbp', '0.634' , " 1Eur = 0.634" }, ;
{'Eur', 'Jpy', '117.452'," 1Eur = 117.452"}}


on this sample I have 5 lines but it can have many lines
adatatext:= array(5)

INI oIni FILE "inical.ini"
for n= 1 to 5
GET adatatext[n] SECTION "Cambi" ENTRY str(n) OF oIni
ENDINI

and then

for n=1 to 4
aadd(aData, HB_ATokens( aDataText[n] , "|" ) )
next


why it is wrong ?
only I not Know the right number of lines ... how I can make to load all lines ?

exist a function to calc how much lines I found on csection "Cambi" and I can make at init

adatatext:= array( number_of_lines)
Since from 1991/1992 ( fw for clipper Rel. 14.4 - Momos)
I use : FiveWin for Harbour November 2023 - January 2024 - Harbour 3.2.0dev (harbour_bcc770_32_20240309) - Bcc7.70 - xMate ver. 1.15.3 - PellesC - mail: silvio[dot]falconi[at]gmail[dot]com
User avatar
Silvio.Falconi
 
Posts: 7056
Joined: Thu Oct 18, 2012 7:17 pm

Re: Read record from a inifile

Postby dagiayunus » Mon Nov 24, 2014 11:02 am

Dear silvio


Using hb_IniRead and using for each you can load all the lines of ini file.

aIni := hb_IniRead( inifilename )

FOR EACH cSection IN aIni:Keys
?
? "Section [" + cSection + "]"
FOR EACH cKey IN aSect:Keys
? cKey + " = " + aSect[ cKey ]
NEXT
NEXT

Regards
Dagia Yunus.
Regards
Yunus

FWH 21.02
dagiayunus
 
Posts: 85
Joined: Wed Nov 19, 2014 1:04 pm

Re: Read record from a inifile

Postby Silvio.Falconi » Mon Nov 24, 2014 11:08 am

I use xharbour , I not found iniread func
Since from 1991/1992 ( fw for clipper Rel. 14.4 - Momos)
I use : FiveWin for Harbour November 2023 - January 2024 - Harbour 3.2.0dev (harbour_bcc770_32_20240309) - Bcc7.70 - xMate ver. 1.15.3 - PellesC - mail: silvio[dot]falconi[at]gmail[dot]com
User avatar
Silvio.Falconi
 
Posts: 7056
Joined: Thu Oct 18, 2012 7:17 pm

Re: Read record from a inifile

Postby dagiayunus » Mon Nov 24, 2014 11:27 am

I am using harbour

https://github.com/harbour/core/blob/ma ... /hbini.prg

Regards
Dagia Yunus.
Regards
Yunus

FWH 21.02
dagiayunus
 
Posts: 85
Joined: Wed Nov 19, 2014 1:04 pm

Re: Read record from a inifile

Postby Silvio.Falconi » Mon Nov 24, 2014 12:02 pm

Found the solution .....from oldest clipper ...

 Function test()
      aMyIni   := IniLoad( "Calc.ini" )
     ? IniCount( aMyIni, "Cambi" )
return nil


return me the number of lines

Code: Select all  Expand view


#include "fivewin.ch"
#include "common.ch"
#include "fileio.ch"


#define BLOKSIZE 128
#define CR CHR(13)
#define LF CHR(10)
#define CRLF ( CR + LF )
#define INIEXT ".INI"
#define NULSECT "**NUL SECTION**"

   Function test()
      aMyIni   := IniLoad( "Calc.ini" )
     ? IniCount( aMyIni, "Cambi" )


return nil



FUNCTION IniLoad( cFile )
    LOCAL lSuccess, nFileEnd, nCurrent, hIniFile, cLine, aIni
    LOCAL cItem,    cValue,   nEquPos
*    DEFAULT cFile TO "APPLIC"
    IF ! ( "." $ cFile )
        cFile += INIEXT
    ENDIF
    aIni := {}                                  // Initialise array
    hIniFile  := FOPEN( cFile, FO_READ + FO_DENYWRITE )
    IF FERROR() == 0
        IF EMPTY( aIni )
            AADD( aIni, { NULSECT, {} } )
        ENDIF
        nFileEnd := FSEEK( hIniFile, 0, FS_END )     // Find end of file
        FSEEK( hIniFile, 0, FS_SET)                  // Reposition at top
        DO WHILE FSEEK( hIniFile, 0, FS_RELATIVE ) < nFileEnd
            cLine := FReadLyn( hIniFile )
            DO CASE
                CASE EMPTY( cLine )
                    AADD( aIni[ LEN( aIni ), 2 ], { "", "" } )
                CASE LEFT( cLine , 1 ) == ";"
                    AADD( aIni[ LEN( aIni ), 2 ], { cLine, "" } )
                CASE LEFT( cLine, 1 ) == "[" .AND. RIGHT( cLine, 1 ) == "]"
                    cSection := SUBSTR( cLine, 2 )
                    cSection := LEFT( cSection, LEN( cSection ) - 1 )
                    AADD( aIni, { cSection, {} } )
                CASE (nEquPos := AT( "=", cLine )) > 0
                    cItem   := LEFT( cLine , nEquPos - 1)
                    cValue  := SUBSTR( cLine, nEquPos + 1)
                    AADD( aIni[ LEN( aIni ), 2 ], { cItem, cValue } )
                OTHERWISE
                    * Do nothing - it has the wrong structure
            ENDCASE
        ENDDO
        FCLOSE( hIniFile )
    ENDIF
    RETURN( aIni )





   FUNCTION IniCount( aIni, xSection )
    LOCAL nOutPos, nCount := 0
    IF xSection == NIL
        nCount := LEN( aIni ) - 1
    ELSE
        nOutPos := 0
        IF VALTYPE( xSection ) == "N"
            nOutPos := xSection + 1
        ELSE
            nOutPos := ASCAN( aIni, {|x| UPPER(x[1]) == UPPER(xSection) } )
        ENDIF
        IF nOutPos >= 1 .AND. nOutPos <= LEN( aIni )
            nCount := 0
            AEVAL( aIni[ nOutPos, 2 ], {|x| IF(IsAKey(x[1]), nCount++, 0)} )
        ENDIF
    ENDIF
RETURN( nCount )



STATIC FUNCTION IsAKey( cItem )
    LOCAL lRetVal
    lRetVal := ( LEFT( cItem, 1 ) != ";" )
    lRetVal := lRetVal .AND. (! EMPTY( cItem ) )
    RETURN( lRetVal )


 STATIC FUNCTION FReadLyn( hReadFile )
    LOCAL cReadBuf, cPostBuf, nFilePos, nCharPos, lReadMor, nReadAmt
    lFileEnd := .F.
    nFilePos := FSEEK( hReadFile, 0, FS_RELATIVE )
    cReadBuf := SPACE( BLOKSIZE )
    cPostBuf := ""
    lReadMor := .T.
    DO WHILE lReadMor
        nReadAmt := FREAD( hReadFile, @cReadBuf, BLOKSIZE )
        lReadMor := (! ( CR $ cReadBuf )) .AND. ( nReadAmt == BLOKSIZE )
        IF lReadMor
            cPostBuf += cReadBuf
            cReadBuf := SPACE( BLOKSIZE )
        ELSE
            IF CR $ cReadBuf
                nCharPos := AT( CR, cReadBuf ) - 1
            ELSE
                nCharPos := nReadAmt
            ENDIF
            cPostBuf += LEFT( cReadBuf, nCharPos )
            FSEEK( hReadFile, nFilePos + LEN( cPostBuf ) + 1, FS_SET )
        ENDIF
    ENDDO
    IF LEFT( cPostBuf, 1 ) == LF
        cPostBuf := SUBSTR( cPostBuf, 2 )
    ENDIF
RETURN( cPostBuf )
 
Since from 1991/1992 ( fw for clipper Rel. 14.4 - Momos)
I use : FiveWin for Harbour November 2023 - January 2024 - Harbour 3.2.0dev (harbour_bcc770_32_20240309) - Bcc7.70 - xMate ver. 1.15.3 - PellesC - mail: silvio[dot]falconi[at]gmail[dot]com
User avatar
Silvio.Falconi
 
Posts: 7056
Joined: Thu Oct 18, 2012 7:17 pm


Return to FiveWin for Harbour/xHarbour

Who is online

Users browsing this forum: No registered users and 37 guests