xref: /PHP-7.2/ext/mbstring/libmbfl/tests/emoji.c (revision b7a7b1a6)
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 
24 //#define TEST_DOCOMO
25 //#define TEST_KDDI
26 #define TEST_SOFTBANK
27 
main(int argc,char ** argv)28 int main(int argc, char **argv)
29 {
30 	enum mbfl_no_encoding from_encoding, to_encoding;
31 	enum mbfl_no_language no_language;
32 	mbfl_buffer_converter *convd = NULL, *convd2 = NULL;
33 	mbfl_memory_device dev, dev2;
34 	mbfl_string string, result, *ret;
35 #ifdef TEST_DOCOMO
36 	//char str[] = {0xF9,0xD7,0x00}; // U+2122
37 	//char str[] = {0xF9,0x82,0x00};   // U+1F195
38 	char str[] = {0xF9,0xD6,0x00};   // U+00A9
39 #endif
40 #ifdef TEST_KDDI
41 	//char str[] = {0xF7,0x6A,0x00};// U+2122
42 	//char str[] = {0xF7,0xE5,0x00};  // U+1F195
43 	//char str[] = {0xF3,0xD2,0x00}; // U+1F1E8 U+1F1F3
44 	char str[] = {0xF7,0x74,0x00};  // U+00A9
45 #endif
46 #ifdef TEST_SOFTBANK
47 	//char str[] = {0xFB,0xD7,0x00};// U+2122
48 	//char str[] = {0xF7,0xB2,0x00};  // U+1F195
49 	//char str[] = {0xFB,0xB3,0x00}; // U+1F1E8 U+1F1F3
50 	char str[] = {0xF7,0xEE,0x00}; // U+00A9
51 #endif
52 	int final = 0;
53 	int state = 0;
54 	int i;
55 
56 	no_language = mbfl_name2no_language("Japanese");
57 #ifdef TEST_DOCOMO
58 	from_encoding = mbfl_name2no_encoding("SJIS-win#DOCOMO");
59 #endif
60 #ifdef TEST_KDDI
61 	from_encoding = mbfl_name2no_encoding("SJIS-win#KDDI");
62 #endif
63 #ifdef TEST_SOFTBANK
64 	from_encoding = mbfl_name2no_encoding("SJIS-win#SOFTBANK");
65 #endif
66 	to_encoding = mbfl_name2no_encoding("UTF-8");
67 
68 	convd = mbfl_buffer_converter_new(from_encoding, to_encoding, 0);
69 
70 	mbfl_memory_device_init(&dev, 0, 4096);
71 	mbfl_string_init_set(&string, no_language, from_encoding);
72 	mbfl_memory_device_realloc(&dev, dev.length + dev.allocsz, dev.allocsz);
73 
74 	strcpy(dev.buffer, str);
75 	dev.pos += strlen(str);
76 
77 	mbfl_memory_device_result(&dev, &string);
78 	mbfl_string_init_set(&result, no_language, to_encoding);
79 	ret = mbfl_buffer_converter_feed_result(convd, &string, &result);
80 
81 #if 0
82 	for (i = 0; i < result.len; i+= 2) {
83 		if (result.val[i] >= 0xD8 && result.val[i] < 0xE0) { // Surrogate pair
84 			int h = (result.val[i] & 0x07)<<8 | result.val[i+1];
85 			int l = (result.val[i+2] & 0x03)<<8 | result.val[i+3];
86 			int c = (h<<(2+8)) | l;
87 			printf("U+%x\n",c+0x10000);
88 			i+=2;
89 		} else {
90 			printf("U+%x\n",(result.val[i] << 8) | result.val[i+1]);
91 		}
92 	}
93 	hexdump(&result);
94 #endif
95 
96 
97 #if 1
98 	convd2 = mbfl_buffer_converter_new(to_encoding, from_encoding, 0);
99 	mbfl_memory_device_init(&dev2, 0, 4096);
100 	mbfl_string_init_set(&string, no_language, to_encoding);
101 	mbfl_memory_device_realloc(&dev2, dev2.length + dev2.allocsz, dev2.allocsz);
102 
103 	memcpy(dev2.buffer, result.val, result.len+1);
104 	dev2.pos += strlen(dev2.buffer);
105 
106 	mbfl_memory_device_result(&dev2, &string);
107 	mbfl_string_init_set(&result, no_language, from_encoding);
108 	ret = mbfl_buffer_converter_feed_result(convd2, &string, &result);
109 	hexdump(&result);
110 	mbfl_buffer_converter_delete(convd2);
111 #endif
112 	mbfl_string_clear(&result);
113 	mbfl_string_clear(&string);
114 
115 	mbfl_buffer_converter_delete(convd);
116 
117 
118 	return EXIT_SUCCESS;
119 }
120