Back to index

php5  5.3.10
conv_encoding.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 from_encoding, to_encoding;
00027        enum mbfl_no_language no_language;
00028        mbfl_buffer_converter *convd = NULL;
00029        mbfl_memory_device dev;
00030        mbfl_string string, result, *ret;
00031        int final = 0;
00032        int state = 0;
00033 
00034        if (argc < 4) {
00035               fprintf(stderr, "Usage: %s lang to_encoding from_encoding\n", argv[0]);
00036               return EXIT_FAILURE;
00037        }
00038 
00039        if ((no_language = mbfl_name2no_language(argv[1])) ==
00040                      mbfl_no_language_invalid) {
00041               printf("Unsupported NLS: %s\n", argv[1]);
00042               return EXIT_FAILURE;
00043        }
00044 
00045        if ((to_encoding = mbfl_name2no_encoding(argv[2])) ==
00046                      mbfl_no_encoding_invalid) {
00047               printf("Unsupported encoding: %s\n", argv[2]);
00048               return EXIT_FAILURE;
00049        }
00050 
00051        if ((from_encoding = mbfl_name2no_encoding(argv[3])) ==
00052                      mbfl_no_encoding_invalid) {
00053               printf("Unsupported encoding: %s\n", argv[3]);
00054               return EXIT_FAILURE;
00055        }
00056 
00057        convd = mbfl_buffer_converter_new(from_encoding, to_encoding, 0);
00058 
00059        do {
00060               mbfl_memory_device_init(&dev, 0, 4096);
00061               mbfl_string_init_set(&string, no_language, from_encoding);
00062 
00063               for (;;) {
00064                      const int c = fgetc(stdin);
00065 
00066                      if (c == EOF) {
00067                             final = 1;
00068                             break;
00069                      } else if (c == 10) {
00070                             if (state == 1) {
00071                                    state = 0;
00072                                    continue;
00073                             }
00074                             break;
00075                      } else if (c == 13) {
00076                             state = 1;
00077                             break;
00078                      }
00079 
00080                      if (dev.pos >= dev.length) {
00081                             if (dev.length + dev.allocsz < dev.length) {
00082                                    printf("Unable to allocate memory\n");
00083                                    return EXIT_FAILURE;
00084                             }
00085 
00086                             mbfl_memory_device_realloc(&dev, dev.length + dev.allocsz,
00087                                           dev.allocsz);
00088                      }
00089 
00090                      dev.buffer[dev.pos++] = (unsigned char)c;
00091               }
00092 
00093               mbfl_memory_device_result(&dev, &string);
00094               mbfl_string_init_set(&result, no_language, to_encoding);
00095               ret = mbfl_buffer_converter_feed_result(convd, &string, &result);
00096               hexdump(&result);
00097               mbfl_string_clear(&result);
00098               mbfl_string_clear(&string);
00099        } while (!final);
00100 
00101        mbfl_buffer_converter_delete(convd);      
00102 
00103        return EXIT_SUCCESS;
00104 }