zsv 1.4.4-aarch64-linux-gnu

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (77) hide show
  1. checksums.yaml +7 -0
  2. data/CHANGELOG.md +187 -0
  3. data/LICENSE +21 -0
  4. data/README.md +203 -0
  5. data/ext/zsv/ZSV_UPSTREAM +7 -0
  6. data/ext/zsv/common.h +22 -0
  7. data/ext/zsv/extconf.rb +102 -0
  8. data/ext/zsv/options.c +126 -0
  9. data/ext/zsv/options.h +28 -0
  10. data/ext/zsv/options_internal.h +8 -0
  11. data/ext/zsv/parser.c +503 -0
  12. data/ext/zsv/parser.h +75 -0
  13. data/ext/zsv/row.c +169 -0
  14. data/ext/zsv/row.h +45 -0
  15. data/ext/zsv/upstream.rb +55 -0
  16. data/ext/zsv/vendor/zsv/LICENSE +21 -0
  17. data/ext/zsv/vendor/zsv/VENDORED_FROM +2 -0
  18. data/ext/zsv/vendor/zsv/configure +1212 -0
  19. data/ext/zsv/vendor/zsv/include/zsv/api.h +375 -0
  20. data/ext/zsv/vendor/zsv/include/zsv/common.h +397 -0
  21. data/ext/zsv/vendor/zsv/include/zsv/ext/implementation.h +62 -0
  22. data/ext/zsv/vendor/zsv/include/zsv/ext/implementation_private.h +113 -0
  23. data/ext/zsv/vendor/zsv/include/zsv/ext/sheet.h +80 -0
  24. data/ext/zsv/vendor/zsv/include/zsv/ext.h +333 -0
  25. data/ext/zsv/vendor/zsv/include/zsv/utils/appname.h +51 -0
  26. data/ext/zsv/vendor/zsv/include/zsv/utils/arg.h +139 -0
  27. data/ext/zsv/vendor/zsv/include/zsv/utils/cache.h +49 -0
  28. data/ext/zsv/vendor/zsv/include/zsv/utils/clock.h +36 -0
  29. data/ext/zsv/vendor/zsv/include/zsv/utils/compiler.h +69 -0
  30. data/ext/zsv/vendor/zsv/include/zsv/utils/db.h +19 -0
  31. data/ext/zsv/vendor/zsv/include/zsv/utils/dirs.h +149 -0
  32. data/ext/zsv/vendor/zsv/include/zsv/utils/dl.h +22 -0
  33. data/ext/zsv/vendor/zsv/include/zsv/utils/emcc/fs_api.h +28 -0
  34. data/ext/zsv/vendor/zsv/include/zsv/utils/err.h +22 -0
  35. data/ext/zsv/vendor/zsv/include/zsv/utils/file-mem.h +18 -0
  36. data/ext/zsv/vendor/zsv/include/zsv/utils/file.h +125 -0
  37. data/ext/zsv/vendor/zsv/include/zsv/utils/jq.h +65 -0
  38. data/ext/zsv/vendor/zsv/include/zsv/utils/json.h +19 -0
  39. data/ext/zsv/vendor/zsv/include/zsv/utils/mem.h +19 -0
  40. data/ext/zsv/vendor/zsv/include/zsv/utils/memmem.h +13 -0
  41. data/ext/zsv/vendor/zsv/include/zsv/utils/os.h +145 -0
  42. data/ext/zsv/vendor/zsv/include/zsv/utils/output.h +34 -0
  43. data/ext/zsv/vendor/zsv/include/zsv/utils/overwrite.h +71 -0
  44. data/ext/zsv/vendor/zsv/include/zsv/utils/overwrite_writer.h +53 -0
  45. data/ext/zsv/vendor/zsv/include/zsv/utils/prop.h +119 -0
  46. data/ext/zsv/vendor/zsv/include/zsv/utils/rng.h +35 -0
  47. data/ext/zsv/vendor/zsv/include/zsv/utils/signal.h +25 -0
  48. data/ext/zsv/vendor/zsv/include/zsv/utils/sql.h +13 -0
  49. data/ext/zsv/vendor/zsv/include/zsv/utils/string.h +173 -0
  50. data/ext/zsv/vendor/zsv/include/zsv/utils/utf8.h +41 -0
  51. data/ext/zsv/vendor/zsv/include/zsv/utils/win/dl.h +25 -0
  52. data/ext/zsv/vendor/zsv/include/zsv/utils/writer.h +109 -0
  53. data/ext/zsv/vendor/zsv/include/zsv/zsv_export.h +33 -0
  54. data/ext/zsv/vendor/zsv/include/zsv.h.in +18 -0
  55. data/ext/zsv/vendor/zsv/src/Makefile +238 -0
  56. data/ext/zsv/vendor/zsv/src/vector_delim.c +60 -0
  57. data/ext/zsv/vendor/zsv/src/zsv.c +622 -0
  58. data/ext/zsv/vendor/zsv/src/zsv_internal.c +789 -0
  59. data/ext/zsv/vendor/zsv/src/zsv_scan_delim.c +285 -0
  60. data/ext/zsv/vendor/zsv/src/zsv_scan_delim_fast.c +741 -0
  61. data/ext/zsv/vendor/zsv/src/zsv_scan_fixed.c +88 -0
  62. data/ext/zsv/vendor/zsv/src/zsv_scan_simd_avx2.h +89 -0
  63. data/ext/zsv/vendor/zsv/src/zsv_scan_simd_neon.h +80 -0
  64. data/ext/zsv/vendor/zsv/src/zsv_scan_simd_sse2.h +93 -0
  65. data/ext/zsv/vendor/zsv/src/zsv_scan_simd_wasm.h +79 -0
  66. data/ext/zsv/vendor/zsv/src/zsv_strencode.c +84 -0
  67. data/ext/zsv/zsv_ext.c +417 -0
  68. data/lib/zsv/3.3/zsv.so +0 -0
  69. data/lib/zsv/3.4/zsv.so +0 -0
  70. data/lib/zsv/4.0/zsv.so +0 -0
  71. data/lib/zsv/errors.rb +32 -0
  72. data/lib/zsv/input.rb +88 -0
  73. data/lib/zsv/options.rb +132 -0
  74. data/lib/zsv/strict_validator.rb +124 -0
  75. data/lib/zsv/version.rb +5 -0
  76. data/lib/zsv.rb +183 -0
  77. metadata +196 -0
