convert.c

Go to the documentation of this file.
00001 /*
00002      This file is part of libextractor.
00003      (C) 2004 Vidyut Samanta and Christian Grothoff
00004 
00005      libextractor is free software; you can redistribute it and/or modify
00006      it under the terms of the GNU General Public License as published
00007      by the Free Software Foundation; either version 2, or (at your
00008      option) any later version.
00009 
00010      libextractor is distributed in the hope that it will be useful, but
00011      WITHOUT ANY WARRANTY; without even the implied warranty of
00012      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00013      General Public License for more details.
00014 
00015      You should have received a copy of the GNU General Public License
00016      along with libextractor; see the file COPYING.  If not, write to the
00017      Free Software Foundation, Inc., 59 Temple Place - Suite 330,
00018      Boston, MA 02111-1307, USA.
00019  */
00020 
00021 #include "platform.h"
00022 #include "extractor.h"
00023 #include "convert.h"
00024 
00025 /**
00026  * Convert the len characters long character sequence
00027  * given in input that is in the given charset
00028  * to UTF-8.
00029  * @return the converted string (0-terminated),
00030  *  if conversion fails, a copy of the orignal
00031  *  string is returned.
00032  */
00033 char *
00034 EXTRACTOR_common_convert_to_utf8 (const char *input, size_t len, const char *charset)
00035 {
00036   size_t tmpSize;
00037   size_t finSize;
00038   char *tmp;
00039   char *ret;
00040   char *itmp;
00041   const char *i;
00042   iconv_t cd;
00043 
00044   i = input;
00045   cd = iconv_open ("UTF-8", charset);
00046   if (cd == (iconv_t) - 1)
00047     return strdup (i);
00048   tmpSize = 3 * len + 4;
00049   tmp = malloc (tmpSize);
00050   itmp = tmp;
00051   finSize = tmpSize;
00052   if (iconv (cd, (char **) &input, &len, &itmp, &finSize) == (size_t) - 1)
00053     {
00054       iconv_close (cd);
00055       free (tmp);
00056       return strdup (i);
00057     }
00058   ret = malloc (tmpSize - finSize + 1);
00059   memcpy (ret, tmp, tmpSize - finSize);
00060   ret[tmpSize - finSize] = '\0';
00061   free (tmp);
00062   iconv_close (cd);
00063   return ret;
00064 }
00065 
00066 /* end of convert.c */

Generated on Fri Jan 9 16:44:25 2009 for libextractor by  doxygen 1.5.1