Just starting out? Need help? Post your questions and find answers here.
Pureabc
User
Posts: 76 Joined: Mon Jan 16, 2006 1:11 am
Post
by Pureabc » Mon Feb 17, 2014 12:33 am
I am working on a database with Big5 (Traditional Chinese) character set; I would like to display those as UTF.
After several attempts, I came across with the libconv library.
ftp://ftp.zlatkovic.com/libxml/iconv-1.9.2.win32.zip
[Edit] I just found this FREE Basic header, which seems closer to purebasic.
Can someone help convert the header file:
Code: Select all
#DEFINE __need_size_t
#INCLUDE ONCE "crt/stddef.bi"
TYPE As ANY PTR iconv_t
EXTERN iconv_open As FUNCTION CDECL(BYVAL As CONST ZSTRING PTR, BYVAL As CONST ZSTRING PTR) As iconv_t
EXTERN iconv As FUNCTION CDECL(BYVAL As iconv_t, BYVAL As ZSTRING PTR PTR, BYVAL As size_t PTR, BYVAL As ZSTRING PTR PTR, BYVAL As size_t PTR) As size_t
EXTERN iconv_close As FUNCTION CDECL(BYVAL As iconv_t) As INTEGER
#ENDIF
Thank you very much.
Last edited by
Pureabc on Fri Feb 21, 2014 11:21 pm, edited 1 time in total.
idle
Always Here
Posts: 5915 Joined: Fri Sep 21, 2007 5:52 am
Location: New Zealand
Post
by idle » Mon Feb 17, 2014 5:10 am
UTF8 or UTF16?
PB does UTF8
Code: Select all
Global purebasic.s
Global *stringmem = ?textc
Global len = ?textd - ?textc
purebasic.s = PeekS(*stringmem,len,#PB_UTF8)
MessageRequester("purebasic",purebasic)
DataSection
textc:
Data.a 231,186,175,229,159,186,231,161,128 ;pure basic in chinese google translation
textd:
EndDataSection
Windows 11, Manjaro, Raspberry Pi OS
Pureabc
User
Posts: 76 Joined: Mon Jan 16, 2006 1:11 am
Post
by Pureabc » Mon Feb 17, 2014 6:28 am
UTF8 will work just fine.
Can someone help convert the header?
Thanks.
Pureabc
User
Posts: 76 Joined: Mon Jan 16, 2006 1:11 am
Post
by Pureabc » Fri Feb 21, 2014 11:22 pm
I just found the FreeBasic header, not sure how to handle the PTR PTR datatype.
Can someone help on this?
Thank you.
idle
Always Here
Posts: 5915 Joined: Fri Sep 21, 2007 5:52 am
Location: New Zealand
Post
by idle » Sat Feb 22, 2014 12:13 am
this may work
Code: Select all
#ICONV_TRIVIALP = 0 ;/* INT *argument */
#ICONV_GET_TRANSLITERATE = 1 ; /* INT *argument */
#ICONV_SET_TRANSLITERATE = 2 ; /* const INT *argument */
#ICONV_GET_DISCARD_ILSEQ = 3; /* INT *argument */
#ICONV_SET_DISCARD_ILSEQ =4;
ImportC "iconv.lib"
iconv_open (tocode.s,fromcode.s);
iconv (cd.i,*inbuf,*inbytesleft.Integer,*outbuf,*outbytesleft.Integer);
iconv_close(cd.i);
EndImport
Windows 11, Manjaro, Raspberry Pi OS
Zebuddi123
Enthusiast
Posts: 796 Joined: Wed Feb 01, 2012 3:30 pm
Location: Nottinghamshire UK
Contact:
Post
by Zebuddi123 » Sat Feb 22, 2014 1:18 am
not sure how to handle the PTR PTR datatype.
pointer to another pointer (Hungarian notation i believe)
https://en.wikibooks.org/wiki/Windows_P ... Data_Types
GoTo menu right hand side click print then create a collection. then you can select any of the pages you want and make yourself a little pdf about win data types
Zebuddi.
malleo, caput, bang. Ego, comprehendunt in tempore
Pureabc
User
Posts: 76 Joined: Mon Jan 16, 2006 1:11 am
Post
by Pureabc » Sat Feb 22, 2014 11:36 pm
I tried the following code, but am getting memory read errors.
Can someone help fixing this?
Code: Select all
#libiconv_TRIVIALP = 0 ;/* INT *argument */
#libiconv_GET_TRANSLITERATE = 1 ; /* INT *argument */
#libiconv_SET_TRANSLITERATE = 2 ; /* const INT *argument */
#libiconv_GET_DISCARD_ILSEQ = 3; /* INT *argument */
#libiconv_SET_DISCARD_ILSEQ =4;
ImportC "iconv.lib"
libiconv_open (tocode.s,fromcode.s);
libiconv (cd.i,*inbuf,*inbytesleft.Integer,*outbuf,*outbytesleft.Integer);
libiconv_close(cd.i);
EndImport
cd.i = libiconv_open("UTF-8","BIG5")
Tstr.s = "¸ê®Æ";
in_len.i = Len(Tstr)
outstr.s=" "
out_len.i = Len(outstr)
libiconv(cd,@Tstr,in_len,@outstr,out_len);
libiconv_close(cd);
Debug "converted :"+ outstr
End
Thank you.
Pureabc
User
Posts: 76 Joined: Mon Jan 16, 2006 1:11 am
Post
by Pureabc » Sun Feb 23, 2014 10:09 pm
I am stuck on this PTR PTR data type.
Where and when is the string stored with this?
Pure Basic does not support **inbuf.string, how do I get around this?
ts-soft
Always Here
Posts: 5756 Joined: Thu Jun 24, 2004 2:44 pm
Location: Berlin - Germany
Post
by ts-soft » Sun Feb 23, 2014 10:18 pm
not tested but:
Code: Select all
outstr.s=" "
*poutstr = @outstr
*ppoutstr = @*poutstr
example:
Code: Select all
bla.s = "bla"
*pbla = @bla
Debug *pbla
*ppbla = @*pbla
Debug *ppbla
Debug PeekS(PeekI(*ppbla))
PureBasic 5.73 |
SpiderBasic 2.30 |
Windows 10 Pro (x64) |
Linux Mint 20.1 (x64)
Old bugs good, new bugs bad! Updates are evil: might fix old bugs and introduce no new ones.
Demivec
Addict
Posts: 4270 Joined: Mon Jul 25, 2005 3:51 pm
Location: Utah, USA
Post
by Demivec » Mon Feb 24, 2014 4:05 am
ts-soft wrote: not tested but:
Code: Select all
outstr.s=" "
*poutstr = @outstr
*ppoutstr = @*poutstr
example:
Code: Select all
bla.s = "bla"
*pbla = @bla
Debug *pbla
*ppbla = @*pbla
Debug *ppbla
Debug PeekS(PeekI(*ppbla))
As another alternative to ts-soft's example you can also redefine the code slightly and make it a structured String pointer:
Code: Select all
outstr.s=" "
*poutstr = @outstr
*ppoutstr.String = @*poutstr
example:
Code: Select all
bla.s = "bla"
*pbla = @bla
Debug *pbla
*ppbla.String = @*pbla
Debug *ppbla
Debug *ppbla\s
ts-soft
Always Here
Posts: 5756 Joined: Thu Jun 24, 2004 2:44 pm
Location: Berlin - Germany
Post
by ts-soft » Mon Feb 24, 2014 7:50 am
PureBasic 5.73 |
SpiderBasic 2.30 |
Windows 10 Pro (x64) |
Linux Mint 20.1 (x64)
Old bugs good, new bugs bad! Updates are evil: might fix old bugs and introduce no new ones.
Pureabc
User
Posts: 76 Joined: Mon Jan 16, 2006 1:11 am
Post
by Pureabc » Mon Feb 24, 2014 7:52 am
Ts_soft,
Your explanation and example make sense.
I will work on this more.
Thank you.
infratec
Always Here
Posts: 7620 Joined: Sun Sep 07, 2008 12:45 pm
Location: Germany
Post
by infratec » Mon Feb 24, 2014 2:29 pm
Hi,
try this:
Code: Select all
EnableExplicit
ImportC "iconv.lib"
libiconv_open(tocode.s, fromcode.s)
libiconv(cd.i, *inbuf, *inbytesleft, *outbuf, *outbytesleft)
libiconv_close(cd.i)
EndImport
Define.i cd, in_len, out_len
Define T$, Out$
Define *TPtr, *OutPtr, *HelpPtr
cd = libiconv_open("UTF-8","BIG5")
If cd > -1
T$ = "¸ê®Æ";
in_len = Len(T$) ; is decreased by iconv
*TPtr = @T$
Out$ = Space(50)
out_len = Len(Out$) ; is decreased by iconv
*HelpPtr = @Out$ ; because address is increased by iconv !!!
*OutPtr = *HelpPtr
Debug T$
Debug "bytes to convert: " + Str(in_len)
If libiconv(cd, @*TPtr, @in_len, @*OutPtr, @out_len) > -1
Debug "converted bytes: " + Str(50 - out_len)
Debug "converted :"+ Out$
EndIf
libiconv_close(cd)
EndIf
But since I don't know what's the right answer ...
Bernd
idle
Always Here
Posts: 5915 Joined: Fri Sep 21, 2007 5:52 am
Location: New Zealand
Post
by idle » Mon Feb 24, 2014 8:10 pm
This works as expected
Code: Select all
#libiconv_TRIVIALP = 0 ;/* INT *argument */
#libiconv_GET_TRANSLITERATE = 1 ; /* INT *argument */
#libiconv_SET_TRANSLITERATE = 2 ; /* const INT *argument */
#libiconv_GET_DISCARD_ILSEQ = 3; /* INT *argument */
#libiconv_SET_DISCARD_ILSEQ =4;
Structure char
a.a[0]
EndStructure
ImportC "iconv.lib"
libiconv_open (tocode.s,fromcode.s);
libiconv (cd.i,*inbuf,*inbytesleft.Integer,*outbuf,*outbytesleft.Integer);
libiconv_close(cd.i);
EndImport
cd.i = libiconv_open("utf-16be","ASCII")
Debug cd
Global in.s = "ABC"
Global in_len
in_len = 3
Global OutSize = 10
Global *out.char = AllocateMemory(OutSize)
Global out_len=OutSize
Global *inbuf = @in ;address of a input string
Global *outbuf.char = *out ;is a pointer allready
result = libiconv(cd,@*inbuf,@in_len,@*outbuf,@out_len);
Debug result
Debug in_len
Debug out_len
libiconv_close(cd);
Global output.s
For a = 0 To OutSize-Out_len
output.s + Hex(*out\a[a],#PB_Byte) + " "
Next
Debug "converted :" + output
Debug "matches : 0 41 0 42 0 43 0"
End
Windows 11, Manjaro, Raspberry Pi OS
infratec
Always Here
Posts: 7620 Joined: Sun Sep 07, 2008 12:45 pm
Location: Germany
Post
by infratec » Mon Feb 24, 2014 8:30 pm
Btw.:
The output buffer should be 4 * len of input.
Because the largest coding is 4 byte instead of 1
according to the .h file:
out_len is the remaining size of the buffer, since it is decreased by libiconv
So the real out_len is size of out buffer - out_len.
And it is also written that the addresspointers are increased by libiconv.
So if you use later AllocateMemory(), you also have to work with help ptr,
else you loose your start address and the pointer for FreeMemory()
Bernd