Back to index

lightning-sunbird  0.9+nobinonly
utf8conv.c
Go to the documentation of this file.
00001 /* utf8conv.c: Test driver for UTF8 conversions in unistring.c */
00002 
00003 /* public declarations */
00004 #include "unistring.h"
00005 
00006 #include <assert.h>
00007 
00008 int main(int argc, char *argv[])
00009 {
00010   int argNo, ucs16_opt, utf8_opt, j;
00011   int result, remaining, encoded, decoded;
00012 
00013   if (argc < 2) {
00014     printf("Usage: %s (-ucs16 <unicode-hex-string>|-utf8 <utf8-hex-string>|<ascii-chars>)\n",
00015            argv[0]);
00016     exit(1);
00017   }
00018 
00019   ucs16_opt = 0;
00020   utf8_opt = 0;
00021   argNo = 1;
00022 
00023   if (argc > 1) {
00024     argNo = 2;
00025     if (strcmp(argv[1],"-ucs16") == 0) {
00026       ucs16_opt = 1;
00027     } else if (strcmp(argv[1],"-utf8") == 0) {
00028       utf8_opt = 1;
00029     }
00030   }
00031 
00032   if (ucs16_opt) {
00033     char* tems = argv[argNo];
00034     int nus = strlen(argv[argNo])/4;
00035     int ns = 3*nus;
00036     UNICHAR* us = (UNICHAR*) malloc((nus+1)*sizeof(UNICHAR));
00037     unsigned char* s = (unsigned char*) malloc(ns+1);
00038     char temus[5] = {0,0,0,0,0};
00039 
00040     for (j=0; j<nus; j++) {
00041       temus[0] = tems[4*j];
00042       temus[1] = tems[4*j+1];
00043       temus[2] = tems[4*j+2];
00044       temus[3] = tems[4*j+3];
00045       sscanf(temus, "%x", &us[j]);
00046     }
00047     us[nus] = U_NUL;
00048 
00049     ucsprint(stderr, us, nus);
00050     fprintf(stderr, "\n");
00051 
00052     ucstoutf8(us, nus, s, ns, &remaining, &encoded);
00053     assert(encoded <= ns);
00054     ns = encoded;
00055     s[ns] = '\0';
00056 
00057     printf("UTF8(%d)=0x", ns);
00058     for (j=0; j<ns; j++) {
00059       printf("%02x",s[j]);
00060     }
00061     printf(", remaining=%d\n", remaining);
00062 
00063     result = utf8toucs(s, ns, us, nus, 0, &remaining, &decoded);
00064     assert(decoded <= nus);
00065     us[decoded] = U_NUL;
00066 
00067     printf("UCS(%d)=0x", decoded);
00068 
00069     for (j=0; j<decoded; j++) {
00070       printf("%04x",us[j]);
00071     }
00072     printf(", remaining=%d, result=%d\n", remaining, result);
00073 
00074     free(us);
00075     free(s);
00076 
00077   } else {
00078     char* tems = argv[argNo];
00079     int ns, nus;
00080     char* s;
00081     UNICHAR* us;
00082 
00083     ns = (utf8_opt) ? strlen(argv[argNo])/2 : strlen(argv[argNo]);
00084     s = (char*) malloc(ns+1);
00085     nus = ns;
00086     us = (UNICHAR*) malloc((nus+1)*sizeof(UNICHAR));
00087 
00088     if (utf8_opt) {
00089       char temstr[3] = {0,0,0};
00090       int ival;
00091       for (j=0; j<ns; j++) {
00092         temstr[0] = tems[2*j];
00093         temstr[1] = tems[2*j+1];
00094         sscanf(temstr, "%x", &ival);
00095         s[j] = (unsigned char) ival;
00096       }
00097     } else {
00098       for (j=0; j<ns; j++)
00099         s[j] = tems[j];
00100     }
00101     s[ns] = '\0';
00102 
00103     result = utf8toucs(s, ns, us, nus, 0, &remaining, &decoded);
00104     assert(decoded <= nus);
00105     nus = decoded;
00106     us[nus] = U_NUL;
00107 
00108     printf("UCS(%d)=0x", nus);
00109 
00110     for (j=0; j<nus; j++) {
00111       printf("%04x",us[j]);
00112     }
00113 
00114     printf(", remaining=%d, result=%d\n", remaining, result);
00115 
00116     ucstoutf8(us, nus, s, ns, &remaining, &encoded);
00117 
00118     assert(encoded <= ns);
00119     s[encoded] = '\0';
00120 
00121     if (utf8_opt) {
00122       printf("UTF8(%d)=0x", encoded);
00123 
00124       for (j=0; j<encoded; j++)
00125         printf("%02x", s[j]);
00126 
00127       printf(", remaining=%d\n", remaining);
00128 
00129     } else {
00130       printf("UTF8(%d)='%s', remaining=%d\n", encoded, s, remaining);
00131     }
00132 
00133     free(us);
00134     free(s);
00135   }
00136 
00137   return 0;
00138 }