zsv 1.4.3 → 1.4.4

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 (72) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +140 -2
  3. data/README.md +129 -275
  4. data/ext/zsv/ZSV_UPSTREAM +7 -0
  5. data/ext/zsv/common.h +0 -12
  6. data/ext/zsv/extconf.rb +70 -122
  7. data/ext/zsv/options.c +63 -62
  8. data/ext/zsv/options.h +8 -11
  9. data/ext/zsv/parser.c +390 -201
  10. data/ext/zsv/parser.h +35 -22
  11. data/ext/zsv/row.c +75 -28
  12. data/ext/zsv/row.h +15 -9
  13. data/ext/zsv/upstream.rb +55 -0
  14. data/ext/zsv/vendor/zsv/LICENSE +21 -0
  15. data/ext/zsv/vendor/zsv/VENDORED_FROM +2 -0
  16. data/ext/zsv/vendor/zsv/configure +1212 -0
  17. data/ext/zsv/vendor/zsv/include/zsv/api.h +375 -0
  18. data/ext/zsv/vendor/zsv/include/zsv/common.h +397 -0
  19. data/ext/zsv/vendor/zsv/include/zsv/ext/implementation.h +62 -0
  20. data/ext/zsv/vendor/zsv/include/zsv/ext/implementation_private.h +113 -0
  21. data/ext/zsv/vendor/zsv/include/zsv/ext/sheet.h +80 -0
  22. data/ext/zsv/vendor/zsv/include/zsv/ext.h +333 -0
  23. data/ext/zsv/vendor/zsv/include/zsv/utils/appname.h +51 -0
  24. data/ext/zsv/vendor/zsv/include/zsv/utils/arg.h +139 -0
  25. data/ext/zsv/vendor/zsv/include/zsv/utils/cache.h +49 -0
  26. data/ext/zsv/vendor/zsv/include/zsv/utils/clock.h +36 -0
  27. data/ext/zsv/vendor/zsv/include/zsv/utils/compiler.h +69 -0
  28. data/ext/zsv/vendor/zsv/include/zsv/utils/db.h +19 -0
  29. data/ext/zsv/vendor/zsv/include/zsv/utils/dirs.h +149 -0
  30. data/ext/zsv/vendor/zsv/include/zsv/utils/dl.h +22 -0
  31. data/ext/zsv/vendor/zsv/include/zsv/utils/emcc/fs_api.h +28 -0
  32. data/ext/zsv/vendor/zsv/include/zsv/utils/err.h +22 -0
  33. data/ext/zsv/vendor/zsv/include/zsv/utils/file-mem.h +18 -0
  34. data/ext/zsv/vendor/zsv/include/zsv/utils/file.h +125 -0
  35. data/ext/zsv/vendor/zsv/include/zsv/utils/jq.h +65 -0
  36. data/ext/zsv/vendor/zsv/include/zsv/utils/json.h +19 -0
  37. data/ext/zsv/vendor/zsv/include/zsv/utils/mem.h +19 -0
  38. data/ext/zsv/vendor/zsv/include/zsv/utils/memmem.h +13 -0
  39. data/ext/zsv/vendor/zsv/include/zsv/utils/os.h +145 -0
  40. data/ext/zsv/vendor/zsv/include/zsv/utils/output.h +34 -0
  41. data/ext/zsv/vendor/zsv/include/zsv/utils/overwrite.h +71 -0
  42. data/ext/zsv/vendor/zsv/include/zsv/utils/overwrite_writer.h +53 -0
  43. data/ext/zsv/vendor/zsv/include/zsv/utils/prop.h +119 -0
  44. data/ext/zsv/vendor/zsv/include/zsv/utils/rng.h +35 -0
  45. data/ext/zsv/vendor/zsv/include/zsv/utils/signal.h +25 -0
  46. data/ext/zsv/vendor/zsv/include/zsv/utils/sql.h +13 -0
  47. data/ext/zsv/vendor/zsv/include/zsv/utils/string.h +173 -0
  48. data/ext/zsv/vendor/zsv/include/zsv/utils/utf8.h +41 -0
  49. data/ext/zsv/vendor/zsv/include/zsv/utils/win/dl.h +25 -0
  50. data/ext/zsv/vendor/zsv/include/zsv/utils/writer.h +109 -0
  51. data/ext/zsv/vendor/zsv/include/zsv/zsv_export.h +33 -0
  52. data/ext/zsv/vendor/zsv/include/zsv.h.in +18 -0
  53. data/ext/zsv/vendor/zsv/src/Makefile +238 -0
  54. data/ext/zsv/vendor/zsv/src/vector_delim.c +60 -0
  55. data/ext/zsv/vendor/zsv/src/zsv.c +622 -0
  56. data/ext/zsv/vendor/zsv/src/zsv_internal.c +789 -0
  57. data/ext/zsv/vendor/zsv/src/zsv_scan_delim.c +285 -0
  58. data/ext/zsv/vendor/zsv/src/zsv_scan_delim_fast.c +741 -0
  59. data/ext/zsv/vendor/zsv/src/zsv_scan_fixed.c +88 -0
  60. data/ext/zsv/vendor/zsv/src/zsv_scan_simd_avx2.h +89 -0
  61. data/ext/zsv/vendor/zsv/src/zsv_scan_simd_neon.h +80 -0
  62. data/ext/zsv/vendor/zsv/src/zsv_scan_simd_sse2.h +93 -0
  63. data/ext/zsv/vendor/zsv/src/zsv_scan_simd_wasm.h +79 -0
  64. data/ext/zsv/vendor/zsv/src/zsv_strencode.c +84 -0
  65. data/ext/zsv/zsv_ext.c +176 -105
  66. data/lib/zsv/errors.rb +32 -0
  67. data/lib/zsv/input.rb +88 -0
  68. data/lib/zsv/options.rb +132 -0
  69. data/lib/zsv/strict_validator.rb +124 -0
  70. data/lib/zsv/version.rb +1 -1
  71. data/lib/zsv.rb +137 -35
  72. metadata +82 -11
