1 /**
2 * this is a small sample script to use libmbfl.
3 * Rui Hirokawa <hirokawa@php.net>
4 *
5 * this file is encoded in EUC-JP.
6 */
7
8 #include <stdio.h>
9 #include <stdlib.h>
10 #include <string.h>
11 #include "mbfl/mbfilter.h"
12
main(int argc,char ** argv)13 int main(int argc, char **argv)
14 {
15 enum mbfl_no_encoding no_encoding;
16 enum mbfl_no_language no_language;
17 mbfl_memory_device dev;
18 mbfl_string string;
19 int final = 0;
20 int state = 0;
21
22 if (argc < 3) {
23 fprintf(stderr, "Usage: %s lang encoding\n", argv[0]);
24 return EXIT_FAILURE;
25 }
26
27 if ((no_language = mbfl_name2no_language(argv[1])) ==
28 mbfl_no_language_invalid) {
29 printf("Unsupported NLS: %s\n", argv[1]);
30 return EXIT_FAILURE;
31 }
32
33 if ((no_encoding = mbfl_name2no_encoding(argv[2])) ==
34 mbfl_no_encoding_invalid) {
35 printf("Unsupported encoding: %s\n", argv[2]);
36 return EXIT_FAILURE;
37 }
38
39 do {
40 mbfl_memory_device_init(&dev, 0, 4096);
41 mbfl_string_init_set(&string, no_language, no_encoding);
42
43 for (;;) {
44 const int c = fgetc(stdin);
45
46 if (c == EOF) {
47 final = 1;
48 break;
49 } else if (c == 10) {
50 if (state == 1) {
51 state = 0;
52 continue;
53 }
54 break;
55 } else if (c == 13) {
56 state = 1;
57 break;
58 }
59
60 if (dev.pos >= dev.length) {
61 if (dev.length + dev.allocsz < dev.length) {
62 printf("Unable to allocate memory\n");
63 return EXIT_FAILURE;
64 }
65
66 mbfl_memory_device_realloc(&dev, dev.length + dev.allocsz,
67 dev.allocsz);
68 }
69
70 dev.buffer[dev.pos++] = (unsigned char)c;
71 }
72
73 mbfl_memory_device_result(&dev, &string);
74 printf("%d\n", mbfl_strwidth(&string));
75 mbfl_string_clear(&string);
76 } while (!final);
77
78 return EXIT_SUCCESS;
79 }
80