Back to index

php5  5.3.10
conv_kana.c
Go to the documentation of this file.
00001 
00008 #include <stdio.h>
00009 #include <stdlib.h>
00010 #include <string.h>
00011 #include "mbfl/mbfilter.h"
00012 
00013 static void hexdump(const mbfl_string *ptr)
00014 {
00015        unsigned int i;
00016 
00017        for (i = 0; i < ptr->len; i++) {
00018               printf("%%%02x", ptr->val[i]);
00019        }
00020 
00021        printf(" (%u)\n", ptr->len);
00022 }
00023 
00024 int main(int argc, char **argv)
00025 {
00026        enum mbfl_no_encoding no_enc;
00027        const enum mbfl_no_language no_lang = mbfl_no_language_japanese;
00028        mbfl_memory_device dev;
00029        mbfl_string string, result;
00030        int final = 0;
00031        int state = 0;
00032        int mode = 0;
00033 
00034        if (argc < 3) {
00035               fprintf(stderr, "Usage: %s encoding flags\n", argv[0]);
00036               return EXIT_FAILURE;
00037        }
00038 
00039        if ((no_enc = mbfl_name2no_encoding(argv[1])) ==
00040                      mbfl_no_encoding_invalid) {
00041               printf("Unsupported encoding: %s\n", argv[1]);
00042               return EXIT_FAILURE;
00043        }
00044 
00045        {
00046               const char *p;
00047 
00048               for (p= argv[2] + strlen(argv[2]); p > argv[2]; ) {
00049                      switch (*(--p)) {
00050                      case 'A':
00051                             mode |= 0x1;
00052                             break;
00053                      case 'a':
00054                             mode |= 0x10;
00055                             break;
00056                      case 'R':
00057                             mode |= 0x2;
00058                             break;
00059                      case 'r':
00060                             mode |= 0x20;
00061                             break;
00062                      case 'N':
00063                             mode |= 0x4;
00064                             break;
00065                      case 'n':
00066                             mode |= 0x40;
00067                             break;
00068                      case 'S':
00069                             mode |= 0x8;
00070                             break;
00071                      case 's':
00072                             mode |= 0x80;
00073                             break;
00074                      case 'K':
00075                             mode |= 0x100;
00076                             break;
00077                      case 'k':
00078                             mode |= 0x1000;
00079                             break;
00080                      case 'H':
00081                             mode |= 0x200;
00082                             break;
00083                      case 'h':
00084                             mode |= 0x2000;
00085                             break;
00086                      case 'V':
00087                             mode |= 0x800;
00088                             break;
00089                      case 'C':
00090                             mode |= 0x10000;
00091                             break;
00092                      case 'c':
00093                             mode |= 0x20000;
00094                             break;
00095                      case 'M':
00096                             mode |= 0x100000;
00097                             break;
00098                      case 'm':
00099                             mode |= 0x200000;
00100                             break;
00101                      }
00102               }
00103        }
00104 
00105        do {
00106               mbfl_memory_device_init(&dev, 0, 4096);
00107               mbfl_string_init_set(&string, no_lang, no_enc);
00108 
00109               for (;;) {
00110                      const int c = fgetc(stdin);
00111 
00112                      if (c == EOF) {
00113                             final = 1;
00114                             break;
00115                      } else if (c == 10) {
00116                             if (state == 1) {
00117                                    state = 0;
00118                                    continue;
00119                             }
00120                             break;
00121                      } else if (c == 13) {
00122                             state = 1;
00123                             break;
00124                      }
00125 
00126                      if (dev.pos >= dev.length) {
00127                             if (dev.length + dev.allocsz < dev.length) {
00128                                    printf("Unable to allocate memory\n");
00129                                    return EXIT_FAILURE;
00130                             }
00131 
00132                             mbfl_memory_device_realloc(&dev, dev.length + dev.allocsz,
00133                                           dev.allocsz);
00134                      }
00135 
00136                      dev.buffer[dev.pos++] = (unsigned char)c;
00137               }
00138 
00139               mbfl_memory_device_result(&dev, &string);
00140               mbfl_ja_jp_hantozen(&string, &result, mode);
00141               hexdump(&result);
00142               mbfl_string_clear(&result);
00143               mbfl_string_clear(&string);
00144        } while (!final);
00145 
00146        return EXIT_SUCCESS;
00147 }