@@ -0,0 +1,333 @@
1
+ /*
2
+ * Copyright (C) 2021 Liquidaty and the zsv/lib contributors
3
+ * All rights reserved
4
+ *
5
+ * This file is part of zsv/lib, distributed under the license defined at
6
+ * https://opensource.org/licenses/MIT
7
+ */
8
+
9
+ #ifndef ZSV_EXT_H
10
+ #define ZSV_EXT_H
11
+
12
+ #define ZSV_EXTENSION_ID_MIN_LEN 2
13
+ #define ZSV_EXTENSION_ID_MAX_LEN 8
14
+
15
+ #include <stdio.h>
16
+ #include <stdint.h>
17
+ #include "common.h"
18
+ #include "ext/sheet.h"
19
+ #include "utils/sql.h"
20
+ #include "utils/prop.h"
21
+ #include "utils/writer.h"
22
+
23
+ /**
24
+ * @file ext.h
25
+ * @brief ZSV extension api, common to zsvlib and any zsv extension
26
+ * @defgroup zsv_extension api
27
+ * @ingroup ZSV
28
+ * @{
29
+ */
30
+ ZSV_BEGIN_DECL
31
+
32
+ /**
33
+ * Status and error codes for zsv extensions. If a command's `main()` program
34
+ * returns a status other than zsv_ext_status_ok, `zsv_ext_errcode()` will be
35
+ * called and its result used for the final exit code. If it returns
36
+ * zsv_ext_status_error, then furthermore, `zsv_ext_errstr()` will be called
37
+ * and any non-null result displayed as an error message
38
+ */
39
+ enum zsv_ext_status {
40
+ zsv_ext_status_ok = 0,
41
+ zsv_ext_status_memory,
42
+ zsv_ext_status_unrecognized_cmd,
43
+
44
+ /**
45
+ * if a zsv_ext function returns zsv_ext_status_error, an error message
46
+ * is generated by calling the module's `zsv_ext_errcode()` and `zsv_ext_errstr()`
47
+ * functions. Otherwise an error string is generated from the zsv_ext_status code
48
+ */
49
+ zsv_ext_status_error,
50
+
51
+ /**
52
+ * operation not permitted, such as an extension attempting
53
+ * to modify a buffer that is owned by a different extension
54
+ */
55
+ zsv_ext_status_not_permitted,
56
+
57
+ /**
58
+ * zsv_ext_status_other is treated as a silent error by ZSV, e.g. if you have
59
+ * already handled your error and do not want any further error message displayed
60
+ */
61
+ zsv_ext_status_other
62
+ };
63
+
64
+ /**
65
+ * The context in which any call to your extension is made, used with:
66
+ * `set_ext_context()`
67
+ * `get_ext_context()`
68
+ */
69
+ typedef void *zsv_execution_context;
70
+
71
+ /**
72
+ * Signature of the function called for each implemented sub-command
73
+ */
74
+ typedef enum zsv_ext_status (*zsv_ext_main)(zsv_execution_context ctx, int argc, const char *argv[],
75
+ struct zsv_opts *opts);
76
+
77
+ /**
78
+ * ZSV callbacks structure
79
+ *
80
+ * Contains the addresses of the ZSV functions that can be used by custom
81
+ * extensions, and which are passed from ZSV to the extension via the
82
+ * custom `zsv_ext_init()` implementation. See zsv_ext.h for additional
83
+ * extension specifications, or examples/extension.c for an example
84
+ * implementation
85
+ *
86
+ * For a description of each callback, see the corresponding zsv_-prefixed
87
+ * function in zsv.h
88
+ */
89
+ struct zsvsheet_buffer_data {
90
+ unsigned char has_row_num : 1;
91
+ unsigned char _ : 7;
92
+ };
93
+
94
+ typedef uint32_t zsvsheet_cell_attr_t;
95
+ enum zsvsheet_cell_profile_t {
96
+ zsvsheet_cell_attr_profile_none = 0,
97
+ zsvsheet_cell_attr_profile_link,
98
+ };
99
+
100
+ struct zsv_ext_callbacks {
101
+ void (*set_row_handler)(zsv_parser handle, void (*row)(void *ctx));
102
+ void (*set_context)(zsv_parser handle, void *ctx);
103
+ enum zsv_status (*parse_more)(struct zsv_scanner *scanner);
104
+ void (*abort)(struct zsv_scanner *scanner);
105
+ size_t (*cell_count)(zsv_parser parser);
106
+ struct zsv_cell (*get_cell)(zsv_parser parser, size_t ix);
107
+ enum zsv_status (*finish)(zsv_parser);
108
+ enum zsv_status (*delete)(zsv_parser);
109
+
110
+ /**
111
+ * Set or get custom context (e.g. to persist private data between
112
+ * ZSV-initiated calls to your extension)
113
+ */
114
+ void (*ext_set_context)(zsv_execution_context ctx, void *private_context);
115
+ void *(*ext_get_context)(zsv_execution_context ctx);
116
+
117
+ zsv_parser (*ext_get_parser)(zsv_execution_context ctx);
118
+
119
+ /**
120
+ * To add an extension command, invoke `ext_add_command`, passing it your command's
121
+ * handler function as a callback with a `zsv_ext_main` signature
122
+ */
123
+ enum zsv_ext_status (*ext_add_command)(zsv_execution_context ctx, const char *id, const char *help,
124
+ zsv_ext_main main);
125
+ void (*ext_set_help)(zsv_execution_context ctx, const char *help);
126
+ void (*ext_set_license)(zsv_execution_context ctx, const char *license);
127
+ void (*ext_set_thirdparty)(zsv_execution_context ctx, const char *thirdparty[]);
128
+
129
+ /**
130
+ * fetch options from execution context. used to fetch just the ctx-related parser
131
+ * opts, including any of the below if specified by the user when zsv was invoked:
132
+ * -B,--buff-size <N>
133
+ * -c,--max-column-count <N>
134
+ * -r,--max-row-size <N>
135
+ * -t,--tab-delim
136
+ * -O,--other-delim <C>
137
+ * -q,--no-quote
138
+ * -v,--verbose
139
+ *
140
+ * @param ctx execution context
141
+ * @return option values
142
+ */
143
+ struct zsv_opts (*ext_parser_opts)(zsv_execution_context ctx);
144
+
145
+ /**
146
+ * convenience function that calls ext_args_to_opts, allocates parser,
147
+ * sets custom ctx, runs parser, and de-allocates parser
148
+ */
149
+ enum zsv_ext_status (*ext_parse_all)(zsv_execution_context ctx, void *user_context, void (*row_handler)(void *ctx),
150
+ struct zsv_opts *const custom);
151
+ /**
152
+ * As an alternative to `ext_parse_all()`, for more granular control:
153
+ * ```
154
+ * struct zsv_opts opts = custom ? *custom : zsv_get_default_opts();
155
+ * zsv_parser parser = zsv_new(&opts);
156
+ * if(!parser)
157
+ * stat = zsv_ext_status_memory;
158
+ * else {
159
+ * opts.row = YOUR_COMMAND_rowhandler;
160
+ * // ... set other options ...
161
+ * zsv_parser p = new_with_context(ctx, &opts);
162
+ * while((stat = zsv_parse_more(parser)) == zsv_status_ok) ;
163
+ * if(stat == zsv_status_no_more_input)
164
+ * stat = zsv_finish(p);
165
+ * zsv_delete(p);
166
+ * }
167
+ * ```
168
+ */
169
+
170
+ /****************************************
171
+ * Registering a custom `sheet` command *
172
+ ****************************************/
173
+ zsvsheet_proc_id_t (*ext_sheet_register_proc)(const char *name, const char *description,
174
+ zsvsheet_status (*handler)(zsvsheet_proc_context_t ctx));
175
+
176
+ /**
177
+ * Bind a command to a key binding
178
+ * TO DO: allow binding of key that already exists; in which case
179
+ * allow handler to act as middleware that can cancel or allow other handlers to be executed
180
+ *
181
+ * @return 0 on success, else error
182
+ */
183
+ int (*ext_sheet_register_proc_key_binding)(char ch, zsvsheet_proc_id_t proc_id);
184
+
185
+ /*** Custom command prompt ***/
186
+ /**
187
+ * Set the prompt for entering a subcommand
188
+ * @param s text to set the subcommand prompt to. must be < 256 bytes in length
189
+ * returns zsvsheet_status_ok on success
190
+ */
191
+ zsvsheet_status (*ext_sheet_prompt)(zsvsheet_proc_context_t ctx, char *buffer, size_t bufsz, const char *fmt, ...);
192
+
193
+ /*** Custom command handling ***/
194
+ /**
195
+ * Set a status message
196
+ */
197
+ zsvsheet_status (*ext_sheet_set_status)(zsvsheet_proc_context_t ctx, const char *fmt, ...);
198
+
199
+ /**
200
+ * Get the key press that triggered this subcommand handler
201
+ */
202
+ int (*ext_sheet_keypress)(zsvsheet_proc_context_t ctx);
203
+
204
+ /****** Managing buffers ******/
205
+ /**
206
+ * Get the current buffer
207
+ */
208
+ zsvsheet_buffer_t (*ext_sheet_buffer_current)(zsvsheet_proc_context_t ctx);
209
+
210
+ /**
211
+ * Get the prior buffer
212
+ */
213
+ zsvsheet_buffer_t (*ext_sheet_buffer_prior)(zsvsheet_buffer_t b);
214
+
215
+ /**
216
+ * Get info about a buffer
217
+ */
218
+ struct zsvsheet_buffer_data (*ext_sheet_buffer_info)(zsvsheet_buffer_t);
219
+
220
+ /**
221
+ * Get the filename associated with a buffer
222
+ */
223
+ const char *(*ext_sheet_buffer_filename)(zsvsheet_buffer_t);
224
+
225
+ /**
226
+ * Get the data file associated with a buffer. This might not be the same as the filename,
227
+ * such as when the data has been filtered. Unsaved cell edits are first written to a new
228
+ * data file, so the file reflects them; the name can differ from an earlier call's, and
229
+ * an earlier name must not be used afterwards. Call from the thread that handles sheet
230
+ * commands. Returns NULL if writing the edits fails
231
+ */
232
+ const char *(*ext_sheet_buffer_data_filename)(zsvsheet_buffer_t);
233
+
234
+ /**
235
+ * Get the currently-selected cell
236
+ */
237
+ zsvsheet_status (*ext_sheet_buffer_get_selected_cell)(zsvsheet_buffer_t, struct zsvsheet_rowcol *);
238
+
239
+ /**
240
+ * Open a tabular file as a new buffer
241
+ */
242
+ zsvsheet_status (*ext_sheet_open_file)(zsvsheet_proc_context_t, const char *filepath, struct zsv_opts *zopts);
243
+
244
+ /**
245
+ * Set custom context
246
+ * @param on_close optional callback to invoke when the buffer is closed
247
+ *
248
+ * @return zsv_ext_status_ok on success, else zsv_ext_status error code
249
+ */
250
+ enum zsv_ext_status (*ext_sheet_buffer_set_ctx)(zsvsheet_buffer_t h, void *ctx, void (*on_close)(void *));
251
+
252
+ /**
253
+ * Get custom context previously set via zsvsheet_buffer_set_ctx()
254
+ * @param ctx_out result will be written to this address
255
+ *
256
+ * @return zsv_ext_status_ok on success, else zsv_ext_status error code
257
+ */
258
+ enum zsv_ext_status (*ext_sheet_buffer_get_ctx)(zsvsheet_buffer_t h, void **ctx_out);
259
+
260
+ /**
261
+ *
262
+ */
263
+ zsvsheet_cell_attr_t (*ext_sheet_cell_profile_attrs)(enum zsvsheet_cell_profile_t);
264
+
265
+ /**
266
+ * Set custom cell attributes
267
+ */
268
+ void (*ext_sheet_buffer_set_cell_attrs)(zsvsheet_buffer_t h,
269
+ enum zsv_ext_status (*get_cell_attrs)(void *pdh, zsvsheet_cell_attr_t *attrs,
270
+ size_t start_row, size_t row_count,
271
+ size_t cols));
272
+
273
+ /**
274
+ * Set custom handler on Enter key press
275
+ *
276
+ * @return zsv_ext_status_ok on success, else zsv_ext_status error code
277
+ */
278
+ enum zsv_ext_status (*ext_sheet_buffer_on_newline)(zsvsheet_buffer_t h,
279
+ zsvsheet_status (*on_newline)(zsvsheet_proc_context_t));
280
+
281
+ /**
282
+ * Get zsv_opts used to open the buffer's data file. Like ext_sheet_buffer_data_filename(),
283
+ * first writes unsaved cell edits to a new data file, whose options these are
284
+ */
285
+ struct zsv_opts (*ext_sheet_buffer_get_zsv_opts)(zsvsheet_buffer_t h);
286
+
287
+ /**
288
+ * SQLITE3 helpers
289
+ */
290
+ int (*ext_sqlite3_add_csv)(struct zsv_sqlite3_db *zdb, const char *csv_filename, struct zsv_opts *opts,
291
+ struct zsv_prop_handler *custom_prop_handler);
292
+ void (*ext_sqlite3_db_delete)(zsv_sqlite3_db_t);
293
+ zsv_sqlite3_db_t (*ext_sqlite3_db_new)(struct zsv_sqlite3_dbopts *dbopts);
294
+
295
+ /**
296
+ * Create a new buffer from the current one using a transformation
297
+ * and make the new buffer the current one
298
+ *
299
+ * Note that the transformation is performed in a seperate thread so the user_context
300
+ * must not be a stack variable
301
+ *
302
+ * See struct zsvsheet_buffer_transformation_opts in zsv/ext/sheet.h
303
+ */
304
+ zsvsheet_status (*ext_sheet_push_transformation)(zsvsheet_proc_context_t ctx,
305
+ struct zsvsheet_buffer_transformation_opts opts);
306
+
307
+ /**
308
+ * Get the writer associated with a transformation.
309
+ *
310
+ * The transformation itself is passed as the context variable to the row handler
311
+ */
312
+ zsv_csv_writer (*ext_sheet_transformation_writer)(zsvsheet_transformation trn);
313
+
314
+ /**
315
+ * Get the user provided context from the context provided to a transformation row handler
316
+ */
317
+ void *(*ext_sheet_transformation_user_context)(zsvsheet_transformation trn);
318
+
319
+ /**
320
+ * Get the parser from the context provided to a transformation row handler
321
+ */
322
+ zsv_parser (*ext_sheet_transformation_parser)(zsvsheet_transformation trn);
323
+
324
+ /**
325
+ * Get the filename that the transformation writer outputs to from the context provided to a transformation row
326
+ * handler.
327
+ */
328
+ const char *(*ext_sheet_transformation_filename)(zsvsheet_transformation trn);
329
+ };
330
+
331
+ /** @} */
332
+ ZSV_END_DECL
333
+ #endif
@@ -0,0 +1,51 @@
1
+ /*
2
+ * Copyright (C) 2021 Liquidaty and the zsv/lib contributors
3
+ * All rights reserved
4
+ *
5
+ * This file is part of zsv/lib, distributed under the license defined at
6
+ * https://opensource.org/licenses/MIT
7
+ */
8
+
9
+ #ifndef ZSV_UTILS_APPNAME_H
10
+ #define ZSV_UTILS_APPNAME_H
11
+
12
+ #include <stdio.h> /* FILE */
13
+
14
+ /*
15
+ * The zsv commands can be bundled into the `zsv` CLI, built standalone, or
16
+ * embedded into a third-party app. Their help/usage text must therefore refer
17
+ * to the host program by its *runtime* name (e.g. "zsv" or "xxx"), not a name
18
+ * fixed at compile time.
19
+ *
20
+ * Usage strings embed the ZSV_USAGE_PROG placeholder wherever the host program
21
+ * name belongs; zsv_print_usage() substitutes the runtime name when printing.
22
+ * The subcommand portion ("compare", "count", ...) is supplied separately via
23
+ * the APPNAME macro (see zsv_command.h), so a single token suffices here.
24
+ */
25
+
26
+ /* Compile-time fallback; an embedder may override with
27
+ * -DZSV_DEFAULT_PROG_NAME='"xxx"' */
28
+ #ifndef ZSV_DEFAULT_PROG_NAME
29
+ #define ZSV_DEFAULT_PROG_NAME "zsv"
30
+ #endif
31
+
32
+ /* Placeholder for the host program name within usage[] string literals.
33
+ * Uses a control character that never appears in legitimate help text. */
34
+ #define ZSV_USAGE_PROG "\x01"
35
+
36
+ /* Set the host program name once at startup, from argv[0] (the basename is
37
+ * extracted) or an explicit override. host may be NULL or empty, in which case
38
+ * the current value (default ZSV_DEFAULT_PROG_NAME) is retained. */
39
+ void zsv_set_prog_name(const char *host);
40
+
41
+ /* The host program name, e.g. "zsv" or "xxx". Never NULL. */
42
+ const char *zsv_prog_name(void);
43
+
44
+ /* Print a NULL-terminated array of usage lines to the given stream, substituting
45
+ * ZSV_USAGE_PROG with zsv_prog_name() and appending a newline to each line. */
46
+ void zsv_fprint_usage(FILE *f, const char *const *lines);
47
+
48
+ /* Convenience wrapper for zsv_fprint_usage(stdout, lines). */
49
+ void zsv_print_usage(const char *const *lines);
50
+
51
+ #endif
@@ -0,0 +1,139 @@
1
+ /*
2
+ * Copyright (C) 2021 Liquidaty and the zsv/lib contributors
3
+ * All rights reserved
4
+ *
5
+ * This file is part of zsv/lib, distributed under the license defined at
6
+ * https://opensource.org/licenses/MIT
7
+ */
8
+
9
+ #ifndef ZSV_ARG_H
10
+ #define ZSV_ARG_H
11
+
12
+ #include <zsv/common.h>
13
+
14
+ /* havearg(): case-insensitive partial arg matching */
15
+ char havearg(const char *arg, const char *form1, size_t min_len1, const char *form2, size_t min_len2);
16
+
17
+ /**
18
+ * set or get default parser options
19
+ */
20
+ void zsv_set_default_opts(struct zsv_opts);
21
+
22
+ struct zsv_opts zsv_get_default_opts(void);
23
+
24
+ void zsv_clear_default_opts(void);
25
+
26
+ #ifdef ZSV_EXTRAS
27
+
28
+ /**
29
+ * set the default option progress callback (e.g. from wasm where `struct zsv_opts`
30
+ * cannot be independently accessed)
31
+ * @param cb callback to call
32
+ * @param ctx pointer passed to callback
33
+ * @param frequency number of rows to parse between progress calls
34
+ */
35
+ void zsv_set_default_progress_callback(zsv_progress_callback cb, void *ctx, size_t rows_interval,
36
+ unsigned int seconds_interval);
37
+
38
+ /**
39
+ * set the default option completed callback (e.g. from wasm where `struct zsv_opts`
40
+ * cannot be independently accessed)
41
+ * @param cb callback to call
42
+ * @param ctx pointer passed to callback
43
+ */
44
+ void zsv_set_default_completed_callback(zsv_completed_callback cb, void *ctx);
45
+
46
+ #endif
47
+
48
+ /**
49
+ * Convert common command-line arguments to zsv_opts
50
+ * Return new argc/argv values with processed args stripped out, each arg
51
+ * matched by zsv_arg_to_opts(). Every arg is scanned, "--" and the values of
52
+ * the command's own options included, since only the command knows which args
53
+ * those are; a command that takes arbitrary option values calls
54
+ * zsv_arg_to_opts() from its own parser instead.
55
+ * Initializes opts_out with `zsv_get_default_opts()`, then with
56
+ * the below common options if present:
57
+ * -B,--buff-size <N>
58
+ * -c,--max-column-count <N>
59
+ * -r,--max-row-size <N>
60
+ * -t,--tab-delim
61
+ * -O,--other-delim <C>
62
+ * -q,--no-quote
63
+ * -R,--skip-head <n>: skip n initial rows
64
+ * -S,--keep-blank-headers: disable default behavior of ignoring leading blank rows
65
+ * -d,--header-row-span <n>: apply header depth (rowspan) of n
66
+ * --stdin-filename <path>: apply saved file properties associated with
67
+ * the given path to input read from stdin
68
+ * -v,--verbose
69
+ * -u,--malformed-utf8-replacement <string>
70
+ * -0,--header-row <header>
71
+ * -1,--apply-overwrites, -L,--limit-rows <n> (ZSV_EXTRAS only)
72
+ * --only-crlf (unless ZSV_NO_ONLY_CRLF), --parser <default|fast|compat>
73
+ *
74
+ * @param argc count of args to process
75
+ * @param argv args to process
76
+ * @param argc_out count of unprocessed args
77
+ * @param argv_out array of unprocessed arg values. Must be allocated by caller
78
+ * with size of at least argc * sizeof(*argv)
79
+ * @param opts_out options, updated to reflect any processed args
80
+ * @return zero on success, non-zero on error
81
+ */
82
+ enum zsv_status zsv_args_to_opts(int argc, const char *argv[], int *argc_out, const char **argv_out,
83
+ struct zsv_opts *opts_out);
84
+
85
+ enum zsv_arg_match {
86
+ zsv_arg_none = 0, /* not a common option: the caller handles argv[*arg_i] */
87
+ zsv_arg_ok, /* applied to opts */
88
+ zsv_arg_err /* invalid, with the message on stderr */
89
+ };
90
+
91
+ /**
92
+ * Match argv[*arg_i] against the common options zsv_args_to_opts() handles and
93
+ * apply it to opts. For a command that parses its own args: call it where the
94
+ * command's own options fail to match, so the value of one of its options is
95
+ * never read as a common option.
96
+ *
97
+ * "-" (stdin), "--" and non-option args are zsv_arg_none. On zsv_arg_ok and
98
+ * zsv_arg_err, *arg_i is left at the last arg consumed (the option's value, if
99
+ * it takes one), so the caller's loop increment moves past it.
100
+ *
101
+ * @param argc count of args
102
+ * @param argv args
103
+ * @param arg_i index of the arg to match; updated as above
104
+ * @param opts options to update; the caller initializes them, e.g. with
105
+ * zsv_get_default_opts()
106
+ */
107
+ enum zsv_arg_match zsv_arg_to_opts(int argc, const char *argv[], int *arg_i, struct zsv_opts *opts);
108
+
109
+ /**
110
+ * Fetch the next arg, if it exists, else print an error message
111
+ * The argc_i argument does not need to be valid; it will be checked
112
+ * against argc.
113
+ *
114
+ * Example:
115
+ * const char *value = zsv_next_arg(++arg_i, argc, argv, &err);
116
+ *
117
+ * @param argc_i index of next arg to test
118
+ * @param argc total arg count
119
+ * @param argv args to process
120
+ * @param err return non-zero in the case of error
121
+ * @return next argument, if it exists, else NULL
122
+ */
123
+ const char *zsv_next_arg(int arg_i, int argc, const char *argv[], int *err);
124
+
125
+ /**
126
+ * zsv_arg_is_option: true for an option-shaped token ('-x', '-xyz', '--name');
127
+ * false for a lone "-" (stdin) and for non-dash tokens. Use at a command's
128
+ * "treat as input" fall-through to detect a token that no option branch matched.
129
+ */
130
+ int zsv_arg_is_option(const char *arg);
131
+
132
+ /**
133
+ * zsv_err_unrecognized_option: print the canonical unrecognized-option
134
+ * diagnostic ("Unrecognized option: <arg>") to stderr and return the uniform
135
+ * nonzero exit status used across all commands.
136
+ */
137
+ int zsv_err_unrecognized_option(const char *arg);
138
+
139
+ #endif
@@ -0,0 +1,49 @@
1
+ #ifndef ZSV_CACHE_H
2
+ #define ZSV_CACHE_H
3
+
4
+ /**
5
+ * Overridable constants: ZSV_CACHE_PREFIX and ZSV_TEMPFILE_SUFFIX
6
+ */
7
+
8
+ #ifndef ZSV_CACHE_PREFIX
9
+ #define ZSV_CACHE_PREFIX "zsv"
10
+ #endif
11
+
12
+ #ifndef ZSV_TEMPFILE_SUFFIX
13
+ #define ZSV_TEMPFILE_SUFFIX ".~zsv"
14
+ #endif
15
+
16
+ /**
17
+ * Cache path definitions
18
+ */
19
+ #ifdef _WIN32
20
+ #define ZSV_CACHE_DIR "." ZSV_CACHE_PREFIX "\\data"
21
+ #else
22
+ #define ZSV_CACHE_DIR "." ZSV_CACHE_PREFIX "/data"
23
+ #endif
24
+
25
+ #define ZSV_CACHE_PROPERTIES_NAME "props"
26
+
27
+ /**
28
+ * Return the folder or file path to the cache for a given data file
29
+ * Caller must free the returned result
30
+ */
31
+ unsigned char *zsv_cache_path(const unsigned char *data_filepath, const unsigned char *cache_filename, char temp_file);
32
+
33
+ enum zsv_cache_type {
34
+ zsv_cache_type_property = 1,
35
+ zsv_cache_type_tag,
36
+ zsv_cache_type_overwrite
37
+ };
38
+
39
+ unsigned char *zsv_cache_filepath(const unsigned char *data_filepath, enum zsv_cache_type type, char create_dir,
40
+ char temp_file);
41
+
42
+ int zsv_cache_print(const unsigned char *filepath, enum zsv_cache_type ctype, const unsigned char *default_value);
43
+
44
+ int zsv_cache_remove(const unsigned char *filepath, enum zsv_cache_type ctype);
45
+
46
+ int zsv_modify_cache_file(const unsigned char *filepath, enum zsv_cache_type ctype, const unsigned char *json_value1,
47
+ const unsigned char *json_value2, const unsigned char *filter);
48
+
49
+ #endif
@@ -0,0 +1,36 @@
1
+ /*
2
+ * Copyright (C) 2021 Liquidaty and the zsv/lib contributors
3
+ * All rights reserved
4
+ *
5
+ * This file is part of zsv/lib, distributed under the license defined at
6
+ * https://opensource.org/licenses/MIT
7
+ */
8
+
9
+ #ifndef __ZSV_UTIL_CLOCK_H__
10
+ #define __ZSV_UTIL_CLOCK_H__
11
+
12
+ #include <time.h>
13
+ #include <stdio.h>
14
+
15
+ void zsv_clocks_begin(void);
16
+ void zsv_clocks_end(void);
17
+
18
+ size_t zsv_fread_clock(void *restrict ptr, size_t size, size_t nitems, FILE *restrict stream);
19
+ size_t zsv_fwrite_clock(const void *restrict ptr, size_t size, size_t nitems, FILE *restrict stream);
20
+ int zsv_fflush_clock(FILE *stream);
21
+
22
+ #if USE_CLOCK
23
+
24
+ #define ZSV_FREAD zsv_fread_clock
25
+ #define ZSV_FWRITE zsv_fwrite_clock
26
+ #define ZSV_FFLUSH zsv_fflush_clock
27
+
28
+ #else
29
+
30
+ #define ZSV_FREAD fread
31
+ #define ZSV_FWRITE fwrite
32
+ #define ZSV_FFLUSH fflush
33
+
34
+ #endif
35
+
36
+ #endif
@@ -0,0 +1,69 @@
1
+ /*
2
+ * Copyright (C) 2021 Tai Chi Minh Ralph Eastwood
3
+ * All rights reserved
4
+ *
5
+ * This file is part of zsv/lib, distributed under the license defined at
6
+ * https://opensource.org/licenses/MIT
7
+ */
8
+
9
+ #ifndef ZSV_UTILS_COMPILER
10
+ #define ZSV_UTILS_COMPILER
11
+
12
+ #ifdef HAVE___BUILTIN_EXPECT
13
+
14
+ #ifndef LIKELY
15
+ #define LIKELY(x) __builtin_expect(!!(x), 1)
16
+ #endif
17
+
18
+ #ifndef UNLIKELY
19
+ #define UNLIKELY(x) __builtin_expect(!!(x), 0)
20
+ #endif
21
+
22
+ #ifndef VERY_LIKELY
23
+ #ifdef NO___BUILTIN_EXPECT_WITH_PROBABILITY
24
+ #define VERY_LIKELY(x) LIKELY(x)
25
+ #else
26
+ #define VERY_LIKELY(x) __builtin_expect_with_probability(!!(x), 1, 0.999)
27
+ #endif
28
+ #endif
29
+
30
+ #ifndef VERY_UNLIKELY
31
+ #ifdef NO___BUILTIN_EXPECT_WITH_PROBABILITY
32
+ #define VERY_UNLIKELY(x) UNLIKELY(x)
33
+ #else
34
+ #define VERY_UNLIKELY(x) __builtin_expect_with_probability(!!(x), 0, 0.999)
35
+ #endif
36
+ #endif
37
+
38
+ #else
39
+ /* no HAVE___BUILTIN_EXPECT */
40
+ #ifndef LIKELY
41
+ #define LIKELY(x) (!!(x))
42
+ #endif
43
+
44
+ #ifndef UNLIKELY
45
+ #define UNLIKELY(x) (!!(x))
46
+ #endif
47
+
48
+ #ifndef VERY_LIKELY
49
+ #define VERY_LIKELY(x) (!!(x))
50
+ #endif
51
+
52
+ #ifndef VERY_UNLIKELY
53
+ #define VERY_UNLIKELY(x) (!!(x))
54
+ #endif
55
+
56
+ #endif /* HAVE___BUILTIN_EXPECT */
57
+
58
+ /* Warn (and, under -Werror, fail to compile) when a function's return value is ignored by a caller. */
59
+ #ifndef ZSV_WARN_UNUSED_RESULT
60
+ #if defined(__GNUC__) || defined(__clang__)
61
+ #define ZSV_WARN_UNUSED_RESULT __attribute__((warn_unused_result))
62
+ #elif defined(_MSC_VER)
63
+ #define ZSV_WARN_UNUSED_RESULT _Check_return_
64
+ #else
65
+ #define ZSV_WARN_UNUSED_RESULT
66
+ #endif
67
+ #endif
68
+
69
+ #endif /* ZSV_UTILS_COMPILER */
@@ -0,0 +1,19 @@
1
+ /*
2
+ * Copyright (C) 2021 Liquidaty and the zsv/lib contributors
3
+ * All rights reserved
4
+ *
5
+ * This file is part of zsv/lib, distributed under the license defined at
6
+ * https://opensource.org/licenses/MIT
7
+ */
8
+
9
+ #ifndef ZSV_DB_H
10
+ #define ZSV_DB_H
11
+
12
+ #include <sqlite3.h>
13
+ #include <jsonwriter.h>
14
+
15
+ // zsv_dbtable2json: convert a db table to json
16
+ // @limit: if nonzero, limit number of rows processed
17
+ int zsv_dbtable2json(sqlite3 *db, const char *tname, jsonwriter_handle jsw, size_t limit);
18
+
19
+ #endif