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
hexdump(const mbfl_string * ptr)13 static void hexdump(const mbfl_string *ptr)
14 {
15 unsigned int i;
16
17 for (i = 0; i < ptr->len; i++) {
18 printf("%%%02x", ptr->val[i]);
19 }
20
21 printf(" (%u)\n", ptr->len);
22 }
23
main(int argc,char ** argv)24 int main(int argc, char **argv)
25 {
26 enum mbfl_no_encoding no_enc;
27 const enum mbfl_no_language no_lang = mbfl_no_language_japanese;
28 mbfl_memory_device dev;
29 mbfl_string string, result;
30 int final = 0;
31 int state = 0;
32 int mode = 0;
33
34 if (argc < 3) {
35 fprintf(stderr, "Usage: %s encoding flags\n", argv[0]);
36 return EXIT_FAILURE;
37 }
38
39 if ((no_enc = mbfl_name2no_encoding(argv[1])) ==
40 mbfl_no_encoding_invalid) {
41 printf("Unsupported encoding: %s\n", argv[1]);
42 return EXIT_FAILURE;
43 }
44
45 {
46 const char *p;
47
48 for (p= argv[2] + strlen(argv[2]); p > argv[2]; ) {
49 switch (*(--p)) {
50 case 'A':
51 mode |= 0x1;
52 break;
53 case 'a':
54 mode |= 0x10;
55 break;
56 case 'R':
57 mode |= 0x2;
58 break;
59 case 'r':
60 mode |= 0x20;
61 break;
62 case 'N':
63 mode |= 0x4;
64 break;
65 case 'n':
66 mode |= 0x40;
67 break;
68 case 'S':
69 mode |= 0x8;
70 break;
71 case 's':
72 mode |= 0x80;
73 break;
74 case 'K':
75 mode |= 0x100;
76 break;
77 case 'k':
78 mode |= 0x1000;
79 break;
80 case 'H':
81 mode |= 0x200;
82 break;
83 case 'h':
84 mode |= 0x2000;
85 break;
86 case 'V':
87 mode |= 0x800;
88 break;
89 case 'C':
90 mode |= 0x10000;
91 break;
92 case 'c':
93 mode |= 0x20000;
94 break;
95 case 'M':
96 mode |= 0x100000;
97 break;
98 case 'm':
99 mode |= 0x200000;
100 break;
101 }
102 }
103 }
104
105 do {
106 mbfl_memory_device_init(&dev, 0, 4096);
107 mbfl_string_init_set(&string, no_lang, no_enc);
108
109 for (;;) {
110 const int c = fgetc(stdin);
111
112 if (c == EOF) {
113 final = 1;
114 break;
115 } else if (c == 10) {
116 if (state == 1) {
117 state = 0;
118 continue;
119 }
120 break;
121 } else if (c == 13) {
122 state = 1;
123 break;
124 }
125
126 if (dev.pos >= dev.length) {
127 if (dev.length + dev.allocsz < dev.length) {
128 printf("Unable to allocate memory\n");
129 return EXIT_FAILURE;
130 }
131
132 mbfl_memory_device_realloc(&dev, dev.length + dev.allocsz,
133 dev.allocsz);
134 }
135
136 dev.buffer[dev.pos++] = (unsigned char)c;
137 }
138
139 mbfl_memory_device_result(&dev, &string);
140 mbfl_ja_jp_hantozen(&string, &result, mode);
141 hexdump(&result);
142 mbfl_string_clear(&result);
143 mbfl_string_clear(&string);
144 } while (!final);
145
146 return EXIT_SUCCESS;
147 }
148