Page 1 of 2

Reading .ini Files

Posted: Thu Jul 22, 2010 7:15 pm
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

Re: Reading .ini Files

Posted: Thu Jul 22, 2010 7:56 pm
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

Re: Reading .ini Files

Posted: Thu Jul 22, 2010 9:46 pm
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

Re: Reading .ini Files

Posted: Fri Jul 23, 2010 4:20 am
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      	

Re: Reading .ini Files

Posted: Fri Jul 23, 2010 4:43 am
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!?

Re: Reading .ini Files

Posted: Fri Jul 23, 2010 10:56 am
by melliott42
Rathi, Esgici,

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

Michael

Re: Reading .ini Files

Posted: Sat Jul 24, 2010 4:25 pm
by sudip
rathinagiri wrote:Sudip,

Looks like payroll!?
Yes :lol:
They are very useful for storing formulae for different financial years. :)

Re: Reading .ini Files

Posted: Mon Jul 26, 2010 3:58 pm
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


Re: Reading .ini Files

Posted: Mon Jul 26, 2010 4:03 pm
by Rathinagiri
Yep. That's what I had given in my sample.

Reading .ini Files

Posted: Fri Jul 26, 2013 2:01 am
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 ?