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,253 @@
|
|
|
1
|
+
#include <yajl_helper/yajl_helper.h>
|
|
2
|
+
#include <zsv/utils/file.h>
|
|
3
|
+
#include <zsv/utils/os.h>
|
|
4
|
+
|
|
5
|
+
struct zsv_dir_from_json_ctx {
|
|
6
|
+
const char *filepath_prefix;
|
|
7
|
+
unsigned char buff[4096];
|
|
8
|
+
size_t content_start;
|
|
9
|
+
FILE *out;
|
|
10
|
+
char *out_filepath;
|
|
11
|
+
struct jv_to_json_ctx jctx;
|
|
12
|
+
zsv_jq_handle zjq;
|
|
13
|
+
|
|
14
|
+
int err;
|
|
15
|
+
unsigned char in_obj : 1;
|
|
16
|
+
unsigned char do_check : 1;
|
|
17
|
+
unsigned char dry : 1;
|
|
18
|
+
unsigned char _ : 5;
|
|
19
|
+
};
|
|
20
|
+
|
|
21
|
+
static void zsv_dir_from_json_close_out(struct zsv_dir_from_json_ctx *ctx) {
|
|
22
|
+
if (ctx->zjq) {
|
|
23
|
+
zsv_jq_finish(ctx->zjq);
|
|
24
|
+
zsv_jq_delete(ctx->zjq);
|
|
25
|
+
ctx->zjq = NULL;
|
|
26
|
+
}
|
|
27
|
+
if (ctx->out) {
|
|
28
|
+
fclose(ctx->out);
|
|
29
|
+
ctx->out = NULL;
|
|
30
|
+
free(ctx->out_filepath);
|
|
31
|
+
ctx->out_filepath = NULL;
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
static int zsv_dir_from_json_map_key(yajl_helper_t yh, const unsigned char *s, size_t len) {
|
|
36
|
+
if (yajl_helper_level(yh) == 1 && len) { // new property file entry
|
|
37
|
+
struct zsv_dir_from_json_ctx *ctx = yajl_helper_ctx(yh);
|
|
38
|
+
|
|
39
|
+
char *fn = NULL;
|
|
40
|
+
if (ctx->filepath_prefix)
|
|
41
|
+
asprintf(&fn, "%s%c%.*s", ctx->filepath_prefix, FILESLASH, (int)len, s);
|
|
42
|
+
else
|
|
43
|
+
asprintf(&fn, "%.*s", (int)len, s);
|
|
44
|
+
|
|
45
|
+
// if we have any backslashes, replace with fwd slash
|
|
46
|
+
if (fn)
|
|
47
|
+
for (int i = 0, j = strlen(fn); i < j; i++)
|
|
48
|
+
if (fn[i] == '\\')
|
|
49
|
+
fn[i] = '/';
|
|
50
|
+
if (!fn) {
|
|
51
|
+
errno = ENOMEM;
|
|
52
|
+
perror(NULL);
|
|
53
|
+
} else if (ctx->do_check) {
|
|
54
|
+
// we just want to check if the destination file exists
|
|
55
|
+
if (access(fn, F_OK) != -1) { // it exists
|
|
56
|
+
ctx->err = errno = EEXIST;
|
|
57
|
+
perror(fn);
|
|
58
|
+
}
|
|
59
|
+
} else if (ctx->dry) { // just output the name of the file
|
|
60
|
+
printf("%s\n", fn);
|
|
61
|
+
} else if (zsv_mkdirs(fn, 1)) {
|
|
62
|
+
fprintf(stderr, "Unable to create directories for %s\n", fn);
|
|
63
|
+
} else if (!((ctx->out = zsv_fopen(fn, "wb")))) {
|
|
64
|
+
perror(fn);
|
|
65
|
+
} else {
|
|
66
|
+
ctx->out_filepath = fn;
|
|
67
|
+
fn = NULL;
|
|
68
|
+
|
|
69
|
+
// if it's a JSON file, use a jq filter to pretty-print
|
|
70
|
+
if (strlen(ctx->out_filepath) > 5 &&
|
|
71
|
+
!zsv_stricmp((const unsigned char *)ctx->out_filepath + strlen(ctx->out_filepath) - 5,
|
|
72
|
+
(const unsigned char *)".json")) {
|
|
73
|
+
ctx->jctx.write1 = zsv_jq_fwrite1;
|
|
74
|
+
ctx->jctx.ctx = ctx->out;
|
|
75
|
+
ctx->jctx.flags = JV_PRINT_PRETTY | JV_PRINT_SPACE1;
|
|
76
|
+
enum zsv_jq_status jqstat;
|
|
77
|
+
ctx->zjq = zsv_jq_new((const unsigned char *)".", jv_to_json_func, &ctx->jctx, &jqstat);
|
|
78
|
+
if (!ctx->zjq) {
|
|
79
|
+
fprintf(stderr, "zsv_jq_new: unable to open for %s\n", ctx->out_filepath);
|
|
80
|
+
zsv_dir_from_json_close_out(ctx);
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
free(fn);
|
|
85
|
+
}
|
|
86
|
+
return 1;
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
static int zsv_dir_from_json_start_obj(yajl_helper_t yh) {
|
|
90
|
+
if (yajl_helper_level(yh) == 2) {
|
|
91
|
+
struct zsv_dir_from_json_ctx *ctx = yajl_helper_ctx(yh);
|
|
92
|
+
ctx->in_obj = 1;
|
|
93
|
+
ctx->content_start = yajl_get_bytes_consumed(yajl_helper_yajl(yh)) - 1;
|
|
94
|
+
}
|
|
95
|
+
return 1;
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
// zsv_dir_from_json_flush(): return err
|
|
99
|
+
static int zsv_dir_from_json_flush(yajl_handle yajl, struct zsv_dir_from_json_ctx *ctx) {
|
|
100
|
+
if (ctx->zjq) {
|
|
101
|
+
size_t current_position = yajl_get_bytes_consumed(yajl);
|
|
102
|
+
if (current_position <= ctx->content_start)
|
|
103
|
+
fprintf(stderr, "Error! zsv_dir_from_json_flush unexpected current position\n");
|
|
104
|
+
else
|
|
105
|
+
zsv_jq_parse(ctx->zjq, ctx->buff + ctx->content_start, current_position - ctx->content_start);
|
|
106
|
+
ctx->content_start = 0;
|
|
107
|
+
}
|
|
108
|
+
return 0;
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
static int zsv_dir_from_json_end_obj(yajl_helper_t yh) {
|
|
112
|
+
if (yajl_helper_level(yh) == 1) { // just finished level 2
|
|
113
|
+
struct zsv_dir_from_json_ctx *ctx = yajl_helper_ctx(yh);
|
|
114
|
+
zsv_dir_from_json_flush(yajl_helper_yajl(yh), yajl_helper_ctx(yh));
|
|
115
|
+
zsv_dir_from_json_close_out(ctx);
|
|
116
|
+
ctx->in_obj = 0;
|
|
117
|
+
}
|
|
118
|
+
return 1;
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
static int zsv_dir_from_json_process_value(yajl_helper_t yh, struct json_value *value) {
|
|
122
|
+
if (yajl_helper_level(yh) == 1) { // just finished level 2
|
|
123
|
+
struct zsv_dir_from_json_ctx *ctx = yajl_helper_ctx(yh);
|
|
124
|
+
const unsigned char *jsstr;
|
|
125
|
+
size_t len;
|
|
126
|
+
json_value_default_string(value, &jsstr, &len);
|
|
127
|
+
if (ctx->zjq) {
|
|
128
|
+
unsigned char *js = len ? zsv_json_from_str_n(jsstr, len) : NULL;
|
|
129
|
+
if (js)
|
|
130
|
+
zsv_jq_parse(ctx->zjq, js, strlen((char *)js));
|
|
131
|
+
else
|
|
132
|
+
zsv_jq_parse(ctx->zjq, "null", 4);
|
|
133
|
+
free(js);
|
|
134
|
+
} else if (len && ctx->out)
|
|
135
|
+
fwrite(jsstr, 1, len, ctx->out);
|
|
136
|
+
zsv_dir_from_json_close_out(ctx);
|
|
137
|
+
}
|
|
138
|
+
return 1;
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
/**
|
|
142
|
+
* Convert a JSON stream into file and directory contents
|
|
143
|
+
* This function is the inverse of zsv_dir_to_json()
|
|
144
|
+
* Output schema is a dictionary where key = path and value = contents
|
|
145
|
+
* Files named with .json suffix will be exported as JSON (content must be valid JSON)
|
|
146
|
+
* Files named with any other suffix will be exported as a single string value (do not try with large files)
|
|
147
|
+
*/
|
|
148
|
+
int zsv_dir_from_json(const unsigned char *target_dir, FILE *src,
|
|
149
|
+
unsigned int flags, // ZSV_DIR_FLAG_XX
|
|
150
|
+
unsigned char _verbose) {
|
|
151
|
+
(void)(_verbose);
|
|
152
|
+
int err = 0;
|
|
153
|
+
int force = !!(flags & ZSV_DIR_FLAG_FORCE);
|
|
154
|
+
int dry = !!(flags & ZSV_DIR_FLAG_DRY);
|
|
155
|
+
char *tmp_fn = NULL; // only used if force = 0 and src == stdin
|
|
156
|
+
if (!force) {
|
|
157
|
+
// if input is stdin, we'll need to read it twice, so save it first
|
|
158
|
+
// this isn't the most efficient way to do it, as it reads it 3 times
|
|
159
|
+
// but it's easier and the diff is immaterial
|
|
160
|
+
if (src == stdin) {
|
|
161
|
+
src = NULL;
|
|
162
|
+
tmp_fn = zsv_get_temp_filename("zsv_prop_XXXXXXXX");
|
|
163
|
+
FILE *tmp_f;
|
|
164
|
+
if (!tmp_fn) {
|
|
165
|
+
err = errno = ENOMEM;
|
|
166
|
+
perror(NULL);
|
|
167
|
+
} else if (!(tmp_f = zsv_fopen(tmp_fn, "wb"))) {
|
|
168
|
+
err = errno;
|
|
169
|
+
perror(tmp_fn);
|
|
170
|
+
} else {
|
|
171
|
+
err = zsv_copy_file_ptr(stdin, tmp_f);
|
|
172
|
+
fclose(tmp_f);
|
|
173
|
+
if (!(src = zsv_fopen(tmp_fn, "rb"))) {
|
|
174
|
+
err = errno;
|
|
175
|
+
perror(tmp_fn);
|
|
176
|
+
}
|
|
177
|
+
}
|
|
178
|
+
}
|
|
179
|
+
}
|
|
180
|
+
|
|
181
|
+
if (!err) {
|
|
182
|
+
// we will run this loop either once (force) or twice (no force):
|
|
183
|
+
// 1. check before running (no force)
|
|
184
|
+
// 2. do the import
|
|
185
|
+
char do_check = !force;
|
|
186
|
+
if (do_check && !zsv_dir_exists((const char *)target_dir))
|
|
187
|
+
do_check = 0;
|
|
188
|
+
|
|
189
|
+
for (int i = do_check ? 0 : 1; i < 2 && !err; i++) {
|
|
190
|
+
do_check = i == 0;
|
|
191
|
+
|
|
192
|
+
size_t bytes_read;
|
|
193
|
+
yajl_helper_t yh;
|
|
194
|
+
struct zsv_dir_from_json_ctx ctx = {0};
|
|
195
|
+
ctx.filepath_prefix = (const char *)target_dir;
|
|
196
|
+
|
|
197
|
+
int (*start_obj)(yajl_helper_t yh) = NULL;
|
|
198
|
+
int (*end_obj)(yajl_helper_t yh) = NULL;
|
|
199
|
+
int (*process_value)(struct yajl_helper_parse_state *, struct json_value *) = NULL;
|
|
200
|
+
|
|
201
|
+
if (do_check)
|
|
202
|
+
ctx.do_check = do_check;
|
|
203
|
+
else {
|
|
204
|
+
ctx.dry = dry;
|
|
205
|
+
if (!ctx.dry) {
|
|
206
|
+
start_obj = zsv_dir_from_json_start_obj;
|
|
207
|
+
end_obj = zsv_dir_from_json_end_obj;
|
|
208
|
+
process_value = zsv_dir_from_json_process_value;
|
|
209
|
+
}
|
|
210
|
+
}
|
|
211
|
+
|
|
212
|
+
yh = yajl_helper_new(32, start_obj, end_obj, // map start/end
|
|
213
|
+
zsv_dir_from_json_map_key, start_obj, end_obj, // array start/end
|
|
214
|
+
process_value, &ctx);
|
|
215
|
+
if (!yh) {
|
|
216
|
+
err = errno = ENOMEM;
|
|
217
|
+
perror(NULL);
|
|
218
|
+
} else {
|
|
219
|
+
yajl_handle y = yajl_helper_yajl(yh);
|
|
220
|
+
while ((bytes_read = fread(ctx.buff, 1, sizeof(ctx.buff), src)) > 0) {
|
|
221
|
+
if (yajl_parse(y, ctx.buff, bytes_read) != yajl_status_ok)
|
|
222
|
+
yajl_helper_print_err(y, ctx.buff, bytes_read);
|
|
223
|
+
if (ctx.in_obj)
|
|
224
|
+
zsv_dir_from_json_flush(y, &ctx);
|
|
225
|
+
}
|
|
226
|
+
if (yajl_complete_parse(y) != yajl_status_ok)
|
|
227
|
+
yajl_helper_print_err(y, ctx.buff, bytes_read);
|
|
228
|
+
|
|
229
|
+
if (ctx.out) { // e.g. if bad JSON and parse failed
|
|
230
|
+
fclose(ctx.out);
|
|
231
|
+
free(ctx.out_filepath);
|
|
232
|
+
}
|
|
233
|
+
yajl_helper_delete(yh);
|
|
234
|
+
}
|
|
235
|
+
|
|
236
|
+
if (ctx.err)
|
|
237
|
+
err = ctx.err;
|
|
238
|
+
if (i == 0) {
|
|
239
|
+
rewind(src);
|
|
240
|
+
if (errno) {
|
|
241
|
+
err = errno;
|
|
242
|
+
perror(NULL);
|
|
243
|
+
}
|
|
244
|
+
}
|
|
245
|
+
}
|
|
246
|
+
}
|
|
247
|
+
if (tmp_fn) {
|
|
248
|
+
unlink(tmp_fn);
|
|
249
|
+
free(tmp_fn);
|
|
250
|
+
}
|
|
251
|
+
|
|
252
|
+
return err;
|
|
253
|
+
}
|
|
@@ -0,0 +1,121 @@
|
|
|
1
|
+
struct zsv_dir_foreach_to_json_ctx {
|
|
2
|
+
struct zsv_dir_filter zsv_dir_filter;
|
|
3
|
+
const unsigned char *parent_dir;
|
|
4
|
+
struct jv_to_json_ctx jctx;
|
|
5
|
+
zsv_jq_handle zjq;
|
|
6
|
+
unsigned count; // number of files exported so far
|
|
7
|
+
int err;
|
|
8
|
+
};
|
|
9
|
+
|
|
10
|
+
static int zsv_dir_foreach_to_json(struct zsv_foreach_dirent_handle *h, size_t depth) {
|
|
11
|
+
struct zsv_dir_foreach_to_json_ctx *ctx = h->ctx;
|
|
12
|
+
h->ctx = ctx->zsv_dir_filter.ctx;
|
|
13
|
+
if (!ctx->zsv_dir_filter.filter(h, depth))
|
|
14
|
+
h->no_recurse = 1; // skip this node (only matters if node is dir)
|
|
15
|
+
else if (!h->is_dir) { // process this file
|
|
16
|
+
char suffix = 0;
|
|
17
|
+
if (strlen(h->parent_and_entry) > 5 &&
|
|
18
|
+
!zsv_stricmp((const unsigned char *)h->parent_and_entry + strlen(h->parent_and_entry) - 5,
|
|
19
|
+
(const unsigned char *)".json"))
|
|
20
|
+
suffix = 'j'; // json
|
|
21
|
+
else if (strlen(h->parent_and_entry) > 4 &&
|
|
22
|
+
!zsv_stricmp((const unsigned char *)h->parent_and_entry + strlen(h->parent_and_entry) - 4,
|
|
23
|
+
(const unsigned char *)".txt"))
|
|
24
|
+
suffix = 't'; // text
|
|
25
|
+
if (suffix) {
|
|
26
|
+
// for now, only handle json or txt
|
|
27
|
+
FILE *f = fopen(h->parent_and_entry, "rb");
|
|
28
|
+
if (!f)
|
|
29
|
+
perror(h->parent_and_entry);
|
|
30
|
+
else {
|
|
31
|
+
// create an entry for this file. the map key is the file name; its value is the file contents
|
|
32
|
+
unsigned char *js =
|
|
33
|
+
zsv_json_from_str((const unsigned char *)h->parent_and_entry + strlen((const char *)ctx->parent_dir) + 1);
|
|
34
|
+
if (!js)
|
|
35
|
+
errno = ENOMEM, perror(NULL);
|
|
36
|
+
else if (*js) {
|
|
37
|
+
if (ctx->count > 0)
|
|
38
|
+
if (zsv_jq_parse(ctx->zjq, ",", 1))
|
|
39
|
+
ctx->err = 1;
|
|
40
|
+
if (!ctx->err) {
|
|
41
|
+
ctx->count++;
|
|
42
|
+
if (zsv_jq_parse(ctx->zjq, js, strlen((const char *)js)) || zsv_jq_parse(ctx->zjq, ":", 1))
|
|
43
|
+
ctx->err = 1;
|
|
44
|
+
else {
|
|
45
|
+
switch (suffix) {
|
|
46
|
+
case 'j': // json
|
|
47
|
+
if (zsv_jq_parse_file(ctx->zjq, f))
|
|
48
|
+
ctx->err = 1;
|
|
49
|
+
break;
|
|
50
|
+
case 't': // txt
|
|
51
|
+
// for now we are going to limit txt file values to 4096 chars and JSON-stringify it
|
|
52
|
+
{
|
|
53
|
+
unsigned char buff[4096];
|
|
54
|
+
size_t n = fread(buff, 1, sizeof(buff), f);
|
|
55
|
+
unsigned char *txt_js = NULL;
|
|
56
|
+
if (n) {
|
|
57
|
+
txt_js = zsv_json_from_str_n(buff, n);
|
|
58
|
+
if (zsv_jq_parse(ctx->zjq, txt_js ? txt_js : (const unsigned char *)"null",
|
|
59
|
+
txt_js ? strlen((const char *)txt_js) : 4))
|
|
60
|
+
ctx->err = 1;
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
break;
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
free(js);
|
|
69
|
+
fclose(f);
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
h->ctx = ctx;
|
|
74
|
+
return 0;
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
/**
|
|
78
|
+
* Convert directory contents into a single JSON file
|
|
79
|
+
* Output schema is a dictionary where key = path and value = contents
|
|
80
|
+
* Files named with .json suffix will be exported as JSON (content must be valid JSON)
|
|
81
|
+
* Files named with any other suffix will be exported as a single string value (do not try with large files)
|
|
82
|
+
*
|
|
83
|
+
* @param parent_dir : directory to export
|
|
84
|
+
* @param dest : file path to output to, or NULL to output to stdout
|
|
85
|
+
*/
|
|
86
|
+
int zsv_dir_to_json(const unsigned char *parent_dir, const unsigned char *output_filename,
|
|
87
|
+
struct zsv_dir_filter *zsv_dir_filter, unsigned char verbose) {
|
|
88
|
+
int err = 0;
|
|
89
|
+
FILE *fdest = output_filename ? fopen((const char *)output_filename, "wb") : stdout;
|
|
90
|
+
if (!fdest)
|
|
91
|
+
err = errno, perror((const char *)output_filename);
|
|
92
|
+
else {
|
|
93
|
+
struct zsv_dir_foreach_to_json_ctx ctx = {0};
|
|
94
|
+
ctx.zsv_dir_filter = *zsv_dir_filter;
|
|
95
|
+
ctx.parent_dir = parent_dir;
|
|
96
|
+
|
|
97
|
+
// use a jq filter to pretty-print
|
|
98
|
+
ctx.jctx.write1 = zsv_jq_fwrite1;
|
|
99
|
+
ctx.jctx.ctx = fdest;
|
|
100
|
+
ctx.jctx.flags = JV_PRINT_PRETTY | JV_PRINT_SPACE1;
|
|
101
|
+
enum zsv_jq_status jqstat;
|
|
102
|
+
ctx.zjq = zsv_jq_new((const unsigned char *)".", jv_to_json_func, &ctx.jctx, &jqstat);
|
|
103
|
+
if (!ctx.zjq)
|
|
104
|
+
err = 1, fprintf(stderr, "zsv_jq_new\n");
|
|
105
|
+
else {
|
|
106
|
+
if (jqstat == zsv_jq_status_ok && zsv_jq_parse(ctx.zjq, "{", 1) == zsv_jq_status_ok) {
|
|
107
|
+
// export each file
|
|
108
|
+
zsv_foreach_dirent((const char *)parent_dir, ctx.zsv_dir_filter.max_depth, zsv_dir_foreach_to_json, &ctx,
|
|
109
|
+
verbose);
|
|
110
|
+
if (!ctx.err && zsv_jq_parse(ctx.zjq, "}", 1))
|
|
111
|
+
ctx.err = 1;
|
|
112
|
+
if (!ctx.err && zsv_jq_finish(ctx.zjq))
|
|
113
|
+
ctx.err = 1;
|
|
114
|
+
zsv_jq_delete(ctx.zjq);
|
|
115
|
+
}
|
|
116
|
+
err = ctx.err;
|
|
117
|
+
}
|
|
118
|
+
fclose(fdest);
|
|
119
|
+
}
|
|
120
|
+
return err;
|
|
121
|
+
}
|
|
@@ -0,0 +1,20 @@
|
|
|
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
|
+
#include <zsv/utils/dl.h>
|
|
10
|
+
|
|
11
|
+
void (*zsv_dlsym(void *restrict handle, const char *restrict name))(void) {
|
|
12
|
+
#pragma GCC diagnostic push
|
|
13
|
+
#pragma GCC diagnostic ignored "-Wpedantic"
|
|
14
|
+
return dlsym(handle, name);
|
|
15
|
+
#pragma GCC diagnostic pop
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
#ifdef _WIN32
|
|
19
|
+
#include "win/dl.c"
|
|
20
|
+
#endif
|
|
@@ -0,0 +1,159 @@
|
|
|
1
|
+
#include <stdlib.h>
|
|
2
|
+
#include <stdio.h>
|
|
3
|
+
#include <string.h>
|
|
4
|
+
#include <unistd.h>
|
|
5
|
+
#include <dirent.h>
|
|
6
|
+
#include <sys/stat.h>
|
|
7
|
+
|
|
8
|
+
#ifdef __EMSCRIPTEN__
|
|
9
|
+
#include <emscripten.h>
|
|
10
|
+
#else
|
|
11
|
+
#define EMSCRIPTEN_KEEPALIVE
|
|
12
|
+
#endif
|
|
13
|
+
|
|
14
|
+
#include <zsv/utils/emcc/fs_api.h>
|
|
15
|
+
|
|
16
|
+
struct fs_data {
|
|
17
|
+
FILE *f;
|
|
18
|
+
};
|
|
19
|
+
|
|
20
|
+
EMSCRIPTEN_KEEPALIVE
|
|
21
|
+
fshandle fsopen(const char *fn, const char *mode) {
|
|
22
|
+
struct fs_data *d = calloc(1, sizeof(*d));
|
|
23
|
+
if (d)
|
|
24
|
+
d->f = fopen(fn, mode);
|
|
25
|
+
return d;
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
EMSCRIPTEN_KEEPALIVE
|
|
29
|
+
int fsclose(fshandle h) {
|
|
30
|
+
if (h) {
|
|
31
|
+
int rc = fclose(h->f);
|
|
32
|
+
free(h);
|
|
33
|
+
return rc;
|
|
34
|
+
}
|
|
35
|
+
return 1;
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
EMSCRIPTEN_KEEPALIVE
|
|
39
|
+
size_t fswrite(const void *ptr, size_t n, size_t m, fshandle h) {
|
|
40
|
+
return fwrite(ptr, n, m, h->f);
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
EMSCRIPTEN_KEEPALIVE
|
|
44
|
+
void stdflush(int addNewline) {
|
|
45
|
+
fflush(stdout);
|
|
46
|
+
if (addNewline)
|
|
47
|
+
fprintf(stdout, "\n");
|
|
48
|
+
|
|
49
|
+
fflush(stderr);
|
|
50
|
+
if (addNewline)
|
|
51
|
+
fprintf(stderr, "\n");
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
/*
|
|
55
|
+
EMSCRIPTEN_KEEPALIVE
|
|
56
|
+
size_t bytesum(const char *fn) {
|
|
57
|
+
size_t sum = 0;
|
|
58
|
+
FILE *f = fopen(fn, "rb");
|
|
59
|
+
if(f) {
|
|
60
|
+
char buff[1024];
|
|
61
|
+
size_t bytes_read;
|
|
62
|
+
while((bytes_read = fread(buff, 1, sizeof(buff), f))) {
|
|
63
|
+
for(size_t i = 0; i < bytes_read; i++) {
|
|
64
|
+
sum += buff[i];
|
|
65
|
+
sum = sum % 1000000000;
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
fclose(f);
|
|
69
|
+
}
|
|
70
|
+
return sum;
|
|
71
|
+
}
|
|
72
|
+
*/
|
|
73
|
+
|
|
74
|
+
EMSCRIPTEN_KEEPALIVE
|
|
75
|
+
size_t fsread(void *ptr, size_t n, size_t m, fshandle h) {
|
|
76
|
+
return fread(ptr, n, m, h->f);
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
EMSCRIPTEN_KEEPALIVE
|
|
80
|
+
int fsrm(const char *fn) {
|
|
81
|
+
return unlink(fn);
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
EMSCRIPTEN_KEEPALIVE
|
|
85
|
+
int fsmkdir(const char *path) {
|
|
86
|
+
#ifdef _WIN32
|
|
87
|
+
return mkdir(path);
|
|
88
|
+
#endif
|
|
89
|
+
return mkdir(path, S_IRWXU);
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
EMSCRIPTEN_KEEPALIVE
|
|
93
|
+
int fsprint(const char *s) {
|
|
94
|
+
fwrite(s, 1, strlen(s), stdout);
|
|
95
|
+
return 0;
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
EMSCRIPTEN_KEEPALIVE
|
|
99
|
+
int fsprinterr(const char *s) {
|
|
100
|
+
fwrite(s, 1, strlen(s), stderr);
|
|
101
|
+
fprintf(stderr, "printed fsprinterr\n");
|
|
102
|
+
return 0;
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
/**
|
|
106
|
+
* return single string of entries delimited by newline
|
|
107
|
+
* caller must free()
|
|
108
|
+
*/
|
|
109
|
+
EMSCRIPTEN_KEEPALIVE
|
|
110
|
+
char *fsls(const char *dir) {
|
|
111
|
+
struct dirent *de; // Pointer for directory entry
|
|
112
|
+
// opendir() returns a pointer of DIR type.
|
|
113
|
+
if (!dir || !*dir)
|
|
114
|
+
dir = ".";
|
|
115
|
+
DIR *dr = opendir(dir);
|
|
116
|
+
if (!dr)
|
|
117
|
+
fprintf(stderr, "Could not open current directory");
|
|
118
|
+
else {
|
|
119
|
+
size_t total_mem = 0;
|
|
120
|
+
struct tmp_str_list {
|
|
121
|
+
struct tmp_str_list *next;
|
|
122
|
+
char *c;
|
|
123
|
+
};
|
|
124
|
+
struct tmp_str_list *head = NULL, **tail = &head;
|
|
125
|
+
while ((de = readdir(dr)) != NULL) { // see http://pubs.opengroup.org/onlinepubs/7990989775/xsh/readdir.html
|
|
126
|
+
struct tmp_str_list *tmp = malloc(sizeof(*tmp));
|
|
127
|
+
if (tmp) {
|
|
128
|
+
tmp->next = NULL;
|
|
129
|
+
tmp->c = strdup(de->d_name);
|
|
130
|
+
if (!tmp->c)
|
|
131
|
+
free(tmp);
|
|
132
|
+
else {
|
|
133
|
+
total_mem += (1 + strlen(tmp->c));
|
|
134
|
+
*tail = tmp;
|
|
135
|
+
tail = &tmp->next;
|
|
136
|
+
}
|
|
137
|
+
}
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
char *final_result = NULL;
|
|
141
|
+
if (total_mem && (final_result = malloc(total_mem + 1))) {
|
|
142
|
+
size_t offset = 0;
|
|
143
|
+
for (struct tmp_str_list *tmp = head, *next; tmp; tmp = next) {
|
|
144
|
+
next = tmp->next;
|
|
145
|
+
size_t len = strlen(tmp->c);
|
|
146
|
+
memcpy(final_result + offset, tmp->c, len);
|
|
147
|
+
offset += len + 1;
|
|
148
|
+
final_result[offset - 1] = '\n';
|
|
149
|
+
free(tmp->c);
|
|
150
|
+
free(tmp);
|
|
151
|
+
}
|
|
152
|
+
final_result[total_mem - 1] = '\0';
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
closedir(dr);
|
|
156
|
+
return final_result;
|
|
157
|
+
}
|
|
158
|
+
return NULL;
|
|
159
|
+
}
|
|
@@ -0,0 +1,24 @@
|
|
|
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
|
+
#include <stdio.h>
|
|
10
|
+
#include <stdarg.h>
|
|
11
|
+
|
|
12
|
+
int zsv_app_printerr(const char *appname, int eno, const char *fmt, ...) {
|
|
13
|
+
if (appname && *appname)
|
|
14
|
+
fprintf(stderr, "(%s) %i: ", appname, eno);
|
|
15
|
+
else
|
|
16
|
+
fprintf(stderr, "%i: ", eno);
|
|
17
|
+
|
|
18
|
+
va_list argv;
|
|
19
|
+
va_start(argv, fmt);
|
|
20
|
+
vfprintf(stderr, fmt, argv);
|
|
21
|
+
va_end(argv);
|
|
22
|
+
fprintf(stderr, "\n");
|
|
23
|
+
return eno;
|
|
24
|
+
}
|