REad From INI FILE

REad From INI FILE

Postby Eoeo » Fri Aug 24, 2012 9:05 pm

I found this function to save on an array the string from Ini FIle


FUNCTION GetIniArray(cSection,cFile) // reads a complete section from a *.ini file into an array
LOCAL nI
LOCAL aString := {" "," "}

aEntry := GetPvProfA(cSection,NIL, "", cFile)

? MlCount(aEntry)

IF EMPTY(aEntry)
return {}
ENDIF

for nI:=1 to LEN(aEntry)
AADD(aString,{aEntry,GetPvProfString(cSection,aEntry[nI],"",cFile)})

next

return aString




BUt I need GetPvProfA and the I wish an array with two fields

sample
[Cars]

Alfa= mmmmmmm
Opel=lllllllll
Daihatsu=oooooooo

An array two field aentry and string of each aentry
User avatar
Eoeo
 
Posts: 222
Joined: Mon Jun 04, 2012 12:00 pm

Re: REad From INI FILE

Postby ukoenig » Sat Aug 25, 2012 11:27 pm

Silvio,

Image

a Test-array :

aData[1][1] := { "Toyota", "Value 1" }
aData[2][1] := { "Volvo", "Value 2" }
aData[3][1] := { "Opel", "Value 3" }
aData[4][1] := { "Ford", "Value 4" }
aData[5][1] := { "Peugot", "Value 5" }


The INI-file :

[System]
cField_1=Toyota ,Value 1
cField_2=Volvo ,Value 2
cField_3=Opel ,Value 3
cField_4=Ford ,Value 4
cField_5=Peugot ,Value 5


Code: Select all  Expand view

#INCLUDE "Fivewin.ch"

STATIC c_Path, aData[5][1]

Function MAIN()
Local oDlg, oTxtFont
Local oGet[10]

DEFINE FONT oTxtfont NAME 'Arial' SIZE 0,-14 BOLD

c_path := GETCURDIR()
IF !FILE( c_path + "\Test.EXE" ) // Change to Name of Application !!!!!!!
    c_path := CURDRIVE() + ":\" + GETCURDIR()
ENDIF

