TUM-Logo

libRASCH

 

Home
 

General

About libRASCH/News
Design
Screen shots
Sample programs (with source code)
License
 

Download

libRASCH
Tools
 

Documentation

User
Developer
 

Resources

Mailing list
Supported Formats
Plugins
Status
Links
 
Contact
About this site
 
Last updated
Tue Mar 27 23:03:52 2007
libRASCH: i18n.c Source File

i18n.c

Go to the documentation of this file.
00001 
00009 /*----------------------------------------------------------------------------
00010  *
00011  * Copyright (C) 2004, Raphael Schneider
00012  * See the file COPYING for information on usage and redistribution.
00013  *
00014  * $Header: /home/cvs_repos.bak/librasch/src/shared/i18n.c,v 1.4 2004/07/27 08:49:35 rasch Exp $
00015  *
00016  *--------------------------------------------------------------------------*/
00017 
00018 
00019 #include <string.h>
00020 
00021 #define _LIBRASCH_BUILD
00022 #include <ra_priv.h>
00023 #include <locale.h>
00024 #include <iconv.h>
00025 
00026 
00027 /* prototypes needed only here */
00028 int do_i18n_conversion_inplace(char *string, size_t dest_size, const char *from, const char *to);
00029 int do_i18n_conversion(const char *src, char *dest, size_t dest_size, const char *from, const char *to);
00030 int local_is_utf8();
00031 
00032 
00033 int
00034 local_to_utf8(const char *src, char *dest, size_t dest_size)
00035 {
00036         return do_i18n_conversion(src, dest, dest_size, ra_i18n_codeset, "UTF-8");
00037 } /* local_to_utf8() */
00038 
00039 
00040 int
00041 local_to_utf8_inplace(char *string, size_t buf_size)
00042 {
00043         return do_i18n_conversion_inplace(string, buf_size, ra_i18n_codeset, "UTF-8");
00044 } /* local_to_utf8_inplace() */
00045 
00046 
00047 int
00048 utf8_to_local(const char *src, char *dest, size_t dest_size)
00049 {
00050         return do_i18n_conversion(src, dest, dest_size, "UTF-8", ra_i18n_codeset);
00051 } /* utf8_to_local() */
00052 
00053 
00054 int
00055 utf8_to_local_inplace(char *string, size_t buf_size)
00056 {
00057         return do_i18n_conversion_inplace(string, buf_size, "UTF-8", ra_i18n_codeset);
00058 } /* local_to_utf8_inplace() */
00059 
00060 
00061 int
00062 do_i18n_conversion_inplace(char *string, size_t dest_size, const char *from, const char *to)
00063 {
00064         char *conv = NULL;
00065         size_t len;
00066         int ret;
00067 
00068         if (string == NULL)
00069                 return -1;
00070         if (string[0] == '\0')
00071                 return 0;
00072 
00073         len = strlen(string) * 2;
00074         conv = malloc(len);
00075         ret = do_i18n_conversion(string, conv, len, from, to);
00076         strncpy(string, conv, dest_size);
00077         free(conv);
00078 
00079         return ret;
00080 } /* do_i18n_conversion_inplace() */
00081 
00082 
00083 int
00084 do_i18n_conversion(const char *src, char *dest, size_t dest_size, const char *from, const char *to)
00085 {
00086         int ret = 0;
00087         iconv_t cd;
00088         size_t in_bytes, out_bytes;
00089         char *in, *out;
00090 
00091         if ((src == NULL) || (dest == NULL) || (dest_size <= 0))
00092                 return -1;
00093 
00094         if (src[0] == '\0')
00095         {
00096                 dest[0] = '\0';
00097                 return 0;
00098         }
00099 
00100         /* if no i18n support or locale is UTF-8 then do no conversion */
00101         if ((ra_i18n_codeset[0] == '\0') || (local_is_utf8()))
00102         {
00103                 strncpy(dest, src, dest_size);
00104                 return 0;
00105         }
00106 
00107         cd = iconv_open(to, from);
00108         /* if iconv can not be opened, just copy string but return error */
00109         if (cd == (iconv_t)-1)
00110         {
00111                 strncpy(dest, src, dest_size);
00112                 return -1;
00113         }
00114 
00115         /* convert string, do no special error handling */
00116         in_bytes = strlen(src);
00117         out_bytes = dest_size;
00118         in = (char *)src;
00119         out = dest;
00120         if (iconv(cd, &in, &in_bytes, &out, &out_bytes) == (size_t)-1)
00121                 ret = -2;
00122         
00123         dest[dest_size-out_bytes] = '\0';
00124 
00125         iconv_close(cd);
00126 
00127         return ret;
00128 }  /* do_i18n_conversion() */
00129 
00130 
00131 int
00132 local_is_utf8()
00133 {
00134         if (strcmp(ra_i18n_codeset, "UTF-8") == 0)
00135                 return 1;
00136 
00137         return 0;
00138 }  /* local_is_utf8() */

Generated on Fri May 27 11:32:38 2005 for libRASCH by  doxygen 1.4.2