Page 1 of 2

Help Convert FreeBasic header for Iconv.dll/iconv.lib

Posted: Mon Feb 17, 2014 12:33 am
by Pureabc
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.

Re: Help Convert C++ header for Iconv.dll/iconv.lib to pureb

Posted: Mon Feb 17, 2014 5:10 am
by idle
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 

Re: Help Convert C++ header for Iconv.dll/iconv.lib to pureb

Posted: Mon Feb 17, 2014 6:28 am
by Pureabc
UTF8 will work just fine.

Can someone help convert the header?

Thanks.

Re: Help Convert FreeBasic header for Iconv.dll/iconv.lib

Posted: Fri Feb 21, 2014 11:22 pm
by Pureabc
I just found the FreeBasic header, not sure how to handle the PTR PTR datatype.

Can someone help on this?

Thank you.

Re: Help Convert FreeBasic header for Iconv.dll/iconv.lib

Posted: Sat Feb 22, 2014 12:13 am
by idle
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 


Re: Help Convert FreeBasic header for Iconv.dll/iconv.lib

Posted: Sat Feb 22, 2014 1:18 am
by Zebuddi123
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. :)

Re: Help Convert FreeBasic header for Iconv.dll/iconv.lib

Posted: Sat Feb 22, 2014 11:36 pm
by Pureabc
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.

Re: Help Convert FreeBasic header for Iconv.dll/iconv.lib

Posted: Sun Feb 23, 2014 10:09 pm
by Pureabc
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?

Re: Help Convert FreeBasic header for Iconv.dll/iconv.lib

Posted: Sun Feb 23, 2014 10:18 pm
by ts-soft
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))

Re: Help Convert FreeBasic header for Iconv.dll/iconv.lib

Posted: Mon Feb 24, 2014 4:05 am
by Demivec
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

Re: Help Convert FreeBasic header for Iconv.dll/iconv.lib

Posted: Mon Feb 24, 2014 7:50 am
by ts-soft
Here a example of PTR PTR from purebasic helpfile:
http://www.purebasic.com/documentation/ ... tring.html

Re: Help Convert FreeBasic header for Iconv.dll/iconv.lib

Posted: Mon Feb 24, 2014 7:52 am
by Pureabc
Ts_soft,

Your explanation and example make sense.

I will work on this more.

Thank you.

Re: Help Convert FreeBasic header for Iconv.dll/iconv.lib

Posted: Mon Feb 24, 2014 2:29 pm
by infratec
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

Re: Help Convert FreeBasic header for Iconv.dll/iconv.lib

Posted: Mon Feb 24, 2014 8:10 pm
by idle
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
  
  
  

Re: Help Convert FreeBasic header for Iconv.dll/iconv.lib

Posted: Mon Feb 24, 2014 8:30 pm
by infratec
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