| 1 | /* |
| 2 | Copyright(C) 2015 Brazil |
| 3 | Copyright(C) 2019-2022 Sutou Kouhei <[email protected]> |
| 4 | |
| 5 | This library is free software; you can redistribute it and/or |
| 6 | modify it under the terms of the GNU Lesser General Public |
| 7 | License as published by the Free Software Foundation; either |
| 8 | version 2.1 of the License, or (at your option) any later version. |
| 9 | |
| 10 | This library is distributed in the hope that it will be useful, |
| 11 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 13 | Lesser General Public License for more details. |
| 14 | |
| 15 | You should have received a copy of the GNU Lesser General Public |
| 16 | License along with this library; if not, write to the Free Software |
| 17 | Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
| 18 | */ |
| 19 | |
| 20 | #include "grn_ctx.h"cxx . . $ `<file>/lib/grn_ctx.h`/ |
| 21 | #include "grn_str.h"cxx . . $ `<file>/lib/grn_str.h`/ |
| 22 | |
| 23 | #include <errno.h> |
| 24 | #include <stdio.h> |
| 25 | #include <string.h> |
| 26 | |
| 27 | #ifdef WIN32 |
| 28 | # include <share.h> |
| 29 | #else |
| 30 | # include <sys/select.h> |
| 31 | #endif /* WIN32 */ |
| 32 | |
| 33 | struct _grn_file_readercxx . . $ _grn_file_reader# { |
| 34 | FILEcxx . . $ FILE# *filecxx . . $ _grn_file_reader#file.; |
| 35 | boolcxx . . $ `<external>/13015445228558014418/stdbool.h:36:9`! file_need_closecxx . . $ _grn_file_reader#file_need_close.; |
| 36 | }; |
| 37 | |
| 38 | grn_file_readercxx . . $ grn_file_reader# * |
| 39 | grn_file_reader_opencxx . . $ grn_file_reader_open(d6cc0d8869c2b747).(grn_ctxcxx . . $ grn_ctx# *ctx, const char *path) |
| 40 | { |
| 41 | grn_file_readercxx . . $ grn_file_reader# *reader; |
| 42 | FILEcxx . . $ FILE# *file; |
| 43 | boolcxx . . $ `<external>/13015445228558014418/stdbool.h:36:9`! file_need_close; |
| 44 | |
| 45 | GRN_API_ENTERcxx . . $ `lib/grn_ctx.h:41:9`!; |
| 46 | |
| 47 | if (!path) { |
| 48 | ERRcxx . . $ `lib/grn_error.h:156:9`!(GRN_INVALID_ARGUMENT, "[file-reader][open] path must not NULL"); |
| 49 | GRN_API_RETURNcxx . . $ `lib/grn_ctx.h:56:9`!(NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/16/include/stddef.h:419:9`!); |
| 50 | } |
| 51 | |
| 52 | if (strcmpcxx . . $ strcmp(5ae29d726ae57f88).(path, "-") == 0) { |
| 53 | file = stdincxx . . $ `<external>/232824719582829767/stdio.h:157:9`!; |
| 54 | file_need_close = falsecxx . . $ `<external>/13015445228558014418/stdbool.h:38:9`!; |
| 55 | } else { |
| 56 | file = grn_fopencxx . . $ `include/groonga/portability.h:76:11`!(path, "r"); |
| 57 | if (!file) { |
| 58 | SERRcxx . . $ `lib/grn_error.h:411:11`!("[file-reader][open] failed to open path: <%s>", path); |
| 59 | GRN_API_RETURNcxx . . $ `lib/grn_ctx.h:56:9`!(NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/16/include/stddef.h:419:9`!); |
| 60 | } |
| 61 | file_need_close = truecxx . . $ `<external>/13015445228558014418/stdbool.h:37:9`!; |
| 62 | } |
| 63 | |
| 64 | reader = GRN_CALLOCcxx . . $ `lib/grn_alloc.h:42:9`!(sizeof(grn_file_reader)); |
| 65 | if (!reader) { |
| 66 | if (file_need_close) { |
| 67 | fclosecxx . . $ fclose(89bc1efcdac0ca22).(file); |
| 68 | } |
| 69 | ERRcxx . . $ `lib/grn_error.h:156:9`!(GRN_NO_MEMORY_AVAILABLE, |
| 70 | "[file-reader][open] failed to allocate reader: <%s>", path); |
| 71 | GRN_API_RETURNcxx . . $ `lib/grn_ctx.h:56:9`!(NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/16/include/stddef.h:419:9`!); |
| 72 | } |
| 73 | reader->filecxx . . $ _grn_file_reader#file. = file; |
| 74 | reader->file_need_closecxx . . $ _grn_file_reader#file_need_close. = file_need_close; |
| 75 | |
| 76 | GRN_API_RETURNcxx . . $ `lib/grn_ctx.h:56:9`!(reader); |
| 77 | } |
| 78 | |
| 79 | void |
| 80 | grn_file_reader_closecxx . . $ grn_file_reader_close(43c8c29f514ba60e).(grn_ctxcxx . . $ grn_ctx# *ctx, grn_file_readercxx . . $ grn_file_reader# *reader) |
| 81 | { |
| 82 | if (!reader) { |
| 83 | return; |
| 84 | } |
| 85 | |
| 86 | if (reader->file_need_closecxx . . $ _grn_file_reader#file_need_close.) { |
| 87 | if (fclosecxx . . $ fclose(89bc1efcdac0ca22).(reader->filecxx . . $ _grn_file_reader#file.) != 0) { |
| 88 | GRN_LOGcxx . . $ `include/groonga/groonga.h:2493:9`!(ctx, GRN_LOG_ERROR, |
| 89 | "[file-reader][close] failed to close: <%s>", |
| 90 | grn_strerror(errnocxx . . $ `<external>/17633486526465735210/errno.h:38:10`!)); |
| 91 | } |
| 92 | } |
| 93 | |
| 94 | GRN_FREEcxx . . $ `lib/grn_alloc.h:49:9`!(reader); |
| 95 | } |
| 96 | |
| 97 | grn_rccxx . . $ grn_rc# |
| 98 | grn_file_reader_read_linecxx . . $ grn_file_reader_read_line(d4c727e0d06e40af).(grn_ctxcxx . . $ grn_ctx# *ctx, |
| 99 | grn_file_readercxx . . $ grn_file_reader# *reader, |
| 100 | grn_objcxx . . $ grn_obj# *buffer) |
| 101 | { |
| 102 | grn_rccxx . . $ grn_rc# rc = GRN_END_OF_DATAcxx . . $ GRN_END_OF_DATA.; |
| 103 | |
| 104 | for (;;) { |
| 105 | size_t len; |
| 106 | |
| 107 | #define BUFFER_SIZEcxx . . $ `lib/file_reader.c:107:9`! 4096 |
| 108 | grn_rccxx . . $ grn_rc# reserve_rc = grn_bulk_reservecxx . . $ grn_bulk_reserve(f9a66385f261f61d).(ctx, buffer, BUFFER_SIZEcxx . . $ `lib/file_reader.c:107:9`!); |
| 109 | if (reserve_rc != GRN_SUCCESScxx . . $ GRN_SUCCESS.) { |
| 110 | rc = reserve_rc; |
| 111 | break; |
| 112 | } |
| 113 | if (!fgetscxx . . $ fgets(efb0aaba7ac866ae).(GRN_BULK_CURRcxx . . $ `include/groonga/groonga.h:2650:9`!(buffer), BUFFER_SIZEcxx . . $ `lib/file_reader.c:107:9`!, reader->filecxx . . $ _grn_file_reader#file.)) { |
| 114 | if (errnocxx . . $ `<external>/17633486526465735210/errno.h:38:10`! == EAGAINcxx . . $ `<external>/8987895939074593727/errno-base.h:15:9`! || errnocxx . . $ `<external>/17633486526465735210/errno.h:38:10`! == EWOULDBLOCKcxx . . $ `<external>/17922322188510340118/errno.h:22:9`!) { |
| 115 | int fd = grn_filenocxx . . $ `include/groonga/portability.h:185:11`!(reader->file); |
| 116 | int n_fds = 1; |
| 117 | fd_setcxx . . $ fd_set# read_fds; |
| 118 | FD_ZEROcxx . . $ `<external>/946878812840035423/select.h:88:9`!(&read_fds); |
| 119 | FD_SETcxx . . $ `<external>/946878812840035423/select.h:85:9`!(fd, &read_fds); |
| 120 | int n_ready_fds = selectcxx . . $ select(6d24c7514c8a5b88).(n_fds, &read_fds, 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`!, NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/16/include/stddef.h:419:9`!); |
| 121 | if (n_ready_fds != 1) { |
| 122 | break; |
| 123 | } |
| 124 | errnocxx . . $ `<external>/17633486526465735210/errno.h:38:10`! = 0; |
| 125 | continue; |
| 126 | } |
| 127 | break; |
| 128 | } |
| 129 | #undef BUFFER_SIZEcxx . . $ `lib/file_reader.c:107:9`! |
| 130 | |
| 131 | if (!(len = strlencxx . . $ strlen(751346f8406fd082).(GRN_BULK_CURRcxx . . $ `include/groonga/groonga.h:2650:9`!(buffer)))) { break; } |
| 132 | GRN_BULK_INCR_LENcxx . . $ `include/groonga/groonga.h:2627:9`!(buffer, len); |
| 133 | rc = GRN_SUCCESScxx . . $ GRN_SUCCESS.; |
| 134 | if (GRN_BULK_CURRcxx . . $ `include/groonga/groonga.h:2650:9`!(buffer)[-1] == '\n') { break; } |
| 135 | } |
| 136 | |
| 137 | return rc; |
| 138 | } |