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,322 @@
|
|
|
1
|
+
#include <stdlib.h>
|
|
2
|
+
#include <string.h>
|
|
3
|
+
#include <pthread.h>
|
|
4
|
+
#include <zsv/utils/prop.h>
|
|
5
|
+
#include "sheet_internal.h"
|
|
6
|
+
#include "screen_buffer.h"
|
|
7
|
+
#include "../utils/index.h"
|
|
8
|
+
#include "index.h"
|
|
9
|
+
|
|
10
|
+
#if defined(WIN32) || defined(_WIN32)
|
|
11
|
+
#ifndef NO_MEMMEM
|
|
12
|
+
#define NO_MEMMEM
|
|
13
|
+
#endif
|
|
14
|
+
#include <zsv/utils/memmem.h>
|
|
15
|
+
#endif
|
|
16
|
+
#include <zsv/utils/writer.h>
|
|
17
|
+
#include <zsv/utils/file.h>
|
|
18
|
+
|
|
19
|
+
// zsvsheet_found_in_row: return 0 if not found, else 1-based index of column
|
|
20
|
+
static size_t zsvsheet_found_in_row(zsv_parser parser, size_t col_start, size_t col_count, const char *target,
|
|
21
|
+
size_t target_len, size_t specified_column_plus_1,
|
|
22
|
+
char find_exact // not yet implemented
|
|
23
|
+
) {
|
|
24
|
+
if (col_start >= col_count)
|
|
25
|
+
return 0;
|
|
26
|
+
|
|
27
|
+
struct zsv_cell first_cell = zsv_get_cell(parser, col_start);
|
|
28
|
+
struct zsv_cell last_cell = zsv_get_cell(parser, col_count - 1);
|
|
29
|
+
|
|
30
|
+
if (memmem(first_cell.str, last_cell.str - first_cell.str + last_cell.len, target, target_len)) {
|
|
31
|
+
for (size_t i = col_start; i < col_count; i++) {
|
|
32
|
+
if (specified_column_plus_1 == 0 || i + 1 == specified_column_plus_1) {
|
|
33
|
+
struct zsv_cell c = zsv_get_cell(parser, i);
|
|
34
|
+
if (find_exact) {
|
|
35
|
+
if (c.len == target_len && !memcmp(c.str, target, c.len))
|
|
36
|
+
return i + 1;
|
|
37
|
+
} else if (memmem(c.str, c.len, target, target_len))
|
|
38
|
+
return i + 1;
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
return 0;
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
static void *get_data_index(void *d);
|
|
46
|
+
|
|
47
|
+
static void get_data_index_async(struct zsvsheet_ui_buffer *uibuffp, const char *filename, struct zsv_opts *optsp,
|
|
48
|
+
struct zsv_prop_handler *custom_prop_handler, char *old_ui_status) {
|
|
49
|
+
struct zsvsheet_index_opts *ixopts = calloc(1, sizeof(*ixopts));
|
|
50
|
+
ixopts->mutexp = &uibuffp->mutex;
|
|
51
|
+
ixopts->filename = filename;
|
|
52
|
+
ixopts->zsv_opts = *optsp;
|
|
53
|
+
ixopts->custom_prop_handler = custom_prop_handler;
|
|
54
|
+
ixopts->uib = uibuffp;
|
|
55
|
+
ixopts->uib->ixopts = ixopts;
|
|
56
|
+
ixopts->old_ui_status = old_ui_status;
|
|
57
|
+
|
|
58
|
+
if (uibuffp->worker_active)
|
|
59
|
+
zsvsheet_ui_buffer_join_worker(uibuffp);
|
|
60
|
+
zsvsheet_ui_buffer_create_worker(uibuffp, get_data_index, ixopts);
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
static int read_data(struct zsvsheet_ui_buffer **uibufferp, // a new zsvsheet_ui_buffer will be allocated
|
|
64
|
+
struct zsvsheet_ui_buffer_opts *uibopts, // if *uibufferp == NULL and uibopts != NULL
|
|
65
|
+
size_t start_row, size_t start_col, size_t header_span, struct zsvsheet_opts *zsvsheet_opts,
|
|
66
|
+
struct zsv_prop_handler *custom_prop_handler) {
|
|
67
|
+
int rc = 0;
|
|
68
|
+
struct uib_parse_errs parse_errs;
|
|
69
|
+
uib_parse_errs_init(&parse_errs, 100);
|
|
70
|
+
FILE *fp = NULL;
|
|
71
|
+
const char *filename = (uibufferp && *uibufferp) ? (*uibufferp)->filename : uibopts ? uibopts->filename : NULL;
|
|
72
|
+
struct zsv_opts opts = {0};
|
|
73
|
+
if (uibufferp && *uibufferp)
|
|
74
|
+
opts = (*uibufferp)->zsv_opts;
|
|
75
|
+
else if (uibopts)
|
|
76
|
+
opts = uibopts->zsv_opts;
|
|
77
|
+
|
|
78
|
+
struct zsvsheet_ui_buffer *uibuff = uibufferp ? *uibufferp : NULL;
|
|
79
|
+
size_t remaining_rows_to_skip = start_row;
|
|
80
|
+
size_t remaining_header_to_skip = header_span;
|
|
81
|
+
size_t original_row_num = 0;
|
|
82
|
+
|
|
83
|
+
if (uibuff) {
|
|
84
|
+
if (uibuff->data_filename)
|
|
85
|
+
filename = uibuff->data_filename;
|
|
86
|
+
else if (uibuff->filename)
|
|
87
|
+
filename = uibuff->filename;
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
if (!filename && uibopts) {
|
|
91
|
+
if (uibopts->data_filename)
|
|
92
|
+
filename = uibopts->data_filename;
|
|
93
|
+
else if (uibopts->filename)
|
|
94
|
+
filename = uibopts->filename;
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
assert(filename != NULL);
|
|
98
|
+
fp = fopen(filename, "rb");
|
|
99
|
+
if (!fp) {
|
|
100
|
+
rc = errno;
|
|
101
|
+
goto done;
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
opts.stream = fp; // Input file stream
|
|
105
|
+
|
|
106
|
+
opts.errprintf = uib_parse_errs_printf;
|
|
107
|
+
if (uibuff)
|
|
108
|
+
opts.errf = &uibuff->parse_errs;
|
|
109
|
+
else
|
|
110
|
+
opts.errf = &parse_errs;
|
|
111
|
+
|
|
112
|
+
zsv_parser parser = {0};
|
|
113
|
+
if (zsv_new_with_properties(&opts, custom_prop_handler, filename, &parser) != zsv_status_ok) {
|
|
114
|
+
rc = errno ? errno : -1;
|
|
115
|
+
goto done;
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
// clear error logging before reusing opts
|
|
119
|
+
opts.errprintf = zsv_no_printf;
|
|
120
|
+
if (uibuff) {
|
|
121
|
+
pthread_mutex_lock(&uibuff->mutex);
|
|
122
|
+
|
|
123
|
+
enum zsv_index_status zst = zsv_index_status_ok;
|
|
124
|
+
if (zsvsheet_ui_buffer_index_ready(uibuff, 1)) {
|
|
125
|
+
opts.header_span = 0;
|
|
126
|
+
opts.rows_to_ignore = 0;
|
|
127
|
+
|
|
128
|
+
zst = zsv_index_seek_row(uibuff->index, &opts, start_row);
|
|
129
|
+
|
|
130
|
+
zsv_delete(parser);
|
|
131
|
+
parser = zsv_new(&opts);
|
|
132
|
+
|
|
133
|
+
remaining_header_to_skip = 0;
|
|
134
|
+
remaining_rows_to_skip = 0;
|
|
135
|
+
original_row_num = header_span + start_row;
|
|
136
|
+
}
|
|
137
|
+
pthread_mutex_unlock(&uibuff->mutex);
|
|
138
|
+
if (zst != zsv_index_status_ok) {
|
|
139
|
+
rc = errno ? errno : -1;
|
|
140
|
+
goto done;
|
|
141
|
+
}
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
size_t rows_read = header_span;
|
|
145
|
+
|
|
146
|
+
size_t find_len = zsvsheet_opts->find ? strlen(zsvsheet_opts->find) : 0;
|
|
147
|
+
size_t rows_searched = 0;
|
|
148
|
+
zsvsheet_screen_buffer_t buffer = uibuff ? uibuff->buffer : NULL;
|
|
149
|
+
if (uibuff && uibuff->has_row_num)
|
|
150
|
+
zsvsheet_opts->hide_row_nums = 1;
|
|
151
|
+
|
|
152
|
+
while (zsv_next_row(parser) == zsv_status_row &&
|
|
153
|
+
(rows_read == 0 || rows_read < zsvsheet_screen_buffer_rows(buffer))) { // for each row
|
|
154
|
+
|
|
155
|
+
size_t col_count = zsv_cell_count(parser);
|
|
156
|
+
if (uibuff == NULL && uibufferp && uibopts && col_count > 0) {
|
|
157
|
+
enum zsvsheet_priv_status stat;
|
|
158
|
+
struct zsvsheet_ui_buffer *tmp_uibuff = NULL;
|
|
159
|
+
if (!(buffer = zsvsheet_screen_buffer_new(col_count, uibopts->buff_opts, &stat)) ||
|
|
160
|
+
stat != zsvsheet_priv_status_ok || !(tmp_uibuff = zsvsheet_ui_buffer_new(buffer, uibopts))) {
|
|
161
|
+
if (tmp_uibuff)
|
|
162
|
+
zsvsheet_ui_buffer_delete(tmp_uibuff);
|
|
163
|
+
else
|
|
164
|
+
zsvsheet_screen_buffer_delete(buffer);
|
|
165
|
+
rc = -1;
|
|
166
|
+
goto done;
|
|
167
|
+
}
|
|
168
|
+
*uibufferp = uibuff = tmp_uibuff;
|
|
169
|
+
if (uibuff) {
|
|
170
|
+
uibuff->parse_errs = parse_errs; // transfer errors
|
|
171
|
+
memset(&parse_errs, 0, sizeof(parse_errs)); // prevent double-free
|
|
172
|
+
}
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
// row number
|
|
176
|
+
size_t rownum_column_offset = 0;
|
|
177
|
+
if (rows_read == 0 && zsvsheet_opts->hide_row_nums == 0) {
|
|
178
|
+
// Check if we already have Row #
|
|
179
|
+
// TO DO: instead of checking the header, use (*uibufferp)->has_row_num
|
|
180
|
+
struct zsv_cell c = zsv_get_cell(parser, 0);
|
|
181
|
+
if (c.len == ZSVSHEET_ROWNUM_HEADER_LEN && !memcmp(c.str, ZSVSHEET_ROWNUM_HEADER, c.len)) {
|
|
182
|
+
zsvsheet_opts->hide_row_nums = 1;
|
|
183
|
+
if (uibuff)
|
|
184
|
+
uibuff->has_row_num = 1;
|
|
185
|
+
}
|
|
186
|
+
}
|
|
187
|
+
|
|
188
|
+
original_row_num++;
|
|
189
|
+
if (remaining_header_to_skip > 0) {
|
|
190
|
+
remaining_header_to_skip--;
|
|
191
|
+
continue;
|
|
192
|
+
}
|
|
193
|
+
if (uibuff) {
|
|
194
|
+
if (col_count + !buffer->opts.no_rownum_column > buffer->cols) {
|
|
195
|
+
if (zsvsheet_screen_buffer_grow(buffer, col_count) != zsvsheet_priv_status_ok) {
|
|
196
|
+
rc = -1;
|
|
197
|
+
goto done;
|
|
198
|
+
}
|
|
199
|
+
}
|
|
200
|
+
if (col_count > uibuff->dimensions.col_count)
|
|
201
|
+
uibuff->dimensions.col_count = col_count;
|
|
202
|
+
}
|
|
203
|
+
|
|
204
|
+
if (remaining_rows_to_skip > 0) {
|
|
205
|
+
remaining_rows_to_skip--;
|
|
206
|
+
continue;
|
|
207
|
+
}
|
|
208
|
+
|
|
209
|
+
if (zsvsheet_opts->find) { // find the next occurrence
|
|
210
|
+
rows_searched++;
|
|
211
|
+
size_t colIndexPlus1 =
|
|
212
|
+
zsvsheet_found_in_row(parser, zsvsheet_opts->found_colnum, col_count, zsvsheet_opts->find, find_len,
|
|
213
|
+
zsvsheet_opts->find_specified_column_plus_1, zsvsheet_opts->find_exact);
|
|
214
|
+
if (colIndexPlus1) {
|
|
215
|
+
zsvsheet_opts->found_rownum = rows_searched + start_row;
|
|
216
|
+
zsvsheet_opts->found_colnum = colIndexPlus1 - 1;
|
|
217
|
+
break;
|
|
218
|
+
} else
|
|
219
|
+
zsvsheet_opts->found_colnum = 0; // next row search starts at beg of row
|
|
220
|
+
continue;
|
|
221
|
+
}
|
|
222
|
+
|
|
223
|
+
if (zsvsheet_opts->hide_row_nums == 0) {
|
|
224
|
+
if (rows_read == 0) // header
|
|
225
|
+
zsvsheet_screen_buffer_write_cell(buffer, 0, 0, (const unsigned char *)ZSVSHEET_ROWNUM_HEADER);
|
|
226
|
+
else {
|
|
227
|
+
char buff[32];
|
|
228
|
+
int n = snprintf(buff, sizeof(buff), "%zu", original_row_num - 1);
|
|
229
|
+
if (!(n > 0 && n < (int)sizeof(buff)))
|
|
230
|
+
sprintf(buff, "########");
|
|
231
|
+
zsvsheet_screen_buffer_write_cell(buffer, rows_read, 0, (unsigned char *)buff);
|
|
232
|
+
}
|
|
233
|
+
rownum_column_offset = 1;
|
|
234
|
+
}
|
|
235
|
+
|
|
236
|
+
for (size_t i = start_col; i < col_count && i + rownum_column_offset < zsvsheet_screen_buffer_cols(buffer); i++) {
|
|
237
|
+
struct zsv_cell c = zsv_get_cell(parser, i);
|
|
238
|
+
if (c.len)
|
|
239
|
+
zsvsheet_screen_buffer_write_cell_w_len(buffer, rows_read, i + rownum_column_offset, c.str, c.len);
|
|
240
|
+
}
|
|
241
|
+
|
|
242
|
+
rows_read++;
|
|
243
|
+
}
|
|
244
|
+
if (!uibuff) {
|
|
245
|
+
rc = 0;
|
|
246
|
+
goto done;
|
|
247
|
+
}
|
|
248
|
+
|
|
249
|
+
pthread_mutex_lock(&uibuff->mutex);
|
|
250
|
+
char need_index = !uibuff->index_started && !uibuff->write_in_progress;
|
|
251
|
+
char *old_ui_status = uibuff->status;
|
|
252
|
+
if (need_index)
|
|
253
|
+
uibuff->index_started = 1;
|
|
254
|
+
pthread_mutex_unlock(&uibuff->mutex);
|
|
255
|
+
|
|
256
|
+
if (need_index) {
|
|
257
|
+
if (asprintf(&uibuff->status, "%s(building index) ", old_ui_status ? old_ui_status : "") == -1) {
|
|
258
|
+
rc = -1;
|
|
259
|
+
goto done;
|
|
260
|
+
}
|
|
261
|
+
|
|
262
|
+
uibuff->buff_used_rows = rows_read;
|
|
263
|
+
uibuff->dimensions.row_count = rows_read;
|
|
264
|
+
if (original_row_num > 1 && rows_read > 0) {
|
|
265
|
+
opts.stream = NULL;
|
|
266
|
+
get_data_index_async(uibuff, filename, &opts, custom_prop_handler, old_ui_status);
|
|
267
|
+
}
|
|
268
|
+
} else if (rows_read > uibuff->buff_used_rows) {
|
|
269
|
+
uibuff->buff_used_rows = rows_read;
|
|
270
|
+
uibuff->dimensions.row_count = rows_read;
|
|
271
|
+
}
|
|
272
|
+
|
|
273
|
+
done:
|
|
274
|
+
uib_parse_errs_clear(&parse_errs);
|
|
275
|
+
if (fp)
|
|
276
|
+
fclose(fp);
|
|
277
|
+
zsv_delete(parser);
|
|
278
|
+
return rc;
|
|
279
|
+
}
|
|
280
|
+
|
|
281
|
+
// get_data_index(): return an index for constant-time access
|
|
282
|
+
static void *get_data_index(void *gdi) {
|
|
283
|
+
struct zsvsheet_index_opts *d = gdi;
|
|
284
|
+
pthread_mutex_t *mutexp = d->mutexp;
|
|
285
|
+
int *errp = d->errp;
|
|
286
|
+
struct zsvsheet_ui_buffer *uib = d->uib;
|
|
287
|
+
char *ui_status = uib->status;
|
|
288
|
+
|
|
289
|
+
enum zsv_index_status ix_status = build_memory_index(d);
|
|
290
|
+
|
|
291
|
+
if (ix_status != zsv_index_status_ok) {
|
|
292
|
+
pthread_mutex_lock(mutexp);
|
|
293
|
+
if (errp != NULL)
|
|
294
|
+
*errp = errno;
|
|
295
|
+
free(d);
|
|
296
|
+
pthread_mutex_unlock(mutexp);
|
|
297
|
+
return NULL;
|
|
298
|
+
}
|
|
299
|
+
|
|
300
|
+
pthread_mutex_lock(mutexp);
|
|
301
|
+
uib->status = d->old_ui_status;
|
|
302
|
+
uib->ixopts = NULL;
|
|
303
|
+
pthread_mutex_unlock(mutexp);
|
|
304
|
+
|
|
305
|
+
free(ui_status);
|
|
306
|
+
free(d);
|
|
307
|
+
|
|
308
|
+
return NULL;
|
|
309
|
+
}
|
|
310
|
+
|
|
311
|
+
static size_t zsvsheet_find_next(struct zsvsheet_ui_buffer *uib, struct zsvsheet_opts *zsvsheet_opts,
|
|
312
|
+
size_t header_span, struct zsv_prop_handler *custom_prop_handler) {
|
|
313
|
+
struct zsvsheet_rowcol *input_offset = &uib->input_offset;
|
|
314
|
+
struct zsvsheet_rowcol *buff_offset = &uib->buff_offset;
|
|
315
|
+
size_t cursor_row = uib->cursor_row;
|
|
316
|
+
size_t start_row = input_offset->row + buff_offset->row + header_span + cursor_row - 1;
|
|
317
|
+
if (start_row > 0)
|
|
318
|
+
start_row--;
|
|
319
|
+
read_data(&uib, NULL, start_row, 0, header_span, zsvsheet_opts, custom_prop_handler);
|
|
320
|
+
zsvsheet_opts->find = NULL;
|
|
321
|
+
return zsvsheet_opts->found_rownum;
|
|
322
|
+
}
|
|
@@ -0,0 +1,203 @@
|
|
|
1
|
+
#include "screen_buffer.h"
|
|
2
|
+
|
|
3
|
+
#include <zsv/ext.h> // zsvsheet_cell_attr_t
|
|
4
|
+
|
|
5
|
+
struct zsvsheet_screen_buffer {
|
|
6
|
+
size_t cols;
|
|
7
|
+
size_t long_cell_count;
|
|
8
|
+
struct zsvsheet_screen_buffer_opts opts;
|
|
9
|
+
unsigned char *data;
|
|
10
|
+
zsvsheet_cell_attr_t *cell_attrs; // used for per-cell attron() and attroff()
|
|
11
|
+
// to do: add hooks for extension
|
|
12
|
+
};
|
|
13
|
+
|
|
14
|
+
static inline size_t buffer_data_offset(zsvsheet_screen_buffer_t buff, size_t row, size_t col) {
|
|
15
|
+
assert(row < buff->opts.rows && col < buff->cols);
|
|
16
|
+
return row * buff->cols * buff->opts.cell_buff_len + col * buff->opts.cell_buff_len;
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
static void set_long_cell(zsvsheet_screen_buffer_t buff, size_t offset, char *heap) {
|
|
20
|
+
char **target = (char **)(buff->data + offset);
|
|
21
|
+
*target = heap;
|
|
22
|
+
// set flag indicating that this is long cell
|
|
23
|
+
*(buff->data + offset + buff->opts.cell_buff_len - 1) = (char)1;
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
static char *get_long_cell(zsvsheet_screen_buffer_t buff, size_t offset) {
|
|
27
|
+
char **valuep = (char **)(buff->data + offset);
|
|
28
|
+
if (valuep)
|
|
29
|
+
return *valuep;
|
|
30
|
+
return NULL;
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
static inline int is_long_cell(const unsigned char *mem, size_t cell_buff_len) {
|
|
34
|
+
return *(mem + cell_buff_len - 1) != '\0';
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
size_t zsvsheet_screen_buffer_cols(zsvsheet_screen_buffer_t buff) {
|
|
38
|
+
return buff->cols;
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
size_t zsvsheet_screen_buffer_rows(zsvsheet_screen_buffer_t buff) {
|
|
42
|
+
return buff->opts.rows;
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
static void free_long_cell(zsvsheet_screen_buffer_t buff, size_t offset) {
|
|
46
|
+
if (is_long_cell(buff->data + offset, buff->opts.cell_buff_len)) {
|
|
47
|
+
char *value_copy = get_long_cell(buff, offset);
|
|
48
|
+
free(value_copy);
|
|
49
|
+
memset(buff->data + offset, 0, buff->opts.cell_buff_len);
|
|
50
|
+
buff->long_cell_count--;
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
void zsvsheet_screen_buffer_delete(zsvsheet_screen_buffer_t buff) {
|
|
55
|
+
if (buff) {
|
|
56
|
+
for (size_t i = 0; i < buff->opts.rows && buff->long_cell_count > 0; i++) {
|
|
57
|
+
for (size_t j = 0; j < buff->cols && buff->long_cell_count > 0; j++) {
|
|
58
|
+
size_t offset = buffer_data_offset(buff, i, j);
|
|
59
|
+
free_long_cell(buff, offset);
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
free(buff->cell_attrs);
|
|
63
|
+
free(buff->data);
|
|
64
|
+
free(buff);
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
zsvsheet_screen_buffer_t zsvsheet_screen_buffer_new(size_t cols, struct zsvsheet_screen_buffer_opts *opts,
|
|
69
|
+
enum zsvsheet_priv_status *stat) {
|
|
70
|
+
struct zsvsheet_screen_buffer_opts bopts = {0};
|
|
71
|
+
if (!opts)
|
|
72
|
+
opts = &bopts;
|
|
73
|
+
*stat = zsvsheet_priv_status_ok;
|
|
74
|
+
if (opts->rows == 0)
|
|
75
|
+
opts->rows = ZSVSHEET_SCREEN_BUFFER_DEFAULT_ROW_COUNT;
|
|
76
|
+
else if (opts->rows < 256)
|
|
77
|
+
opts->rows = 256;
|
|
78
|
+
if (opts->cell_buff_len == 0)
|
|
79
|
+
opts->cell_buff_len = ZSVSHEET_SCREEN_BUFFER_DEFAULT_CELL_BUFF_LEN;
|
|
80
|
+
if (opts->max_cell_len == 0)
|
|
81
|
+
opts->max_cell_len = ZSVSHEET_SCREEN_BUFFER_DEFAULT_MAX_CELL_LEN;
|
|
82
|
+
if (opts->cell_buff_len < sizeof(void *) * 2)
|
|
83
|
+
*stat = zsvsheet_priv_status_error;
|
|
84
|
+
else {
|
|
85
|
+
if (!opts->no_rownum_column)
|
|
86
|
+
cols++;
|
|
87
|
+
void *data = calloc(opts->rows, cols * opts->cell_buff_len);
|
|
88
|
+
if (!data)
|
|
89
|
+
*stat = zsvsheet_priv_status_memory;
|
|
90
|
+
else {
|
|
91
|
+
struct zsvsheet_screen_buffer *buff = calloc(1, sizeof(*buff));
|
|
92
|
+
if (!buff)
|
|
93
|
+
*stat = zsvsheet_priv_status_memory;
|
|
94
|
+
else {
|
|
95
|
+
buff->opts.rows = opts->rows;
|
|
96
|
+
buff->cols = cols;
|
|
97
|
+
buff->data = data;
|
|
98
|
+
buff->opts = *opts;
|
|
99
|
+
return buff;
|
|
100
|
+
}
|
|
101
|
+
}
|
|
102
|
+
free(data);
|
|
103
|
+
}
|
|
104
|
+
return NULL;
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
enum zsvsheet_priv_status zsvsheet_screen_buffer_grow(zsvsheet_screen_buffer_t buff, size_t cols) {
|
|
108
|
+
size_t old_cols = buff->cols;
|
|
109
|
+
size_t rows = buff->opts.rows;
|
|
110
|
+
size_t cell_buff_len = buff->opts.cell_buff_len;
|
|
111
|
+
size_t old_row_len = old_cols * cell_buff_len;
|
|
112
|
+
|
|
113
|
+
if (!buff->opts.no_rownum_column)
|
|
114
|
+
cols++;
|
|
115
|
+
|
|
116
|
+
size_t row_len = cols * cell_buff_len;
|
|
117
|
+
|
|
118
|
+
assert(cols > old_cols);
|
|
119
|
+
|
|
120
|
+
void *old_data = buff->data;
|
|
121
|
+
void *data = calloc(rows, cols * cell_buff_len);
|
|
122
|
+
if (!data)
|
|
123
|
+
return zsvsheet_priv_status_memory;
|
|
124
|
+
|
|
125
|
+
for (size_t i = 0; i < rows; i++) {
|
|
126
|
+
size_t old_row_off = i * old_row_len;
|
|
127
|
+
size_t row_off = i * row_len;
|
|
128
|
+
|
|
129
|
+
memcpy((char *)data + row_off, (char *)old_data + old_row_off, old_row_len);
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
buff->data = data;
|
|
133
|
+
buff->cols = cols;
|
|
134
|
+
|
|
135
|
+
free(old_data);
|
|
136
|
+
|
|
137
|
+
return zsvsheet_priv_status_ok;
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
#ifndef UTF8_NOT_FIRST_CHAR
|
|
141
|
+
#define UTF8_NOT_FIRST_CHAR(x) ((x & 0xC0) == 0x80)
|
|
142
|
+
#endif
|
|
143
|
+
|
|
144
|
+
enum zsvsheet_priv_status zsvsheet_screen_buffer_write_cell_w_len(zsvsheet_screen_buffer_t buff, size_t row, size_t col,
|
|
145
|
+
const unsigned char *value, size_t len) {
|
|
146
|
+
enum zsvsheet_priv_status stat = zsvsheet_priv_status_ok;
|
|
147
|
+
size_t offset = buffer_data_offset(buff, row, col);
|
|
148
|
+
free_long_cell(buff, offset);
|
|
149
|
+
if (len < buff->opts.cell_buff_len) {
|
|
150
|
+
if (len)
|
|
151
|
+
// copy into fixed-size buff
|
|
152
|
+
memcpy(buff->data + offset, value, len);
|
|
153
|
+
*(buff->data + offset + len) = '\0';
|
|
154
|
+
} else {
|
|
155
|
+
// we have a long cell
|
|
156
|
+
if (len > buff->opts.max_cell_len) {
|
|
157
|
+
len = buff->opts.max_cell_len;
|
|
158
|
+
while (len > 0 && value[len] >= 128 && UTF8_NOT_FIRST_CHAR(value[len]))
|
|
159
|
+
// we are in the middle of a multibyte char, so back up
|
|
160
|
+
len--;
|
|
161
|
+
}
|
|
162
|
+
if (!len) // the only reason len could be 0 is if our input was not valid utf8, but check to make sure anyway
|
|
163
|
+
stat = zsvsheet_priv_status_utf8;
|
|
164
|
+
else {
|
|
165
|
+
char *value_copy = malloc(1 + len);
|
|
166
|
+
if (value_copy) {
|
|
167
|
+
memcpy(value_copy, value, len);
|
|
168
|
+
value_copy[len] = '\0';
|
|
169
|
+
set_long_cell(buff, offset, value_copy);
|
|
170
|
+
buff->long_cell_count++;
|
|
171
|
+
} else
|
|
172
|
+
stat = zsvsheet_priv_status_memory;
|
|
173
|
+
}
|
|
174
|
+
}
|
|
175
|
+
return stat;
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
enum zsvsheet_priv_status zsvsheet_screen_buffer_write_cell(zsvsheet_screen_buffer_t buff, size_t row, size_t col,
|
|
179
|
+
const unsigned char *value) {
|
|
180
|
+
return zsvsheet_screen_buffer_write_cell_w_len(buff, row, col, value, strlen((void *)value));
|
|
181
|
+
}
|
|
182
|
+
|
|
183
|
+
int zsvsheet_screen_buffer_cell_attrs(zsvsheet_screen_buffer_t buff, size_t row, size_t col) {
|
|
184
|
+
if (buff->cell_attrs) {
|
|
185
|
+
size_t offset = row * buff->cols + col;
|
|
186
|
+
return buff->cell_attrs[offset];
|
|
187
|
+
}
|
|
188
|
+
return 0;
|
|
189
|
+
}
|
|
190
|
+
|
|
191
|
+
const unsigned char *zsvsheet_screen_buffer_cell_display(zsvsheet_screen_buffer_t buff, size_t row, size_t col) {
|
|
192
|
+
if (row < buff->opts.rows && col < buff->cols) {
|
|
193
|
+
size_t offset = row * buff->cols * buff->opts.cell_buff_len + col * buff->opts.cell_buff_len;
|
|
194
|
+
const unsigned char *cell = &buff->data[offset];
|
|
195
|
+
if (!is_long_cell(cell, buff->opts.cell_buff_len))
|
|
196
|
+
return cell;
|
|
197
|
+
|
|
198
|
+
// if the fixed-length cell memory does not end with NULL,
|
|
199
|
+
// then the cell value is a pointer to memory holding the value
|
|
200
|
+
return (unsigned char *)get_long_cell(buff, offset);
|
|
201
|
+
}
|
|
202
|
+
return NULL;
|
|
203
|
+
}
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
#ifndef ZSVSHEET_SCREEN_BUFFER_H
|
|
2
|
+
#define ZSVSHEET_SCREEN_BUFFER_H
|
|
3
|
+
|
|
4
|
+
#define ZSVSHEET_SCREEN_BUFFER_DEFAULT_CELL_BUFF_LEN 16
|
|
5
|
+
#define ZSVSHEET_SCREEN_BUFFER_DEFAULT_MAX_CELL_LEN 32768 - 1
|
|
6
|
+
#define ZSVSHEET_SCREEN_BUFFER_DEFAULT_ROW_COUNT 1024
|
|
7
|
+
|
|
8
|
+
typedef struct zsvsheet_screen_buffer *zsvsheet_screen_buffer_t;
|
|
9
|
+
|
|
10
|
+
struct zsvsheet_screen_buffer_opts {
|
|
11
|
+
size_t cell_buff_len; // default = 16. must be >= 2 * sizeof(void *)
|
|
12
|
+
size_t max_cell_len; // length in bytes; defaults to 32767
|
|
13
|
+
size_t rows; // rows to buffer. cannot be < 256
|
|
14
|
+
char no_rownum_column; // reserved. TO DO: if set, omit row num column
|
|
15
|
+
};
|
|
16
|
+
|
|
17
|
+
zsvsheet_screen_buffer_t zsvsheet_screen_buffer_new(size_t cols, struct zsvsheet_screen_buffer_opts *opts,
|
|
18
|
+
enum zsvsheet_priv_status *stat);
|
|
19
|
+
enum zsvsheet_priv_status zsvsheet_screen_buffer_grow(zsvsheet_screen_buffer_t buff, size_t cols);
|
|
20
|
+
|
|
21
|
+
enum zsvsheet_priv_status zsvsheet_screen_buffer_write_cell(zsvsheet_screen_buffer_t buff, size_t row, size_t col,
|
|
22
|
+
const unsigned char *value);
|
|
23
|
+
|
|
24
|
+
enum zsvsheet_priv_status zsvsheet_screen_buffer_write_cell_w_len(zsvsheet_screen_buffer_t buff, size_t row, size_t col,
|
|
25
|
+
const unsigned char *value, size_t len);
|
|
26
|
+
|
|
27
|
+
const unsigned char *zsvsheet_screen_buffer_cell_display(zsvsheet_screen_buffer_t buff, size_t row, size_t col);
|
|
28
|
+
|
|
29
|
+
int zsvsheet_screen_buffer_cell_attrs(zsvsheet_screen_buffer_t buff, size_t row, size_t col);
|
|
30
|
+
|
|
31
|
+
void zsvsheet_screen_buffer_delete(zsvsheet_screen_buffer_t);
|
|
32
|
+
|
|
33
|
+
size_t zsvsheet_screen_buffer_cols(zsvsheet_screen_buffer_t);
|
|
34
|
+
size_t zsvsheet_screen_buffer_rows(zsvsheet_screen_buffer_t buff);
|
|
35
|
+
|
|
36
|
+
#endif
|