Codebase list fcitx-cloudpinyin / a059f6c
Import upstream version 0.3.7+git20191103.1.a42ae67 Debian Janitor 1 year, 4 months ago
5 changed file(s) with 232 addition(s) and 46 deletion(s). Raw diff Collapse all Expand all
+0
-13
.gitignore less more
0 *~
1 build*/
2 .*
3 !.git*
4 .git/
5 *.tar.*
6 *.kdev4
7 *.kate-swp
8 *.orig
9 tags
10 astyle.sh
11 cscope.*
12 *.part
689689 }
690690
691691 #define LOADING_TIME_QUICK_THRESHOLD 300
692 #define DUP_PLACE_HOLDER "\xe2\x98\x81"
692 #define DUP_PLACE_HOLDER "\xe2\x98\xba"
693693
694694 void CloudPinyinFillCandidateWord(FcitxCloudPinyin* cloudpinyin,
695695 const char* pinyin)
77 #include <ctype.h>
88 #include "cloudpinyin.h"
99
10 char* GoogleParsePinyin(FcitxCloudPinyin* cloudpinyin, CurlQueue* queue)
11 {
12 char *start = NULL, *end = NULL;
13 if (!queue->str) {
14 return NULL;
15 }
16 if ((start = strstr(queue->str, "\",[\"")) != NULL)
17 {
18 start += strlen( "\",[\"");
10 static inline boolean ishex(char ch)
11 {
12 if ((ch >= '0' && ch <= '9') || (ch >='a' && ch <='f') || (ch >='A' && ch <='F'))
13 return true;
14 return false;
15 }
16
17 static inline unsigned char tohex(char ch)
18 {
19 if (ch >= '0' && ch <= '9')
20 return ch - '0';
21 if (ch >='a' && ch <='f')
22 return ch - 'a' + 10;
23 if (ch >='A' && ch <='F')
24 return ch - 'A' + 10;
25 return 0;
26 }
27
28 void SogouParseKey(FcitxCloudPinyin* cloudpinyin, CurlQueue* queue)
29 {
30 char* str = fcitx_utils_trim(queue->str);
31 const char* ime_patch_key = "ime_patch_key = \"";
32 size_t len = strlen(str);
33 if (len == SOGOU_KEY_LENGTH + strlen(ime_patch_key) + 1
34 && strncmp(str, ime_patch_key, strlen(ime_patch_key)) == 0
35 && str[len - 1] == '\"') {
36 sscanf(str,"ime_patch_key = \"%s\"", cloudpinyin->key);
37 cloudpinyin->initialized = true;
38 cloudpinyin->key[SOGOU_KEY_LENGTH] = '\0';
39 }
40
41 free(str);
42 }
43
44 char* MapSogouStringToHalf(const char* string)
45 {
46 const char* s = string;
47 const char* sn;
48 size_t len = strlen(string);
49 char* half = fcitx_utils_malloc0(sizeof(char) * (len + 1));
50 char* halfp = half;
51 int upperCount = 0;
52
53 while (*s) {
54 unsigned int chr = 0;
55
56 sn = fcitx_utf8_get_char(s, &chr);
57
58 /* from A to Z */
59 if ((chr >= 0xff21 && chr <= 0xff3a) || (chr >= 0xff41 && chr <= 0xff5a)) {
60 *halfp = (char) (chr & 0xff) + 0x20;
61 if (isupper(*halfp))
62 upperCount ++;
63 halfp ++;
64 }
65 else {
66 while(s < sn) {
67 *halfp = *s;
68 if (isupper(*halfp))
69 upperCount ++;
70 s++;
71 halfp++;
72 }
73 }
74
75 s = sn;
76 }
77 if (*half && isupper(*half) && upperCount == 1) {
78 *half = tolower(*half);
79 }
80 return half;
81 }
82
83 char* SogouParsePinyin(FcitxCloudPinyin* cloudpinyin, CurlQueue* queue)
84 {
85 char *start = NULL, *end = NULL;
86 if ((start = strchr(queue->str, '"')) != NULL && (end = strstr(queue->str, "%EF%BC%9A")) != NULL)
87 {
88 start ++;
89 if (start < end)
90 {
91 size_t length = end - start;
92 int conv_length;
93 char *unescapedstring = curl_easy_unescape(queue->curl, start, length, &conv_length);
94 char *realstring = MapSogouStringToHalf(unescapedstring);
95 curl_free(unescapedstring);
96 return realstring;
97 }
98 }
99 return NULL;
100 }
101
102 void QQParseKey(FcitxCloudPinyin* cloudpinyin, CurlQueue* queue)
103 {
104 char* str = fcitx_utils_trim(queue->str);
105 const char* ime_patch_key = "{\"key\":\"";
106 if (strncmp(str, ime_patch_key, strlen(ime_patch_key)) == 0)
107 {
108 if (sscanf(str,"{\"key\":\"%32s\",\"ret\":\"suc\"}", cloudpinyin->key) > 0)
109 {
110 cloudpinyin->initialized = true;
111 cloudpinyin->key[QQ_KEY_LENGTH] = '\0';
112 }
113 }
114
115 free(str);
116 }
117
118 char* QQParsePinyin(FcitxCloudPinyin* cloudpinyin, CurlQueue* queue)
119 {
120 char *start = NULL, *end = NULL;
121 if ((start = strstr(queue->str, "\"rs\":[\"")) != NULL)
122 {
123 start += strlen( "\"rs\":[\"");
19124 if ((end = strstr(start, "\"")) != NULL)
20125 {
21126 size_t length = end - start;
22127 char *realstring = fcitx_utils_malloc0(sizeof(char) * (length + 1));
23128 strncpy(realstring, start, length);
24129 realstring[length] = '\0';
25 if (fcitx_utf8_check_string(realstring)) {
26 return realstring;
27 } else {
28 free(realstring);
29 return NULL;
30 }
31 }
32 }
33 return NULL;
34 }
35
36 char* BaiduParsePinyin(FcitxCloudPinyin* cloudpinyin, CurlQueue* queue)
37 {
38 char *start = NULL, *end = NULL;
39 if (!queue->str) {
40 return NULL;
41 }
42 if ((start = strstr(queue->str, "[[\"")) != NULL)
43 {
44 start += strlen( "[[\"");
45 if ((end = strstr(start, "\",")) != NULL)
130 return realstring;
131 }
132 }
133 return NULL;
134 }
135
136 char* GoogleParsePinyin(FcitxCloudPinyin* cloudpinyin, CurlQueue* queue)
137 {
138 char *start = NULL, *end = NULL;
139 if ((start = strstr(queue->str, "\",[\"")) != NULL)
140 {
141 start += strlen( "\",[\"");
142 if ((end = strstr(start, "\"")) != NULL)
46143 {
47144 size_t length = end - start;
48145 char *realstring = fcitx_utils_malloc0(sizeof(char) * (length + 1));
49146 strncpy(realstring, start, length);
50147 realstring[length] = '\0';
51 if (fcitx_utf8_check_string(realstring)) {
148 return realstring;
149 }
150 }
151 return NULL;
152 }
153
154 char* BaiduParsePinyin(FcitxCloudPinyin* cloudpinyin, CurlQueue* queue)
155 {
156 char *start = NULL, *end = NULL;
157 static iconv_t conv = 0;
158 if (conv == 0)
159 conv = iconv_open("utf-8", "utf-16be");
160
161 if (conv == (iconv_t)(-1))
162 return NULL;
163 if ((start = strstr(queue->str, "[[[\"")) != NULL)
164 {
165 start += strlen( "[[[\"");
166 if ((end = strstr(start, "\",")) != NULL)
167 {
168 size_t length = end - start;
169 if (length % 6 != 0 || length == 0)
170 return NULL;
171
172 size_t i = 0, j = 0;
173 char* buf = fcitx_utils_malloc0((length / 6 + 1) * 2);
174 while (i < length)
175 {
176 if (start[i] == '\\' && start[i+1] == 'u')
177 {
178 if (ishex(start[i+2]) && ishex(start[i+3]) && ishex(start[i+4]) && ishex(start[i+5]))
179 {
180 buf[j++] = (tohex(start[i+2]) << 4) | tohex(start[i+3]);
181 buf[j++] = (tohex(start[i+4]) << 4) | tohex(start[i+5]);
182 }
183 else
184 break;
185 }
186
187 i += 6;
188 }
189
190 if (i != length)
191 {
192 free(buf);
193 return NULL;
194 }
195 buf[j++] = 0;
196 buf[j++] = 0;
197 size_t len = UTF8_MAX_LENGTH * (length / 6) * sizeof(char);
198 char* realstring = fcitx_utils_malloc0(UTF8_MAX_LENGTH * (length / 6) * sizeof(char));
199 IconvStr p = buf; char *pp = realstring;
200 iconv(conv, &p, &j, &pp, &len);
201
202 free(buf);
203 if (fcitx_utf8_check_string(realstring))
52204 return realstring;
53 } else {
205 else
206 {
54207 free(realstring);
55208 return NULL;
56209 }
0 include_directories(
1 ${PROJECT_SOURCE_DIR}/src
2 ${PROJECT_BINARY_DIR}
3 ${CMAKE_CURRENT_SOURCE_DIR}
4 ${CURL_INCLUDE_DIRS}
5 ${FCITX4_FCITX_INCLUDE_DIRS}
6 ${FCITX4_FCITX_CONFIG_INCLUDE_DIRS}
7 ${FCITX4_FCITX_UTILS_INCLUDE_DIRS}
8 ${LIBINTL_INCLUDE_DIR}
9 ${PTHREAD_INCLUDE_DIR}
10 )
11
12 link_directories(
13 ${CURL_LIBRARY_DIRS}
14 ${FCITX4_FCITX_UTILS_LIBRARY_DIRS}
15 )
16
17 add_executable(testhalf testhalf.c ../src/parse.c)
18
19 target_link_libraries( testhalf
20 ${PTHREAD_LIBRARIES}
21 ${FCITX4_FCITX_UTILS_LIBRARIES}
22 ${CURL_LIBRARIES}
23 ${LIBINTL_LIBRARIES}
24 )
25
26 add_test(testhalf testhalf)
0 #include "parse.h"
1
2 #include <assert.h>
3
4 int main(int argc, char* argv[])
5 {
6 char* result = MapSogouStringToHalf("ABCD");
7 printf("%s\n", result);
8 assert(strcmp(result, "ABCD") == 0);
9
10 free(result);
11
12 result = MapSogouStringToHalf("我a测b你CD的");
13 printf("%s\n", result);
14 assert(strcmp(result, "我a测b你CD的") == 0);
15
16 free(result);
17 return 0;
18 }