Back to index

php5  5.3.10
strcut.c
Go to the documentation of this file.
00001 
00008 #include <stdio.h>
00009 #include <stdlib.h>
00010 #include <string.h>
00011 #include <errno.h>
00012 #include "mbfl/mbfilter.h"
00013 
00014 static void hexdump(const mbfl_string *ptr)
00015 {
00016        unsigned int i;
00017 
00018        for (i = 0; i < ptr->len; i++) {
00019               printf("%%%02x", ptr->val[i]);
00020        }
00021 
00022        printf(" (%u)\n", ptr->len);
00023 }
00024 
00025 int main(int argc, char **argv)
00026 {
00027        enum mbfl_no_encoding no_encoding;
00028        enum mbfl_no_language no_language;
00029        mbfl_memory_device dev;
00030        mbfl_string string;
00031        int offset, length;
00032        int final = 0;
00033        int state = 0;
00034 
00035        if (argc < 5) {
00036               fprintf(stderr, "Usage: %s lang encoding offset length\n", argv[0]);
00037               return EXIT_FAILURE;
00038        }
00039 
00040        if ((no_language = mbfl_name2no_language(argv[1])) ==
00041                      mbfl_no_language_invalid) {
00042               printf("Unsupported NLS: %s\n", argv[1]);
00043               return EXIT_FAILURE;
00044        }
00045 
00046        if ((no_encoding = mbfl_name2no_encoding(argv[2])) ==
00047                      mbfl_no_encoding_invalid) {
00048               printf("Unsupported encoding: %s\n", argv[2]);
00049               return EXIT_FAILURE;
00050        }
00051 
00052        errno = 0;
00053        offset = strtol(argv[3], NULL, 10);
00054        if (errno) {
00055               printf("Invalid offset: %s\n", argv[3]);
00056               return EXIT_FAILURE;
00057        }
00058 
00059        length = strtol(argv[4], NULL, 10);
00060        if (errno) {
00061               printf("Invalid length: %s\n", argv[4]);
00062               return EXIT_FAILURE;
00063        }
00064 
00065 
00066        do {
00067               mbfl_string result;
00068 
00069               mbfl_memory_device_init(&dev, 0, 4096);
00070               mbfl_string_init_set(&string, no_language, no_encoding);
00071 
00072               for (;;) {
00073                      const int c = fgetc(stdin);
00074 
00075                      if (c == EOF) {
00076                             final = 1;
00077                             break;
00078                      } else if (c == 10) {
00079                             if (state == 1) {
00080                                    state = 0;
00081                                    continue;
00082                             }
00083                             break;
00084                      } else if (c == 13) {
00085                             state = 1;
00086                             break;
00087                      }
00088 
00089                      if (dev.pos >= dev.length) {
00090                             if (dev.length + dev.allocsz < dev.length) {
00091                                    printf("Unable to allocate memory\n");
00092                                    return EXIT_FAILURE;
00093                             }
00094 
00095                             mbfl_memory_device_realloc(&dev, dev.length + dev.allocsz,
00096                                           dev.allocsz);
00097                      }
00098 
00099                      dev.buffer[dev.pos++] = (unsigned char)c;
00100               }
00101 
00102               mbfl_memory_device_result(&dev, &string);
00103               if (mbfl_strcut(&string, &result, offset, length)) {
00104                      hexdump(&result);
00105                      mbfl_string_clear(&result);
00106               } else {
00107                      printf("***ERROR***\n");
00108               }
00109               mbfl_string_clear(&string);
00110        } while (!final);
00111 
00112        return EXIT_SUCCESS;
00113 }