HMG 3.4.3

HMG Unicode versions 3.1.x related

Moderator: Rathinagiri

User avatar
Pablo César
Posts: 4059
Joined: Wed Sep 08, 2010 1:18 pm
Location: Curitiba - Brasil

HMG 3.4.3

Post by Pablo César »

Rathinagiri wrote:IMHO, it may be because of the older version of Harbour that HMG uses.

If possible, please try to rebuild HMG with the latest version of Harbour and whether we have the same problem.
I don't think so... :?

In the fact there is a problem to read Chinese and Japanese characters from INI files and seems a bug in the HMG C function (this is a common demand).

To write in UTF16LE_BOM looks OK. You can check by visualize INI file in Notepad++ when was created.

The imediate return of GetPrivateProfileString of the string does not come completely (characters are missing) as Mr. Hui Yi said.

I tried to replace GetPrivateProfileString in C function to GetPrivateProfileStringW but doesn't work it... :(

We also need to remember that an error was reported when INI file is being created: viewtopic.php?p=46479#p46479. But this I already found the solution and pass onto Claudio. Probably in next HMG relase he's gonna be included. Anyway, In my HMG is already fixed and the problem now is not the INI (UNICODE) creation. The problem is in READ process.

Some other C programmer advice to use GetPrivateProfileStringA and then decode with MultiByteToWideChar. But this will requires the CodePage info... :? (sounds It's like going back in the past).

Probably the C programmers gurus can give us a help in this...

I can not ask for anything more for Claudio, he is saturated with many things to do ... :oops:

Sorry, I can not help it because of lack of C knowledge ... :|
HMGing a better world
"Matter tells space how to curve, space tells matter how to move."
Albert Einstein
User avatar
kcarmody
Posts: 152
Joined: Tue Oct 07, 2014 11:13 am
Contact:

Re: HMG 3.4.3

Post by kcarmody »

huiyi_ch wrote:I found that when hmg3.4.3 (Unicode) in reading the INI file, if the INI file exists in Chinese characters, then read out the text may not be correct!
For example, compile and run the following program, in the INI file is "我非常喜欢HMG!" and read out the string is "我非常".
Harbour has done another dirty trick to you. If a block of INI FILE .. SET statements contains Unicode strings, the INI file is written in UTF-16 format. But INI FILE .. GET statements cannot read a UTF-16 file, or a UTF-8 file, only an ANSI file.

HMG uses UTF-8 by default. For common string functions, this works OK. But for less used functions, some things do not yet work properly. As you have discovered, this includes INI FILE and APPEND FROM DELMITED.

So to do INI files with Unicode, you must write your own handler, using common functions like MEMOREAD, HB_MEMOWRIT (don't use MEMOWRIT), and HB_ATOKENS. I've written some INI file functions in the attached program, which works as expected. You can use them or modify them for your needs.

Code: Select all

#include <hmg.ch>

FUNCTION main()

LOCAL cIniFile := GetStartupFolder() + '\AltDemo.ini'
LOCAL aRet

SetIniValue(cIniFile)
aRet := GetValue(cIniFile)
AEVAL( aRet, { |x| MsgInfo( x ) } )

RETURN NIL

PROCEDURE SetIniValue( cIni )

LOCAL cMemo := '', xLine, cKey, xValue
LOCAL aLines := { ;
  'HMG', ;
  {'DESCRIBE_1', "我非常喜欢HMG!"} , ;
  {'DESCRIBE_2', "I LIKE HMG VERY MUCH!"}, ;
  {'Vers', 3.43 } }

FOR EACH xLine IN aLines
  DO CASE
  CASE HB_ISCHAR(xLine)
    cMemo += '[' + xLine + ']' + CRLF
  CASE HB_ISARRAY(xLine) .AND. LEN(xLine) == 2
    cKey   := xLine[1]
    xValue := xLine[2]
    DO CASE
    CASE HB_ISCHAR(xValue)
      cMemo += xLine[1] + '=' + xLine[2] + CRLF
    CASE HB_ISNUMERIC(xValue)
      cMemo += xLine[1] + '=' + LTRIM(STR(xLine[2])) + CRLF
    END
  END
NEXT
HB_MEMOWRIT(cIni, cMemo)

RETURN

FUNCTION GetValue( cIni )

LOCAL aLines := HB_ATOKENS(MEMOREAD(cIni), CRLF)
LOCAL cDescrib_1 := "", cDescrib_2 := "", nVers := 0
LOCAL cLine

FOR EACH cLine IN aLines
  cLine := ALLTRIM(cLine)
  IF LEFT(cLine, 1) == '[' .AND. RIGHT(cLine, 1) == ']'
    cSection := SUBSTR(cLine, 2, LEN(cLine)-2)
  ELSEIF '=' $ cLine
    aFields := HB_ATOKENS(cLine, '=')
    IF LEN(aFields) >= 2
      cEntry := ALLTRIM(UPPER(aFields[1]))
      cValue := ALLTRIM(aFields[2])
      DO CASE
      CASE cSection == 'HMG' .AND. cEntry == 'DESCRIBE_1' 
        cDescrib_1 := cValue
      CASE cSection == 'HMG' .AND. cEntry == 'DESCRIBE_2'
        cDescrib_2 := cValue
      CASE cSection == 'HMG' .AND. cEntry == 'VERS'
        nVers := VAL(cValue)
      END
    END
  END
NEXT

RETURN {cDescrib_1, cDescrib_2, nVers}
Attachments
AltIniTest.zip
(1.48 MiB) Downloaded 334 times
Last edited by kcarmody on Wed Nov 23, 2016 11:44 am, edited 1 time in total.
User avatar
Pablo César
Posts: 4059
Joined: Wed Sep 08, 2010 1:18 pm
Location: Curitiba - Brasil

HMG 3.4.3

Post by Pablo César »

WOW !

Thank you Kevin, for explaination and solution.
HMGing a better world
"Matter tells space how to curve, space tells matter how to move."
Albert Einstein
User avatar
Pablo César
Posts: 4059
Joined: Wed Sep 08, 2010 1:18 pm
Location: Curitiba - Brasil

HMG 3.4.3

Post by Pablo César »

I am still thinking that Harbour is not the blame of this strange behaviour...

.INI files are very old stuff. They were existing decades before the Unicode was introduced.

It's simple ASCII files but how be conciliated ANSI and UNICODE in the same system?

By another side, I read that UTF16-big Endian) is indicated to use at an INI file format. The other encodings do not work correctly... :?

See: http://www.codeproject.com/Articles/907 ... -INI-files
HMGing a better world
"Matter tells space how to curve, space tells matter how to move."
Albert Einstein
User avatar
srvet_claudio
Posts: 2193
Joined: Thu Feb 25, 2010 8:43 pm
Location: Uruguay
Contact:

Re: HMG 3.4.3

Post by srvet_claudio »

huiyi_ch wrote:我发现当HMG3.4.3(UNICODE )在读取INI文件时,如果INI 文件中存在汉字,则读取出来的文本可能不正确!
比如,编译运行下面程序,在INI文件中是“我非常喜欢HMG!”,而读取出的字符串是“我非常”

I found that when hmg3.4.3 (Unicode) in reading the INI file, if the INI file exists in Chinese characters, then read out the text may not be correct!
For example, compile and run the following program, in the INI file is "我非常喜欢HMG!" and read out the string is "我非常".
My englist is very poor,so please forgive me.

Code: Select all

#include <hmg.ch>
FUNCTION main()
LOCAL cIniFile := GetStartupFolder() + '\demo.ini'
LOCAL aRet

SetIniValue(cIniFile)
aRet := GetValue(cIniFile)
AEVAL( aRet, { |x| MsgInfo( x ) } )
RETURN NIL


PROCEDURE SetIniValue( cIni )

BEGIN INI FILE cIni
  SET SECTION 'HMG' ENTRY 'Vers' TO 3.43
  SET SECTION 'HMG' ENTRY 'DESCRIBE_1' TO "我非常喜欢HMG!"
  SET SECTION 'HMG' ENTRY 'DESCRIBE_2' TO "I LIKE HMG VERY MUCH!"
END INI

RETURN


FUNCTION GetValue( cIni )
LOCAL cDescrib_1:="", cDescrib_2:="",nVers:=0

BEGIN INI FILE (cIni)
  GET cDescrib_1 SECTION 'HMG' ENTRY 'DESCRIBE_1' DEFAULT ''
  GET cDescrib_2 SECTION 'HMG' ENTRY 'DESCRIBE_2' DEFAULT ''
  GET nVers SECTION 'HMG' ENTRY 'Vers' DEFAULT 3.43
END INI

RETURN {cDescrib_1,cDescrib_2, nVers}
test_ini.rar
The problem is in the API function GetPrivateProfileString()
Add to your code and see:

Code: Select all

msgdebug( HMG_GetPrivateProfileSection ( cIniFile, "HMG" ) )
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

HMG 3.4.3

Post by Pablo César »

srvet_claudio wrote:The problem is in the API function GetPrivateProfileString()
Add to your code and see:

Code: Select all

msgdebug( HMG_GetPrivateProfileSection ( cIniFile, "HMG" ) )
So, in this case Claudio, we should replace with our own _GetIni function ?
HMGing a better world
"Matter tells space how to curve, space tells matter how to move."
Albert Einstein
User avatar
srvet_claudio
Posts: 2193
Joined: Thu Feb 25, 2010 8:43 pm
Location: Uruguay
Contact:

Re: HMG 3.4.3

Post by srvet_claudio »

Pablo César wrote:
srvet_claudio wrote:The problem is in the API function GetPrivateProfileString()
Add to your code and see:

Code: Select all

msgdebug( HMG_GetPrivateProfileSection ( cIniFile, "HMG" ) )
So, in this case Claudio, we should replace with our own _GetIni function ?
Yes
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

HMG 3.4.3

Post by Pablo César »

srvet_claudio wrote:
Pablo César wrote:
srvet_claudio wrote:The problem is in the API function GetPrivateProfileString()
Add to your code and see:

Code: Select all

msgdebug( HMG_GetPrivateProfileSection ( cIniFile, "HMG" ) )
So, in this case Claudio, we should replace with our own _GetIni function ?
Yes
So, let me be the first to show you my show my corrections:
hmg.3.4.3.rar
Source file h_ini.prg with full path only
(2.49 KiB) Downloaded 281 times
All corrections were described in each place as occurred. And if you Claudio, find it valid to be replaced with the official HMG file, please do not hesitate to do so.

It's great to feel useful for HMG improvements. :)

At Mr. Hui Yi,

If you do not want to wait for next HMG be released and use this file source as PATCH of actual version: please proceed to unpack this file overwriting existing h_ini.prg file at your HMG_Root_Folder\SOURCE\Ini subfolder. Then at this same subfolder execute buildlib.bat if you are working in 32bits of HMG or execute buildlib64.bat if your HMG is 64bits.

I have included your code example but with some extra for different types SET/GET at INI file:
test_ini.rar
huiyi_ch Test pack (Source and executable files)
(1.24 MiB) Downloaded 338 times
Any bug, please be kind to be reported here.

This procedures was not tested in ANSI mode. If someone is available to test it, please let us know if is OK.
My concern is that I have used unicode Harbour functions and not sure if is workable for ANSI too functions like hb_USubStr(), hb_UAt() and familiy... :?

Rgds to all
HMGing a better world
"Matter tells space how to curve, space tells matter how to move."
Albert Einstein
huiyi_ch
Posts: 172
Joined: Sat May 21, 2016 5:27 am

Re: HMG 3.4.3

Post by huiyi_ch »

经过初步测试,程序(unicode 和ANSI 模式)对于INI文件中的中文字符,存、取一切正常,问题得到完美解决。在这里,我非常感谢Dr. Claudio Soto和Pablo César 二位的辛勤付出。现在HMG对于中文的兼容性更好了。我顺便说一下,以前反映的关于函数getstatupfolder()存在的问题viewtopic.php?f=43&t=4738&start=80,请再关注一下!如能在下一个版本解决了问题,那就太好了!

After a preliminary test, the program(unicode and ANSI mode) for the INI file in Chinese characters, set and get all normal, the problem have been solved perfectly. Here, I am very grateful with the untiring efforts of Dr. Claudio Soto and Pablo César . Now,HMG is better for the Chinese compatibility. I by the way, I reflected on the question of the function getstatupfolder() [url] viewtopic.php?f=43&t=4738&start=80 [/ url], please pay attention a bit! If the next version the question will be solved , it would be great.
User avatar
Pablo César
Posts: 4059
Joined: Wed Sep 08, 2010 1:18 pm
Location: Curitiba - Brasil

HMG 3.4.3

Post by Pablo César »

huiyi_ch wrote:After a preliminary test, the program(unicode and ANSI mode) for the INI file in Chinese characters, set and get all normal, the problem have been solved perfectly.
Thank you Mr. Yi for reporting your results and happy to know is satisfactory. The problem was not only for chinese characters, there are other languages with special characters that it could have been affected at time ago... now is solved, I guess.
huiyi_ch wrote:...question of the function getstatupfolder() viewtopic.php?f=43&t=4738&start=80, please pay attention a bit! If the next version the question will be solved , it would be great.
Now I am understanding what you was saying... You wrote chinese language mixed with codes and is very often to ignore other languages when we do not understanding... (sorry but it's the true, I often see this in Spanish section). Also your codes are not exemplified very well and you not so clear in your objective. Because we do not know what you presented it is your problem or if you are only presenting your opinion...

That is why when we do not master the English language, it is good to post in both languages (as you did in your last message posted).
Thus the user can translate himself from the native language at any other conciliating language (for example). Because sometimes google translator makes translation errors.

Regarding your suggestion (how to remove last unnecessary backslash), I do not seem to be the ideal solution. Since HMG works with UNICODE as default and these functions you have indicated are not for handling strings in UNICODE mode. And certainly folder names sometimes contain local language characters, so it should be handled in UNICODE mode not in ANSI.

My suggestion is to replace the GetStartUpFolder() function with the proper Harbour function use, like this:

Code: Select all

Function GetStartUpFolder()         // by Pablo on November, 2016 - To remove last unnecessary backslash
Return cFilePath(hb_ProgName())
Claudio, could this solution be adopted in HMG core ? Or replace GetStartUpFolder() function (eliminating at h_controlmisc.prg) with:

#xtranslate GetStartUpFolder() =>cFilePath(hb_ProgName())

At ch file, as you prefer. :)

Remarks: Must be tested in ANSI mode to be sure all working properly for both modes. Please Mr. Yi: test it and revert to us.
HMGing a better world
"Matter tells space how to curve, space tells matter how to move."
Albert Einstein
Post Reply