Reading .ini Files

General Help regarding HMG, Compilation, Linking, Samples

Moderator: Rathinagiri

melliott42
Posts: 119
Joined: Wed Feb 18, 2009 2:14 pm

Reading .ini Files

Post by melliott42 »

Hello,

I need to work with a .ini file in HMG. For example, how would I read the value of "driver" under the [connection] section in the below .ini file?

Code: Select all

[connection]
driver=mydriver

Thanks,

Michael
User avatar
Rathinagiri
Posts: 5480
Joined: Tue Jul 29, 2008 6:30 pm
DBs Used: MariaDB, SQLite, SQLCipher and MySQL
Location: Sivakasi, India
Contact:

Re: Reading .ini Files

Post by Rathinagiri »

We can use like this...

Code: Select all

   cDriver := ""
   BEGIN INI File "driver.ini"
      get cDriver section "connection" entry "driver"
   END ini
That's all.

Same way for writing...

Code: Select all

   cDriver := "This is my driver"
   BEGIN INI File "driver.ini"
      set section "connection" entry "driver" to cDriver
   END ini
East or West HMG is the Best.
South or North HMG is worth.
...the possibilities are endless.
User avatar
esgici
Posts: 4543
Joined: Wed Jul 30, 2008 9:17 pm
DBs Used: DBF
Location: iskenderun / Turkiye
Contact:

Re: Reading .ini Files

Post by esgici »

Hi Rathi

I can't found any explanation about this BEGIN INI / END INI structure in our documentation :(

Regarding my notes it's like this :

Code: Select all

BEGIN INI [ FILENAME | FILE | DISK <cIniFile> ] 

   GET <uVar> 
       [ SECTION <cSection> ] 
       [ ENTRY <cEntry> ] 
       [ DEFAULT <uDefault> ] 

   SET [ SECTION <cSection> ] 
       [ ENTRY <cEntry> ] 
       [ TO <uVal> ] 

   DEL SECTION <cSection> 
       [ ENTRY <cEntry> ]

END INI
Does I'm missed it or it's really missing or it's a Harbour implementation, not HMG ?

Thanks in advance.

--

Esgici
Viva INTERNATIONAL HMG :D
User avatar
sudip
Posts: 1456
Joined: Sat Mar 07, 2009 11:52 am
Location: Kolkata, WB, India

Re: Reading .ini Files

Post by sudip »

Hello,

I used following coding in one of my projects. Here formula_* are public memory variables. Also ini files are different for different financial year (_accyrid).

Code: Select all

//------------------- SaveIni   
function SaveIni()
   local mInifile := alltrim(_accyrid)+".ini"
	BEGIN INI FILE (mInifile)
		SET SECTION "Formula"   ENTRY "EPF"             To formula_epf
		SET SECTION "Formula"   ENTRY "PF_C"            To formula_pf_c
		SET SECTION "Formula"   ENTRY "PF_E"            To formula_pf_e
		SET SECTION "Formula"   ENTRY "ESI_C"           To formula_esi_c
		SET SECTION "Formula"   ENTRY "ESI_E"           To formula_esi_e
   END INI
   return nil				
   

//------------------- ReadIni
function ReadIni()
   local mInifile := alltrim(_accyrid)+".ini"

	If ! File(mInifile)
	  SaveIni()
	EndIf
	
	BEGIN INI FILE (mInifile)
		GET formula_epf SECTION "Formula" ENTRY "EPF"
		GET formula_pf_c SECTION "Formula" ENTRY "PF_C"
		GET formula_pf_e SECTION "Formula" ENTRY "PF_E"
		GET formula_esi_c SECTION "Formula" ENTRY "ESI_C"
		GET formula_esi_e SECTION "Formula" ENTRY "ESI_E"
	END INI
	
	return nil      	
With best regards,
Sudip
User avatar
Rathinagiri
Posts: 5480
Joined: Tue Jul 29, 2008 6:30 pm
DBs Used: MariaDB, SQLite, SQLCipher and MySQL
Location: Sivakasi, India
Contact:

Re: Reading .ini Files

Post by Rathinagiri »

Esgici,

Thanks a lot for giving the syntax. Roberto had re-organized Crypt, EditEx, INI and graph libraries. During that time, the doc might have been missed out.

Sudip,

Looks like payroll!?
East or West HMG is the Best.
South or North HMG is worth.
...the possibilities are endless.
melliott42
Posts: 119
Joined: Wed Feb 18, 2009 2:14 pm

Re: Reading .ini Files

Post by melliott42 »

Rathi, Esgici,

Thanks so much. That looks so nicely implemented! :-)

Michael
User avatar
sudip
Posts: 1456
Joined: Sat Mar 07, 2009 11:52 am
Location: Kolkata, WB, India

Re: Reading .ini Files

Post by sudip »

rathinagiri wrote:Sudip,

Looks like payroll!?
Yes :lol:
They are very useful for storing formulae for different financial years. :)
With best regards,
Sudip
melliott42
Posts: 119
Joined: Wed Feb 18, 2009 2:14 pm

Re: Reading .ini Files

Post by melliott42 »

I have found though that I MUST preinitialize my BEGIN INI variables before being used. This was very frustrating until I resolved it. Just declaring them was not sufficient.

Example

Code: Select all

   local cSmtp_Server, cSmtp_Port, cSubject, cEmail_From, cEmail_To, cBody, cFile, cTimeOut

   // Get\Set Connection Values
   cSmtp_Server := ""
   cSmtp_Port   := ""
   cSubject     := "=== Test Email ==="
   cEmail_From  := ""
   cEmail_To    := ""
   cBody        := "This is a test mail sent at: " + DtoC(date()) + " " + time()
   cFile        := ""
   cTimeOut     := ""

   BEGIN INI FILE (gcIniFile)
      GET cSmtp_Server  SECTION "email" ENTRY "smtp_server"
      GET cSmtp_Port    SECTION "email" ENTRY "smtp_port"
      GET cEmail_From   SECTION "email" ENTRY "email_from"
      GET cEmail_To     SECTION "email" ENTRY "email_to"
      GET cTimeOut      SECTION "email" ENTRY "timeout"
   END INI

User avatar
Rathinagiri
Posts: 5480
Joined: Tue Jul 29, 2008 6:30 pm
DBs Used: MariaDB, SQLite, SQLCipher and MySQL
Location: Sivakasi, India
Contact:

Re: Reading .ini Files

Post by Rathinagiri »

Yep. That's what I had given in my sample.
East or West HMG is the Best.
South or North HMG is worth.
...the possibilities are endless.
User avatar
Pablo César
Posts: 4059
Joined: Wed Sep 08, 2010 1:18 pm
Location: Curitiba - Brasil

Reading .ini Files

Post by Pablo César »

Now in HMG UNICODE version, recently I have found some troubles in INI files when is be written. I mean when I use "SET" with variables which contains UNICODE format. If I open INI file with Notepad++, I see that file was written in ANSI mode.

Dear Claudio, do you think this it would be an error or I shall convert all my INI_variables to ANSI mode ?

I have noted in C:\hmg.3.1.4\SOURCE\INI\c_ini.c and C:\hmg.3.1.4\SOURCE\INI\h_ini.prg those are using WritePrivaeProfileString and probably (please confirm) shall be used WritePrivateProfileStringW in place of that.

Or could be the problem in xChar Function at C:\hmg.3.1.4\SOURCE\INI\h_ini.prg, when need to convert UNICODE/ANSI ?
HMGing a better world
"Matter tells space how to curve, space tells matter how to move."
Albert Einstein
Post Reply