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,149 @@
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_DIRS_H
10
+ #define ZSV_DIRS_H
11
+
12
+ #include <stdio.h>
13
+
14
+ /* Maximum length of file name */
15
+ #if !defined(FILENAME_MAX)
16
+ #define FILENAME_MAX MAX_PATH
17
+ #endif
18
+
19
+ /* file slash */
20
+ #if !defined(FILESLASH)
21
+ #ifdef _WIN32
22
+ #define FILESLASH '\\'
23
+ #else
24
+ #define FILESLASH '/'
25
+ #endif
26
+ #endif
27
+
28
+ /**
29
+ * Get os-independent configuration file path
30
+ * prefix should be determined at compile time e.g. /usr/local or ""
31
+ * @return length written to buff, or 0 if failed
32
+ */
33
+ size_t zsv_get_config_dir(char *buff, size_t buffsize, const char *prefix);
34
+
35
+ /**
36
+ * Get the path of the current executable
37
+ * @return length written to buff, or 0 if the path could not be determined
38
+ */
39
+ size_t zsv_get_executable_path(char *buff, size_t buffsize);
40
+
41
+ /**
42
+ * Get current user's home dir, without trailing slash
43
+ * On win, any backslashes are replaced with fwd slash
44
+ * ex: zsv_get_home_dir(char[MAX_PATH], MAX_PATH)
45
+ * returns 0 if no home dir could be found
46
+ * returns > 0 and < bufflen on success
47
+ * returns > 0 and >= bufflen if buffer was too small
48
+ */
49
+ int zsv_get_home_dir(char *buff, size_t bufflen);
50
+
51
+ /**
52
+ * Check if a directory exists
53
+ * return true (non-zero) or false (zero)
54
+ */
55
+ int zsv_dir_exists(const char *path);
56
+
57
+ /**
58
+ * mkdir that works with UNC paths on Win
59
+ * if fail, use zsv_perror() instead of perror()
60
+ */
61
+ int zsv_mkdir(const char *path);
62
+
63
+ /**
64
+ * Make a directory, as well as any intermediate dirs
65
+ * On non-Windows, new dirs are created with mode 0777, less the process umask
66
+ * return zero on success
67
+ */
68
+ int zsv_mkdirs(const char *path, char path_is_filename);
69
+
70
+ /**
71
+ * Recursively remove a directory and all of its contents
72
+ * return zero on success
73
+ */
74
+ int zsv_remove_dir_recursive(const unsigned char *path);
75
+
76
+ #include <sys/stat.h>
77
+
78
+ struct zsv_foreach_dirent_handle {
79
+ const char *parent; /* name of the parent directory */
80
+ const char *entry; /* file / dir name of current entry being processed */
81
+ const char *parent_and_entry; /* parent + entry separated by file separator */
82
+ const struct stat stat; /* stat of current entry */
83
+
84
+ void *ctx; /* caller-provided context to pass to handler */
85
+
86
+ unsigned char verbose : 1;
87
+ unsigned char is_dir : 1; /* non-zero if this entry is a directory */
88
+ unsigned char no_recurse : 1; /* set to 1 when handling a dir to prevent recursing into it */
89
+ unsigned char _ : 5;
90
+ };
91
+
92
+ typedef int (*zsv_foreach_dirent_handler)(struct zsv_foreach_dirent_handle *h, size_t depth);
93
+
94
+ /**
95
+ * Recursively process entries (files and folders) in a directory
96
+ *
97
+ * @param dir_path : path of directory to begin processing children of
98
+ * @param max_depth : maximum depth to recurse, or 0 for no maximum
99
+ * @param handler : caller-provided entry handler. return 0 on success, non-zero on error
100
+ * @param ctx : pointer passed to the handler
101
+ * @param verbose : non-zero for verbose output
102
+ *
103
+ * returns error
104
+ */
105
+ int zsv_foreach_dirent(const char *dir_path, size_t max_depth, zsv_foreach_dirent_handler handler, void *ctx,
106
+ char verbose);
107
+
108
+ struct zsv_dir_filter {
109
+ zsv_foreach_dirent_handler
110
+ filter; /* filter function; return 1 to process this node. if node is dir, return 0 to skip entirely */
111
+ size_t max_depth; /* max depth to recurse */
112
+ void *ctx; /* pointer to pass to filter function */
113
+ };
114
+
115
+ /**
116
+ * Convert directory contents into a single JSON file
117
+ * Output schema is a dictionary where key = path and value = contents
118
+ * Files named with .json suffix will be exported as JSON (content must be valid JSON)
119
+ * Files named with any other suffix will be exported as a single string value (do not try with large files)
120
+ *
121
+ * @param parent_dir : directory to export
122
+ * @param output_filename : file path to output to, or NULL to output to stdout
123
+ * @param file_filter : filter determining which files to export
124
+ */
125
+ int zsv_dir_to_json(const unsigned char *parent_dir, const unsigned char *output_filename,
126
+ struct zsv_dir_filter *file_filter, unsigned char verbose);
127
+
128
+ /**
129
+ * Convert a JSON stream into file and directory contents
130
+ * This function is the inverse of zsv_dir_to_json()
131
+ * Output schema is a dictionary where key = path and value = contents
132
+ * Files named with .json suffix will be exported as JSON (content must be valid JSON)
133
+ * Files named with any other suffix will be exported as a single string value (do not try with large files)
134
+ *
135
+ * @param target_dir : directory to create / import into
136
+ * @param : file path to output to, or NULL to output to stdout
137
+ * @flags : ZSV_DIR_FLAG_xxx values as defined below
138
+ *
139
+ * Returns 0 on success, non-zero on error
140
+ */
141
+
142
+ #define ZSV_DIR_FLAG_FORCE 1 /* overwrite target files if they already exist */
143
+ #define ZSV_DIR_FLAG_DRY 2 /* dry run, output names of files that would be created/overwritten */
144
+
145
+ int zsv_dir_from_json(const unsigned char *target_dir, FILE *fsrc,
146
+ unsigned int flags, // ZSV_DIR_FLAG_XX
147
+ unsigned char verbose);
148
+
149
+ #endif
@@ -0,0 +1,22 @@
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_H
10
+ #define ZSV_DL_H
11
+
12
+ #ifdef _WIN32
13
+ #include <zsv/utils/win/dl.h>
14
+ #define DL_SUFFIX "dll"
15
+ #else
16
+ #include <dlfcn.h>
17
+ #define DL_SUFFIX "so"
18
+ #endif
19
+
20
+ void (*zsv_dlsym(void *restrict handle, const char *restrict name))(void);
21
+
22
+ #endif
@@ -0,0 +1,28 @@
1
+ #ifndef LQ_FS_API_H
2
+ #define LQ_FS_API_H
3
+
4
+ typedef struct fs_data *fshandle;
5
+
6
+ fshandle fsopen(const char *fn, const char *mode);
7
+
8
+ int fsclose(fshandle h);
9
+
10
+ size_t fswrite(const void *ptr, size_t n, size_t m, fshandle h);
11
+
12
+ void stdflush(int addNewline);
13
+
14
+ size_t bytesum(const char *fn);
15
+
16
+ size_t fsread(void *ptr, size_t n, size_t m, fshandle h);
17
+
18
+ int fsrm(const char *fn);
19
+
20
+ int fsmkdir(const char *path);
21
+
22
+ int fsprint(const char *s);
23
+
24
+ int fsprinterr(const char *s);
25
+
26
+ char *fsls(const char *dir);
27
+
28
+ #endif
@@ -0,0 +1,22 @@
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_ERR_H
10
+ #define ZSV_ERR_H
11
+
12
+ int zsv_app_printerr(const char *appname, int eno, const char *fmt, ...);
13
+
14
+ #ifdef APPNAME
15
+ #define zsv_printerr(...) zsv_app_printerr(APPNAME, __VA_ARGS__)
16
+ #else
17
+ #pragma message("zsv_printerr defined without APPNAME")
18
+ #pragma message("to avoid this, include err.h after defining APPNAME to quoted string")
19
+ #define zsv_printerr(...) zsv_app_printerr(0x0, __VA_ARGS__)
20
+ #endif
21
+
22
+ #endif
@@ -0,0 +1,18 @@
1
+ #ifndef ZSV_MEMFILE_H_H
2
+ #define ZSV_MEMFILE_H_H
3
+
4
+ #include <stdio.h>
5
+ #include <stdlib.h>
6
+ #include <stdbool.h>
7
+
8
+ typedef struct zsv_memfile zsv_memfile;
9
+
10
+ // Public API Functions
11
+ zsv_memfile *zsv_memfile_open(size_t buffsz);
12
+ // fwrite() semantics: returns the number of complete items written (n on full success)
13
+ size_t zsv_memfile_write(const void *data, size_t sz, size_t n, zsv_memfile *zfm);
14
+ int zsv_memfile_rewind(zsv_memfile *zfm);
15
+ size_t zsv_memfile_read(void *buffer, size_t size, size_t nitems, zsv_memfile *zfm);
16
+ void zsv_memfile_close(zsv_memfile *zfm);
17
+
18
+ #endif // ZSV_FILEMEM_H_H
@@ -0,0 +1,125 @@
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_FILE_H
10
+ #define ZSV_FILE_H
11
+
12
+ #include <stdio.h>
13
+ #include "file-mem.h"
14
+
15
+ #ifndef LINEEND
16
+ #if defined(WIN32) || defined(_WIN64) || defined(_WIN32)
17
+ #define LINEEND "\r\n"
18
+ #else
19
+ #define LINEEND "\n"
20
+ #endif
21
+ #endif // LINEEND
22
+ /**
23
+ * Get a temp file name. The file is created (empty, mode 0600); the returned
24
+ * value, if any, will have been allocated on the heap, and the caller should
25
+ * `free()`.
26
+ *
27
+ * On POSIX the directory is `$TMPDIR`, falling back to the current directory
28
+ * when it is unset, empty, or unusable (a fallback warns on stderr). On Windows
29
+ * it is `GetTempPath()` (TMP/TEMP/USERPROFILE) with no fallback.
30
+ *
31
+ * @param prefix string with which the resulting file name will be prefixed
32
+ */
33
+ char *zsv_get_temp_filename(const char *prefix);
34
+
35
+ /**
36
+ * Get a temp file name for a caller that creates the file itself with
37
+ * exclusive-create semantics (`O_CREAT|O_EXCL`), such as the toonwriter /
38
+ * json2toon spill store. Same as `zsv_get_temp_filename()` except the
39
+ * placeholder file it creates is removed, so the caller's create succeeds.
40
+ * The caller's `O_EXCL` still fails closed, so hijacking the path means
41
+ * guessing the name within that window -- which is `mkstemp`-random on POSIX,
42
+ * but only weakly unpredictable on Windows, where `GetTempFileName()` derives
43
+ * it from the system time.
44
+ *
45
+ * @param prefix truncated to the 3 chars `zsv_get_temp_filename()` accepts,
46
+ * so a library's longer fixed prefix is safe to pass through
47
+ */
48
+ char *zsv_get_temp_filename_excl(const char *prefix);
49
+
50
+ /**
51
+ * Replacement for tmpfile().
52
+ * Returns filename; file must be manually removed after fclose
53
+ *
54
+ * @param mode optional mode passed to fopen(); if NULL, defaults to "wb"
55
+ */
56
+ FILE *zsv_tmpfile(const char *prefix, char **filename, const char *mode);
57
+
58
+ /**
59
+ * Check if a file exists and is readable (with fopen + "rb")
60
+ *
61
+ * @param filename
62
+ * @returns: true (1) if file exists
63
+ */
64
+ int zsv_file_exists(const char *filename);
65
+
66
+ /**
67
+ * Check whether an open stream is a regular file (as opposed to a pipe, FIFO,
68
+ * terminal or other non-seekable source). Returns 0 if it cannot be determined
69
+ */
70
+ int zsv_file_is_regular(FILE *f);
71
+
72
+ /**
73
+ * Check if a file exists and is readable (with fopen + "rb")
74
+ *
75
+ * @param filename
76
+ * @param err if file is not readbale, *err is set to a code as defined in errno.h
77
+ * @param f_out: if provided, on success, set to the opened file ptr
78
+ * @returns: true (1) if file exists and is readable
79
+ */
80
+ int zsv_file_readable(const char *filename, int *err, FILE **f_out);
81
+
82
+ /**
83
+ * Function that is the same as `fwrite()`, but can be used as a callback
84
+ * argument to `zsv_set_scan_filter()`
85
+ *
86
+ * @param FILEp pointer to a FILE object, cast as a void *, to write to
87
+ * @param buff pointer to the buffer of data to write
88
+ * @param bytes_read number of bytes in the buffer to write
89
+ */
90
+ size_t zsv_filter_write(void *FILEp, unsigned char *buff, size_t bytes_read);
91
+
92
+ /**
93
+ * Get a file path's directory length and base name
94
+ * Returns the length of the directory portion of the path
95
+ * and the base name portion of the path
96
+ */
97
+ size_t zsv_dir_len_basename(const char *filepath, const char **basename);
98
+
99
+ /**
100
+ * Copy a file. Create any needed directories
101
+ * On error, prints error message and returns non-zero
102
+ */
103
+ int zsv_copy_file(const char *src, const char *dest);
104
+
105
+ /**
106
+ * Copy a file, given source and destination FILE pointers
107
+ * Return error number per errno.h
108
+ */
109
+ int zsv_copy_file_ptr(FILE *src, FILE *dest);
110
+
111
+ /**
112
+ * Copy a file-like, given source and destination handles
113
+ * and read/write functions
114
+ * Return error number per errno.h
115
+ */
116
+ int zsv_copy_filelike_ptr(
117
+ FILE *src, size_t (*freadx)(void *restrict ptr, size_t size, size_t nitems, void *restrict stream), FILE *dest,
118
+ size_t (*fwritex)(const void *restrict ptr, size_t size, size_t nitems, void *restrict stream));
119
+ /**
120
+ * printf that does nothing. useful in certain circumstances for ignoring
121
+ * errors e.g. opening the same file twice and not dupicating error logs
122
+ */
123
+ int zsv_no_printf(void *, const char *format, ...);
124
+
125
+ #endif
@@ -0,0 +1,65 @@
1
+ #ifndef LQJQ_INTERNAL_H
2
+ #define LQJQ_INTERNAL_H
3
+
4
+ #include <ctype.h>
5
+ #include <jq.h>
6
+
7
+ enum zsv_jq_status {
8
+ zsv_jq_status_ok = 0,
9
+ zsv_jq_status_compile,
10
+ zsv_jq_status_memory,
11
+ zsv_jq_status_error
12
+ };
13
+
14
+ size_t zsv_jq_fwrite1(void *restrict FILE_ptr, const void *restrict buff, size_t len);
15
+
16
+ struct jv_to_json_ctx {
17
+ size_t (*write1)(void *restrict ctx, const void *restrict buff, size_t len); // e.g. zsv_jq_fwrite1
18
+ void *ctx; // e.g. FILE *
19
+ int flags; // passed on to jv_dumpf / jv_dump_string
20
+ char raw_output;
21
+ char output_started;
22
+ };
23
+
24
+ void jv_to_json_w_ctx(jv value, void *jv_to_json_ctx);
25
+
26
+ typedef struct zsv_jq_data *zsv_jq_handle;
27
+
28
+ zsv_jq_handle zsv_jq_new(const unsigned char *filter, void (*func)(jv, void *), void *ctx, enum zsv_jq_status *statusp);
29
+
30
+ // for streaming parsing
31
+ zsv_jq_handle zsv_jq_new_stream(const unsigned char *filter, void (*func)(jv, void *), void *ctx,
32
+ enum zsv_jq_status *statusp);
33
+
34
+ enum zsv_jq_status zsv_jq_parse_file(zsv_jq_handle h, FILE *f);
35
+
36
+ enum zsv_jq_status zsv_jq_parse(zsv_jq_handle restrict h, const void *restrict s, size_t len);
37
+
38
+ size_t zsv_jq_write(const char *s, size_t n, size_t m, zsv_jq_handle h);
39
+
40
+ enum zsv_jq_status zsv_jq_finish(zsv_jq_handle h);
41
+
42
+ void zsv_jq_trace(zsv_jq_handle h, FILE *);
43
+
44
+ void zsv_jq_delete(zsv_jq_handle h);
45
+
46
+ // helper functions
47
+ void jv_to_lqjq(jv value, void *zsv_jq_handle);
48
+
49
+ void jv_to_csv(jv value, void *file);
50
+
51
+ void jv_to_bool(jv value, void *char_result);
52
+
53
+ void jv_to_txt(jv value, void *file);
54
+
55
+ struct jv_to_csv_multi_ctx {
56
+ FILE *(*get_file)(void *ctx);
57
+ void *ctx;
58
+ };
59
+
60
+ // jv_to_csv_multi: write csv rows to different files
61
+ void jv_to_csv_multi(jv value, void *jv_to_csv_multi_ctx);
62
+
63
+ void jv_to_json_func(jv value, void *jv_to_json_func_dat);
64
+
65
+ #endif
@@ -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_JSON_H
10
+ #define ZSV_JSON_H
11
+
12
+ /**
13
+ * Convert a string to json (including the surrounding quotes)
14
+ * Returns newly-allocated string (caller must free())
15
+ */
16
+ unsigned char *zsv_json_from_str(const unsigned char *s);
17
+ unsigned char *zsv_json_from_str_n(const unsigned char *s, size_t len);
18
+
19
+ #endif
@@ -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_MEM_H
10
+ #define ZSV_MEM_H
11
+
12
+ /* zsv_memdup(): return copy with double-NULL terminator. caller must free() */
13
+ void *zsv_memdup(const void *src, size_t n);
14
+
15
+ #ifndef MEMDUP
16
+ #define MEMDUP zsv_memdup
17
+ #endif
18
+
19
+ #endif
@@ -0,0 +1,13 @@
1
+ #ifndef ZSV_MEMMEM_H
2
+ #define ZSV_MEMMEM_H
3
+
4
+ #ifdef NO_MEMMEM
5
+
6
+ #include <stdint.h>
7
+ void *zsvmemmem(const void *h0, size_t k, const void *n0, size_t l);
8
+
9
+ #define memmem zsvmemmem
10
+
11
+ #endif
12
+
13
+ #endif
@@ -0,0 +1,145 @@
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_OS_H
10
+ #define ZSV_OS_H
11
+
12
+ // the macros below expand to fopen()/remove(); a consumer including only this
13
+ // header would otherwise get an implicit declaration
14
+ #include <stdio.h>
15
+
16
+ void zsv_perror(const char *);
17
+
18
+ /*
19
+ * Path encoding on Windows: a path of MAX_PATH bytes or more, or whose absolute
20
+ * form is MAX_PATH UTF-16 units or more, and every path given to
21
+ * zsv_replace_file(), zsv_copy_permissions(), zsv_create_new_file(),
22
+ * zsv_same_file() or zsv_remove(), are read as UTF-8; a shorter path given to
23
+ * the CRT (fopen(), rename(), mkdir()), zsv_fopen() or zsv_dir_exists() is read
24
+ * in the process ANSI code page. zsv's executables make the two agree by setting
25
+ * the ANSI code page to UTF-8 with an application manifest (app/win/utf8.manifest
26
+ * in the zsv sources), which Windows 10 1903 and later honor. A program that
27
+ * links these utilities needs the same manifest for non-ASCII names to work at
28
+ * every length
29
+ */
30
+
31
+ /**
32
+ * zsv_fopen(): same as normal fopen(), except on Win it also works with long filenames
33
+ */
34
+ #ifndef _WIN32
35
+ #define zsv_fopen fopen
36
+ #else
37
+ FILE *zsv_fopen(const char *fname, const char *mode);
38
+ // NULL when original_path is empty, short and !always_prefix, not UTF-8, or on error
39
+ char *zsv_ensureLongPathPrefix(const char *original_path, unsigned char always_prefix);
40
+ #endif
41
+
42
+ /**
43
+ * zsv_mkstemp(): same as normal mkstemp(), which wasi-libc does not have
44
+ * (wasm has no temp directory concept). Creates and opens a file whose name is
45
+ * `tmpl` with its trailing 'X's (at least 6) replaced; `tmpl` is modified in
46
+ * place. Returns an open fd, or -1 with errno set.
47
+ *
48
+ * A function rather than a macro aliasing mkstemp(): glibc hides mkstemp()
49
+ * under -std=cNN, so a macro would leave this header un-self-sufficient for a
50
+ * consumer that has not set a feature-test macro.
51
+ */
52
+ int zsv_mkstemp(char *tmpl);
53
+
54
+ /**
55
+ * zsv_remove(): same as normal remove()
56
+ but for files only, and on Win it also works with long filenames
57
+ */
58
+ #ifndef _WIN32
59
+ #define zsv_remove remove
60
+ #else
61
+ int zsv_remove_winlp(const char *path_utf8);
62
+ #define zsv_remove zsv_remove_winlp
63
+ #endif
64
+
65
+ /**
66
+ * zsv_replace_file(): rename src over dest, copying across devices if needed.
67
+ * Returns 0, or an errno value that errno is also set to. On Windows both paths
68
+ * are UTF-8 and may exceed MAX_PATH; a path that is not UTF-8 gives EILSEQ
69
+ */
70
+ int zsv_replace_file(const char *src, const char *dest);
71
+
72
+ /**
73
+ * zsv_same_file(): whether both paths name the same file, by device and file id, so
74
+ * links and differently-spelled paths match. Returns 1 if they do, 0 if they do not
75
+ * (including when either does not exist), or -1 if that cannot be determined
76
+ */
77
+ int zsv_same_file(const char *a, const char *b);
78
+
79
+ /**
80
+ * zsv_copy_permissions(): give file `to` the permission bits of existing file `from`,
81
+ * and on POSIX its group. Where the group cannot be set, `to` gets no more group
82
+ * access than others have, so no other group gains access. On Windows the only bit
83
+ * is the read-only attribute. Use before `to` replaces `from`. Returns 0, or an
84
+ * errno value (on Windows, EILSEQ for a path that is not UTF-8)
85
+ */
86
+ int zsv_copy_permissions(const char *from, const char *to);
87
+
88
+ /**
89
+ * zsv_create_new_file(): create an empty file at `path`, which must not exist, with
90
+ * the permissions the system gives a new file (umask, directory defaults). Returns 0,
91
+ * or an errno value (EEXIST if `path` exists; on Windows, EILSEQ for a path that is
92
+ * not UTF-8)
93
+ */
94
+ int zsv_create_new_file(const char *path);
95
+
96
+ /**
97
+ * zsv_fsync(): flush a file descriptor's data to storage; 0 on success, else -1
98
+ * with errno set. A function for the reason zsv_mkstemp() is one
99
+ */
100
+ int zsv_fsync(int fd);
101
+
102
+ /**
103
+ * zsv_final_path(): the path of the file `path` names after following symbolic
104
+ * links, so that replacing it replaces that file rather than the link. A copy of
105
+ * `path` if it does not exist, or on Windows. Returns heap memory the caller frees,
106
+ * or NULL with errno set (ENOENT for a link to nothing)
107
+ */
108
+ char *zsv_final_path(const char *path);
109
+
110
+ /**
111
+ * ZSV_STDIN_IS_TTY(): nonzero when stdin is an interactive terminal
112
+ */
113
+ #ifndef _WIN32
114
+ #include <unistd.h>
115
+ #define ZSV_STDIN_IS_TTY() isatty(fileno(stdin))
116
+ #else
117
+ #include <io.h>
118
+ #define ZSV_STDIN_IS_TTY() _isatty(_fileno(stdin))
119
+ #endif
120
+
121
+ #ifdef _WIN32
122
+ #include <windows.h>
123
+
124
+ #ifndef ARRAY_SIZE
125
+ #define ARRAY_SIZE(array) (sizeof(array) / sizeof(array[0]))
126
+ #endif
127
+
128
+ // converts UTF-8 `path` into wbuf, cutting it off at PATH_MAX; wbuf is empty if the path
129
+ // is not UTF-8. For short names such as a DLL's; file paths go through the long-path helpers
130
+ void zsv_win_to_unicode(const void *path, wchar_t *wbuf, size_t wbuf_len);
131
+
132
+ #endif // #ifdef _WIN32
133
+
134
+ /**
135
+ * get number of cores
136
+ */
137
+ unsigned int zsv_get_number_of_cores(void);
138
+
139
+ /**
140
+ * zsv_now_ms(): milliseconds from a monotonic clock, for measuring elapsed time; only
141
+ * differences are meaningful. Returns 0 if the platform clock is unavailable, in which
142
+ * case a caller that compares two readings should treat the difference as unknown
143
+ */
144
+ unsigned long long zsv_now_ms(void);
145
+ #endif // ZSV_OS_H
@@ -0,0 +1,34 @@
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_OUTPUT_H
10
+ #define ZSV_OUTPUT_H
11
+
12
+ #include <stdlib.h>
13
+
14
+ enum zsv_output_format {
15
+ zsv_output_format_json = 0,
16
+ zsv_output_format_toon = 1
17
+ };
18
+
19
+ /*
20
+ * get_default_output_format(): the output format a command should use when the
21
+ * caller has not explicitly selected one. Returns TOON when the AI_AGENT
22
+ * environment variable is set to a non-blank value (so agents get token-dense
23
+ * TOON by default), otherwise JSON. An explicit command-line format option
24
+ * always overrides this.
25
+ */
26
+ static inline enum zsv_output_format get_default_output_format(void) {
27
+ const char *e = getenv("AI_AGENT");
28
+ if (e)
29
+ while (*e == ' ' || *e == '\t' || *e == '\r' || *e == '\n')
30
+ e++;
31
+ return e && *e ? zsv_output_format_toon : zsv_output_format_json;
32
+ }
33
+
34
+ #endif