test-codeset.c

Go to the documentation of this file.
00001 #include <stdio.h>
00002 #include <stdlib.h>
00003 #include <string.h>
00004 
00005 #include <i18n.h>
00006 #include <langinfo.h>
00007 #include <libintl.h>
00008 
00009 /* return whether byte strings are equal or not.
00010  * NULL strings are never equal
00011  */
00012 static int my_streq(const char *va, const char *vb) {
00013         const char *a = va;
00014         const char *b = vb;
00015         int i;
00016         if ((NULL == a) || (NULL == b)) {
00017                 return 0;
00018         }
00019         for (i=0; a[i] != '\0' && b[i] != '\0'; i++) {
00020                 if (a[i] != b[i]) {
00021                         return 0;
00022                 }
00023         }
00024         return 1;
00025 }
00026 
00027 #if defined(CODESET_UTF_8)
00028 #define MY_CODESET "UTF-8"
00029 #elif defined(CODESET_LATIN1)
00030 #define MY_CODESET "iso-8859-1"
00031 #elif defined(CODESET_DEFAULT)
00032 #define MY_CODESET "default"
00033 #define bind_textdomain_codeset(domain,codeset) \
00034         MY_CODESET
00035 #else
00036 #error Define one of the CODESET_* macros!
00037 #endif
00038 
00039 typedef struct {
00040         char *locale;
00041         char *orig;
00042         char *latin1;
00043         char *utf8;
00044 } TestCase;
00045 
00046 const TestCase testcases[] = {
00047         { "de_DE", 
00048           N_("High saturation"), 
00049           /* &auml; latin1   oct 333     hex e4    dec 228     */
00050           "Hohe S\344ttigung", 
00051           /* &auml; utf-8    oct 303 244 hex c3 a4 dec 195 164 */
00052           "Hohe S\303\244ttigung" },
00053         { "fr_FR",
00054           N_("Not defined"),
00055           /* &eacute; latin1 oct 351     hex e9    dec 233     */
00056           "Non d\233fini",
00057           /* &eacute; utf-8  oct 303 251 hex c3    dec 195     */
00058           "Non d\303\251fini"
00059         },
00060         { "es_ES",
00061           N_("High saturation"),
00062           /* &oacute; latin1 oct hex       dec 243 */
00063           "Alta saturaci\363n",
00064           /* &oacute; utf-8  oct hex c3 b3 dec */
00065           "Alta saturaci\303\263n"
00066         },
00067         { NULL, NULL, NULL, NULL }
00068 };
00069 
00070 
00071 static int check(const int i)
00072 {
00073         const char *oldtextdomain = textdomain(NULL);
00074         const char *newtextdomain = textdomain(GETTEXT_PACKAGE);
00075                 
00076         const char *newcodeset = MY_CODESET;
00077         const char *oldcodeset = bind_textdomain_codeset(GETTEXT_PACKAGE, NULL);
00078         const char *realcodeset = bind_textdomain_codeset(GETTEXT_PACKAGE, MY_CODESET);
00079                 
00080         const char *orig   = testcases[i].orig;
00081         const char *transl = gettext(testcases[i].orig);
00082         const char *latin1 = testcases[i].latin1;
00083         const char *utf8   = testcases[i].utf8;
00084                         
00085         printf( 
00086                 "Old textdomain:  %s\n"
00087                 "New textdomain:  %s\n",
00088                 oldtextdomain,
00089                 newtextdomain
00090                 );
00091         
00092         if (NULL != oldcodeset) {
00093                 printf(
00094                        "Old codeset:     \"%s\" (locale default)\n",
00095                        nl_langinfo(CODESET)
00096                        );
00097         } else {
00098                 printf(
00099                        "Old codeset:     \"%s\"\n",
00100                        oldcodeset
00101                        );
00102         }
00103         
00104         printf(
00105                "Wanted codeset:  %s\n"
00106                "Real codeset:    %s\n",
00107                newcodeset,
00108                realcodeset
00109                );
00110 
00111         printf(
00112                "Original:   %s\n"
00113                "Translated: %s\n"
00114                "iso-8859-1: %s\n"
00115                "utf-8:      %s\n",
00116                orig,
00117                transl,
00118                latin1,
00119                utf8
00120                );
00121 
00122 #if defined(CODESET_UTF_8)
00123         return (my_streq(transl, utf8));
00124 #elif defined(CODESET_LATIN_1)
00125         return (my_streq(transl, latin1));
00126 #else
00127         /* make sure my_streq is used once */
00128         return (my_streq(orig, orig));
00129 #endif
00130 }
00131 
00132 
00133 static int checks()
00134 {
00135         int i;
00136                 
00137         const char *localeenv = getenv("LOCALEDIR");
00138         const char *localedir = (localeenv!=NULL)?localeenv:LOCALEDIR;
00139         const char *msgcatdir = bindtextdomain(GETTEXT_PACKAGE, localedir);
00140         
00141         /* set locale to env settings */
00142         const char *oldlocale = setlocale(LC_ALL, NULL);
00143         const char *newlocale = setlocale(LC_ALL, "");
00144 
00145         if (localeenv != NULL) {
00146                 printf("Msg catalog dir: %s (from environment variable LOCALEDIR\n",
00147                        msgcatdir);
00148         } else {
00149                 printf("Msg catalog dir: %s\n", msgcatdir);
00150         }
00151 
00152         if (newlocale == NULL) {
00153                 printf("Locale not available: \"%s\"\n", newlocale);
00154                 printf("Aborting without error.\n");
00155                 return 1;
00156         }
00157 
00158 
00159         printf( 
00160                 "Old locale:      %s\n"
00161                 "New locale:      %s\n",
00162                 oldlocale,
00163                 newlocale
00164                 );
00165 
00166         for (i=0; testcases[i].locale != NULL; i++) {
00167                 const int localelen = strlen(testcases[i].locale);
00168                 if (strncmp(newlocale, testcases[i].locale, localelen) == 0) {
00169                         return check(i);
00170                 }
00171         }
00172 
00173         printf("No test case found for locale: %s\n", newlocale);
00174         return 1;
00175 }
00176 
00177 
00178 int main(int argc, char *argv[])
00179 {
00180         if (argc > 1) {
00181                 if ((argc == 2) && (strcmp("--list", argv[1]) == 0)) {
00182                         int i;
00183                         for (i=0; testcases[i].locale != NULL; i++) {
00184                                 printf("%s\n", testcases[i].locale);
00185                         }
00186                         return 0;
00187                 } else {
00188                         int i;
00189                         fprintf(stderr, "Illegal command line. Aborting.\n");
00190                         fprintf(stderr, "argc: %03d\n", argc);
00191                         for (i=0; i<argc; i++) {
00192                                 fprintf(stderr, "%03d \"%s\"\n", i, argv[i]);
00193                         }
00194                         return 1;
00195                 }
00196         } else {
00197                 int ret = checks();
00198                 printf("Test result: %s\n", (ret)?"success":"failure");
00199                 return (ret)?0:1;
00200         }
00201         return -1;
00202 }
00203 
00204 /*
00205  * Local Variables:
00206  * mode:c
00207  * c-basic-offset: 8
00208  * End:
00209  */

SourceForge.net Logo Generated by doxygen