zsv 1.3.0
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 +36 -0
- data/LICENSE +21 -0
- data/README.md +311 -0
- data/ext/zsv/common.h +34 -0
- data/ext/zsv/extconf.rb +137 -0
- data/ext/zsv/options.c +126 -0
- data/ext/zsv/options.h +31 -0
- data/ext/zsv/options_internal.h +8 -0
- data/ext/zsv/parser.c +300 -0
- data/ext/zsv/parser.h +62 -0
- data/ext/zsv/row.c +122 -0
- data/ext/zsv/row.h +39 -0
- data/ext/zsv/vendor/zsv-1.3.0/app/2db.c +756 -0
- data/ext/zsv/vendor/zsv-1.3.0/app/2json.c +381 -0
- data/ext/zsv/vendor/zsv-1.3.0/app/2tsv.c +228 -0
- data/ext/zsv/vendor/zsv-1.3.0/app/builtin/help.c +123 -0
- data/ext/zsv/vendor/zsv-1.3.0/app/builtin/license.c +39 -0
- data/ext/zsv/vendor/zsv-1.3.0/app/builtin/register.c +104 -0
- data/ext/zsv/vendor/zsv-1.3.0/app/builtin/thirdparty.c +41 -0
- data/ext/zsv/vendor/zsv-1.3.0/app/builtin/unregister.c +1 -0
- data/ext/zsv/vendor/zsv-1.3.0/app/builtin/version.c +14 -0
- data/ext/zsv/vendor/zsv-1.3.0/app/check/simdutf_wrapper.h +19 -0
- data/ext/zsv/vendor/zsv-1.3.0/app/check/utf8.c +116 -0
- data/ext/zsv/vendor/zsv-1.3.0/app/check.c +194 -0
- data/ext/zsv/vendor/zsv-1.3.0/app/cli.c +796 -0
- data/ext/zsv/vendor/zsv-1.3.0/app/cli_const.h +41 -0
- data/ext/zsv/vendor/zsv-1.3.0/app/cli_export.h +16 -0
- data/ext/zsv/vendor/zsv-1.3.0/app/cli_ini.c +280 -0
- data/ext/zsv/vendor/zsv-1.3.0/app/cli_internal.h +36 -0
- data/ext/zsv/vendor/zsv-1.3.0/app/compare.c +913 -0
- data/ext/zsv/vendor/zsv-1.3.0/app/compare.h +23 -0
- data/ext/zsv/vendor/zsv-1.3.0/app/compare_added_column.c +20 -0
- data/ext/zsv/vendor/zsv-1.3.0/app/compare_internal.h +140 -0
- data/ext/zsv/vendor/zsv-1.3.0/app/compare_sort.c +91 -0
- data/ext/zsv/vendor/zsv-1.3.0/app/compare_unique_colname.c +81 -0
- data/ext/zsv/vendor/zsv-1.3.0/app/count-pull.c +82 -0
- data/ext/zsv/vendor/zsv-1.3.0/app/count.c +404 -0
- data/ext/zsv/vendor/zsv-1.3.0/app/desc.c +569 -0
- data/ext/zsv/vendor/zsv-1.3.0/app/echo.c +365 -0
- data/ext/zsv/vendor/zsv-1.3.0/app/ext_example/my_extension.c +366 -0
- data/ext/zsv/vendor/zsv-1.3.0/app/ext_example/mysheet_extension.c +341 -0
- data/ext/zsv/vendor/zsv-1.3.0/app/ext_template/YOUR_EXTENSION_zsvext.c +263 -0
- data/ext/zsv/vendor/zsv-1.3.0/app/external/inih/ini.c +298 -0
- data/ext/zsv/vendor/zsv-1.3.0/app/external/inih/ini.h +157 -0
- data/ext/zsv/vendor/zsv-1.3.0/app/external/json_writer-1.01/json_numeric.c +177 -0
- data/ext/zsv/vendor/zsv-1.3.0/app/external/json_writer-1.01/jsonwriter.c +444 -0
- data/ext/zsv/vendor/zsv-1.3.0/app/external/json_writer-1.01/jsonwriter.h +145 -0
- data/ext/zsv/vendor/zsv-1.3.0/app/external/json_writer-1.01/utils.c +110 -0
- data/ext/zsv/vendor/zsv-1.3.0/app/external/memfile-1.0/include/memfile.h +15 -0
- data/ext/zsv/vendor/zsv-1.3.0/app/external/memfile-1.0/src/memfile.c +64 -0
- data/ext/zsv/vendor/zsv-1.3.0/app/external/sglib/sglib.h +1955 -0
- data/ext/zsv/vendor/zsv-1.3.0/app/external/simdutf/simdutf.h +6802 -0
- data/ext/zsv/vendor/zsv-1.3.0/app/external/sqlite3/sqlite3.c +230517 -0
- data/ext/zsv/vendor/zsv-1.3.0/app/external/sqlite3/sqlite3.h +12174 -0
- data/ext/zsv/vendor/zsv-1.3.0/app/external/sqlite3/sqlite3_and_csv_vtab.c +2 -0
- data/ext/zsv/vendor/zsv-1.3.0/app/external/sqlite3/sqlite3_csv_vtab-mem.c +142 -0
- data/ext/zsv/vendor/zsv-1.3.0/app/external/sqlite3/sqlite3_csv_vtab-mem.h +49 -0
- data/ext/zsv/vendor/zsv-1.3.0/app/external/sqlite3/sqlite3_csv_vtab-zsv.c +485 -0
- data/ext/zsv/vendor/zsv-1.3.0/app/external/sqlite3/sqlite3_csv_vtab.c +1015 -0
- data/ext/zsv/vendor/zsv-1.3.0/app/external/sqlite3/sqlite3ext.h +663 -0
- data/ext/zsv/vendor/zsv-1.3.0/app/external/sqlite3/vtab_helper.c +85 -0
- data/ext/zsv/vendor/zsv-1.3.0/app/external/yajl/build/yajl-2.1.1/include/yajl/yajl_common.h +75 -0
- data/ext/zsv/vendor/zsv-1.3.0/app/external/yajl/build/yajl-2.1.1/include/yajl/yajl_gen.h +167 -0
- data/ext/zsv/vendor/zsv-1.3.0/app/external/yajl/build/yajl-2.1.1/include/yajl/yajl_parse.h +228 -0
- data/ext/zsv/vendor/zsv-1.3.0/app/external/yajl/build/yajl-2.1.1/include/yajl/yajl_tree.h +186 -0
- data/ext/zsv/vendor/zsv-1.3.0/app/external/yajl/build/yajl-2.1.1/include/yajl/yajl_version.h +23 -0
- data/ext/zsv/vendor/zsv-1.3.0/app/external/yajl/src/api/yajl_common.h +76 -0
- data/ext/zsv/vendor/zsv-1.3.0/app/external/yajl/src/api/yajl_gen.h +167 -0
- data/ext/zsv/vendor/zsv-1.3.0/app/external/yajl/src/api/yajl_parse.h +238 -0
- data/ext/zsv/vendor/zsv-1.3.0/app/external/yajl/src/api/yajl_tree.h +186 -0
- data/ext/zsv/vendor/zsv-1.3.0/app/external/yajl/src/yajl.c +184 -0
- data/ext/zsv/vendor/zsv-1.3.0/app/external/yajl/src/yajl_alloc.c +52 -0
- data/ext/zsv/vendor/zsv-1.3.0/app/external/yajl/src/yajl_alloc.h +34 -0
- data/ext/zsv/vendor/zsv-1.3.0/app/external/yajl/src/yajl_buf.c +103 -0
- data/ext/zsv/vendor/zsv-1.3.0/app/external/yajl/src/yajl_buf.h +57 -0
- data/ext/zsv/vendor/zsv-1.3.0/app/external/yajl/src/yajl_bytestack.h +69 -0
- data/ext/zsv/vendor/zsv-1.3.0/app/external/yajl/src/yajl_encode.c +220 -0
- data/ext/zsv/vendor/zsv-1.3.0/app/external/yajl/src/yajl_encode.h +34 -0
- data/ext/zsv/vendor/zsv-1.3.0/app/external/yajl/src/yajl_gen.c +362 -0
- data/ext/zsv/vendor/zsv-1.3.0/app/external/yajl/src/yajl_lex.c +764 -0
- data/ext/zsv/vendor/zsv-1.3.0/app/external/yajl/src/yajl_lex.h +117 -0
- data/ext/zsv/vendor/zsv-1.3.0/app/external/yajl/src/yajl_parser.c +508 -0
- data/ext/zsv/vendor/zsv-1.3.0/app/external/yajl/src/yajl_parser.h +78 -0
- data/ext/zsv/vendor/zsv-1.3.0/app/external/yajl/src/yajl_tree.c +505 -0
- data/ext/zsv/vendor/zsv-1.3.0/app/external/yajl/src/yajl_version.c +7 -0
- data/ext/zsv/vendor/zsv-1.3.0/app/external/yajl_helper/yajl_helper/json_value.h +59 -0
- data/ext/zsv/vendor/zsv-1.3.0/app/external/yajl_helper/yajl_helper/yajl_helper.h +208 -0
- data/ext/zsv/vendor/zsv-1.3.0/app/external/yajl_helper/yajl_helper.c +795 -0
- data/ext/zsv/vendor/zsv-1.3.0/app/external/yajl_helper/yajl_helper_internal.h +28 -0
- data/ext/zsv/vendor/zsv-1.3.0/app/flatten.c +851 -0
- data/ext/zsv/vendor/zsv-1.3.0/app/jq.c +106 -0
- data/ext/zsv/vendor/zsv-1.3.0/app/jq.h +6 -0
- data/ext/zsv/vendor/zsv-1.3.0/app/mv.c +113 -0
- data/ext/zsv/vendor/zsv-1.3.0/app/noop.c +90 -0
- data/ext/zsv/vendor/zsv-1.3.0/app/overwrite.c +295 -0
- data/ext/zsv/vendor/zsv-1.3.0/app/paste.c +175 -0
- data/ext/zsv/vendor/zsv-1.3.0/app/pretty.c +693 -0
- data/ext/zsv/vendor/zsv-1.3.0/app/prop.c +980 -0
- data/ext/zsv/vendor/zsv-1.3.0/app/rm.c +131 -0
- data/ext/zsv/vendor/zsv-1.3.0/app/select/fixed.c +130 -0
- data/ext/zsv/vendor/zsv-1.3.0/app/select/internal.h +118 -0
- data/ext/zsv/vendor/zsv-1.3.0/app/select/parallel.c +45 -0
- data/ext/zsv/vendor/zsv-1.3.0/app/select/parallel.h +41 -0
- data/ext/zsv/vendor/zsv-1.3.0/app/select/processing.c +107 -0
- data/ext/zsv/vendor/zsv-1.3.0/app/select/rand.c +20 -0
- data/ext/zsv/vendor/zsv-1.3.0/app/select/regex.c +61 -0
- data/ext/zsv/vendor/zsv-1.3.0/app/select/search.c +14 -0
- data/ext/zsv/vendor/zsv-1.3.0/app/select/selection.c +192 -0
- data/ext/zsv/vendor/zsv-1.3.0/app/select/usage.c +72 -0
- data/ext/zsv/vendor/zsv-1.3.0/app/select-pull.c +812 -0
- data/ext/zsv/vendor/zsv-1.3.0/app/select.c +753 -0
- data/ext/zsv/vendor/zsv-1.3.0/app/serialize.c +372 -0
- data/ext/zsv/vendor/zsv-1.3.0/app/sheet/curses.h +15 -0
- data/ext/zsv/vendor/zsv-1.3.0/app/sheet/cursor.c +119 -0
- data/ext/zsv/vendor/zsv-1.3.0/app/sheet/errors.c +45 -0
- data/ext/zsv/vendor/zsv-1.3.0/app/sheet/file.c +63 -0
- data/ext/zsv/vendor/zsv-1.3.0/app/sheet/file.h +12 -0
- data/ext/zsv/vendor/zsv-1.3.0/app/sheet/filter.c +166 -0
- data/ext/zsv/vendor/zsv-1.3.0/app/sheet/handlers.c +214 -0
- data/ext/zsv/vendor/zsv-1.3.0/app/sheet/handlers_internal.h +128 -0
- data/ext/zsv/vendor/zsv-1.3.0/app/sheet/help.c +43 -0
- data/ext/zsv/vendor/zsv-1.3.0/app/sheet/index.c +81 -0
- data/ext/zsv/vendor/zsv-1.3.0/app/sheet/index.h +25 -0
- data/ext/zsv/vendor/zsv-1.3.0/app/sheet/key-bindings.c +325 -0
- data/ext/zsv/vendor/zsv-1.3.0/app/sheet/key-bindings.h +73 -0
- data/ext/zsv/vendor/zsv-1.3.0/app/sheet/lexer.c +203 -0
- data/ext/zsv/vendor/zsv-1.3.0/app/sheet/newline_handler.c +7 -0
- data/ext/zsv/vendor/zsv-1.3.0/app/sheet/pivot.c +318 -0
- data/ext/zsv/vendor/zsv-1.3.0/app/sheet/procedure.c +134 -0
- data/ext/zsv/vendor/zsv-1.3.0/app/sheet/procedure.h +119 -0
- data/ext/zsv/vendor/zsv-1.3.0/app/sheet/read-data.c +322 -0
- data/ext/zsv/vendor/zsv-1.3.0/app/sheet/screen_buffer.c +203 -0
- data/ext/zsv/vendor/zsv-1.3.0/app/sheet/screen_buffer.h +36 -0
- data/ext/zsv/vendor/zsv-1.3.0/app/sheet/sheet-sql.c +167 -0
- data/ext/zsv/vendor/zsv-1.3.0/app/sheet/sheet_internal.h +36 -0
- data/ext/zsv/vendor/zsv-1.3.0/app/sheet/sqlfilter.c +153 -0
- data/ext/zsv/vendor/zsv-1.3.0/app/sheet/terminfo.c +32 -0
- data/ext/zsv/vendor/zsv-1.3.0/app/sheet/transformation.c +312 -0
- data/ext/zsv/vendor/zsv-1.3.0/app/sheet/transformation.h +29 -0
- data/ext/zsv/vendor/zsv-1.3.0/app/sheet/ui_buffer.c +266 -0
- data/ext/zsv/vendor/zsv-1.3.0/app/sheet/usage.c +9 -0
- data/ext/zsv/vendor/zsv-1.3.0/app/sheet/utf8-width.c +60 -0
- data/ext/zsv/vendor/zsv-1.3.0/app/sheet.c +1007 -0
- data/ext/zsv/vendor/zsv-1.3.0/app/sql.c +453 -0
- data/ext/zsv/vendor/zsv-1.3.0/app/sql_internal.c +101 -0
- data/ext/zsv/vendor/zsv-1.3.0/app/sql_internal.h +49 -0
- data/ext/zsv/vendor/zsv-1.3.0/app/stack.c +393 -0
- data/ext/zsv/vendor/zsv-1.3.0/app/utils/arg.c +322 -0
- data/ext/zsv/vendor/zsv-1.3.0/app/utils/cache.c +228 -0
- data/ext/zsv/vendor/zsv-1.3.0/app/utils/cat.c +91 -0
- data/ext/zsv/vendor/zsv-1.3.0/app/utils/chunk.c +240 -0
- data/ext/zsv/vendor/zsv-1.3.0/app/utils/chunk.h +63 -0
- data/ext/zsv/vendor/zsv-1.3.0/app/utils/clock.c +57 -0
- data/ext/zsv/vendor/zsv-1.3.0/app/utils/db.c +148 -0
- data/ext/zsv/vendor/zsv-1.3.0/app/utils/dirs-no-jq.c +2 -0
- data/ext/zsv/vendor/zsv-1.3.0/app/utils/dirs.c +427 -0
- data/ext/zsv/vendor/zsv-1.3.0/app/utils/dirs_from_json.c +253 -0
- data/ext/zsv/vendor/zsv-1.3.0/app/utils/dirs_to_json.c +121 -0
- data/ext/zsv/vendor/zsv-1.3.0/app/utils/dl.c +20 -0
- data/ext/zsv/vendor/zsv-1.3.0/app/utils/emcc/fs_api.c +159 -0
- data/ext/zsv/vendor/zsv-1.3.0/app/utils/err.c +24 -0
- data/ext/zsv/vendor/zsv-1.3.0/app/utils/file-mem.c +180 -0
- data/ext/zsv/vendor/zsv-1.3.0/app/utils/file.c +256 -0
- data/ext/zsv/vendor/zsv-1.3.0/app/utils/index.c +197 -0
- data/ext/zsv/vendor/zsv-1.3.0/app/utils/index.h +49 -0
- data/ext/zsv/vendor/zsv-1.3.0/app/utils/jq.c +400 -0
- data/ext/zsv/vendor/zsv-1.3.0/app/utils/json.c +120 -0
- data/ext/zsv/vendor/zsv-1.3.0/app/utils/mem.c +18 -0
- data/ext/zsv/vendor/zsv-1.3.0/app/utils/memmem.c +132 -0
- data/ext/zsv/vendor/zsv-1.3.0/app/utils/os.c +178 -0
- data/ext/zsv/vendor/zsv-1.3.0/app/utils/overwrite.c +258 -0
- data/ext/zsv/vendor/zsv-1.3.0/app/utils/overwrite_writer.c +246 -0
- data/ext/zsv/vendor/zsv-1.3.0/app/utils/pcre2-8/pcre2-8-test.c +123 -0
- data/ext/zsv/vendor/zsv-1.3.0/app/utils/pcre2-8/pcre2-8.c +153 -0
- data/ext/zsv/vendor/zsv-1.3.0/app/utils/pcre2-8/pcre2-8.h +54 -0
- data/ext/zsv/vendor/zsv-1.3.0/app/utils/prop.c +267 -0
- data/ext/zsv/vendor/zsv-1.3.0/app/utils/signal.c +53 -0
- data/ext/zsv/vendor/zsv-1.3.0/app/utils/string.c +357 -0
- data/ext/zsv/vendor/zsv-1.3.0/app/utils/win/dir_exists_longpath.c +83 -0
- data/ext/zsv/vendor/zsv-1.3.0/app/utils/win/dl.c +33 -0
- data/ext/zsv/vendor/zsv-1.3.0/app/utils/win/fopen_longpath.c +184 -0
- data/ext/zsv/vendor/zsv-1.3.0/app/utils/win/foreach_dirent_longpath.c +292 -0
- data/ext/zsv/vendor/zsv-1.3.0/app/utils/win/io.c +259 -0
- data/ext/zsv/vendor/zsv-1.3.0/app/utils/win/io.h +13 -0
- data/ext/zsv/vendor/zsv-1.3.0/app/utils/win/mkdir_longpath.c +255 -0
- data/ext/zsv/vendor/zsv-1.3.0/app/utils/win/remove_longpath.c +96 -0
- data/ext/zsv/vendor/zsv-1.3.0/app/utils/writer.c +361 -0
- data/ext/zsv/vendor/zsv-1.3.0/app/zsv_command.h +40 -0
- data/ext/zsv/vendor/zsv-1.3.0/app/zsv_command_standalone.c +16 -0
- data/ext/zsv/vendor/zsv-1.3.0/app/zsv_main.h +44 -0
- data/ext/zsv/vendor/zsv-1.3.0/examples/js/zsv_parser_api_dummy.c +3 -0
- data/ext/zsv/vendor/zsv-1.3.0/examples/lib/parse_by_chunk.c +100 -0
- data/ext/zsv/vendor/zsv-1.3.0/examples/lib/print_my_column.c +143 -0
- data/ext/zsv/vendor/zsv-1.3.0/examples/lib/pull.c +89 -0
- data/ext/zsv/vendor/zsv-1.3.0/examples/lib/simple.c +123 -0
- data/ext/zsv/vendor/zsv-1.3.0/fuzz/fuzz.c +16 -0
- data/ext/zsv/vendor/zsv-1.3.0/include/zsv/api.h +336 -0
- data/ext/zsv/vendor/zsv-1.3.0/include/zsv/common.h +361 -0
- data/ext/zsv/vendor/zsv-1.3.0/include/zsv/ext/implementation.h +62 -0
- data/ext/zsv/vendor/zsv-1.3.0/include/zsv/ext/implementation_private.h +113 -0
- data/ext/zsv/vendor/zsv-1.3.0/include/zsv/ext/sheet.h +73 -0
- data/ext/zsv/vendor/zsv-1.3.0/include/zsv/ext.h +329 -0
- data/ext/zsv/vendor/zsv-1.3.0/include/zsv/utils/arg.h +90 -0
- data/ext/zsv/vendor/zsv-1.3.0/include/zsv/utils/cache.h +49 -0
- data/ext/zsv/vendor/zsv-1.3.0/include/zsv/utils/clock.h +36 -0
- data/ext/zsv/vendor/zsv-1.3.0/include/zsv/utils/compiler.h +58 -0
- data/ext/zsv/vendor/zsv-1.3.0/include/zsv/utils/db.h +19 -0
- data/ext/zsv/vendor/zsv-1.3.0/include/zsv/utils/dirs.h +147 -0
- data/ext/zsv/vendor/zsv-1.3.0/include/zsv/utils/dl.h +22 -0
- data/ext/zsv/vendor/zsv-1.3.0/include/zsv/utils/emcc/fs_api.h +28 -0
- data/ext/zsv/vendor/zsv-1.3.0/include/zsv/utils/err.h +22 -0
- data/ext/zsv/vendor/zsv-1.3.0/include/zsv/utils/file-mem.h +17 -0
- data/ext/zsv/vendor/zsv-1.3.0/include/zsv/utils/file.h +99 -0
- data/ext/zsv/vendor/zsv-1.3.0/include/zsv/utils/jq.h +65 -0
- data/ext/zsv/vendor/zsv-1.3.0/include/zsv/utils/json.h +19 -0
- data/ext/zsv/vendor/zsv-1.3.0/include/zsv/utils/mem.h +19 -0
- data/ext/zsv/vendor/zsv-1.3.0/include/zsv/utils/memmem.h +13 -0
- data/ext/zsv/vendor/zsv-1.3.0/include/zsv/utils/os.h +54 -0
- data/ext/zsv/vendor/zsv-1.3.0/include/zsv/utils/overwrite.h +71 -0
- data/ext/zsv/vendor/zsv-1.3.0/include/zsv/utils/overwrite_writer.h +53 -0
- data/ext/zsv/vendor/zsv-1.3.0/include/zsv/utils/prop.h +107 -0
- data/ext/zsv/vendor/zsv-1.3.0/include/zsv/utils/signal.h +18 -0
- data/ext/zsv/vendor/zsv-1.3.0/include/zsv/utils/sql.h +11 -0
- data/ext/zsv/vendor/zsv-1.3.0/include/zsv/utils/string.h +148 -0
- data/ext/zsv/vendor/zsv-1.3.0/include/zsv/utils/utf8.h +41 -0
- data/ext/zsv/vendor/zsv-1.3.0/include/zsv/utils/win/dl.h +25 -0
- data/ext/zsv/vendor/zsv-1.3.0/include/zsv/utils/writer.h +101 -0
- data/ext/zsv/vendor/zsv-1.3.0/include/zsv/zsv_export.h +33 -0
- data/ext/zsv/vendor/zsv-1.3.0/include/zsv.h +20 -0
- data/ext/zsv/vendor/zsv-1.3.0/src/vector_delim.c +60 -0
- data/ext/zsv/vendor/zsv-1.3.0/src/zsv.c +484 -0
- data/ext/zsv/vendor/zsv-1.3.0/src/zsv_internal.c +731 -0
- data/ext/zsv/vendor/zsv-1.3.0/src/zsv_scan_delim.c +285 -0
- data/ext/zsv/vendor/zsv-1.3.0/src/zsv_scan_fixed.c +88 -0
- data/ext/zsv/vendor/zsv-1.3.0/src/zsv_strencode.c +51 -0
- data/ext/zsv/zsv_ext.c +343 -0
- data/lib/zsv/version.rb +5 -0
- data/lib/zsv.rb +81 -0
- metadata +340 -0
|
@@ -0,0 +1,365 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* Copyright (C) 2021 Liquidaty and zsv contributors. All rights reserved.
|
|
3
|
+
*
|
|
4
|
+
* This file is part of zsv/lib, distributed under the MIT license as defined at
|
|
5
|
+
* https://opensource.org/licenses/MIT
|
|
6
|
+
*/
|
|
7
|
+
|
|
8
|
+
#include <stdio.h>
|
|
9
|
+
#include <stdlib.h>
|
|
10
|
+
#include <sqlite3.h>
|
|
11
|
+
|
|
12
|
+
#define _GNU_SOURCE 1
|
|
13
|
+
#include <string.h>
|
|
14
|
+
#include <ctype.h>
|
|
15
|
+
|
|
16
|
+
#define ZSV_COMMAND echo
|
|
17
|
+
#include "zsv_command.h"
|
|
18
|
+
|
|
19
|
+
#include <zsv/utils/compiler.h>
|
|
20
|
+
#include <zsv/utils/writer.h>
|
|
21
|
+
#include <zsv/utils/file.h>
|
|
22
|
+
#include <zsv/utils/string.h>
|
|
23
|
+
#include <zsv/utils/mem.h>
|
|
24
|
+
#include <zsv/utils/arg.h>
|
|
25
|
+
#include <zsv/utils/os.h>
|
|
26
|
+
#ifdef ZSV_EXTRAS
|
|
27
|
+
#include <zsv/utils/overwrite.h>
|
|
28
|
+
#endif
|
|
29
|
+
|
|
30
|
+
struct zsv_echo_data {
|
|
31
|
+
FILE *in;
|
|
32
|
+
const char *input_path;
|
|
33
|
+
zsv_csv_writer csv_writer;
|
|
34
|
+
zsv_parser parser;
|
|
35
|
+
size_t row_ix;
|
|
36
|
+
size_t start_row, end_row;
|
|
37
|
+
|
|
38
|
+
unsigned char *skip_until_prefix;
|
|
39
|
+
size_t skip_until_prefix_len;
|
|
40
|
+
|
|
41
|
+
char *tmp_fn;
|
|
42
|
+
unsigned max_nonempty_cols;
|
|
43
|
+
unsigned char trim_white : 1;
|
|
44
|
+
unsigned char trim_columns : 1;
|
|
45
|
+
unsigned char contiguous : 1;
|
|
46
|
+
unsigned char _ : 5;
|
|
47
|
+
};
|
|
48
|
+
|
|
49
|
+
static void zsv_echo_get_max_nonempty_cols(void *ctx) {
|
|
50
|
+
struct zsv_echo_data *data = ctx;
|
|
51
|
+
unsigned row_nonempty_col_count = 0;
|
|
52
|
+
for (size_t i = 0, j = zsv_cell_count(data->parser); i < j; i++) {
|
|
53
|
+
struct zsv_cell cell = zsv_get_cell(data->parser, i);
|
|
54
|
+
if (UNLIKELY(data->trim_white))
|
|
55
|
+
cell.str = (unsigned char *)zsv_strtrim(cell.str, &cell.len);
|
|
56
|
+
if (cell.len)
|
|
57
|
+
row_nonempty_col_count = i + 1;
|
|
58
|
+
}
|
|
59
|
+
if (data->max_nonempty_cols < row_nonempty_col_count)
|
|
60
|
+
data->max_nonempty_cols = row_nonempty_col_count;
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
static void zsv_echo_row(void *ctx) {
|
|
64
|
+
struct zsv_echo_data *data = ctx;
|
|
65
|
+
if (VERY_UNLIKELY((data->end_row > 0 && data->end_row <= data->row_ix))) {
|
|
66
|
+
zsv_abort(data->parser);
|
|
67
|
+
return;
|
|
68
|
+
}
|
|
69
|
+
size_t j = zsv_cell_count(data->parser);
|
|
70
|
+
if (UNLIKELY(data->trim_columns && j > data->max_nonempty_cols))
|
|
71
|
+
j = data->max_nonempty_cols;
|
|
72
|
+
|
|
73
|
+
if (VERY_UNLIKELY(data->row_ix == 0)) { // header
|
|
74
|
+
for (size_t i = 0; i < j; i++) {
|
|
75
|
+
struct zsv_cell cell = zsv_get_cell(data->parser, i);
|
|
76
|
+
if (UNLIKELY(data->trim_white))
|
|
77
|
+
cell.str = (unsigned char *)zsv_strtrim(cell.str, &cell.len);
|
|
78
|
+
zsv_writer_cell(data->csv_writer, i == 0, cell.str, cell.len, cell.quoted);
|
|
79
|
+
}
|
|
80
|
+
} else if (VERY_UNLIKELY(data->contiguous && zsv_row_is_blank(data->parser))) {
|
|
81
|
+
zsv_abort(data->parser);
|
|
82
|
+
} else {
|
|
83
|
+
for (size_t i = 0; i < j; i++) {
|
|
84
|
+
struct zsv_cell cell = zsv_get_cell(data->parser, i);
|
|
85
|
+
if (UNLIKELY(data->trim_white))
|
|
86
|
+
cell.str = (unsigned char *)zsv_strtrim(cell.str, &cell.len);
|
|
87
|
+
zsv_writer_cell(data->csv_writer, i == 0, cell.str, cell.len, cell.quoted);
|
|
88
|
+
}
|
|
89
|
+
}
|
|
90
|
+
data->row_ix++;
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
static void zsv_echo_row_skip_until(void *ctx) {
|
|
94
|
+
struct zsv_echo_data *data = ctx;
|
|
95
|
+
struct zsv_cell cell = zsv_get_cell(data->parser, 0);
|
|
96
|
+
if (cell.len && cell.str && cell.len >= data->skip_until_prefix_len &&
|
|
97
|
+
(!data->skip_until_prefix_len ||
|
|
98
|
+
!zsv_strincmp(cell.str, data->skip_until_prefix_len, data->skip_until_prefix, data->skip_until_prefix_len))) {
|
|
99
|
+
zsv_set_row_handler(data->parser, zsv_echo_row);
|
|
100
|
+
zsv_echo_row(ctx);
|
|
101
|
+
}
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
static void zsv_echo_row_start_at(void *ctx) {
|
|
105
|
+
struct zsv_echo_data *data = ctx;
|
|
106
|
+
if (data->row_ix >= data->start_row - 1) {
|
|
107
|
+
void (*row_handler)(void *) = data->skip_until_prefix ? zsv_echo_row_skip_until : zsv_echo_row;
|
|
108
|
+
zsv_set_row_handler(data->parser, row_handler);
|
|
109
|
+
row_handler(ctx);
|
|
110
|
+
} else
|
|
111
|
+
data->row_ix++;
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
const char *zsv_echo_usage_msg[] = {
|
|
115
|
+
#ifdef ZSV_EXTRAS
|
|
116
|
+
APPNAME ": write tabular input to stdout with optional cell overwrites",
|
|
117
|
+
#else
|
|
118
|
+
APPNAME ": write tabular input to stdout",
|
|
119
|
+
#endif
|
|
120
|
+
"",
|
|
121
|
+
"Usage: " APPNAME " [options] <filename>",
|
|
122
|
+
"",
|
|
123
|
+
"Options:",
|
|
124
|
+
" -b : output with BOM",
|
|
125
|
+
" -o <filename> : filename to save output to",
|
|
126
|
+
" --trim : trim whitespace",
|
|
127
|
+
" --trim-columns : trim blank columns",
|
|
128
|
+
" --contiguous : stop output upon scanning an entire row of blank values",
|
|
129
|
+
" --start-row <N> : only output from row N (starting at 1)",
|
|
130
|
+
" --end-row <N> : only output up to row N (starting at 1)",
|
|
131
|
+
" --between-rows <N> <M> : equivalent to --start-row N --end-row M",
|
|
132
|
+
" --skip-until <value> : skip rows until the row where first column starts with the given value",
|
|
133
|
+
#ifdef ZSV_EXTRAS
|
|
134
|
+
" --overwrite <source> : overwrite cells using given source",
|
|
135
|
+
"",
|
|
136
|
+
"For --overwrite, the <source> may be:",
|
|
137
|
+
"- sqlite3://<filename>[?sql=<query>]",
|
|
138
|
+
" e.g. sqlite3://overwrites.db?sql=select row, column, value from overwrites order by row, column",
|
|
139
|
+
"",
|
|
140
|
+
"- /path/to/file.csv",
|
|
141
|
+
" path to CSV file with columns row,col,val (in that order) and rows pre-sorted by row and column",
|
|
142
|
+
#endif
|
|
143
|
+
NULL,
|
|
144
|
+
};
|
|
145
|
+
|
|
146
|
+
static int zsv_echo_usage(void) {
|
|
147
|
+
for (size_t i = 0; zsv_echo_usage_msg[i]; i++)
|
|
148
|
+
fprintf(stdout, "%s\n", zsv_echo_usage_msg[i]);
|
|
149
|
+
return 1;
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
static void zsv_echo_cleanup(struct zsv_echo_data *data) {
|
|
153
|
+
zsv_writer_delete(data->csv_writer);
|
|
154
|
+
free(data->skip_until_prefix);
|
|
155
|
+
if (data->in && data->in != stdin)
|
|
156
|
+
fclose(data->in);
|
|
157
|
+
|
|
158
|
+
if (data->tmp_fn) {
|
|
159
|
+
zsv_remove(data->tmp_fn);
|
|
160
|
+
free(data->tmp_fn);
|
|
161
|
+
}
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
int ZSV_MAIN_FUNC(ZSV_COMMAND)(int argc, const char *argv[], struct zsv_opts *optsp,
|
|
165
|
+
struct zsv_prop_handler *custom_prop_handler) {
|
|
166
|
+
if (argc < 1 || (argc > 1 && (!strcmp(argv[1], "-h") || !strcmp(argv[1], "--help")))) {
|
|
167
|
+
zsv_echo_usage();
|
|
168
|
+
return 0;
|
|
169
|
+
}
|
|
170
|
+
struct zsv_opts opts = *optsp;
|
|
171
|
+
struct zsv_csv_writer_options writer_opts = zsv_writer_get_default_opts();
|
|
172
|
+
struct zsv_echo_data data = {0};
|
|
173
|
+
#ifdef ZSV_EXTRAS
|
|
174
|
+
struct zsv_overwrite_opts overwrite_opts = {0};
|
|
175
|
+
#endif
|
|
176
|
+
int err = 0;
|
|
177
|
+
|
|
178
|
+
// temporary structure for --between parameters
|
|
179
|
+
struct {
|
|
180
|
+
size_t start_row;
|
|
181
|
+
size_t end_row;
|
|
182
|
+
} between = {0};
|
|
183
|
+
|
|
184
|
+
for (int arg_i = 1; !err && arg_i < argc; arg_i++) {
|
|
185
|
+
const char *arg = argv[arg_i];
|
|
186
|
+
if (!strcmp(arg, "-b"))
|
|
187
|
+
writer_opts.with_bom = 1;
|
|
188
|
+
else if (!strcmp(arg, "-o"))
|
|
189
|
+
writer_opts.output_path = zsv_next_arg(++arg_i, argc, argv, &err);
|
|
190
|
+
else if (!strcmp(arg, "--contiguous"))
|
|
191
|
+
data.contiguous = 1;
|
|
192
|
+
else if (!strcmp(arg, "--trim-columns"))
|
|
193
|
+
data.trim_columns = 1;
|
|
194
|
+
else if (!strcmp(arg, "--trim"))
|
|
195
|
+
data.trim_white = 1;
|
|
196
|
+
else if (!strcmp(arg, "--skip-until")) {
|
|
197
|
+
if (++arg_i >= argc)
|
|
198
|
+
err = zsv_printerr(1, "Option %s requires a value\n", arg);
|
|
199
|
+
else if (!argv[arg_i][0])
|
|
200
|
+
err = zsv_printerr(1, "--skip-until requires a non-empty value\n");
|
|
201
|
+
else {
|
|
202
|
+
free(data.skip_until_prefix);
|
|
203
|
+
data.skip_until_prefix = (unsigned char *)strdup(argv[arg_i]);
|
|
204
|
+
data.skip_until_prefix_len = data.skip_until_prefix ? strlen((char *)data.skip_until_prefix) : 0;
|
|
205
|
+
}
|
|
206
|
+
#ifdef ZSV_EXTRAS
|
|
207
|
+
} else if (!strcmp(arg, "--overwrite")) {
|
|
208
|
+
overwrite_opts.src = zsv_next_arg(++arg_i, argc, argv, &err);
|
|
209
|
+
#endif
|
|
210
|
+
} else if (!strcmp(arg, "--end-row") || !strcmp(arg, "--start-row") || !strcmp(arg, "--between-row") ||
|
|
211
|
+
!strcmp(arg, "--between-rows")) {
|
|
212
|
+
const char *val = zsv_next_arg(++arg_i, argc, argv, &err);
|
|
213
|
+
if (!val || !*val || !(atol(val) > 0)) {
|
|
214
|
+
if (!strcmp(arg, "--between-row") || !strcmp(arg, "--between-rows"))
|
|
215
|
+
err = zsv_printerr(1, "%s requires two integer values 0 < N < M\n", arg);
|
|
216
|
+
else
|
|
217
|
+
err = zsv_printerr(1, "%s requires an integer value > 0\n", arg);
|
|
218
|
+
} else if (!strcmp(arg, "--end-row"))
|
|
219
|
+
data.end_row = (size_t)atol(val);
|
|
220
|
+
else if (!strcmp(arg, "--start-row"))
|
|
221
|
+
data.start_row = (size_t)atol(val);
|
|
222
|
+
else if (!strcmp(arg, "--between-row") || !strcmp(arg, "--between-rows")) {
|
|
223
|
+
between.start_row = (size_t)atol(val);
|
|
224
|
+
val = zsv_next_arg(++arg_i, argc, argv, &err);
|
|
225
|
+
if (!val || !*val || !(atol(val) > (long)data.start_row))
|
|
226
|
+
err = zsv_printerr(1, "%s requires two integer values 0 < N < M\n", arg);
|
|
227
|
+
else
|
|
228
|
+
between.end_row = (size_t)atol(val);
|
|
229
|
+
}
|
|
230
|
+
} else if (!data.in) {
|
|
231
|
+
#ifndef NO_STDIN
|
|
232
|
+
if (!strcmp(arg, "-"))
|
|
233
|
+
data.in = stdin;
|
|
234
|
+
#endif
|
|
235
|
+
if (!data.in) {
|
|
236
|
+
if (!(data.in = fopen(arg, "rb"))) {
|
|
237
|
+
err = 1;
|
|
238
|
+
perror(arg);
|
|
239
|
+
} else
|
|
240
|
+
data.input_path = arg;
|
|
241
|
+
}
|
|
242
|
+
} else
|
|
243
|
+
err = zsv_printerr(1, "Unrecognized option: %s\n", arg);
|
|
244
|
+
}
|
|
245
|
+
|
|
246
|
+
if (!err && !data.in) {
|
|
247
|
+
#ifndef NO_STDIN
|
|
248
|
+
data.in = stdin;
|
|
249
|
+
#else
|
|
250
|
+
err = zsv_printerr(1, "Please specify an input file\n");
|
|
251
|
+
#endif
|
|
252
|
+
}
|
|
253
|
+
|
|
254
|
+
if (!err && between.start_row) {
|
|
255
|
+
if (data.start_row || data.end_row)
|
|
256
|
+
err = zsv_printerr(1, "--between cannot be used together with --start-row or --end-row");
|
|
257
|
+
else {
|
|
258
|
+
data.start_row = between.start_row;
|
|
259
|
+
data.end_row = between.end_row;
|
|
260
|
+
}
|
|
261
|
+
}
|
|
262
|
+
if (data.end_row > 0 && data.end_row < data.start_row)
|
|
263
|
+
err = zsv_printerr(1, "--start-row must be less than --end-row");
|
|
264
|
+
|
|
265
|
+
if (err) {
|
|
266
|
+
zsv_echo_cleanup(&data);
|
|
267
|
+
return 1;
|
|
268
|
+
}
|
|
269
|
+
|
|
270
|
+
unsigned char buff[4096];
|
|
271
|
+
|
|
272
|
+
if (data.start_row)
|
|
273
|
+
opts.row_handler = zsv_echo_row_start_at;
|
|
274
|
+
else if (data.skip_until_prefix)
|
|
275
|
+
opts.row_handler = zsv_echo_row_skip_until;
|
|
276
|
+
else {
|
|
277
|
+
opts.row_handler = zsv_echo_row;
|
|
278
|
+
if (data.trim_columns) {
|
|
279
|
+
// trim columns requires two passes, because we may need to read the entire table
|
|
280
|
+
// to know the maximum number of non-empty columns (e.g. the last row might contain
|
|
281
|
+
// more non-empty columns than the rest of the table
|
|
282
|
+
|
|
283
|
+
// first, save the file if it is stdin
|
|
284
|
+
if (data.in == stdin) {
|
|
285
|
+
if (!(data.tmp_fn = zsv_get_temp_filename("zsv_echo_XXXXXXXX"))) {
|
|
286
|
+
zsv_echo_cleanup(&data);
|
|
287
|
+
return 1;
|
|
288
|
+
}
|
|
289
|
+
|
|
290
|
+
FILE *f = fopen(data.tmp_fn, "wb");
|
|
291
|
+
if (!f) {
|
|
292
|
+
perror(data.tmp_fn);
|
|
293
|
+
zsv_echo_cleanup(&data);
|
|
294
|
+
return 1;
|
|
295
|
+
} else {
|
|
296
|
+
size_t bytes_read;
|
|
297
|
+
while ((bytes_read = fread(buff, 1, sizeof(buff), data.in)) > 0)
|
|
298
|
+
fwrite(buff, 1, bytes_read, f);
|
|
299
|
+
fclose(f);
|
|
300
|
+
if (!(data.in = fopen(data.tmp_fn, "rb"))) {
|
|
301
|
+
perror(data.tmp_fn);
|
|
302
|
+
zsv_echo_cleanup(&data);
|
|
303
|
+
return 1;
|
|
304
|
+
}
|
|
305
|
+
}
|
|
306
|
+
}
|
|
307
|
+
|
|
308
|
+
// next, determine the max number of columns from the left that contain data
|
|
309
|
+
struct zsv_opts tmp_opts = opts;
|
|
310
|
+
tmp_opts.row_handler = zsv_echo_get_max_nonempty_cols;
|
|
311
|
+
tmp_opts.stream = data.in;
|
|
312
|
+
tmp_opts.ctx = &data;
|
|
313
|
+
if (zsv_new_with_properties(&tmp_opts, custom_prop_handler, data.input_path, &data.parser) != zsv_status_ok) {
|
|
314
|
+
zsv_echo_cleanup(&data);
|
|
315
|
+
return 1;
|
|
316
|
+
} else {
|
|
317
|
+
// find the max nonempty col count
|
|
318
|
+
enum zsv_status status;
|
|
319
|
+
while (!zsv_signal_interrupted && (status = zsv_parse_more(data.parser)) == zsv_status_ok)
|
|
320
|
+
;
|
|
321
|
+
zsv_finish(data.parser);
|
|
322
|
+
zsv_delete(data.parser);
|
|
323
|
+
data.parser = NULL;
|
|
324
|
+
|
|
325
|
+
// re-open the input again
|
|
326
|
+
data.in = fopen(data.tmp_fn ? data.tmp_fn : data.input_path, "rb");
|
|
327
|
+
}
|
|
328
|
+
}
|
|
329
|
+
}
|
|
330
|
+
opts.stream = data.in;
|
|
331
|
+
opts.ctx = &data;
|
|
332
|
+
|
|
333
|
+
data.csv_writer = zsv_writer_new(&writer_opts);
|
|
334
|
+
#ifdef ZSV_EXTRAS
|
|
335
|
+
if (overwrite_opts.src) {
|
|
336
|
+
if (!(opts.overwrite.ctx = zsv_overwrite_context_new(&overwrite_opts)))
|
|
337
|
+
err = zsv_printerr(1, "Out of memory!\n");
|
|
338
|
+
else {
|
|
339
|
+
opts.overwrite.open = zsv_overwrite_open;
|
|
340
|
+
opts.overwrite.next = zsv_overwrite_next;
|
|
341
|
+
opts.overwrite.close = zsv_overwrite_context_delete;
|
|
342
|
+
}
|
|
343
|
+
}
|
|
344
|
+
#endif
|
|
345
|
+
if (data.csv_writer && !err) {
|
|
346
|
+
if (zsv_new_with_properties(&opts, custom_prop_handler, data.input_path, &data.parser) != zsv_status_ok)
|
|
347
|
+
err = 1;
|
|
348
|
+
else {
|
|
349
|
+
// create a local csv writer buff for faster performance
|
|
350
|
+
// unsigned char writer_buff[64];
|
|
351
|
+
zsv_writer_set_temp_buff(data.csv_writer, buff, sizeof(buff));
|
|
352
|
+
|
|
353
|
+
// process the input data
|
|
354
|
+
zsv_handle_ctrl_c_signal();
|
|
355
|
+
enum zsv_status status;
|
|
356
|
+
while (!zsv_signal_interrupted && (status = zsv_parse_more(data.parser)) == zsv_status_ok)
|
|
357
|
+
;
|
|
358
|
+
|
|
359
|
+
zsv_finish(data.parser);
|
|
360
|
+
zsv_delete(data.parser);
|
|
361
|
+
}
|
|
362
|
+
}
|
|
363
|
+
zsv_echo_cleanup(&data);
|
|
364
|
+
return err;
|
|
365
|
+
}
|