| 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" |
| 21 | #include "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_reader { |
| 34 | FILE *file; |
| 35 | bool file_need_close; |
| 36 | }; |
| 37 | |
| 38 | grn_file_reader * |
| 39 | grn_file_reader_open(grn_ctx *ctx, const char *path) |
| 40 | { |
| 41 | grn_file_reader *reader; |
| 42 | FILE *file; |
| 43 | bool file_need_close; |
| 44 | |
| 45 | GRN_API_ENTER; |
| 46 | |
| 47 | if (!path) { |
| 48 | ERR(GRN_INVALID_ARGUMENT, "[file-reader][open] path must not NULL"); |
| 49 | GRN_API_RETURN(NULL); |
| 50 | } |
| 51 | |
| 52 | if (strcmp(path, "-") == 0) { |
| 53 | file = stdin; |
| 54 | file_need_close = false; |
| 55 | } else { |
| 56 | file = grn_fopen(path, "r"); |
| 57 | if (!file) { |
| 58 | SERR("[file-reader][open] failed to open path: <%s>", path); |
| 59 | GRN_API_RETURN(NULL); |
| 60 | } |
| 61 | file_need_close = true; |
| 62 | } |
| 63 | |
| 64 | reader = GRN_CALLOC(sizeof(grn_file_reader)); |
| 65 | if (!reader) { |
| 66 | if (file_need_close) { |
| 67 | fclose(file); |
| 68 | } |
| 69 | ERR(GRN_NO_MEMORY_AVAILABLE, |
| 70 | "[file-reader][open] failed to allocate reader: <%s>", path); |
| 71 | GRN_API_RETURN(NULL); |
| 72 | } |
| 73 | reader->file = file; |
| 74 | reader->file_need_close = file_need_close; |
| 75 | |
| 76 | GRN_API_RETURN(reader); |
| 77 | } |
| 78 | |
| 79 | void |
| 80 | grn_file_reader_close(grn_ctx *ctx, grn_file_reader *reader) |
| 81 | { |
| 82 | if (!reader) { |
| 83 | return; |
| 84 | } |
| 85 | |
| 86 | if (reader->file_need_close) { |
| 87 | if (fclose(reader->file) != 0) { |
| 88 | GRN_LOG(ctx, GRN_LOG_ERROR, |
| 89 | "[file-reader][close] failed to close: <%s>", |
| 90 | grn_strerror(errno)); |
| 91 | } |
| 92 | } |
| 93 | |
| 94 | GRN_FREE(reader); |
| 95 | } |
| 96 | |
| 97 | grn_rc |
| 98 | grn_file_reader_read_line(grn_ctx *ctx, |
| 99 | grn_file_reader *reader, |
| 100 | grn_obj *buffer) |
| 101 | { |
| 102 | grn_rc rc = GRN_END_OF_DATA; |
| 103 | |
| 104 | for (;;) { |
| 105 | size_t len; |
| 106 | |
| 107 | #define BUFFER_SIZE 4096 |
| 108 | grn_rc reserve_rc = grn_bulk_reserve(ctx, buffer, BUFFER_SIZE); |
| 109 | if (reserve_rc != GRN_SUCCESS) { |
| 110 | rc = reserve_rc; |
| 111 | break; |
| 112 | } |
| 113 | if (!fgets(GRN_BULK_CURR(buffer), BUFFER_SIZE, reader->file)) { |
| 114 | if (errno == EAGAIN || errno == EWOULDBLOCK) { |
| 115 | int fd = grn_fileno(reader->file); |
| 116 | int n_fds = 1; |
| 117 | fd_set read_fds; |
| 118 | FD_ZERO(&read_fds); |
| 119 | FD_SET(fd, &read_fds); |
| 120 | int n_ready_fds = select(n_fds, &read_fds, NULL, NULL, NULL); |
| 121 | if (n_ready_fds != 1) { |
| 122 | break; |
| 123 | } |
| 124 | errno = 0; |
| 125 | continue; |
| 126 | } |
| 127 | break; |
| 128 | } |
| 129 | #undef BUFFER_SIZE |
| 130 | |
| 131 | if (!(len = strlen(GRN_BULK_CURR(buffer)))) { break; } |
| 132 | GRN_BULK_INCR_LEN(buffer, len); |
| 133 | rc = GRN_SUCCESS; |
| 134 | if (GRN_BULK_CURR(buffer)[-1] == '\n') { break; } |
| 135 | } |
| 136 | |
| 137 | return rc; |
| 138 | } |