La Vita è Bella

Monday, July 09, 2007

Deal with 2 versions of iconv.h

There're 2 versions of iconv.h in which the iconv() have different prototypes.

In Debian, it's:

43 extern size_t iconv (iconv_t __cd, char **__restrict __inbuf,
44                      size_t *__restrict __inbytesleft,
45                      char **__restrict __outbuf,
46                      size_t *__restrict __outbytesleft);

And in other systems, such as Mac OS X, it's:

83 extern size_t iconv (iconv_t cd, const char* * inbuf, size_t *inbytesleft, char* * outbuf, size_t *outbytesleft);

So you can see that the second parameter, "inbuf", can be "const char **" or "char **"

It will make a big problem while writing code for both systems. One resolution is to use condition compile ("#ifdef" blah blah...), but RoachCock@newsmth give me another (and much better) resolution: use operator overload (so it's C++ only):

133 struct iconv_param_adapter {
134         iconv_param_adapter(const char**p) : p(p) {}
135         iconv_param_adapter(char**p) : p((const char**)p) {}
136         operator char**() const
137         {
138                 return (char**)p;
139         }
140         operator const char**() const
141         {
142                 return (const char**)p;
143         }
144         const char** p;
145 };

When you calling "iconv()", call it like this:

111         size_t res = iconv(data, iconv_param_adapter(&s), &inbytesleft, &outnew, &outbytesleft);

Thank you RoachCock!

tags: , , , ,

15:58:25 by fishy - dev - Permanent Link

Revision: 1.2/1.2, last modified on 2007-07- 8 @ 17:01.

Karma: 8 (53.08% out of 130 were positive) [+/-]

You can subscribe to RSS 2.0 feed for comments and trackbacks

There are currently no trackbacks for this item.
Use this TrackBack url to ping this item (right-click, copy link target). If your blog does not support Trackbacks you can manually add your trackback by using this form.


b6s wrote:

I suggested this nice approach to hunspell. Today its developer comes out another solution based on autoconf. If you can accept the dependency to autoconf, then just use ICONV_CONST as the type caster:

size_t res = iconv(data, (ICONV_CONST char**) &s, &inbytesleft, &outnew, &outbytesleft);

It's actually the same to the concept of "#ifdef" preprocessor macros.

The reference:

Tuesday, July 10, 2007 18:12:57


Finalfire wrote:

Uhm, nice info.

Tuesday, July 24, 2007 19:21:08

Add Comment


May the Force be with you. RAmen