| 1 | /* Copyright(C) 2012-2015 Brazil |
| 2 | |
| 3 | This library is free software; you can redistribute it and/or |
| 4 | modify it under the terms of the GNU Lesser General Public |
| 5 | License as published by the Free Software Foundation; either |
| 6 | version 2.1 of the License, or (at your option) any later version. |
| 7 | |
| 8 | This library is distributed in the hope that it will be useful, |
| 9 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 10 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 11 | Lesser General Public License for more details. |
| 12 | |
| 13 | You should have received a copy of the GNU Lesser General Public |
| 14 | License along with this library; if not, write to the Free Software |
| 15 | Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
| 16 | */ |
| 17 | |
| 18 | #ifdef GRN_EMBEDDED |
| 19 | # define GRN_PLUGIN_FUNCTION_TAG query_expanders_tsv |
| 20 | #endif |
| 21 | |
| 22 | #ifdef HAVE_CONFIG_Hcxx . . $ `<built-in>:410:9`! |
| 23 | # include <config.h>cxx . . $ `<file>/build/config.h`/ |
| 24 | #endif /* HAVE_CONFIG_H */ |
| 25 | |
| 26 | #include <groonga/plugin.h>cxx . . $ `<file>/include/groonga/plugin.h`/ |
| 27 | |
| 28 | #include <stdlib.h> |
| 29 | #include <string.h> |
| 30 | |
| 31 | #ifdef WIN32 |
| 32 | # include <windows.h> |
| 33 | # include <share.h> |
| 34 | #endif /* WIN32 */ |
| 35 | |
| 36 | #define MAX_SYNONYM_BYTEScxx . . $ `plugins/query_expanders/tsv.c:36:9`! 4096 |
| 37 | |
| 38 | static grn_hashcxx . . $ grn_hash# *synonymscxx . . $ synonyms. = NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/16/include/stddef.h:419:9`!; |
| 39 | |
| 40 | #ifdef WIN32 |
| 41 | static char win32_synonyms_file[MAX_PATH] = ""; |
| 42 | const char * |
| 43 | get_system_synonyms_file(void) |
| 44 | { |
| 45 | if (win32_synonyms_file[0] == '\0') { |
| 46 | const char *base_dir; |
| 47 | const char *relative_path = GRN_QUERY_EXPANDER_TSV_RELATIVE_SYNONYMS_FILE; |
| 48 | size_t base_dir_length; |
| 49 | |
| 50 | base_dir = grn_plugin_windows_base_dir(); |
| 51 | base_dir_length = strlen(base_dir); |
| 52 | grn_strcpy(win32_synonyms_file, MAX_PATH, base_dir); |
| 53 | grn_strcat(win32_synonyms_file, MAX_PATH, "/"); |
| 54 | grn_strcat(win32_synonyms_file, MAX_PATH, relative_path); |
| 55 | } |
| 56 | return win32_synonyms_file; |
| 57 | } |
| 58 | |
| 59 | #else /* WIN32 */ |
| 60 | const char * |
| 61 | get_system_synonyms_filecxx . . $ get_system_synonyms_file(33a1f659e94feb42).(void) |
| 62 | { |
| 63 | return GRN_QUERY_EXPANDER_TSV_SYNONYMS_FILEcxx . . $ `build/config.h:45:9`!; |
| 64 | } |
| 65 | #endif /* WIN32 */ |
| 66 | |
| 67 | static boolcxx . . $ `<external>/13015445228558014418/stdbool.h:36:9`! |
| 68 | is_comment_markcxx . . $ is_comment_mark(b5fd09639b7a0261).(char character) |
| 69 | { |
| 70 | return character == '#'; |
| 71 | } |
| 72 | |
| 73 | static grn_encodingcxx . . $ grn_encoding# |
| 74 | detect_coding_partcxx . . $ detect_coding_part(907d9ca85ab089d9).(grn_ctxcxx . . $ grn_ctx# *ctx, const char *line, size_t line_length) |
| 75 | { |
| 76 | grn_encodingcxx . . $ grn_encoding# encoding = GRN_ENC_NONEcxx . . $ GRN_ENC_NONE.; |
| 77 | grn_objcxx . . $ grn_obj# null_terminated_line_buffer; |
| 78 | const char *c_line; |
| 79 | const char *coding_part_keyword = "coding: "; |
| 80 | const char *coding_part; |
| 81 | const char *encoding_name; |
| 82 | |
| 83 | GRN_TEXT_INITcxx . . $ `include/groonga/groonga.h:2750:9`!(&null_terminated_line_buffer, 0); |
| 84 | GRN_TEXT_PUTcxx . . $ `include/groonga/groonga.h:2773:9`!(ctx, &null_terminated_line_buffer, line, line_length); |
| 85 | GRN_TEXT_PUTCcxx . . $ `include/groonga/groonga.h:2775:9`!(ctx, &null_terminated_line_buffer, '\0'); |
| 86 | |
| 87 | c_line = GRN_TEXT_VALUEcxx . . $ `include/groonga/groonga.h:2785:9`!(&null_terminated_line_buffer); |
| 88 | coding_part = strstrcxx . . $ `<external>/13506128340496143423/string.h:380:11`!(c_line, coding_part_keyword); |
| 89 | if (coding_part) { |
| 90 | encoding_name = coding_part + strlencxx . . $ strlen(751346f8406fd082).(coding_part_keyword); |
| 91 | if (grn_strncasecmpcxx . . $ `include/groonga/portability.h:127:11`!(encoding_name, "utf-8", strlen("utf-8")) == 0 || |
| 92 | grn_strncasecmpcxx . . $ `include/groonga/portability.h:127:11`!(encoding_name, "utf8", strlen("utf8")) == 0) { |
| 93 | encoding = GRN_ENC_UTF8cxx . . $ GRN_ENC_UTF8.; |
| 94 | } else if (grn_strncasecmpcxx . . $ `include/groonga/portability.h:127:11`!(encoding_name, "sjis", strlen("sjis")) == 0 || |
| 95 | grn_strncasecmpcxx . . $ `include/groonga/portability.h:127:11`!(encoding_name, "Shift_JIS", strlen("Shift_JIS")) == 0) { |
| 96 | encoding = GRN_ENC_SJIScxx . . $ GRN_ENC_SJIS.; |
| 97 | } else if (grn_strncasecmpcxx . . $ `include/groonga/portability.h:127:11`!(encoding_name, "EUC-JP", strlen("EUC-JP")) == 0 || |
| 98 | grn_strncasecmpcxx . . $ `include/groonga/portability.h:127:11`!(encoding_name, "euc_jp", strlen("euc_jp")) == 0) { |
| 99 | encoding = GRN_ENC_EUC_JPcxx . . $ GRN_ENC_EUC_JP.; |
| 100 | } else if (grn_strncasecmpcxx . . $ `include/groonga/portability.h:127:11`!(encoding_name, "latin1", strlen("latin1")) == 0) { |
| 101 | encoding = GRN_ENC_LATIN1cxx . . $ GRN_ENC_LATIN1.; |
| 102 | } else if (grn_strncasecmpcxx . . $ `include/groonga/portability.h:127:11`!(encoding_name, "KOI8-R", strlen("KOI8-R")) == 0 || |
| 103 | grn_strncasecmpcxx . . $ `include/groonga/portability.h:127:11`!(encoding_name, "koi8r", strlen("koi8r")) == 0) { |
| 104 | encoding = GRN_ENC_KOI8Rcxx . . $ GRN_ENC_KOI8R.; |
| 105 | } |
| 106 | } else { |
| 107 | encoding = ctx->encodingcxx . . $ _grn_ctx#encoding.; |
| 108 | } |
| 109 | GRN_OBJ_FINcxx . . $ `include/groonga/groonga.h:958:9`!(ctx, &null_terminated_line_buffer); |
| 110 | |
| 111 | return encoding; |
| 112 | } |
| 113 | |
| 114 | static grn_encodingcxx . . $ grn_encoding# |
| 115 | guess_encodingcxx . . $ guess_encoding(3ab0dda613c26127).(grn_ctxcxx . . $ grn_ctx# *ctx, const char **line, size_t *line_length) |
| 116 | { |
| 117 | const char bom[] = {(char)0xef, (char)0xbb, (char)0xbf}; |
| 118 | size_t bom_length = sizeof(bom); |
| 119 | |
| 120 | if (*line_length >= bom_length && memcmpcxx . . $ memcmp(36f949851efa3055).(*line, bom, bom_length) == 0) { |
| 121 | *line += bom_length; |
| 122 | *line_length -= bom_length; |
| 123 | return GRN_ENC_UTF8cxx . . $ GRN_ENC_UTF8.; |
| 124 | } |
| 125 | |
| 126 | if (!is_comment_markcxx . . $ is_comment_mark(b5fd09639b7a0261).((*line)[0])) { |
| 127 | return ctx->encodingcxx . . $ _grn_ctx#encoding.; |
| 128 | } |
| 129 | |
| 130 | return detect_coding_partcxx . . $ detect_coding_part(907d9ca85ab089d9).(ctx, (*line) + 1, (*line_length) - 1); |
| 131 | } |
| 132 | |
| 133 | static void |
| 134 | parse_synonyms_file_linecxx . . $ parse_synonyms_file_line(1467c2c817310d3).(grn_ctxcxx . . $ grn_ctx# *ctx, const char *line, size_t line_length, |
| 135 | grn_objcxx . . $ grn_obj# *key, grn_objcxx . . $ grn_obj# *value) |
| 136 | { |
| 137 | size_t i = 0; |
| 138 | |
| 139 | if (is_comment_markcxx . . $ is_comment_mark(b5fd09639b7a0261).(line[i])) { |
| 140 | return; |
| 141 | } |
| 142 | |
| 143 | while (i < line_length) { |
| 144 | char character = line[i]; |
| 145 | i++; |
| 146 | if (character == '\t') { |
| 147 | break; |
| 148 | } |
| 149 | GRN_TEXT_PUTCcxx . . $ `include/groonga/groonga.h:2775:9`!(ctx, key, character); |
| 150 | } |
| 151 | |
| 152 | if (i == line_length) { |
| 153 | return; |
| 154 | } |
| 155 | |
| 156 | GRN_TEXT_PUTScxx . . $ `include/groonga/groonga.h:2781:9`!(ctx, value, "(("); |
| 157 | while (i < line_length) { |
| 158 | char character = line[i]; |
| 159 | i++; |
| 160 | if (character == '\t') { |
| 161 | GRN_TEXT_PUTScxx . . $ `include/groonga/groonga.h:2781:9`!(ctx, value, ") OR ("); |
| 162 | } else { |
| 163 | GRN_TEXT_PUTCcxx . . $ `include/groonga/groonga.h:2775:9`!(ctx, value, character); |
| 164 | } |
| 165 | } |
| 166 | GRN_TEXT_PUTScxx . . $ `include/groonga/groonga.h:2781:9`!(ctx, value, "))"); |
| 167 | |
| 168 | { |
| 169 | grn_idcxx . . $ grn_id# id; |
| 170 | void *value_location = NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/16/include/stddef.h:419:9`!; |
| 171 | |
| 172 | id = grn_hash_addcxx . . $ grn_hash_add(251a256fc515235e).(ctx, |
| 173 | synonymscxx . . $ synonyms., |
| 174 | GRN_TEXT_VALUEcxx . . $ `include/groonga/groonga.h:2785:9`!(key), |
| 175 | (unsigned int)GRN_TEXT_LENcxx . . $ `include/groonga/groonga.h:2786:9`!(key), |
| 176 | &value_location, |
| 177 | NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/16/include/stddef.h:419:9`!); |
| 178 | if (id == GRN_ID_NILcxx . . $ `include/groonga/groonga.h:55:9`!) { |
| 179 | GRN_PLUGIN_LOGcxx . . $ `include/groonga/plugin.h:103:9`!(ctx, GRN_LOG_WARNING, |
| 180 | "[plugin][query-expander][tsv] " |
| 181 | "failed to register key: <%.*s>", |
| 182 | (int)GRN_TEXT_LENcxx . . $ `include/groonga/groonga.h:2786:9`!(key), GRN_TEXT_VALUEcxx . . $ `include/groonga/groonga.h:2785:9`!(key)); |
| 183 | return; |
| 184 | } |
| 185 | |
| 186 | if (GRN_TEXT_LENcxx . . $ `include/groonga/groonga.h:2786:9`!(value) <= MAX_SYNONYM_BYTEScxx . . $ `plugins/query_expanders/tsv.c:36:9`! - 1) { |
| 187 | GRN_TEXT_PUTCcxx . . $ `include/groonga/groonga.h:2775:9`!(ctx, value, '\0'); |
| 188 | } else { |
| 189 | grn_bulk_truncatecxx . . $ grn_bulk_truncate(f9a66385f261f61d).(ctx, value, MAX_SYNONYM_BYTEScxx . . $ `plugins/query_expanders/tsv.c:36:9`! - 1); |
| 190 | GRN_TEXT_PUTCcxx . . $ `include/groonga/groonga.h:2775:9`!(ctx, value, '\0'); |
| 191 | } |
| 192 | grn_memcpycxx . . $ `include/groonga/portability.h:32:13`!(value_location, GRN_TEXT_VALUEcxx . . $ `include/groonga/groonga.h:2785:9`!(value), GRN_TEXT_LENcxx . . $ `include/groonga/groonga.h:2786:9`!(value)); |
| 193 | } |
| 194 | } |
| 195 | |
| 196 | static void |
| 197 | load_synonymscxx . . $ load_synonyms(b578cd6e53d5fe91).(grn_ctxcxx . . $ grn_ctx# *ctx) |
| 198 | { |
| 199 | static char path_env[GRN_ENV_BUFFER_SIZEcxx . . $ `include/groonga/portability.h:42:9`!]; |
| 200 | const char *path; |
| 201 | grn_file_readercxx . . $ grn_file_reader# *file_reader; |
| 202 | int number_of_lines; |
| 203 | grn_encodingcxx . . $ grn_encoding# encoding; |
| 204 | grn_objcxx . . $ grn_obj# line, key, value; |
| 205 | |
| 206 | grn_getenvcxx . . $ `include/groonga/portability.h:58:11`!("GRN_QUERY_EXPANDER_TSV_SYNONYMS_FILE", |
| 207 | path_env, |
| 208 | GRN_ENV_BUFFER_SIZEcxx . . $ `include/groonga/portability.h:42:9`!); |
| 209 | if (path_env[0]) { |
| 210 | path = path_env; |
| 211 | } else { |
| 212 | path = get_system_synonyms_filecxx . . $ get_system_synonyms_file(33a1f659e94feb42).(); |
| 213 | } |
| 214 | file_reader = grn_file_reader_opencxx . . $ grn_file_reader_open(d6cc0d8869c2b747).(ctx, path); |
| 215 | if (!file_reader) { |
| 216 | GRN_LOGcxx . . $ `include/groonga/groonga.h:2493:9`!(ctx, GRN_LOG_WARNING, |
| 217 | "[plugin][query-expander][tsv] " |
| 218 | "synonyms file doesn't exist: <%s>", |
| 219 | path); |
| 220 | return; |
| 221 | } |
| 222 | |
| 223 | GRN_TEXT_INITcxx . . $ `include/groonga/groonga.h:2750:9`!(&line, 0); |
| 224 | GRN_TEXT_INITcxx . . $ `include/groonga/groonga.h:2750:9`!(&key, 0); |
| 225 | GRN_TEXT_INITcxx . . $ `include/groonga/groonga.h:2750:9`!(&value, 0); |
| 226 | grn_bulk_reservecxx . . $ grn_bulk_reserve(f9a66385f261f61d).(ctx, &value, MAX_SYNONYM_BYTEScxx . . $ `plugins/query_expanders/tsv.c:36:9`!); |
| 227 | number_of_lines = 0; |
| 228 | while (grn_file_reader_read_linecxx . . $ grn_file_reader_read_line(d4c727e0d06e40af).(ctx, file_reader, &line) == GRN_SUCCESScxx . . $ GRN_SUCCESS.) { |
| 229 | const char *line_value = GRN_TEXT_VALUEcxx . . $ `include/groonga/groonga.h:2785:9`!(&line); |
| 230 | size_t line_length = GRN_TEXT_LENcxx . . $ `include/groonga/groonga.h:2786:9`!(&line); |
| 231 | |
| 232 | if (line_length > 0 && line_value[line_length - 1] == '\n') { |
| 233 | if (line_length > 1 && line_value[line_length - 2] == '\r') { |
| 234 | line_length -= 2; |
| 235 | } else { |
| 236 | line_length -= 1; |
| 237 | } |
| 238 | } |
| 239 | number_of_lines++; |
| 240 | if (number_of_lines == 1) { |
| 241 | encoding = guess_encodingcxx . . $ guess_encoding(3ab0dda613c26127).(ctx, &line_value, &line_length); |
| 242 | } |
| 243 | GRN_BULK_REWINDcxx . . $ `include/groonga/groonga.h:2599:9`!(&key); |
| 244 | GRN_BULK_REWINDcxx . . $ `include/groonga/groonga.h:2599:9`!(&value); |
| 245 | parse_synonyms_file_linecxx . . $ parse_synonyms_file_line(1467c2c817310d3).(ctx, line_value, line_length, &key, &value); |
| 246 | GRN_BULK_REWINDcxx . . $ `include/groonga/groonga.h:2599:9`!(&line); |
| 247 | } |
| 248 | GRN_OBJ_FINcxx . . $ `include/groonga/groonga.h:958:9`!(ctx, &line); |
| 249 | GRN_OBJ_FINcxx . . $ `include/groonga/groonga.h:958:9`!(ctx, &key); |
| 250 | GRN_OBJ_FINcxx . . $ `include/groonga/groonga.h:958:9`!(ctx, &value); |
| 251 | |
| 252 | grn_file_reader_closecxx . . $ grn_file_reader_close(43c8c29f514ba60e).(ctx, file_reader); |
| 253 | } |
| 254 | |
| 255 | static grn_objcxx . . $ grn_obj# * |
| 256 | func_query_expander_tsvcxx . . $ func_query_expander_tsv(fd082858389c8bb0).(grn_ctxcxx . . $ grn_ctx# *ctx, int nargs, grn_objcxx . . $ grn_obj# **args, |
| 257 | grn_user_datacxx . . $ grn_user_data# *user_data) |
| 258 | { |
| 259 | grn_rccxx . . $ grn_rc# rc = GRN_END_OF_DATAcxx . . $ GRN_END_OF_DATA.; |
| 260 | grn_idcxx . . $ grn_id# id; |
| 261 | grn_objcxx . . $ grn_obj# *term, *expanded_term; |
| 262 | void *value; |
| 263 | grn_objcxx . . $ grn_obj# *rc_object; |
| 264 | |
| 265 | term = args[0]; |
| 266 | expanded_term = args[1]; |
| 267 | id = grn_hash_getcxx . . $ grn_hash_get(4e868e5367e71b67).(ctx, |
| 268 | synonymscxx . . $ synonyms., |
| 269 | GRN_TEXT_VALUEcxx . . $ `include/groonga/groonga.h:2785:9`!(term), |
| 270 | (unsigned int)GRN_TEXT_LENcxx . . $ `include/groonga/groonga.h:2786:9`!(term), |
| 271 | &value); |
| 272 | if (id != GRN_ID_NILcxx . . $ `include/groonga/groonga.h:55:9`!) { |
| 273 | const char *query = value; |
| 274 | GRN_TEXT_PUTScxx . . $ `include/groonga/groonga.h:2781:9`!(ctx, expanded_term, query); |
| 275 | rc = GRN_SUCCESScxx . . $ GRN_SUCCESS.; |
| 276 | } |
| 277 | |
| 278 | rc_object = grn_plugin_proc_alloccxx . . $ grn_plugin_proc_alloc(acb267eca6702fc).(ctx, user_data, GRN_DB_INT32cxx . . $ GRN_DB_INT32., 0); |
| 279 | if (rc_object) { |
| 280 | GRN_INT32_SETcxx . . $ `include/groonga/groonga.h:2904:9`!(ctx, rc_object, rc); |
| 281 | } |
| 282 | |
| 283 | return rc_object; |
| 284 | } |
| 285 | |
| 286 | grn_rccxx . . $ grn_rc# |
| 287 | GRN_PLUGIN_INITcxx . . $ `include/groonga/plugin.h:43:9`!(grn_ctxcxx . . $ grn_ctx# *ctx) |
| 288 | { |
| 289 | if (!synonymscxx . . $ synonyms.) { |
| 290 | synonymscxx . . $ synonyms. = grn_hash_createcxx . . $ grn_hash_create(db1bba7e0d64660).(ctx, NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/16/include/stddef.h:419:9`!, |
| 291 | GRN_TABLE_MAX_KEY_SIZEcxx . . $ `include/groonga/table.h:26:9`!, |
| 292 | MAX_SYNONYM_BYTEScxx . . $ `plugins/query_expanders/tsv.c:36:9`!, |
| 293 | GRN_OBJ_TABLE_HASH_KEYcxx . . $ `include/groonga/groonga.h:688:9`! | GRN_OBJ_KEY_VAR_SIZEcxx . . $ `include/groonga/groonga.h:820:9`!); |
| 294 | if (!synonymscxx . . $ synonyms.) { |
| 295 | return ctx->rccxx . . $ _grn_ctx#rc.; |
| 296 | } |
| 297 | load_synonymscxx . . $ load_synonyms(b578cd6e53d5fe91).(ctx); |
| 298 | } |
| 299 | return ctx->rccxx . . $ _grn_ctx#rc.; |
| 300 | } |
| 301 | |
| 302 | grn_rccxx . . $ grn_rc# |
| 303 | GRN_PLUGIN_REGISTERcxx . . $ `include/groonga/plugin.h:44:9`!(grn_ctxcxx . . $ grn_ctx# *ctx) |
| 304 | { |
| 305 | grn_proc_createcxx . . $ grn_proc_create(195df001e5324f57).(ctx, "QueryExpanderTSV", strlencxx . . $ strlen(751346f8406fd082).("QueryExpanderTSV"), |
| 306 | GRN_PROC_FUNCTIONcxx . . $ GRN_PROC_FUNCTION., |
| 307 | func_query_expander_tsvcxx . . $ func_query_expander_tsv(fd082858389c8bb0)., NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/16/include/stddef.h:419:9`!, NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/16/include/stddef.h:419:9`!, |
| 308 | 0, NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/16/include/stddef.h:419:9`!); |
| 309 | return GRN_SUCCESScxx . . $ GRN_SUCCESS.; |
| 310 | } |
| 311 | |
| 312 | grn_rccxx . . $ grn_rc# |
| 313 | GRN_PLUGIN_FINcxx . . $ `include/groonga/plugin.h:45:9`!(grn_ctxcxx . . $ grn_ctx# *ctx) |
| 314 | { |
| 315 | if (synonymscxx . . $ synonyms.) { |
| 316 | grn_hash_closecxx . . $ grn_hash_close(c005d027d163fb0b).(ctx, synonymscxx . . $ synonyms.); |
| 317 | synonymscxx . . $ synonyms. = NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/16/include/stddef.h:419:9`!; |
| 318 | } |
| 319 | return GRN_SUCCESScxx . . $ GRN_SUCCESS.; |
| 320 | } |