data/ext/zsv/options.c ADDED
@@ -0,0 +1,126 @@
1
+ /* Options parsing and conversion implementation */
2
+ #include "options.h"
3
+ #include "options_internal.h"
4
+
5
+ /* Symbol constants for option keys */
6
+ static ID id_headers;
7
+ static ID id_col_sep;
8
+ static ID id_quote_char;
9
+ static ID id_skip_lines;
10
+ static ID id_encoding;
11
+ static ID id_buffer_size;
12
+ static ID id_max_columns;
13
+ static ID id_max_row_size;
14
+
15
+ void zsv_options_init(zsv_ruby_options_t *opts)
16
+ {
17
+ opts->delimiter = ',';
18
+ opts->headers = false;
19
+ opts->header_array = Qnil;
20
+ opts->skip_lines = 0;
21
+ opts->encoding = rb_utf8_encoding();
22
+ opts->buffer_size = ZSV_DEFAULT_SCANNER_BUFFSIZE;
23
+ opts->max_columns = ZSV_MAX_COLS_DEFAULT;
24
+ opts->max_row_size = ZSV_RUBY_DEFAULT_MAX_ROW_SIZE;
25
+ }
26
+
27
+ static VALUE get_option(VALUE opts_hash, ID key)
28
+ {
29
+ return rb_hash_lookup(opts_hash, ID2SYM(key));
30
+ }
31
+
32
+ static char parse_single_char(VALUE value, const char *name)
33
+ {
34
+ StringValue(value);
35
+ if (RSTRING_LEN(value) != 1) {
36
+ rb_raise(rb_eArgError, "%s must be a single character", name);
37
+ }
38
+ return RSTRING_PTR(value)[0];
39
+ }
40
+
41
+ static unsigned parse_positive_uint(VALUE value, const char *name)
42
+ {
43
+ unsigned long n = NUM2ULONG(value);
44
+ if (n == 0 || n > UINT_MAX) {
45
+ rb_raise(rb_eArgError, "%s must be between 1 and %u", name, UINT_MAX);
46
+ }
47
+ return (unsigned)n;
48
+ }
49
+
50
+ void zsv_options_parse(VALUE opts_hash, zsv_ruby_options_t *opts)
51
+ {
52
+ zsv_options_init(opts);
53
+
54
+ if (NIL_P(opts_hash)) {
55
+ return;
56
+ }
57
+
58
+ Check_Type(opts_hash, T_HASH);
59
+
60
+ VALUE col_sep = get_option(opts_hash, id_col_sep);
61
+ if (!NIL_P(col_sep)) {
62
+ opts->delimiter = parse_single_char(col_sep, "col_sep");
63
+ /* zsv silently replaces these delimiters with ',' */
64
+ if (opts->delimiter == '\n' || opts->delimiter == '\r' || opts->delimiter == '"') {
65
+ rb_raise(rb_eArgError, "col_sep cannot be a newline or a double quote");
66
+ }
67
+ }
68
+
69
+ /* zsv only supports '"' as the quote character */
70
+ VALUE quote_char = get_option(opts_hash, id_quote_char);
71
+ if (!NIL_P(quote_char) && parse_single_char(quote_char, "quote_char") != '"') {
72
+ rb_raise(rb_eArgError, "quote_char must be '\"' (zsv does not support other quotes)");
73
+ }
74
+
75
+ VALUE headers = get_option(opts_hash, id_headers);
76
+ if (RB_TYPE_P(headers, T_ARRAY)) {
77
+ opts->headers = true;
78
+ opts->header_array = headers;
79
+ } else {
80
+ opts->headers = RTEST(headers);
81
+ }
82
+
83
+ VALUE skip_lines = get_option(opts_hash, id_skip_lines);
84
+ if (!NIL_P(skip_lines)) {
85
+ opts->skip_lines = NUM2INT(skip_lines);
86
+ if (opts->skip_lines < 0) {
87
+ rb_raise(rb_eArgError, "skip_lines must not be negative");
88
+ }
89
+ }
90
+
91
+ VALUE encoding = get_option(opts_hash, id_encoding);
92
+ if (!NIL_P(encoding)) {
93
+ opts->encoding = rb_to_encoding(encoding);
94
+ }
95
+
96
+ VALUE buffer_size = get_option(opts_hash, id_buffer_size);
97
+ if (!NIL_P(buffer_size)) {
98
+ opts->buffer_size = NUM2SIZET(buffer_size);
99
+ if (opts->buffer_size == 0) {
100
+ rb_raise(rb_eArgError, "buffer_size must be positive");
101
+ }
102
+ }
103
+
104
+ VALUE max_columns = get_option(opts_hash, id_max_columns);
105
+ if (!NIL_P(max_columns)) {
106
+ opts->max_columns = parse_positive_uint(max_columns, "max_columns");
107
+ }
108
+
109
+ VALUE max_row_size = get_option(opts_hash, id_max_row_size);
110
+ if (!NIL_P(max_row_size)) {
111
+ opts->max_row_size = parse_positive_uint(max_row_size, "max_row_size");
112
+ }
113
+ }
114
+
115
+ /* Initialize symbols (call from Init_zsv) */
116
+ void Init_zsv_options(void)
117
+ {
118
+ id_headers = rb_intern("headers");
119
+ id_col_sep = rb_intern("col_sep");
120
+ id_quote_char = rb_intern("quote_char");
121
+ id_skip_lines = rb_intern("skip_lines");
122
+ id_encoding = rb_intern("encoding");
123
+ id_buffer_size = rb_intern("buffer_size");
124
+ id_max_columns = rb_intern("max_columns");
125
+ id_max_row_size = rb_intern("max_row_size");
126
+ }
data/ext/zsv/options.h ADDED
@@ -0,0 +1,28 @@
1
+ /* Options parsing and conversion */
2
+ #ifndef ZSV_RUBY_OPTIONS_H
3
+ #define ZSV_RUBY_OPTIONS_H
4
+
5
+ #include "common.h"
6
+
7
+ /* Longest row (bytes, without its line ending) parsed by default */
8
+ #define ZSV_RUBY_DEFAULT_MAX_ROW_SIZE (256 * 1024)
9
+
10
+ /* Parser options structure */
11
+ typedef struct {
12
+ char delimiter;
13
+ bool headers;
14
+ VALUE header_array; /* Custom headers as Ruby array (marked by the parser) */
15
+ int skip_lines;
16
+ rb_encoding *encoding;
17
+ size_t buffer_size; /* Requested read size; the zsv buffer may be larger */
18
+ unsigned max_columns; /* Most fields in a row; a wider row raises */
19
+ unsigned max_row_size; /* Most bytes in a row; a longer row raises */
20
+ } zsv_ruby_options_t;
21
+
22
+ /* Initialize options with defaults */
23
+ void zsv_options_init(zsv_ruby_options_t *opts);
24
+
25
+ /* Parse Ruby hash into options structure (raises on invalid options) */
26
+ void zsv_options_parse(VALUE opts_hash, zsv_ruby_options_t *opts);
27
+
28
+ #endif /* ZSV_RUBY_OPTIONS_H */
@@ -0,0 +1,8 @@
1
+ /* Internal options symbols - forward declaration */
2
+ #ifndef ZSV_RUBY_OPTIONS_INTERNAL_H
3
+ #define ZSV_RUBY_OPTIONS_INTERNAL_H
4
+
5
+ /* Initialize option symbols (called from Init_zsv) */
6
+ void Init_zsv_options(void);
7
+
8
+ #endif /* ZSV_RUBY_OPTIONS_INTERNAL_H */
data/ext/zsv/parser.c ADDED
@@ -0,0 +1,503 @@
1
+ /* Parser wrapper implementation using zsv's callback API
2
+ *
3
+ * zsv pushes rows into zsv_row_handler() from inside zsv_parse_more(). Ruby
4
+ * code that can raise (allocation, Hash#[]= on custom header objects, IO#read)
5
+ * therefore runs below zsv's C frames. A longjmp through those frames would
6
+ * leave zsv half-way through a buffer, so every callback runs its Ruby code
7
+ * under rb_protect(), records the jump state, and returns normally; the
8
+ * exception is re-raised once control is back in our code, after the parser
9
+ * has been closed.
10
+ *
11
+ * Row size: zsv holds a row in one buffer and, when a row fills it, warns
12
+ * through errprintf and hands the row on cut short. The buffer is therefore
13
+ * sized to hold any row of max_row_size bytes, the row handler raises for a
14
+ * longer one, and the truncation warning raises too, so no row is ever
15
+ * returned incomplete. zsv's overflow_row_handler is not used: zsv 1.4.4 calls
16
+ * it with its own scanner as the context instead of ours. */
17
+ #include "parser.h"
18
+ #include <string.h>
19
+
20
+ static ID id_read;
21
+ static ID id_rewind;
22
+
23
+ static void zsv_parser_create_zsv(zsv_ruby_parser_t *parser);
24
+
25
+ void zsv_parser_init_struct(zsv_ruby_parser_t *parser)
26
+ {
27
+ memset(parser, 0, sizeof(*parser));
28
+ parser->source = Qnil;
29
+ parser->row_buffer = Qnil;
30
+ parser->headers = Qnil;
31
+ zsv_options_init(&parser->options);
32
+ }
33
+
34
+ /* ---- GC support ---------------------------------------------------------- */
35
+
36
+ /* The zsv buffer: the requested read size, but always room for a row of
37
+ * max_row_size bytes plus the byte that ends it. zsv only cuts a row short
38
+ * when the row alone fills the buffer. */
39
+ static size_t zsv_parser_buffer_size(const zsv_ruby_options_t *options)
40
+ {
41
+ size_t row_buffer = (size_t)options->max_row_size + 1;
42
+ if (row_buffer == 0) { /* 32-bit size_t and max_row_size == UINT_MAX */
43
+ row_buffer = SIZE_MAX;
44
+ }
45
+ return options->buffer_size > row_buffer ? options->buffer_size : row_buffer;
46
+ }
47
+
48
+ void zsv_parser_mark(zsv_ruby_parser_t *parser)
49
+ {
50
+ rb_gc_mark_movable(parser->source);
51
+ rb_gc_mark_movable(parser->row_buffer);
52
+ rb_gc_mark_movable(parser->headers);
53
+ rb_gc_mark_movable(parser->options.header_array);
54
+ if (parser->row_builder) {
55
+ zsv_row_builder_mark(parser->row_builder);
56
+ }
57
+ }
58
+
59
+ void zsv_parser_compact(zsv_ruby_parser_t *parser)
60
+ {
61
+ parser->source = rb_gc_location(parser->source);
62
+ parser->row_buffer = rb_gc_location(parser->row_buffer);
63
+ parser->headers = rb_gc_location(parser->headers);
64
+ parser->options.header_array = rb_gc_location(parser->options.header_array);
65
+ if (parser->row_builder) {
66
+ zsv_row_builder_compact(parser->row_builder);
67
+ }
68
+ }
69
+
70
+ size_t zsv_parser_memsize(const zsv_ruby_parser_t *parser)
71
+ {
72
+ size_t size = sizeof(*parser);
73
+ if (parser->row_builder) {
74
+ size += zsv_row_builder_memsize(parser->row_builder);
75
+ }
76
+ if (parser->zsv) {
77
+ size += zsv_parser_buffer_size(&parser->options);
78
+ }
79
+ return size;
80
+ }
81
+
82
+ /* ---- zsv callbacks --------------------------------------------------------- */
83
+
84
+ /* An empty unquoted line: CSV returns [] for it, not [""] */
85
+ static bool zsv_parser_blank_row(zsv_ruby_parser_t *parser, size_t cell_count)
86
+ {
87
+ if (cell_count == 0) {
88
+ return true;
89
+ }
90
+ if (cell_count != 1) {
91
+ return false;
92
+ }
93
+ struct zsv_cell cell = zsv_get_cell(parser->zsv, 0);
94
+ return cell.len == 0 && !(cell.quoted & ZSV_PARSER_QUOTE_CLOSED);
95
+ }
96
+
97
+ static void zsv_parser_build_row(zsv_ruby_parser_t *parser, size_t cell_count)
98
+ {
99
+ zsv_row_builder_reset(parser->row_builder);
100
+ for (size_t i = 0; i < cell_count; i++) {
101
+ struct zsv_cell cell = zsv_get_cell(parser->zsv, i);
102
+ zsv_row_builder_add_cell(parser->row_builder, cell.str, cell.len);
103
+ }
104
+ }
105
+
106
+ /* Raise ZSV::MalformedCSVError for the current record; its #line_number is the
107
+ * 1-based record, as in CSV ("... in line N.") */
108
+ static void zsv_parser_raise_malformed(zsv_ruby_parser_t *parser, VALUE message)
109
+ {
110
+ VALUE args[2] = {message, SIZET2NUM(parser->record_count)};
111
+ rb_exc_raise(rb_class_new_instance(2, args, eMalformedCSVError));
112
+ }
113
+
114
+ static VALUE zsv_row_handler_body(VALUE arg)
115
+ {
116
+ zsv_ruby_parser_t *parser = (zsv_ruby_parser_t *)arg;
117
+ parser->record_count++;
118
+
119
+ if (parser->row_truncated ||
120
+ zsv_row_length_raw_bytes(parser->zsv) > parser->options.max_row_size) {
121
+ zsv_parser_raise_malformed(parser, rb_sprintf("Row longer than max_row_size (%u bytes)",
122
+ parser->options.max_row_size));
123
+ }
124
+ if (parser->column_overflow) {
125
+ zsv_parser_raise_malformed(parser, rb_sprintf("Row with more than max_columns (%u) fields",
126
+ parser->options.max_columns));
127
+ }
128
+
129
+ /* Skip lines if configured */
130
+ if (parser->lines_skipped < (size_t)parser->options.skip_lines) {
131
+ parser->lines_skipped++;
132
+ return Qnil;
133
+ }
134
+
135
+ size_t cell_count = zsv_cell_count(parser->zsv);
136
+ bool blank = zsv_parser_blank_row(parser, cell_count);
137
+
138
+ /* Header row - only if headers: true and no custom headers provided.
139
+ * Blank lines before it are skipped. */
140
+ if (parser->options.headers && !parser->header_row_processed) {
141
+ if (blank) {
142
+ return Qnil;
143
+ }
144
+ zsv_parser_build_row(parser, cell_count);
145
+ parser->headers = zsv_row_builder_to_array(parser->row_builder);
146
+ zsv_row_builder_set_headers(parser->row_builder, parser->headers);
147
+ parser->header_row_processed = true;
148
+ return Qnil;
149
+ }
150
+
151
+ zsv_parser_build_row(parser, blank ? 0 : cell_count);
152
+
153
+ VALUE row = NIL_P(parser->headers) ? zsv_row_builder_to_array(parser->row_builder)
154
+ : zsv_row_builder_to_hash(parser->row_builder);
155
+ rb_ary_push(parser->row_buffer, row);
156
+ return Qnil;
157
+ }
158
+
159
+ static void zsv_row_handler(void *ctx)
160
+ {
161
+ zsv_ruby_parser_t *parser = (zsv_ruby_parser_t *)ctx;
162
+ if (parser->exception_state) {
163
+ return; /* an earlier callback raised; drop the rest of this chunk */
164
+ }
165
+ rb_protect(zsv_row_handler_body, (VALUE)parser, &parser->exception_state);
166
+ }
167
+
168
+ /* zsv prints warnings to stderr. Silence them, and turn the two that mean
169
+ * data loss into errors: both are printed just before the damaged row reaches
170
+ * zsv_row_handler, which raises. */
171
+ static int zsv_errprintf(void *ctx, const char *format, ...)
172
+ {
173
+ zsv_ruby_parser_t *parser = (zsv_ruby_parser_t *)ctx;
174
+ if (strstr(format, "exceeds row max") != NULL) {
175
+ parser->column_overflow = true; /* columns beyond max_columns dropped */
176
+ } else if (strstr(format, "truncated") != NULL) {
177
+ parser->row_truncated = true; /* a row that filled the whole buffer */
178
+ }
179
+ return 0;
180
+ }
181
+
182
+ static size_t zsv_string_read(void *buffer, size_t size, size_t count, void *ctx)
183
+ {
184
+ zsv_ruby_parser_t *parser = (zsv_ruby_parser_t *)ctx;
185
+ if (size == 0) {
186
+ return 0;
187
+ }
188
+
189
+ /* The source is frozen, so its length cannot change between reads. The
190
+ * pointer is re-read on every call because GC compaction may move it. */
191
+ size_t length = RSTRING_LEN(parser->source);
192
+ size_t available = length - parser->source_offset;
193
+ size_t copied = available < size * count ? available : size * count;
194
+ copied -= copied % size;
195
+
196
+ memcpy(buffer, RSTRING_PTR(parser->source) + parser->source_offset, copied);
197
+ parser->source_offset += copied;
198
+ return copied / size;
199
+ }
200
+
201
+ typedef struct {
202
+ zsv_ruby_parser_t *parser;
203
+ size_t requested;
204
+ } zsv_io_read_args_t;
205
+
206
+ static VALUE zsv_io_read_chunk(VALUE arg)
207
+ {
208
+ zsv_io_read_args_t *args = (zsv_io_read_args_t *)arg;
209
+ VALUE io = args->parser->source;
210
+ VALUE chunk = rb_funcall(io, id_read, 1, SIZET2NUM(args->requested));
211
+
212
+ if (NIL_P(chunk)) {
213
+ return Qnil;
214
+ }
215
+ StringValue(chunk);
216
+ if ((size_t)RSTRING_LEN(chunk) > args->requested) {
217
+ rb_raise(rb_eIOError, "%s#read returned more bytes than requested", rb_obj_classname(io));
218
+ }
219
+ return chunk;
220
+ }
221
+
222
+ static size_t zsv_io_read(void *buffer, size_t size, size_t count, void *ctx)
223
+ {
224
+ zsv_ruby_parser_t *parser = (zsv_ruby_parser_t *)ctx;
225
+ if (parser->exception_state || size == 0 || count == 0) {
226
+ return 0;
227
+ }
228
+
229
+ zsv_io_read_args_t args = {parser, size * count};
230
+ VALUE chunk = rb_protect(zsv_io_read_chunk, (VALUE)&args, &parser->exception_state);
231
+ if (parser->exception_state || NIL_P(chunk)) {
232
+ return 0; /* zsv sees EOF; the driver re-raises the exception */
233
+ }
234
+
235
+ size_t length = RSTRING_LEN(chunk);
236
+ memcpy(buffer, RSTRING_PTR(chunk), length);
237
+ RB_GC_GUARD(chunk);
238
+ return length / size;
239
+ }
240
+
241
+ /* ---- setup ------------------------------------------------------------------ */
242
+
243
+ static void zsv_parser_create_zsv(zsv_ruby_parser_t *parser)
244
+ {
245
+ struct zsv_opts zopts;
246
+ memset(&zopts, 0, sizeof(zopts));
247
+
248
+ zopts.delimiter = parser->options.delimiter;
249
+ zopts.buffsize = zsv_parser_buffer_size(&parser->options);
250
+ /* zsv only uses max_row_size to grow buffsize to twice its value: pass
251
+ * half the buffer so zsv keeps ours (and its unsigned doubling cannot
252
+ * wrap); the row limit itself is enforced by zsv_row_handler_body */
253
+ zopts.max_row_size =
254
+ (unsigned)(zopts.buffsize / 2 < UINT_MAX / 2 ? zopts.buffsize / 2 : UINT_MAX / 2);
255
+ zopts.max_columns = parser->options.max_columns;
256
+ zopts.keep_empty_header_rows = 1; /* we handle blank lines ourselves */
257
+ zopts.row_handler = zsv_row_handler;
258
+ zopts.ctx = parser;
259
+ zopts.errprintf = zsv_errprintf;
260
+ zopts.errf = parser;
261
+
262
+ switch (parser->source_kind) {
263
+ case ZSV_SOURCE_FILE:
264
+ zopts.stream = parser->file; /* default reader is fread() */
265
+ break;
266
+ case ZSV_SOURCE_STRING:
267
+ zopts.read = zsv_string_read;
268
+ zopts.stream = parser;
269
+ break;
270
+ case ZSV_SOURCE_IO:
271
+ zopts.read = zsv_io_read;
272
+ zopts.stream = parser;
273
+ break;
274
+ case ZSV_SOURCE_NONE:
275
+ rb_raise(eZSVError, "parser has no input source");
276
+ }
277
+
278
+ parser->zsv = zsv_new(&zopts);
279
+ if (!parser->zsv) {
280
+ parser->closed = true;
281
+ rb_raise(rb_eNoMemError, "failed to create zsv parser");
282
+ }
283
+ }
284
+
285
+ static void zsv_parser_reset_state(zsv_ruby_parser_t *parser)
286
+ {
287
+ parser->record_count = 0;
288
+ parser->lines_skipped = 0;
289
+ parser->eof_reached = false;
290
+ parser->column_overflow = false;
291
+ parser->row_truncated = false;
292
+ parser->exception_state = 0;
293
+ parser->row_buffer = rb_ary_new();
294
+
295
+ /* Custom headers skip the first row; otherwise it is read as the header */
296
+ if (!NIL_P(parser->options.header_array)) {
297
+ parser->headers = parser->options.header_array;
298
+ parser->header_row_processed = true;
299
+ } else {
300
+ parser->headers = Qnil;
301
+ parser->header_row_processed = false;
302
+ }
303
+ zsv_row_builder_set_headers(parser->row_builder, parser->headers);
304
+ }
305
+
306
+ /* Parse options and allocate Ruby-side state. Runs before any OS resource is
307
+ * acquired so an invalid option cannot leak a FILE*. */
308
+ static void zsv_parser_setup(zsv_ruby_parser_t *parser, VALUE opts_hash)
309
+ {
310
+ /* row_builder is set once setup has run, even when the open that follows
311
+ * failed: a second initialize would otherwise leak the first builder */
312
+ if (parser->source_kind != ZSV_SOURCE_NONE || parser->row_builder) {
313
+ rb_raise(rb_eRuntimeError, "ZSV::Parser is already initialized");
314
+ }
315
+
316
+ zsv_options_parse(opts_hash, &parser->options);
317
+ parser->row_builder = zsv_row_builder_new(parser->options.encoding);
318
+ zsv_parser_reset_state(parser);
319
+ }
320
+
321
+ void zsv_parser_init_path(zsv_ruby_parser_t *parser, VALUE path, VALUE opts_hash)
322
+ {
323
+ FilePathValue(path);
324
+ zsv_parser_setup(parser, opts_hash);
325
+
326
+ parser->file = fopen(RSTRING_PTR(path), "rb");
327
+ RB_GC_GUARD(path);
328
+ if (!parser->file) {
329
+ rb_sys_fail_str(path);
330
+ }
331
+ parser->source_kind = ZSV_SOURCE_FILE;
332
+ zsv_parser_create_zsv(parser);
333
+ }
334
+
335
+ void zsv_parser_init_string(zsv_ruby_parser_t *parser, VALUE string, VALUE opts_hash)
336
+ {
337
+ StringValue(string);
338
+ zsv_parser_setup(parser, opts_hash);
339
+
340
+ /* A frozen copy (sharing the buffer) so mutating the caller's string
341
+ * during the parse cannot free or change the bytes zsv is reading */
342
+ parser->source = rb_str_new_frozen(string);
343
+ parser->source_kind = ZSV_SOURCE_STRING;
344
+ zsv_parser_create_zsv(parser);
345
+ }
346
+
347
+ void zsv_parser_init_io(zsv_ruby_parser_t *parser, VALUE io, VALUE opts_hash)
348
+ {
349
+ if (!rb_respond_to(io, id_read)) {
350
+ rb_raise(rb_eTypeError, "expected an IO-like object responding to #read, got %s",
351
+ rb_obj_classname(io));
352
+ }
353
+ zsv_parser_setup(parser, opts_hash);
354
+
355
+ parser->source = io;
356
+ parser->source_kind = ZSV_SOURCE_IO;
357
+ zsv_parser_create_zsv(parser);
358
+ }
359
+
360
+ /* ---- teardown --------------------------------------------------------------- */
361
+
362
+ /* Release zsv and the FILE*. Never calls into Ruby: runs from dfree during GC. */
363
+ static void zsv_parser_release(zsv_ruby_parser_t *parser)
364
+ {
365
+ parser->closed = true;
366
+
367
+ if (parser->zsv) {
368
+ zsv_delete(parser->zsv);
369
+ parser->zsv = NULL;
370
+ }
371
+
372
+ if (parser->file) {
373
+ fclose(parser->file);
374
+ parser->file = NULL;
375
+ }
376
+ }
377
+
378
+ void zsv_parser_free(zsv_ruby_parser_t *parser)
379
+ {
380
+ if (!parser)
381
+ return;
382
+
383
+ zsv_parser_release(parser);
384
+ zsv_row_builder_free(parser->row_builder);
385
+ xfree(parser);
386
+ }
387
+
388
+ static void zsv_parser_ensure_not_parsing(zsv_ruby_parser_t *parser)
389
+ {
390
+ if (parser->parsing) {
391
+ rb_raise(eZSVError, "ZSV::Parser is in use (re-entered from a callback or another thread)");
392
+ }
393
+ }
394
+
395
+ void zsv_parser_close(zsv_ruby_parser_t *parser)
396
+ {
397
+ zsv_parser_ensure_not_parsing(parser);
398
+ zsv_parser_release(parser);
399
+ }
400
+
401
+ /* ---- parsing ------------------------------------------------------------------ */
402
+
403
+ /* Feed zsv one buffer of input; at EOF flush the final row with zsv_finish(),
404
+ * exactly once. Ruby code only runs under rb_protect() in here. */
405
+ static enum zsv_status zsv_parser_parse_chunk(zsv_ruby_parser_t *parser)
406
+ {
407
+ parser->parsing = true;
408
+ enum zsv_status status = zsv_parse_more(parser->zsv);
409
+ if (status == zsv_status_no_more_input && !parser->exception_state) {
410
+ enum zsv_status finish_status = zsv_finish(parser->zsv);
411
+ parser->eof_reached = true;
412
+ if (finish_status != zsv_status_ok) {
413
+ status = finish_status;
414
+ }
415
+ }
416
+ parser->parsing = false;
417
+ return status;
418
+ }
419
+
420
+ static void zsv_parser_raise_on_error(zsv_ruby_parser_t *parser, enum zsv_status status)
421
+ {
422
+ if (parser->exception_state) {
423
+ int state = parser->exception_state;
424
+ zsv_parser_release(parser);
425
+ rb_jump_tag(state);
426
+ }
427
+
428
+ if (status != zsv_status_ok && status != zsv_status_no_more_input) {
429
+ zsv_parser_release(parser);
430
+ rb_raise(eMalformedCSVError, "CSV parsing error: %s", zsv_parse_status_desc(status));
431
+ }
432
+ }
433
+
434
+ static void zsv_parser_fill(zsv_ruby_parser_t *parser)
435
+ {
436
+ /* A chunk can end without completing a row (long rows, skipped or header
437
+ * rows, no trailing newline), so keep going until a row or EOF */
438
+ while (!parser->closed && !parser->eof_reached && RARRAY_LEN(parser->row_buffer) == 0) {
439
+ enum zsv_status status = zsv_parser_parse_chunk(parser);
440
+ zsv_parser_raise_on_error(parser, status);
441
+ rb_thread_check_ints(); /* outside zsv: safe to raise Interrupt here */
442
+ }
443
+ }
444
+
445
+ VALUE zsv_parser_shift(zsv_ruby_parser_t *parser)
446
+ {
447
+ zsv_parser_ensure_not_parsing(parser);
448
+ if (parser->closed) {
449
+ return Qnil;
450
+ }
451
+
452
+ zsv_parser_fill(parser);
453
+
454
+ if (!parser->closed && RARRAY_LEN(parser->row_buffer) > 0) {
455
+ return rb_ary_shift(parser->row_buffer);
456
+ }
457
+ return Qnil;
458
+ }
459
+
460
+ void zsv_parser_rewind(zsv_ruby_parser_t *parser)
461
+ {
462
+ zsv_parser_ensure_not_parsing(parser);
463
+ if (parser->closed) {
464
+ rb_raise(rb_eIOError, "closed parser");
465
+ }
466
+
467
+ switch (parser->source_kind) {
468
+ case ZSV_SOURCE_FILE:
469
+ if (fseek(parser->file, 0, SEEK_SET) != 0) {
470
+ rb_sys_fail("rewind");
471
+ }
472
+ break;
473
+ case ZSV_SOURCE_STRING:
474
+ parser->source_offset = 0;
475
+ break;
476
+ case ZSV_SOURCE_IO:
477
+ rb_funcall(parser->source, id_rewind, 0);
478
+ break;
479
+ case ZSV_SOURCE_NONE:
480
+ rb_raise(eZSVError, "parser has no input source");
481
+ }
482
+
483
+ zsv_parser_reset_state(parser);
484
+ zsv_delete(parser->zsv);
485
+ parser->zsv = NULL;
486
+ zsv_parser_create_zsv(parser);
487
+ }
488
+
489
+ VALUE zsv_parser_headers(zsv_ruby_parser_t *parser)
490
+ {
491
+ return parser->headers;
492
+ }
493
+
494
+ bool zsv_parser_closed(zsv_ruby_parser_t *parser)
495
+ {
496
+ return parser->closed;
497
+ }
498
+
499
+ void Init_zsv_parser(void)
500
+ {
501
+ id_read = rb_intern("read");
502
+ id_rewind = rb_intern("rewind");
503
+ }