Back to index

php5  5.3.10
strwidth.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 int main(int argc, char **argv)
00014 {
00015        enum mbfl_no_encoding no_encoding;
00016        enum mbfl_no_language no_language;
00017        mbfl_memory_device dev;
00018        mbfl_string string;
00019        int final = 0;
00020        int state = 0;
00021 
00022        if (argc < 3) {
00023               fprintf(stderr, "Usage: %s lang encoding\n", argv[0]);
00024               return EXIT_FAILURE;
00025        }
00026 
00027        if ((no_language = mbfl_name2no_language(argv[1])) ==
00028                      mbfl_no_language_invalid) {
00029               printf("Unsupported NLS: %s\n", argv[1]);
00030               return EXIT_FAILURE;
00031        }
00032 
00033        if ((no_encoding = mbfl_name2no_encoding(argv[2])) ==
00034                      mbfl_no_encoding_invalid) {
00035               printf("Unsupported encoding: %s\n", argv[2]);
00036               return EXIT_FAILURE;
00037        }
00038 
00039        do {
00040               mbfl_memory_device_init(&dev, 0, 4096);
00041               mbfl_string_init_set(&string, no_language, no_encoding);
00042 
00043               for (;;) {
00044                      const int c = fgetc(stdin);
00045 
00046                      if (c == EOF) {
00047                             final = 1;
00048                             break;
00049                      } else if (c == 10) {
00050                             if (state == 1) {
00051                                    state = 0;
00052                                    continue;
00053                             }
00054                             break;
00055                      } else if (c == 13) {
00056                             state = 1;
00057                             break;
00058                      }
00059 
00060                      if (dev.pos >= dev.length) {
00061                             if (dev.length + dev.allocsz < dev.length) {
00062                                    printf("Unable to allocate memory\n");
00063                                    return EXIT_FAILURE;
00064                             }
00065 
00066                             mbfl_memory_device_realloc(&dev, dev.length + dev.allocsz,
00067                                           dev.allocsz);
00068                      }
00069 
00070                      dev.buffer[dev.pos++] = (unsigned char)c;
00071               }
00072 
00073               mbfl_memory_device_result(&dev, &string);
00074               printf("%d\n", mbfl_strwidth(&string));
00075               mbfl_string_clear(&string);
00076        } while (!final);
00077 
00078        return EXIT_SUCCESS;
00079 }