Reading .ini Files

General Help regarding HMG, Compilation, Linking, Samples

Moderator: Rathinagiri

User avatar
srvet_claudio
Posts: 2220
Joined: Thu Feb 25, 2010 8:43 pm
Location: Uruguay
Contact:

Re: Reading .ini Files

Post by srvet_claudio »

Pablo César wrote: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 ?
Pablo,
When you compile in Unicode the compiler automatically calls the function that supports Unicode (name ending in W).

I think the problem is here (h_ini.prg):

Code: Select all

    If ! File( cIniFile )
        hFile := FCreate( cIniFile )
    Else
        hFile := FOpen( cIniFile, FO_READ + FO_SHARED )
    EndIf
The functions FOpen, FCreate, Fxxx, etc do not support Unicode.
If the file was created using Unicode characters, WritePrivateProfileString() function writes Unicode characters to the file. Otherwise, WritePrivateProfileString() writes ANSI characters.
I will try to correct.

PS: Please, create an ini file in Unicode with Notepat++ and try write/read Unicode text to see if it works.
Best regards.
Dr. Claudio Soto
(from Uruguay)
http://srvet.blogspot.com
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 »

srvet_claudio wrote:When you compile in Unicode the compiler automatically calls the function that supports Unicode (name ending in W).
But in h_ini.prg is WritePrivaeProfileString (without w), so this doesn't matter ?
srvet_claudio wrote:I think the problem is here (h_ini.prg):

Code: Select all

    If ! File( cIniFile )
        hFile := FCreate( cIniFile )
    Else
        hFile := FOpen( cIniFile, FO_READ + FO_SHARED )
    EndIf
Yes, I also think so but probably there are others functions to be adapted.

I saw this article, probably will help you: http://www.codeproject.com/Articles/907 ... -INI-files
srvet_claudio wrote:Please, create an ini file in Unicode with Notepat++ and try write/read Unicode text to see if it works.
I've tested but not success when are strings with language accents in the ini file. When ini file is in ANSI format and there is not any string with accents like this: ñÑçÇãáÁ... then works ok.
HMGing a better world
"Matter tells space how to curve, space tells matter how to move."
Albert Einstein
User avatar
srvet_claudio
Posts: 2220
Joined: Thu Feb 25, 2010 8:43 pm
Location: Uruguay
Contact:

Re: Reading .ini Files

Post by srvet_claudio »

Pablo César wrote:But in h_ini.prg is WritePrivaeProfileString (without w), so this doesn't matter ?
No, because this is a high-level function defined in the file c_ini.c:

Code: Select all

HB_FUNC( WRITEPRIVATEPROFILESTRING )
{
   TCHAR * lpSection  = (TCHAR *) HMG_parc( 1 );
   TCHAR * lpEntry    = HB_ISCHAR(2) ? (TCHAR *) HMG_parc( 2 ) : NULL ;
   TCHAR * lpData     = HB_ISCHAR(3) ? (TCHAR *) HMG_parc( 3 ) : NULL ;
   TCHAR * lpFileName = (TCHAR *) HMG_parc( 4 );

   if ( WritePrivateProfileString( lpSection , lpEntry , lpData , lpFileName ) )
      hb_retl( TRUE ) ;
   else
      hb_retl(FALSE);
}
In C, WritePrivateProfileString is a macro defined as:

Code: Select all

#ifdef UNICODE
   #define WritePrivateProfileString WritePrivateProfileStringW
#else
   #define WritePrivateProfileString WritePrivateProfileStringA
#endif
Pablo César wrote:I saw this article, probably will help you: http://www.codeproject.com/Articles/907 ... -INI-files
Yes, it may be a good idea to force the file to be in Unicode format changing the header.
Best regards.
Dr. Claudio Soto
(from Uruguay)
http://srvet.blogspot.com
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 »

Thank you master by your clarifications !
srvet_claudio wrote:Yes, it may be a good idea to force the file to be in Unicode format
Yes, I think so... but I am still worry with other handling functions...
srvet_claudio wrote:changing the header.
By changing the header ? That's not what I understood from that article.

