La Vita è Bella

2007-07-09

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!

15:58:25 by fishy - Permanent Link

May the Force be with you. RAmen