aData[1][1] := { "
Toyota",  "Value 1" }
aData[2][1] := { "
Volvo",  "Value 2" }
aData[3][1] := { "
Opel",  "Value 3" }
aData[4][1] := { "
Ford",  "Value 4" }
aData[5][1] := { "
Peugot",  "Value 5" }

DEFINE DIALOG oDlg SIZE 500, 300

READ_INI()

@ 10, 50 GET oGet[1] VAR aData[1][1][1] OF oDlg SIZE 35, 15 PIXEL ;
FONT oTxtFont COLOR "
W+/G" UPDATE
@ 10, 90 GET oGet[2] VAR aData[1][1][2] OF oDlg SIZE 35, 15 PIXEL ;
FONT oTxtFont COLOR "
W+/G"  UPDATE

@ 30, 50 GET oGet[3] VAR aData[2][1][1] OF oDlg SIZE 35, 15 PIXEL ;
FONT oTxtFont COLOR "
W+/G"  UPDATE
@ 30, 90 GET oGet[4] VAR aData[2][1][2] OF oDlg SIZE 35, 15 PIXEL ;
FONT oTxtFont COLOR "
W+/G"  UPDATE

@ 50, 50 GET oGet[5] VAR aData[3][1][1] OF oDlg SIZE 35, 15 PIXEL ;
FONT oTxtFont COLOR "
W+/G"  UPDATE
@ 50, 90 GET oGet[6] VAR aData[3][1][2] OF oDlg SIZE 35, 15 PIXEL ;
FONT oTxtFont COLOR "
W+/G"  UPDATE

@ 70, 50 GET oGet[7] VAR aData[4][1][1] OF oDlg SIZE 35, 15 PIXEL ;
FONT oTxtFont COLOR "
W+/G"  UPDATE
@ 70, 90 GET oGet[8] VAR aData[4][1][2] OF oDlg SIZE 35, 15 PIXEL ;
FONT oTxtFont COLOR "
W+/G"  UPDATE

@ 90, 50 GET oGet[9] VAR aData[5][1][1] OF oDlg SIZE 35, 15 PIXEL ;
FONT oTxtFont COLOR "
W+/G"  UPDATE
@ 90, 90 GET oGet[10] VAR aData[5][1][2] OF oDlg SIZE 35, 15 PIXEL ;
FONT oTxtFont COLOR "
W+/G"  UPDATE

@ 120, 50 BUTTON "
Save to INI"  SIZE 50, 20 PIXEL OF oDlg ;
ACTION ( oDlg:Update(), SAVE_INI() )

ACTIVATE DIALOG oDlg CENTERED

oTxtFont:End()

RETURN( NIL )

// --------- INI - Read -------------

FUNCTION READ_INI()
Local oInifile, cField := "
", nPos1 := 1, nPos2 := 1, cValue := ""

IF !FILE ( c_path + "
\SETTINGS.INI" )
    SAVE_INI()
ELSE
    INI oInifile FILE c_path + "
\SETTINGS.INI"
        I := 1
        FOR I := 1 TO 5
            cField := "
cField_" + ALLTRIM(STR(I))
            GET  cValue SECTION  "
System"  ENTRY cField OF oInifile DEFAULT ""
            // MsgAlert( cValue )
            nPos1 := AT( "
,", Alltrim(cValue) )
            nPos2 := LEN( Alltrim(cValue) ) - nPos1
            aData[I][1][1] := PADR( SUBSTR( cValue, 1, nPos1 -1 ), 10, "
")
            aData[I][1][2] := PADR( SUBSTR( cValue, nPos1 + 1, nPos2 ), 10, "
")
            // msgalert( aData[I][1][1] )
            // msgalert( aData[I][1][2] )
        NEXT
        lSysini := .F.
    ENDINI
ENDIF

RETURN( NIL )

// ---------  INI Write------------------------

FUNCTION SAVE_INI()
LOCAL lSysini := .T., cIniFile := c_path + "
\SETTINGS.INI", cField := "", cValue := ""
I := 1
FOR I := 1 TO LEN(aData)
    // msgalert( aData[I][1][1] )
    // msgalert( aData[I][1][2] )
    cField := "
cField_" + ALLTRIM(STR(I))
        cValue := aData[I][1][1] + "
," + aData[I][1][2]
    IF WritePProString( "
System", cField, cValue, cIniFile ) = .F.
        lSysini := .F.
    ENDIF
NEXT

RETURN( NIL )


Best Regards
Uwe :lol:
Since 1995 ( the first release of FW 1.9 )
i work with FW.
If you have any questions about special functions, maybe i can help.
User avatar
ukoenig
 
Posts: 4043
Joined: Wed Dec 19, 2007 6:40 pm
Location: Germany

Re: REad From INI FILE

Postby Eoeo » Sat Aug 25, 2012 11:47 pm

Uwe,

FOR I := 1 TO 5

I not Know How many Entry there are into section [Cars]

and I wish read the file ini to create an array with two fields one with the name of entry and one with the string of that entry
User avatar
Eoeo
 
Posts: 222
Joined: Mon Jun 04, 2012 12:00 pm

Re: REad From INI FILE

Postby ADutheil » Sun Aug 26, 2012 3:40 am

Try something like this:
Code: Select all  Expand view
PROCEDURE Main
    xBrowse( ReadIni( "Cars" ) )
RETURN


FUNCTION ReadIni( cSection )
LOCAL aEntry := {}
LOCAL oFile := TFileRead():New( "yourfile.ini" )
LOCAL lRead := .F.
LOCAL cTemp


WHILE oFile:MoreToRead()
    cTemp := allTrim( oFile:ReadLine() )
    IF cTemp == "[" + cSection + "]"
        lRead := .T.
    ELSE
        IF subStr( cTemp, 1, 1 ) == "["
            .F.
        ENDIF
    ENDIF
    IF lRead
        aAdd( aEntry, { subStr( cTemp, 1, at( "=", cTemp ) - 1 ), subStr( cTemp, at( "=", cTemp ) + 1 )  } )
    ENDIF
ENDDO
RETURN aEntry
 
Regards,

André Dutheil
FWH 13.04 + HB 3.2 + MSVS 10
ADutheil
 
Posts: 368
Joined: Sun May 31, 2009 6:25 pm
Location: Salvador - Bahia - Brazil

Re: REad From INI FILE

Postby Eoeo » Sun Aug 26, 2012 12:54 pm

ADutheil wrote:Try something like this:
Code: Select all  Expand view
PROCEDURE Main
    xBrowse( ReadIni( "Cars" ) )
RETURN


FUNCTION ReadIni( cSection )
LOCAL aEntry := {}
LOCAL oFile := TFileRead():New( "yourfile.ini" )
LOCAL lRead := .F.
LOCAL cTemp


WHILE oFile:MoreToRead()
    cTemp := allTrim( oFile:ReadLine() )
    IF cTemp == "[" + cSection + "]"
        lRead := .T.
    ELSE
        IF subStr( cTemp, 1, 1 ) == "["
            .F.
        ENDIF
    ENDIF
    IF lRead
        aAdd( aEntry, { subStr( cTemp, 1, at( "=", cTemp ) - 1 ), subStr( cTemp, at( "=", cTemp ) + 1 )  } )
    ENDIF
ENDDO
RETURN aEntry
 



where is TFileRead ? ( I found on this forum )
then I tried bt it not run ok


I changed with

aFilters:=ReadIni( "Filters",cInifile)



@ 30, 10 XBROWSE oLbx OF oDlg ;
SIZE 100,80 PIXEL ;
ARRAY aFilters AUTOCOLS




FUNCTION ReadIni( cSection,cInifile )
LOCAL aEntry := {}
LOCAL oFile := TFileRead():New( cInifile )
LOCAL lRead := .F.
LOCAL cTemp


WHILE oFile:MoreToRead()
cTemp := allTrim( oFile:ReadLine() )
IF cTemp == "[" + cSection + "]"
lRead := .T.
ELSE
IF subStr( cTemp, 1, 1 ) == "["
lRead := .F.
ENDIF
ENDIF
IF lRead

aAdd( aEntry, { subStr( cTemp, 1, at( "=", cTemp ) - 1 ), subStr( cTemp, at( "=", cTemp ) + 1 ) } )
ENDIF
ENDDO
RETURN aEntry
User avatar
Eoeo
 
Posts: 222
Joined: Mon Jun 04, 2012 12:00 pm

Re: REad From INI FILE

Postby ukoenig » Sun Aug 26, 2012 6:29 pm

A complete new Sample :

1. creating a Array with xBrowse
2. saving / restore from INI-file
3. adding / delete a Array from xBrowse
4. adding a Array on xbrowse Eof or by Button
5. save the xBrowse-array to a INI-file

A real xBrowse-array saved and created from INI-file :

//AADD( aData, { "Toyota", "Value 1" } )
//AADD( aData, { "Volvo", "Value 2" } )
//AADD( aData, { "Opel", "Value 3" } )
//AADD( aData, { "Ford", "Value 4" } )
//AADD( aData, { "Peugot", "Value 5" } )
//AADD( aData, { "Mercedes", "Value 6" } )
//AADD( aData, { "Nissan", "Value 7" } )

because of the unknown Array, I use a Counter for the Identification
saving a xBrowse-array, a new INI-file is created

[Cars]
cField_1=Toyota,Value 1
cField_2=Nissan,Value 2
cField_3=Opel,Value 3
cField_4=Ford,Value 4
cField_5=Mercedes,Value 5

Download ( Exe and Prg ) :
http://www.pflegeplus.com/fw_downloads/xbrarray1.zip

Image

Best Regards
Uwe :lol:
Since 1995 ( the first release of FW 1.9 )
i work with FW.
If you have any questions about special functions, maybe i can help.
User avatar
ukoenig
 
Posts: 4043
Joined: Wed Dec 19, 2007 6:40 pm
Location: Germany


Return to FiveWin for Harbour/xHarbour

Who is online

Users browsing this forum: No registered users and 30 guests