I do not even know how to sort it all out. Would you be willing to make such changes?
HMGing a better world
"Matter tells space how to curve, space tells matter how to move."
Albert Einstein
User avatar
srvet_claudio
Posts: 2220
Joined: Thu Feb 25, 2010 8:43 pm
Location: Uruguay
Contact:

Re: Reading .ini Files

Post by srvet_claudio »

Pablo César wrote:
srvet_claudio wrote:changing the header.
By changing the header ? That's not what I understood from that article.
Yes, this is what is done in the article, inserted at the beginning of the file the sequence of bytes: 0xFEFF
Pablo César wrote:I do not even know how to sort it all out. Would you be willing to make such changes?
Yes, I will try to fix it.
Best regards.
Dr. Claudio Soto
(from Uruguay)
http://srvet.blogspot.com
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 »

srvet_claudio wrote:in the article, inserted at the beginning of the file the sequence of bytes: 0xFEFF
Thank you, Dr. Soto ! I understand what was the trick that has been used.
srvet_claudio wrote:Yes, I will try to fix it.
When you have a favorable outcome, you could publish your changes ?

When this case is resolved, you believe that this issue of INI have to do with these cases to be solved ?
HMGing a better world
"Matter tells space how to curve, space tells matter how to move."
Albert Einstein
Carlos Britos
Posts: 245
Joined: Sat Aug 02, 2008 5:03 pm

Re: Reading .ini Files

Post by Carlos Britos »

srvet_claudio wrote: Yes, this is what is done in the article, inserted at the beginning of the file the sequence of bytes: 0xFEFF.
I´ve made a test with the below code and seems to be ok. Not big endian as with 0xFEFF showed in the article.
FWrite( handle, Chr( 0xEF ) + Chr( 0xBB ) + Chr( 0xBF ) ) // BOM 
Regards/Saludos, Carlos (bcd12a)
User avatar
srvet_claudio
Posts: 2220
Joined: Thu Feb 25, 2010 8:43 pm
Location: Uruguay
Contact:

Re: Reading .ini Files

Post by srvet_claudio »

Carlos Britos wrote:
srvet_claudio wrote: Yes, this is what is done in the article, inserted at the beginning of the file the sequence of bytes: 0xFEFF.
I´ve made a test with the below code and seems to be ok. Not big endian as with 0xFEFF showed in the article.
FWrite( handle, Chr( 0xEF ) + Chr( 0xBB ) + Chr( 0xBF ) ) // BOM 
Thanks Carlos.
Best regards.
Dr. Claudio Soto
(from Uruguay)
http://srvet.blogspot.com
User avatar
srvet_claudio
Posts: 2220
Joined: Thu Feb 25, 2010 8:43 pm
Location: Uruguay
Contact:

Re: Reading .ini Files

Post by srvet_claudio »

Pablo César wrote:Would you be willing to make such changes?
Please test this code.
1) replace the files of folder C:\hmg.3.1.4\SOURCE\INI whit this files.
2) and re-build INI.lib

This code only work with INI files created with this code,
previous INI files must be converted first to UTF-16 Little Endian (Notepat++ --> Convert to UCS-2 Little Endian)

I used this format so the article says:
We can use only UTF16-little endian of these formats as an INI file format. The other encodings do not work correctly. Probably, the reason is that Windows NT, 2000 or XP uses the encoding internally. This is why Windows particularly names UTF16-little Endian "Unicode".
Attachments
INI.rar
(3.85 KiB) Downloaded 263 times
Best regards.
Dr. Claudio Soto
(from Uruguay)
http://srvet.blogspot.com
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 »

Downloaded + Unpacked + Copied files in INI Source folder + Lib Re-Compiled + Application Recompiled and works perfectly !

Ini file was created in UCS-2 Little Endian format but first line was created in blank. Probably in h_ini.prg must need to correct file pointer when is using FWrite function.

Other than that, no problems occurred. Everything is working with accented characters at INI file and properlly displaying in the application too.

Thank you Dr. Soto !
HMGing a better world
"Matter tells space how to curve, space tells matter how to move."
Albert Einstein
Post Reply