zsv 1.4.4-x86_64-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.
- checksums.yaml +7 -0
- data/CHANGELOG.md +187 -0
- data/LICENSE +21 -0
- data/README.md +203 -0
- data/ext/zsv/ZSV_UPSTREAM +7 -0
- data/ext/zsv/common.h +22 -0
- data/ext/zsv/extconf.rb +102 -0
- data/ext/zsv/options.c +126 -0
- data/ext/zsv/options.h +28 -0
- data/ext/zsv/options_internal.h +8 -0
- data/ext/zsv/parser.c +503 -0
- data/ext/zsv/parser.h +75 -0
- data/ext/zsv/row.c +169 -0
- data/ext/zsv/row.h +45 -0
- data/ext/zsv/upstream.rb +55 -0
- data/ext/zsv/vendor/zsv/LICENSE +21 -0
- data/ext/zsv/vendor/zsv/VENDORED_FROM +2 -0
- data/ext/zsv/vendor/zsv/configure +1212 -0
- data/ext/zsv/vendor/zsv/include/zsv/api.h +375 -0
- data/ext/zsv/vendor/zsv/include/zsv/common.h +397 -0
- data/ext/zsv/vendor/zsv/include/zsv/ext/implementation.h +62 -0
- data/ext/zsv/vendor/zsv/include/zsv/ext/implementation_private.h +113 -0
- data/ext/zsv/vendor/zsv/include/zsv/ext/sheet.h +80 -0
- data/ext/zsv/vendor/zsv/include/zsv/ext.h +333 -0
- data/ext/zsv/vendor/zsv/include/zsv/utils/appname.h +51 -0
- data/ext/zsv/vendor/zsv/include/zsv/utils/arg.h +139 -0
- data/ext/zsv/vendor/zsv/include/zsv/utils/cache.h +49 -0
- data/ext/zsv/vendor/zsv/include/zsv/utils/clock.h +36 -0
- data/ext/zsv/vendor/zsv/include/zsv/utils/compiler.h +69 -0
- data/ext/zsv/vendor/zsv/include/zsv/utils/db.h +19 -0
- data/ext/zsv/vendor/zsv/include/zsv/utils/dirs.h +149 -0
- data/ext/zsv/vendor/zsv/include/zsv/utils/dl.h +22 -0
- data/ext/zsv/vendor/zsv/include/zsv/utils/emcc/fs_api.h +28 -0
- data/ext/zsv/vendor/zsv/include/zsv/utils/err.h +22 -0
- data/ext/zsv/vendor/zsv/include/zsv/utils/file-mem.h +18 -0
- data/ext/zsv/vendor/zsv/include/zsv/utils/file.h +125 -0
- data/ext/zsv/vendor/zsv/include/zsv/utils/jq.h +65 -0
- data/ext/zsv/vendor/zsv/include/zsv/utils/json.h +19 -0
- data/ext/zsv/vendor/zsv/include/zsv/utils/mem.h +19 -0
- data/ext/zsv/vendor/zsv/include/zsv/utils/memmem.h +13 -0
- data/ext/zsv/vendor/zsv/include/zsv/utils/os.h +145 -0
- data/ext/zsv/vendor/zsv/include/zsv/utils/output.h +34 -0
- data/ext/zsv/vendor/zsv/include/zsv/utils/overwrite.h +71 -0
- data/ext/zsv/vendor/zsv/include/zsv/utils/overwrite_writer.h +53 -0
- data/ext/zsv/vendor/zsv/include/zsv/utils/prop.h +119 -0
- data/ext/zsv/vendor/zsv/include/zsv/utils/rng.h +35 -0
- data/ext/zsv/vendor/zsv/include/zsv/utils/signal.h +25 -0
- data/ext/zsv/vendor/zsv/include/zsv/utils/sql.h +13 -0
- data/ext/zsv/vendor/zsv/include/zsv/utils/string.h +173 -0
- data/ext/zsv/vendor/zsv/include/zsv/utils/utf8.h +41 -0
- data/ext/zsv/vendor/zsv/include/zsv/utils/win/dl.h +25 -0
- data/ext/zsv/vendor/zsv/include/zsv/utils/writer.h +109 -0
- data/ext/zsv/vendor/zsv/include/zsv/zsv_export.h +33 -0
- data/ext/zsv/vendor/zsv/include/zsv.h.in +18 -0
- data/ext/zsv/vendor/zsv/src/Makefile +238 -0
- data/ext/zsv/vendor/zsv/src/vector_delim.c +60 -0
- data/ext/zsv/vendor/zsv/src/zsv.c +622 -0
- data/ext/zsv/vendor/zsv/src/zsv_internal.c +789 -0
- data/ext/zsv/vendor/zsv/src/zsv_scan_delim.c +285 -0
- data/ext/zsv/vendor/zsv/src/zsv_scan_delim_fast.c +741 -0
- data/ext/zsv/vendor/zsv/src/zsv_scan_fixed.c +88 -0
- data/ext/zsv/vendor/zsv/src/zsv_scan_simd_avx2.h +89 -0
- data/ext/zsv/vendor/zsv/src/zsv_scan_simd_neon.h +80 -0
- data/ext/zsv/vendor/zsv/src/zsv_scan_simd_sse2.h +93 -0
- data/ext/zsv/vendor/zsv/src/zsv_scan_simd_wasm.h +79 -0
- data/ext/zsv/vendor/zsv/src/zsv_strencode.c +84 -0
- data/ext/zsv/zsv_ext.c +417 -0
- data/lib/zsv/3.3/zsv.so +0 -0
- data/lib/zsv/3.4/zsv.so +0 -0
- data/lib/zsv/4.0/zsv.so +0 -0
- data/lib/zsv/errors.rb +32 -0
- data/lib/zsv/input.rb +88 -0
- data/lib/zsv/options.rb +132 -0
- data/lib/zsv/strict_validator.rb +124 -0
- data/lib/zsv/version.rb +5 -0
- data/lib/zsv.rb +183 -0
- metadata +196 -0
|
@@ -0,0 +1,71 @@
|
|
|
1
|
+
#ifndef ZSV_OVERWRITE_H
|
|
2
|
+
#define ZSV_OVERWRITE_H
|
|
3
|
+
|
|
4
|
+
#include <sqlite3.h>
|
|
5
|
+
|
|
6
|
+
struct zsv_overwrite_ctx {
|
|
7
|
+
char *src;
|
|
8
|
+
size_t author_ix;
|
|
9
|
+
size_t timestamp_ix;
|
|
10
|
+
size_t old_value_ix;
|
|
11
|
+
// enum zsv_overwrite_type type;
|
|
12
|
+
enum zsv_status (*next)(void *ctx, struct zsv_overwrite_data *odata);
|
|
13
|
+
struct {
|
|
14
|
+
FILE *f;
|
|
15
|
+
zsv_parser parser;
|
|
16
|
+
} csv;
|
|
17
|
+
struct {
|
|
18
|
+
char *filename;
|
|
19
|
+
sqlite3 *db;
|
|
20
|
+
sqlite3_stmt *stmt; // select row, column, overwrite
|
|
21
|
+
const char *sql;
|
|
22
|
+
} sqlite3;
|
|
23
|
+
size_t row_ix;
|
|
24
|
+
};
|
|
25
|
+
|
|
26
|
+
/**
|
|
27
|
+
* The easiest way to enable overwrite support is to use zsv_overwrite_auto()
|
|
28
|
+
* which, given an input located at /path/to/my-data.csv, will assume an overwrite source
|
|
29
|
+
* located at /path/to/.zsv/data/my-data.csv/overwrite.sqlite3
|
|
30
|
+
* in a table named 'overwrites'
|
|
31
|
+
*
|
|
32
|
+
* zsv_overwrite_auto() returns:
|
|
33
|
+
* - zsv_status_done if a valid overwrite file was found
|
|
34
|
+
* - zsv_status_no_more_input if no overwrite file was found
|
|
35
|
+
* - a different status code on error
|
|
36
|
+
*
|
|
37
|
+
* This function is used in app/cli.c via the --apply-overwrites option
|
|
38
|
+
*/
|
|
39
|
+
enum zsv_status zsv_overwrite_auto(struct zsv_opts *opts, const char *csv_filename);
|
|
40
|
+
|
|
41
|
+
/**
|
|
42
|
+
* As an alternative to zsv_overwrite_auto(), you can specify your own
|
|
43
|
+
* source from which to fetch overwrite data.
|
|
44
|
+
* The specified source can be a sqlite3 file or a CSV file
|
|
45
|
+
*
|
|
46
|
+
* This approach is used by app/echo.c
|
|
47
|
+
*/
|
|
48
|
+
struct zsv_overwrite_opts {
|
|
49
|
+
/* src may be either:
|
|
50
|
+
*
|
|
51
|
+
* 1. sqlite3 file source:
|
|
52
|
+
* sqlite3://<filename>[?sql=<query>]",
|
|
53
|
+
*
|
|
54
|
+
* e.g. sqlite3:///path/to/my-overwritedb.sqlite3?sql=select row, column, value from overwrites order by row, column",
|
|
55
|
+
*
|
|
56
|
+
* or
|
|
57
|
+
*
|
|
58
|
+
* 2. CSV file source
|
|
59
|
+
* /path/to/file.csv",
|
|
60
|
+
* where the CSV columns are row,col,val (in that order, with a header row),
|
|
61
|
+
* and rows already-sorted by row and column",
|
|
62
|
+
*/
|
|
63
|
+
const char *src;
|
|
64
|
+
};
|
|
65
|
+
|
|
66
|
+
void *zsv_overwrite_context_new(struct zsv_overwrite_opts *);
|
|
67
|
+
enum zsv_status zsv_overwrite_next(void *h, struct zsv_overwrite_data *odata);
|
|
68
|
+
enum zsv_status zsv_overwrite_open(void *h);
|
|
69
|
+
enum zsv_status zsv_overwrite_context_delete(void *h);
|
|
70
|
+
|
|
71
|
+
#endif
|
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
#include <zsv.h>
|
|
2
|
+
|
|
3
|
+
enum zsvsheet_mode {
|
|
4
|
+
zsvsheet_mode_add,
|
|
5
|
+
zsvsheet_mode_remove,
|
|
6
|
+
zsvsheet_mode_clear,
|
|
7
|
+
zsvsheet_mode_list,
|
|
8
|
+
zsvsheet_mode_bulk,
|
|
9
|
+
};
|
|
10
|
+
|
|
11
|
+
struct zsv_overwrite {
|
|
12
|
+
struct zsv_overwrite_ctx *ctx;
|
|
13
|
+
struct zsv_overwrite_data *overwrite;
|
|
14
|
+
zsv_csv_writer writer;
|
|
15
|
+
enum zsv_status (*next)(struct zsv_overwrite *data);
|
|
16
|
+
enum zsvsheet_mode mode;
|
|
17
|
+
unsigned char force : 1;
|
|
18
|
+
unsigned char a1 : 1;
|
|
19
|
+
unsigned char all : 1;
|
|
20
|
+
unsigned char *bulk_file;
|
|
21
|
+
// bulk indexes
|
|
22
|
+
size_t row_ix;
|
|
23
|
+
size_t col_ix;
|
|
24
|
+
size_t val_ix;
|
|
25
|
+
size_t old_value_ix;
|
|
26
|
+
size_t timestamp_ix;
|
|
27
|
+
size_t author_ix;
|
|
28
|
+
};
|
|
29
|
+
|
|
30
|
+
struct zsv_overwrite_args {
|
|
31
|
+
char *filepath;
|
|
32
|
+
enum zsv_status (*next)(struct zsv_overwrite *data);
|
|
33
|
+
// options
|
|
34
|
+
unsigned char force : 1;
|
|
35
|
+
unsigned char a1 : 1;
|
|
36
|
+
unsigned char all : 1;
|
|
37
|
+
unsigned char *bulk_file;
|
|
38
|
+
struct zsv_overwrite_data *overwrite;
|
|
39
|
+
// commands
|
|
40
|
+
enum zsvsheet_mode mode;
|
|
41
|
+
};
|
|
42
|
+
|
|
43
|
+
struct zsv_overwrite *zsv_overwrite_writer_new(struct zsv_overwrite_args *args, struct zsv_overwrite_opts *ctx_opts);
|
|
44
|
+
|
|
45
|
+
void zsv_overwrite_writer_delete(struct zsv_overwrite *data);
|
|
46
|
+
|
|
47
|
+
enum zsv_status zsv_overwrite_writer_add(struct zsv_overwrite *data);
|
|
48
|
+
|
|
49
|
+
enum zsv_status zsv_overwrite_writer_remove(struct zsv_overwrite *data);
|
|
50
|
+
|
|
51
|
+
enum zsv_status zsv_overwrite_writer_bulk(struct zsv_overwrite *data);
|
|
52
|
+
|
|
53
|
+
enum zsv_status zsv_overwrite_writer_clear(struct zsv_overwrite *data);
|
|
@@ -0,0 +1,119 @@
|
|
|
1
|
+
#ifndef ZSV_PROP_H
|
|
2
|
+
#define ZSV_PROP_H
|
|
3
|
+
|
|
4
|
+
#include <zsv/common.h>
|
|
5
|
+
#include <yajl_helper/yajl_helper.h>
|
|
6
|
+
|
|
7
|
+
struct zsv_file_properties {
|
|
8
|
+
enum zsv_status stat;
|
|
9
|
+
unsigned int skip;
|
|
10
|
+
unsigned int header_span;
|
|
11
|
+
|
|
12
|
+
// flags used by parser only to indicate whether property was specified
|
|
13
|
+
unsigned int skip_specified : 1;
|
|
14
|
+
unsigned int header_span_specified : 1;
|
|
15
|
+
unsigned int _ : 6;
|
|
16
|
+
};
|
|
17
|
+
|
|
18
|
+
struct zsv_prop_handler {
|
|
19
|
+
// handler should return 0 on success or non-zero on error. a non-zero return code will abort the property processing
|
|
20
|
+
int (*handler)(void *property_parser, const char *property_name, struct json_value *value);
|
|
21
|
+
void *ctx;
|
|
22
|
+
};
|
|
23
|
+
|
|
24
|
+
void *zsv_properties_parser_get_custom_ctx(void *property_parser);
|
|
25
|
+
const unsigned char *zsv_properties_parser_get_filepath(void *property_parser);
|
|
26
|
+
struct zsv_opts *zsv_properties_parser_get_opts(void *property_parser_);
|
|
27
|
+
|
|
28
|
+
/**
|
|
29
|
+
* set or get default custom property handler
|
|
30
|
+
*/
|
|
31
|
+
void zsv_set_default_custom_prop_handler(struct zsv_prop_handler custom_prop_handler);
|
|
32
|
+
|
|
33
|
+
struct zsv_prop_handler zsv_get_default_custom_prop_handler(void);
|
|
34
|
+
|
|
35
|
+
void zsv_clear_default_custom_prop_handler(void);
|
|
36
|
+
|
|
37
|
+
/**
|
|
38
|
+
* Load cached file properties into a zsp_opts and/or zsv_file_properties struct
|
|
39
|
+
*
|
|
40
|
+
* @param data_filepath required file path
|
|
41
|
+
* @param opts (optional) parser options to load
|
|
42
|
+
* @param custom_prop custom property handler
|
|
43
|
+
* @return struct zsv_file_properties, with .stat set to zsv_status_ok on success
|
|
44
|
+
*/
|
|
45
|
+
struct zsv_file_properties zsv_cache_load_props(const char *data_filepath, struct zsv_opts *opts,
|
|
46
|
+
struct zsv_prop_handler *custom_prop);
|
|
47
|
+
|
|
48
|
+
/**
|
|
49
|
+
* Create a new properties parser
|
|
50
|
+
*/
|
|
51
|
+
struct zsv_properties_parser;
|
|
52
|
+
struct zsv_properties_parser *zsv_properties_parser_new(const unsigned char *path,
|
|
53
|
+
struct zsv_prop_handler *custom_prop_handler,
|
|
54
|
+
struct zsv_file_properties *fp, struct zsv_opts *opts);
|
|
55
|
+
|
|
56
|
+
/**
|
|
57
|
+
* Finished parsing
|
|
58
|
+
*/
|
|
59
|
+
enum zsv_status zsv_properties_parse_complete(struct zsv_properties_parser *parser);
|
|
60
|
+
|
|
61
|
+
/**
|
|
62
|
+
* Clean up
|
|
63
|
+
*/
|
|
64
|
+
enum zsv_status zsv_properties_parser_destroy(struct zsv_properties_parser *parser);
|
|
65
|
+
|
|
66
|
+
/**
|
|
67
|
+
* zsv_new_with_properties(): use in lieu of zsv_new() to also merge zsv options
|
|
68
|
+
* with any saved properties (such as rows_to_ignore or header_span) for the
|
|
69
|
+
* specified input file. In the event that saved properties conflict with a
|
|
70
|
+
* command-line option, the command-line option "wins" (the property value is
|
|
71
|
+
* ignored), but a warning is printed
|
|
72
|
+
*
|
|
73
|
+
* @param opts parser options. see `zsv_new()`
|
|
74
|
+
* @param cust_prop optional custom file property handler
|
|
75
|
+
* @param input_path path of file whose zsv properties should be loaded
|
|
76
|
+
* (if NULL or "-", opts->stdin_filename, if set, is used in
|
|
77
|
+
* its place). this param is used solely for loading
|
|
78
|
+
* properties and has no impact on the data that is
|
|
79
|
+
* actually parsed, which is determined by opts->stream
|
|
80
|
+
* @param handle_out returns zsv parser handle, or NULL on fail
|
|
81
|
+
*/
|
|
82
|
+
enum zsv_status zsv_new_with_properties(struct zsv_opts *opts, struct zsv_prop_handler *custom_prop,
|
|
83
|
+
const char *input_path, zsv_parser *handle_out);
|
|
84
|
+
|
|
85
|
+
/**
|
|
86
|
+
* zsv_opts_load_properties(): merge into `opts` the saved properties and the
|
|
87
|
+
* extension-implied delimiter for `input_path`, exactly as zsv_new_with_properties()
|
|
88
|
+
* does before it creates the parser. Use it to learn how a file would be parsed.
|
|
89
|
+
* input_path may name a file that does not exist
|
|
90
|
+
*
|
|
91
|
+
* @return zsv_status_ok on success
|
|
92
|
+
*/
|
|
93
|
+
enum zsv_status zsv_opts_load_properties(struct zsv_opts *opts, struct zsv_prop_handler *custom_prop,
|
|
94
|
+
const char *input_path);
|
|
95
|
+
|
|
96
|
+
/**
|
|
97
|
+
* If you are building your own CLI and incorporating zsv CLI commands into it,
|
|
98
|
+
* the `prop` command can be customized by providing your own function
|
|
99
|
+
* for determining whether a file in the property cache is a property file,
|
|
100
|
+
* which can be set using zsv_prop_get_or_set_is_prop_file()
|
|
101
|
+
*
|
|
102
|
+
* @param is_prop_file: your function, that returns non-zero if the given file entry
|
|
103
|
+
* is a property file. If NULL, is set to zsv_is_prop_file
|
|
104
|
+
* @param max_depth : maximum depth of any property file. if is_prop_file was NULL,
|
|
105
|
+
* max_depth is set to 1
|
|
106
|
+
*/
|
|
107
|
+
|
|
108
|
+
#include "dirs.h"
|
|
109
|
+
|
|
110
|
+
struct zsv_dir_filter; /* opaque structure for internal use */
|
|
111
|
+
struct zsv_dir_filter *zsv_prop_get_or_set_is_prop_file(int (*is_prop_file)(struct zsv_foreach_dirent_handle *, size_t),
|
|
112
|
+
int max_depth, char set);
|
|
113
|
+
|
|
114
|
+
/**
|
|
115
|
+
* If you provide your own is_prop_file() function and you also want to include any
|
|
116
|
+
* zsv property file, your is_prop_file() can call zsv_is_prop_file()
|
|
117
|
+
*/
|
|
118
|
+
int zsv_is_prop_file(struct zsv_foreach_dirent_handle *h, size_t depth);
|
|
119
|
+
#endif
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* Copyright (C) 2025 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_RNG_H
|
|
10
|
+
#define ZSV_RNG_H
|
|
11
|
+
|
|
12
|
+
#include <stdint.h>
|
|
13
|
+
|
|
14
|
+
/**
|
|
15
|
+
* Small, self-contained, platform-independent PRNG (xoshiro256**, seeded via
|
|
16
|
+
* splitmix64). Not cryptographic. A given seed yields the same sequence on
|
|
17
|
+
* every compiler and OS, so seeded output is reproducible across platforms.
|
|
18
|
+
*/
|
|
19
|
+
struct zsv_rng {
|
|
20
|
+
uint64_t s[4];
|
|
21
|
+
};
|
|
22
|
+
|
|
23
|
+
/** Initialize from a 64-bit seed; any seed (including 0) is valid */
|
|
24
|
+
void zsv_rng_seed(struct zsv_rng *r, uint64_t seed);
|
|
25
|
+
|
|
26
|
+
/** 64 bits from the best OS entropy source available; never fails */
|
|
27
|
+
uint64_t zsv_rng_entropy(void);
|
|
28
|
+
|
|
29
|
+
/** Next 64-bit value */
|
|
30
|
+
uint64_t zsv_rng_next(struct zsv_rng *r);
|
|
31
|
+
|
|
32
|
+
/** Unbiased value in [0, n). n == 0 returns 0 */
|
|
33
|
+
uint64_t zsv_rng_below(struct zsv_rng *r, uint64_t n);
|
|
34
|
+
|
|
35
|
+
#endif
|
|
@@ -0,0 +1,25 @@
|
|
|
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_SIGNAL_H
|
|
10
|
+
#define ZSV_SIGNAL_H
|
|
11
|
+
|
|
12
|
+
// wasm has no signals: wasi-libc's <signal.h> is a hard #error unless this is
|
|
13
|
+
// defined. Set it here so this installed header can be included on wasi without
|
|
14
|
+
// the consumer having to know; linking still requires -lwasi-emulated-signal.
|
|
15
|
+
#if defined(__wasi__) && !defined(_WASI_EMULATED_SIGNAL)
|
|
16
|
+
#define _WASI_EMULATED_SIGNAL
|
|
17
|
+
#endif
|
|
18
|
+
|
|
19
|
+
#include <signal.h>
|
|
20
|
+
|
|
21
|
+
extern volatile sig_atomic_t zsv_signal_interrupted;
|
|
22
|
+
|
|
23
|
+
void zsv_handle_ctrl_c_signal(void);
|
|
24
|
+
|
|
25
|
+
#endif
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
#ifndef ZSV_UTILS_SQL_H
|
|
2
|
+
#define ZSV_UTILS_SQL_H
|
|
3
|
+
|
|
4
|
+
typedef struct zsv_sqlite3_db *zsv_sqlite3_db_t;
|
|
5
|
+
|
|
6
|
+
struct zsv_sqlite3_dbopts {
|
|
7
|
+
unsigned char in_memory : 1;
|
|
8
|
+
unsigned char dedupe_cols : 1; // auto-rename duplicate input column names (a, a_2, ...)
|
|
9
|
+
unsigned char warn_dupe_rename : 1; // note each such rename on stderr (off for curses UIs)
|
|
10
|
+
unsigned char _ : 5;
|
|
11
|
+
};
|
|
12
|
+
|
|
13
|
+
#endif
|
|
@@ -0,0 +1,173 @@
|
|
|
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 ASCII_STRING_H
|
|
10
|
+
#define ASCII_STRING_H
|
|
11
|
+
|
|
12
|
+
#include "../zsv_export.h"
|
|
13
|
+
#include <stddef.h>
|
|
14
|
+
#include <stdint.h>
|
|
15
|
+
|
|
16
|
+
/*
|
|
17
|
+
* zsv_strtolowercase(): convert to lower case. if built with utf8proc, converts unicode points
|
|
18
|
+
* on malformed UTF8, prints to stderr
|
|
19
|
+
*
|
|
20
|
+
* @param s string to convert
|
|
21
|
+
* @param lenp pointer to length of input string; will be set to length of output string
|
|
22
|
+
*
|
|
23
|
+
* @returns newly-allocated string; caller must free()
|
|
24
|
+
*/
|
|
25
|
+
unsigned char *zsv_strtolowercase(const unsigned char *s, size_t *lenp);
|
|
26
|
+
|
|
27
|
+
/*
|
|
28
|
+
* zsv_strtolowercase_w_err(): convert to lower case. if built with utf8proc, converts unicode points
|
|
29
|
+
* on malformed UTF8, sets *err to non-zero
|
|
30
|
+
*
|
|
31
|
+
*/
|
|
32
|
+
unsigned char *zsv_strtolowercase_w_err(const unsigned char *s, size_t *lenp, int *err);
|
|
33
|
+
|
|
34
|
+
const unsigned char *zsv_strstr(const unsigned char *hay, const unsigned char *needle);
|
|
35
|
+
|
|
36
|
+
/*
|
|
37
|
+
* zsv_stricmp, zsv_strincmp(): case-insensitive comparison (unicode-compatible if built with utf8proc)
|
|
38
|
+
* zsv_strincmp_ascii: ascii case-insensitive comparison
|
|
39
|
+
*
|
|
40
|
+
* @param s1 string to convert
|
|
41
|
+
* @param len1 length of s1
|
|
42
|
+
* @param s2 string to convert
|
|
43
|
+
* @param len2 length of s2
|
|
44
|
+
*
|
|
45
|
+
* @returns 0 if the strings are equal, -1 if s1 < s2, else 1
|
|
46
|
+
*/
|
|
47
|
+
int zsv_stricmp(const unsigned char *s1, const unsigned char *s2);
|
|
48
|
+
int zsv_strincmp(const unsigned char *s1, size_t len1, const unsigned char *s2, size_t len2);
|
|
49
|
+
int zsv_strincmp_ascii(const unsigned char *s1, size_t len1, const unsigned char *s2, size_t len2);
|
|
50
|
+
|
|
51
|
+
#define ZSV_STRFOLD_ASCII 1 // every folded code point is ASCII: zsv_strcasestr_folded() may byte-scan
|
|
52
|
+
#define ZSV_STRFOLD_EXACT 2 // no code point has a case variant: a byte-exact search gives the same answer
|
|
53
|
+
#define ZSV_STRFOLD_MALFORMED 4 // some byte was not valid UTF-8 and is matched byte-wise
|
|
54
|
+
/**
|
|
55
|
+
* zsv_strfold(): lowercase s[0, len) into code points for zsv_strcasestr_folded()
|
|
56
|
+
* (Unicode simple case mapping via utf8proc; ASCII-only without it). A byte b that
|
|
57
|
+
* is not valid UTF-8 is stored as -1 - b, so it matches only that same byte where
|
|
58
|
+
* it is likewise not valid UTF-8.
|
|
59
|
+
*
|
|
60
|
+
* @param outlen receives the number of code points (0 on failure)
|
|
61
|
+
* @param flags receives ZSV_STRFOLD_* bits (0 on failure)
|
|
62
|
+
* @returns newly-allocated array; caller must free(). NULL on allocation failure
|
|
63
|
+
*/
|
|
64
|
+
int32_t *zsv_strfold(const unsigned char *s, size_t len, size_t *outlen, unsigned *flags);
|
|
65
|
+
|
|
66
|
+
/**
|
|
67
|
+
* zsv_strcasestr_folded(): case-insensitive substring test against a needle from zsv_strfold().
|
|
68
|
+
* The haystack is folded on the fly by the same mapping; nothing is allocated.
|
|
69
|
+
*
|
|
70
|
+
* @param ascii non-zero iff zsv_strfold() reported ZSV_STRFOLD_ASCII; selects the byte-scanning kernel
|
|
71
|
+
* @returns non-zero if hay[0, len) contains the needle (an empty needle is found everywhere).
|
|
72
|
+
* Cost is O(len * fold_len) in the worst case and O(len) for a typical short needle
|
|
73
|
+
*/
|
|
74
|
+
int zsv_strcasestr_folded(const unsigned char *hay, size_t len, const int32_t *fold, size_t fold_len, char ascii);
|
|
75
|
+
|
|
76
|
+
#define ZSV_STRWHITE_FLAG_NO_EMBEDDED_NEWLINE 1
|
|
77
|
+
/**
|
|
78
|
+
* zsv_strwhite(): convert consecutive white to single space
|
|
79
|
+
*
|
|
80
|
+
* @param s string to convert
|
|
81
|
+
* @param len length of input string
|
|
82
|
+
* @param flags bitfield of any of the following flags:
|
|
83
|
+
* ZSV_STRWHITE_FLAG_NO_EMBEDDED_NEWLINE
|
|
84
|
+
*/
|
|
85
|
+
size_t zsv_strwhite(unsigned char *s, size_t len, unsigned int flags);
|
|
86
|
+
|
|
87
|
+
/**
|
|
88
|
+
* Force a string to conform to UTF8 encoding. Replaces any non-conforming utf8
|
|
89
|
+
* with the specified char, or removes from the string (and shortens the string)
|
|
90
|
+
* if replace = 0
|
|
91
|
+
* @param s input string. invalid UTF8 bytes will be overwritten
|
|
92
|
+
* @param n length (in bytes) of input
|
|
93
|
+
* @param replace the character to replace any malformed UTF8 bytes with, or 0
|
|
94
|
+
* to remove and shorten the result
|
|
95
|
+
* @param callback optional callback invoked upon scanning malformed UTF8
|
|
96
|
+
* @param ctx context pointer passed to callback
|
|
97
|
+
* @return length of the valid string
|
|
98
|
+
*/
|
|
99
|
+
ZSV_EXPORT
|
|
100
|
+
size_t zsv_strencode(unsigned char *s, size_t n, unsigned char replace,
|
|
101
|
+
int (*malformed_handler)(void *, const unsigned char *s, size_t n, size_t offset),
|
|
102
|
+
void *handler_ctx);
|
|
103
|
+
|
|
104
|
+
size_t zsv_strip_trailing_zeros(const char *s, size_t len);
|
|
105
|
+
|
|
106
|
+
/**
|
|
107
|
+
* Trim trailing whitespace
|
|
108
|
+
* Returns the input string and new length
|
|
109
|
+
*/
|
|
110
|
+
const unsigned char *zsv_strtrim_right(const char unsigned *restrict s, size_t *lenp);
|
|
111
|
+
|
|
112
|
+
/**
|
|
113
|
+
* Trim leading whitespace
|
|
114
|
+
* Returns the pointer to the new string start and new length
|
|
115
|
+
*/
|
|
116
|
+
const unsigned char *zsv_strtrim_left(const char unsigned *restrict s, size_t *lenp);
|
|
117
|
+
|
|
118
|
+
/**
|
|
119
|
+
* Trim leading and trailing whitespace
|
|
120
|
+
Returns the pointer to the new string start and new length
|
|
121
|
+
*/
|
|
122
|
+
const unsigned char *zsv_strtrim(const char unsigned *restrict s, size_t *lenp);
|
|
123
|
+
|
|
124
|
+
/**
|
|
125
|
+
* zsv_strunescape_backslash(): convert consecutive white to single space
|
|
126
|
+
*
|
|
127
|
+
* @param s string to convert
|
|
128
|
+
* @param len length of input string
|
|
129
|
+
* @param flags bitfield of ZSV_STRWHITE_FLAG_XXX values
|
|
130
|
+
*/
|
|
131
|
+
size_t zsv_strunescape_backslash(unsigned char *s, size_t len);
|
|
132
|
+
|
|
133
|
+
/**
|
|
134
|
+
* Get the next UTF8 codepoint in a string
|
|
135
|
+
* Return: length of next character (in bytes), or 0 on error or end of string
|
|
136
|
+
*/
|
|
137
|
+
size_t zsv_strnext(const unsigned char *s, size_t len, int32_t *codepoint);
|
|
138
|
+
|
|
139
|
+
/**
|
|
140
|
+
* Get the last UTF8 codepoint in a string
|
|
141
|
+
* Return: length of last character (in bytes), or 0 on error or end of string
|
|
142
|
+
*/
|
|
143
|
+
size_t zsv_strlast(const unsigned char *s, size_t len, int32_t *codepoint);
|
|
144
|
+
|
|
145
|
+
/**
|
|
146
|
+
* Check if the next char is a plus or minus. If so, return its length, else return 0
|
|
147
|
+
*/
|
|
148
|
+
size_t zsv_strnext_is_sign(const unsigned char *s, size_t len);
|
|
149
|
+
|
|
150
|
+
/**
|
|
151
|
+
* Check if the next char is a currency char. If so, return its length, else return 0
|
|
152
|
+
*/
|
|
153
|
+
size_t zsv_strnext_is_currency(const unsigned char *s, size_t len);
|
|
154
|
+
|
|
155
|
+
/*
|
|
156
|
+
* Convert a string to a double. must convert entire string, else returns error
|
|
157
|
+
* @param s string to convert
|
|
158
|
+
* @param d pointer to converted value, on success
|
|
159
|
+
*
|
|
160
|
+
* @returns 0 on success, non-zero on error
|
|
161
|
+
*/
|
|
162
|
+
int zsv_strtod_exact(const char *s, double *d);
|
|
163
|
+
|
|
164
|
+
/*
|
|
165
|
+
* `zsv_get_cell_trimmed` is equivalent to `zsv_get_cell`, except that it
|
|
166
|
+
* @param s string to convert
|
|
167
|
+
* @param len length of input string
|
|
168
|
+
* returns a value with leading and trailing whitespace removed
|
|
169
|
+
*/
|
|
170
|
+
#include <zsv.h>
|
|
171
|
+
struct zsv_cell zsv_get_cell_trimmed(zsv_parser parser, size_t index);
|
|
172
|
+
|
|
173
|
+
#endif
|
|
@@ -0,0 +1,41 @@
|
|
|
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
|
+
// clang-format off
|
|
10
|
+
|
|
11
|
+
#ifndef ZSV_UTF8_CHARLEN_NOERR
|
|
12
|
+
// ZSV_UTF8_CHARLEN_NOERR: return > 0 if valid utf8, +1 on error
|
|
13
|
+
#define ZSV_UTF8_CHARLEN_NOERR(c) \
|
|
14
|
+
(!(c & 128) ? 1 : \
|
|
15
|
+
(c & 224) == 192 ? 2 : /* 110xxxxx */ \
|
|
16
|
+
(c & 240) == 224 ? 3 : /* 1110xxxx */ \
|
|
17
|
+
(c & 248) == 240 ? 4 : /* 11110xxx */ \
|
|
18
|
+
(c & 252) == 248 ? 5 : /* 111110xx */ \
|
|
19
|
+
(c & 254) == 252 ? 6 : /* 1111110x */ \
|
|
20
|
+
1 /* error */ \
|
|
21
|
+
)
|
|
22
|
+
#endif
|
|
23
|
+
|
|
24
|
+
#ifndef ZSV_UTF8_CHARLEN
|
|
25
|
+
// ZSV_UTF8_CHARLEN: return > 0 if valid utf8, -1 on error
|
|
26
|
+
#define ZSV_UTF8_CHARLEN(c) \
|
|
27
|
+
(!(c & 128) ? 1 : \
|
|
28
|
+
(c & 224) == 192 ? 2 : /* 110xxxxx */ \
|
|
29
|
+
(c & 240) == 224 ? 3 : /* 1110xxxx */ \
|
|
30
|
+
(c & 248) == 240 ? 4 : /* 11110xxx */ \
|
|
31
|
+
(c & 252) == 248 ? 5 : /* 111110xx */ \
|
|
32
|
+
(c & 254) == 252 ? 6 : /* 1111110x */ \
|
|
33
|
+
-1 /* error */ \
|
|
34
|
+
)
|
|
35
|
+
#endif
|
|
36
|
+
|
|
37
|
+
// clang-format on
|
|
38
|
+
|
|
39
|
+
#ifndef ZSV_UTF8_SUBSEQUENT_CHAR_OK
|
|
40
|
+
#define ZSV_UTF8_SUBSEQUENT_CHAR_OK(c) ((c & 192) == 128)
|
|
41
|
+
#endif
|
|
@@ -0,0 +1,25 @@
|
|
|
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_DL_WIN_H
|
|
10
|
+
#define ZSV_DL_WIN_H
|
|
11
|
+
|
|
12
|
+
#ifdef _WIN32
|
|
13
|
+
|
|
14
|
+
#include <windows.h>
|
|
15
|
+
|
|
16
|
+
#define RTLD_LAZY 0
|
|
17
|
+
void *dlopen(const char *dll_name, int flags);
|
|
18
|
+
|
|
19
|
+
void *dlsym(void *handle, const char *symbol);
|
|
20
|
+
|
|
21
|
+
int dlclose(void *handle);
|
|
22
|
+
|
|
23
|
+
#endif
|
|
24
|
+
|
|
25
|
+
#endif
|
|
@@ -0,0 +1,109 @@
|
|
|
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_WRITER_H
|
|
10
|
+
#define ZSV_WRITER_H
|
|
11
|
+
|
|
12
|
+
#include <stdio.h>
|
|
13
|
+
#include <stdint.h> // uint64_t
|
|
14
|
+
#include <zsv/common.h> // struct zsv_cell
|
|
15
|
+
|
|
16
|
+
#define ZSV_WRITER_NEW_ROW 1
|
|
17
|
+
#define ZSV_WRITER_SAME_ROW 0
|
|
18
|
+
|
|
19
|
+
/**
|
|
20
|
+
* csv writer options
|
|
21
|
+
*
|
|
22
|
+
* If `output_path` is non-NULL, `stream` must be NULL or stdout and `write` must be NULL or fwrite
|
|
23
|
+
***/
|
|
24
|
+
struct zsv_csv_writer_options {
|
|
25
|
+
char with_bom;
|
|
26
|
+
size_t (*write)(const void *restrict, size_t size, size_t nitems, void *restrict stream);
|
|
27
|
+
void *stream;
|
|
28
|
+
void (*table_init)(void *);
|
|
29
|
+
void *table_init_ctx;
|
|
30
|
+
const char *output_path; // if provided, will be created by zsv_writer_new() and closed by zsv_writer_delete()
|
|
31
|
+
void (*on_row)(void *);
|
|
32
|
+
void *on_row_ctx;
|
|
33
|
+
|
|
34
|
+
void (*on_delete)(void *);
|
|
35
|
+
void *on_delete_ctx;
|
|
36
|
+
};
|
|
37
|
+
|
|
38
|
+
void zsv_writer_set_default_opts(struct zsv_csv_writer_options opts);
|
|
39
|
+
struct zsv_csv_writer_options zsv_writer_get_default_opts(void);
|
|
40
|
+
|
|
41
|
+
enum zsv_writer_status {
|
|
42
|
+
zsv_writer_status_ok = 0,
|
|
43
|
+
zsv_writer_status_error,
|
|
44
|
+
zsv_writer_status_missing_handle,
|
|
45
|
+
zsv_writer_status_unknown = 999
|
|
46
|
+
};
|
|
47
|
+
|
|
48
|
+
struct zsv_writer_data;
|
|
49
|
+
typedef struct zsv_writer_data *zsv_csv_writer;
|
|
50
|
+
|
|
51
|
+
/**
|
|
52
|
+
* Get a new CSV writer
|
|
53
|
+
*
|
|
54
|
+
* @returns handle, or NULL on error, in which case errno will be set
|
|
55
|
+
*/
|
|
56
|
+
zsv_csv_writer zsv_writer_new(struct zsv_csv_writer_options *opts);
|
|
57
|
+
enum zsv_writer_status zsv_writer_delete(zsv_csv_writer w);
|
|
58
|
+
|
|
59
|
+
enum zsv_writer_status zsv_writer_flush(zsv_csv_writer w);
|
|
60
|
+
|
|
61
|
+
void zsv_writer_set_temp_buff(zsv_csv_writer w, unsigned char *buff, size_t buffsize);
|
|
62
|
+
|
|
63
|
+
enum zsv_writer_status zsv_writer_cell(zsv_csv_writer,
|
|
64
|
+
char new_row, // ZSV_WRITER_NEW_ROW or ZSV_WRITER_SAME_ROW
|
|
65
|
+
const unsigned char *s, size_t len, char check_if_needs_quoting);
|
|
66
|
+
|
|
67
|
+
/**
|
|
68
|
+
* Write an entire row of raw (pre-formatted) cells in one call.
|
|
69
|
+
* Skips per-cell quoting scans. Only valid when cells are already
|
|
70
|
+
* properly formatted (e.g., raw passthrough from the fast parser).
|
|
71
|
+
*/
|
|
72
|
+
enum zsv_writer_status zsv_writer_row_raw(zsv_csv_writer, const struct zsv_cell *cells, unsigned int count);
|
|
73
|
+
|
|
74
|
+
/*
|
|
75
|
+
* Get total bytes that have been written (to disk and buffer)
|
|
76
|
+
*/
|
|
77
|
+
uint64_t zsv_writer_cum_bytes_written(zsv_csv_writer);
|
|
78
|
+
|
|
79
|
+
unsigned char *zsv_writer_str_to_csv(const unsigned char *s, size_t len);
|
|
80
|
+
|
|
81
|
+
/*
|
|
82
|
+
* quote a CSV string, if needed
|
|
83
|
+
* @return NULL if no quoting required, `buff` if quoted value written to buff,
|
|
84
|
+
* or newly-allocated memory if buff not large enough (caller must free)
|
|
85
|
+
*/
|
|
86
|
+
unsigned char *zsv_csv_quote(const unsigned char *utf8_value, size_t len, unsigned char *buff, size_t buffsize);
|
|
87
|
+
|
|
88
|
+
// zsv_writer_cell convenience funcs: zsv_writer_cell_XX where XX = printf specifier
|
|
89
|
+
enum zsv_writer_status zsv_writer_cell_zu(zsv_csv_writer w, char new_row, size_t zu);
|
|
90
|
+
|
|
91
|
+
enum zsv_writer_status zsv_writer_cell_s(zsv_csv_writer w, char new_row, const unsigned char *s,
|
|
92
|
+
char check_if_needs_quoting);
|
|
93
|
+
|
|
94
|
+
enum zsv_writer_status zsv_writer_cell_Lf(zsv_csv_writer w, char new_row,
|
|
95
|
+
const char *fmt_spec, // provide X in %XLf e.g. ".2" or ""
|
|
96
|
+
long double ldbl);
|
|
97
|
+
|
|
98
|
+
// write a blank cell
|
|
99
|
+
enum zsv_writer_status zsv_writer_cell_blank(zsv_csv_writer w, char new_row);
|
|
100
|
+
|
|
101
|
+
/**
|
|
102
|
+
* set a value to be prepended to the next cell value that is written
|
|
103
|
+
*
|
|
104
|
+
* @param w handle to the writer
|
|
105
|
+
* @param s text value to write (can be NULL)
|
|
106
|
+
*/
|
|
107
|
+
void zsv_writer_cell_prepend(zsv_csv_writer w, const unsigned char *s);
|
|
108
|
+
|
|
109
|
+
#endif
|