zsv 1.3.1 → 1.4.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (259) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +153 -2
  3. data/README.md +132 -240
  4. data/ext/zsv/ZSV_UPSTREAM +7 -0
  5. data/ext/zsv/common.h +0 -12
  6. data/ext/zsv/extconf.rb +70 -122
  7. data/ext/zsv/options.c +63 -62
  8. data/ext/zsv/options.h +8 -11
  9. data/ext/zsv/parser.c +390 -201
  10. data/ext/zsv/parser.h +35 -22
  11. data/ext/zsv/row.c +75 -28
  12. data/ext/zsv/row.h +15 -9
  13. data/ext/zsv/upstream.rb +55 -0
  14. data/ext/zsv/vendor/zsv/LICENSE +21 -0
  15. data/ext/zsv/vendor/zsv/VENDORED_FROM +2 -0
  16. data/ext/zsv/vendor/zsv/configure +1212 -0
  17. data/ext/zsv/vendor/{zsv-1.3.0 → zsv}/include/zsv/api.h +39 -0
  18. data/ext/zsv/vendor/{zsv-1.3.0 → zsv}/include/zsv/common.h +36 -0
  19. data/ext/zsv/vendor/{zsv-1.3.0 → zsv}/include/zsv/ext/sheet.h +8 -1
  20. data/ext/zsv/vendor/{zsv-1.3.0 → zsv}/include/zsv/ext.h +6 -2
  21. data/ext/zsv/vendor/zsv/include/zsv/utils/appname.h +51 -0
  22. data/ext/zsv/vendor/{zsv-1.3.0 → zsv}/include/zsv/utils/arg.h +50 -1
  23. data/ext/zsv/vendor/{zsv-1.3.0 → zsv}/include/zsv/utils/compiler.h +19 -8
  24. data/ext/zsv/vendor/{zsv-1.3.0 → zsv}/include/zsv/utils/dirs.h +2 -0
  25. data/ext/zsv/vendor/{zsv-1.3.0 → zsv}/include/zsv/utils/file-mem.h +1 -0
  26. data/ext/zsv/vendor/{zsv-1.3.0 → zsv}/include/zsv/utils/file.h +28 -2
  27. data/ext/zsv/vendor/zsv/include/zsv/utils/os.h +145 -0
  28. data/ext/zsv/vendor/zsv/include/zsv/utils/output.h +34 -0
  29. data/ext/zsv/vendor/{zsv-1.3.0 → zsv}/include/zsv/utils/overwrite_writer.h +1 -1
  30. data/ext/zsv/vendor/{zsv-1.3.0 → zsv}/include/zsv/utils/prop.h +16 -4
  31. data/ext/zsv/vendor/zsv/include/zsv/utils/rng.h +35 -0
  32. data/ext/zsv/vendor/{zsv-1.3.0 → zsv}/include/zsv/utils/signal.h +7 -0
  33. data/ext/zsv/vendor/zsv/include/zsv/utils/sql.h +13 -0
  34. data/ext/zsv/vendor/{zsv-1.3.0 → zsv}/include/zsv/utils/string.h +25 -0
  35. data/ext/zsv/vendor/{zsv-1.3.0 → zsv}/include/zsv/utils/writer.h +9 -1
  36. data/ext/zsv/vendor/{zsv-1.3.0 → zsv}/include/zsv/zsv_export.h +1 -1
  37. data/ext/zsv/vendor/{zsv-1.3.0/include/zsv.h → zsv/include/zsv.h.in} +1 -3
  38. data/ext/zsv/vendor/zsv/src/Makefile +238 -0
  39. data/ext/zsv/vendor/{zsv-1.3.0 → zsv}/src/zsv.c +152 -14
  40. data/ext/zsv/vendor/{zsv-1.3.0 → zsv}/src/zsv_internal.c +108 -50
  41. data/ext/zsv/vendor/zsv/src/zsv_scan_delim_fast.c +741 -0
  42. data/ext/zsv/vendor/zsv/src/zsv_scan_simd_avx2.h +89 -0
  43. data/ext/zsv/vendor/zsv/src/zsv_scan_simd_neon.h +80 -0
  44. data/ext/zsv/vendor/zsv/src/zsv_scan_simd_sse2.h +93 -0
  45. data/ext/zsv/vendor/zsv/src/zsv_scan_simd_wasm.h +79 -0
  46. data/ext/zsv/vendor/zsv/src/zsv_strencode.c +84 -0
  47. data/ext/zsv/zsv_ext.c +176 -105
  48. data/lib/zsv/errors.rb +32 -0
  49. data/lib/zsv/input.rb +88 -0
  50. data/lib/zsv/options.rb +132 -0
  51. data/lib/zsv/strict_validator.rb +124 -0
  52. data/lib/zsv/version.rb +1 -1
  53. data/lib/zsv.rb +137 -35
  54. metadata +81 -230
  55. data/ext/zsv/vendor/zsv-1.3.0/app/2db.c +0 -756
  56. data/ext/zsv/vendor/zsv-1.3.0/app/2json.c +0 -381
  57. data/ext/zsv/vendor/zsv-1.3.0/app/2tsv.c +0 -228
  58. data/ext/zsv/vendor/zsv-1.3.0/app/builtin/help.c +0 -123
  59. data/ext/zsv/vendor/zsv-1.3.0/app/builtin/license.c +0 -39
  60. data/ext/zsv/vendor/zsv-1.3.0/app/builtin/register.c +0 -104
  61. data/ext/zsv/vendor/zsv-1.3.0/app/builtin/thirdparty.c +0 -41
  62. data/ext/zsv/vendor/zsv-1.3.0/app/builtin/unregister.c +0 -1
  63. data/ext/zsv/vendor/zsv-1.3.0/app/builtin/version.c +0 -14
  64. data/ext/zsv/vendor/zsv-1.3.0/app/check/simdutf_wrapper.h +0 -19
  65. data/ext/zsv/vendor/zsv-1.3.0/app/check/utf8.c +0 -116
  66. data/ext/zsv/vendor/zsv-1.3.0/app/check.c +0 -194
  67. data/ext/zsv/vendor/zsv-1.3.0/app/cli.c +0 -796
  68. data/ext/zsv/vendor/zsv-1.3.0/app/cli_const.h +0 -41
  69. data/ext/zsv/vendor/zsv-1.3.0/app/cli_export.h +0 -16
  70. data/ext/zsv/vendor/zsv-1.3.0/app/cli_ini.c +0 -280
  71. data/ext/zsv/vendor/zsv-1.3.0/app/cli_internal.h +0 -36
  72. data/ext/zsv/vendor/zsv-1.3.0/app/compare.c +0 -913
  73. data/ext/zsv/vendor/zsv-1.3.0/app/compare.h +0 -23
  74. data/ext/zsv/vendor/zsv-1.3.0/app/compare_added_column.c +0 -20
  75. data/ext/zsv/vendor/zsv-1.3.0/app/compare_internal.h +0 -140
  76. data/ext/zsv/vendor/zsv-1.3.0/app/compare_sort.c +0 -91
  77. data/ext/zsv/vendor/zsv-1.3.0/app/compare_unique_colname.c +0 -81
  78. data/ext/zsv/vendor/zsv-1.3.0/app/count-pull.c +0 -82
  79. data/ext/zsv/vendor/zsv-1.3.0/app/count.c +0 -404
  80. data/ext/zsv/vendor/zsv-1.3.0/app/desc.c +0 -569
  81. data/ext/zsv/vendor/zsv-1.3.0/app/echo.c +0 -365
  82. data/ext/zsv/vendor/zsv-1.3.0/app/ext_example/my_extension.c +0 -366
  83. data/ext/zsv/vendor/zsv-1.3.0/app/ext_example/mysheet_extension.c +0 -341
  84. data/ext/zsv/vendor/zsv-1.3.0/app/ext_template/YOUR_EXTENSION_zsvext.c +0 -263
  85. data/ext/zsv/vendor/zsv-1.3.0/app/external/inih/ini.c +0 -298
  86. data/ext/zsv/vendor/zsv-1.3.0/app/external/inih/ini.h +0 -157
  87. data/ext/zsv/vendor/zsv-1.3.0/app/external/json_writer-1.01/json_numeric.c +0 -177
  88. data/ext/zsv/vendor/zsv-1.3.0/app/external/json_writer-1.01/jsonwriter.c +0 -444
  89. data/ext/zsv/vendor/zsv-1.3.0/app/external/json_writer-1.01/jsonwriter.h +0 -145
  90. data/ext/zsv/vendor/zsv-1.3.0/app/external/json_writer-1.01/utils.c +0 -110
  91. data/ext/zsv/vendor/zsv-1.3.0/app/external/memfile-1.0/include/memfile.h +0 -15
  92. data/ext/zsv/vendor/zsv-1.3.0/app/external/memfile-1.0/src/memfile.c +0 -64
  93. data/ext/zsv/vendor/zsv-1.3.0/app/external/sglib/sglib.h +0 -1955
  94. data/ext/zsv/vendor/zsv-1.3.0/app/external/simdutf/simdutf.h +0 -6802
  95. data/ext/zsv/vendor/zsv-1.3.0/app/external/sqlite3/sqlite3.c +0 -230517
  96. data/ext/zsv/vendor/zsv-1.3.0/app/external/sqlite3/sqlite3.h +0 -12174
  97. data/ext/zsv/vendor/zsv-1.3.0/app/external/sqlite3/sqlite3_and_csv_vtab.c +0 -2
  98. data/ext/zsv/vendor/zsv-1.3.0/app/external/sqlite3/sqlite3_csv_vtab-mem.c +0 -142
  99. data/ext/zsv/vendor/zsv-1.3.0/app/external/sqlite3/sqlite3_csv_vtab-mem.h +0 -49
  100. data/ext/zsv/vendor/zsv-1.3.0/app/external/sqlite3/sqlite3_csv_vtab-zsv.c +0 -485
  101. data/ext/zsv/vendor/zsv-1.3.0/app/external/sqlite3/sqlite3_csv_vtab.c +0 -1015
  102. data/ext/zsv/vendor/zsv-1.3.0/app/external/sqlite3/sqlite3ext.h +0 -663
  103. data/ext/zsv/vendor/zsv-1.3.0/app/external/sqlite3/vtab_helper.c +0 -85
  104. data/ext/zsv/vendor/zsv-1.3.0/app/external/yajl/build/yajl-2.1.1/include/yajl/yajl_common.h +0 -75
  105. data/ext/zsv/vendor/zsv-1.3.0/app/external/yajl/build/yajl-2.1.1/include/yajl/yajl_gen.h +0 -167
  106. data/ext/zsv/vendor/zsv-1.3.0/app/external/yajl/build/yajl-2.1.1/include/yajl/yajl_parse.h +0 -228
  107. data/ext/zsv/vendor/zsv-1.3.0/app/external/yajl/build/yajl-2.1.1/include/yajl/yajl_tree.h +0 -186
  108. data/ext/zsv/vendor/zsv-1.3.0/app/external/yajl/build/yajl-2.1.1/include/yajl/yajl_version.h +0 -23
  109. data/ext/zsv/vendor/zsv-1.3.0/app/external/yajl/src/api/yajl_common.h +0 -76
  110. data/ext/zsv/vendor/zsv-1.3.0/app/external/yajl/src/api/yajl_gen.h +0 -167
  111. data/ext/zsv/vendor/zsv-1.3.0/app/external/yajl/src/api/yajl_parse.h +0 -238
  112. data/ext/zsv/vendor/zsv-1.3.0/app/external/yajl/src/api/yajl_tree.h +0 -186
  113. data/ext/zsv/vendor/zsv-1.3.0/app/external/yajl/src/yajl.c +0 -184
  114. data/ext/zsv/vendor/zsv-1.3.0/app/external/yajl/src/yajl_alloc.c +0 -52
  115. data/ext/zsv/vendor/zsv-1.3.0/app/external/yajl/src/yajl_alloc.h +0 -34
  116. data/ext/zsv/vendor/zsv-1.3.0/app/external/yajl/src/yajl_buf.c +0 -103
  117. data/ext/zsv/vendor/zsv-1.3.0/app/external/yajl/src/yajl_buf.h +0 -57
  118. data/ext/zsv/vendor/zsv-1.3.0/app/external/yajl/src/yajl_bytestack.h +0 -69
  119. data/ext/zsv/vendor/zsv-1.3.0/app/external/yajl/src/yajl_encode.c +0 -220
  120. data/ext/zsv/vendor/zsv-1.3.0/app/external/yajl/src/yajl_encode.h +0 -34
  121. data/ext/zsv/vendor/zsv-1.3.0/app/external/yajl/src/yajl_gen.c +0 -362
  122. data/ext/zsv/vendor/zsv-1.3.0/app/external/yajl/src/yajl_lex.c +0 -764
  123. data/ext/zsv/vendor/zsv-1.3.0/app/external/yajl/src/yajl_lex.h +0 -117
  124. data/ext/zsv/vendor/zsv-1.3.0/app/external/yajl/src/yajl_parser.c +0 -508
  125. data/ext/zsv/vendor/zsv-1.3.0/app/external/yajl/src/yajl_parser.h +0 -78
  126. data/ext/zsv/vendor/zsv-1.3.0/app/external/yajl/src/yajl_tree.c +0 -505
  127. data/ext/zsv/vendor/zsv-1.3.0/app/external/yajl/src/yajl_version.c +0 -7
  128. data/ext/zsv/vendor/zsv-1.3.0/app/external/yajl_helper/yajl_helper/json_value.h +0 -59
  129. data/ext/zsv/vendor/zsv-1.3.0/app/external/yajl_helper/yajl_helper/yajl_helper.h +0 -208
  130. data/ext/zsv/vendor/zsv-1.3.0/app/external/yajl_helper/yajl_helper.c +0 -795
  131. data/ext/zsv/vendor/zsv-1.3.0/app/external/yajl_helper/yajl_helper_internal.h +0 -28
  132. data/ext/zsv/vendor/zsv-1.3.0/app/flatten.c +0 -851
  133. data/ext/zsv/vendor/zsv-1.3.0/app/jq.c +0 -106
  134. data/ext/zsv/vendor/zsv-1.3.0/app/jq.h +0 -6
  135. data/ext/zsv/vendor/zsv-1.3.0/app/mv.c +0 -113
  136. data/ext/zsv/vendor/zsv-1.3.0/app/noop.c +0 -90
  137. data/ext/zsv/vendor/zsv-1.3.0/app/overwrite.c +0 -295
  138. data/ext/zsv/vendor/zsv-1.3.0/app/paste.c +0 -175
  139. data/ext/zsv/vendor/zsv-1.3.0/app/pretty.c +0 -693
  140. data/ext/zsv/vendor/zsv-1.3.0/app/prop.c +0 -980
  141. data/ext/zsv/vendor/zsv-1.3.0/app/rm.c +0 -131
  142. data/ext/zsv/vendor/zsv-1.3.0/app/select/fixed.c +0 -130
  143. data/ext/zsv/vendor/zsv-1.3.0/app/select/internal.h +0 -118
  144. data/ext/zsv/vendor/zsv-1.3.0/app/select/parallel.c +0 -45
  145. data/ext/zsv/vendor/zsv-1.3.0/app/select/parallel.h +0 -41
  146. data/ext/zsv/vendor/zsv-1.3.0/app/select/processing.c +0 -107
  147. data/ext/zsv/vendor/zsv-1.3.0/app/select/rand.c +0 -20
  148. data/ext/zsv/vendor/zsv-1.3.0/app/select/regex.c +0 -61
  149. data/ext/zsv/vendor/zsv-1.3.0/app/select/search.c +0 -14
  150. data/ext/zsv/vendor/zsv-1.3.0/app/select/selection.c +0 -192
  151. data/ext/zsv/vendor/zsv-1.3.0/app/select/usage.c +0 -72
  152. data/ext/zsv/vendor/zsv-1.3.0/app/select-pull.c +0 -812
  153. data/ext/zsv/vendor/zsv-1.3.0/app/select.c +0 -753
  154. data/ext/zsv/vendor/zsv-1.3.0/app/serialize.c +0 -372
  155. data/ext/zsv/vendor/zsv-1.3.0/app/sheet/curses.h +0 -15
  156. data/ext/zsv/vendor/zsv-1.3.0/app/sheet/cursor.c +0 -119
  157. data/ext/zsv/vendor/zsv-1.3.0/app/sheet/errors.c +0 -45
  158. data/ext/zsv/vendor/zsv-1.3.0/app/sheet/file.c +0 -63
  159. data/ext/zsv/vendor/zsv-1.3.0/app/sheet/file.h +0 -12
  160. data/ext/zsv/vendor/zsv-1.3.0/app/sheet/filter.c +0 -166
  161. data/ext/zsv/vendor/zsv-1.3.0/app/sheet/handlers.c +0 -214
  162. data/ext/zsv/vendor/zsv-1.3.0/app/sheet/handlers_internal.h +0 -128
  163. data/ext/zsv/vendor/zsv-1.3.0/app/sheet/help.c +0 -43
  164. data/ext/zsv/vendor/zsv-1.3.0/app/sheet/index.c +0 -81
  165. data/ext/zsv/vendor/zsv-1.3.0/app/sheet/index.h +0 -25
  166. data/ext/zsv/vendor/zsv-1.3.0/app/sheet/key-bindings.c +0 -325
  167. data/ext/zsv/vendor/zsv-1.3.0/app/sheet/key-bindings.h +0 -73
  168. data/ext/zsv/vendor/zsv-1.3.0/app/sheet/lexer.c +0 -203
  169. data/ext/zsv/vendor/zsv-1.3.0/app/sheet/newline_handler.c +0 -7
  170. data/ext/zsv/vendor/zsv-1.3.0/app/sheet/pivot.c +0 -318
  171. data/ext/zsv/vendor/zsv-1.3.0/app/sheet/procedure.c +0 -134
  172. data/ext/zsv/vendor/zsv-1.3.0/app/sheet/procedure.h +0 -119
  173. data/ext/zsv/vendor/zsv-1.3.0/app/sheet/read-data.c +0 -322
  174. data/ext/zsv/vendor/zsv-1.3.0/app/sheet/screen_buffer.c +0 -203
  175. data/ext/zsv/vendor/zsv-1.3.0/app/sheet/screen_buffer.h +0 -36
  176. data/ext/zsv/vendor/zsv-1.3.0/app/sheet/sheet-sql.c +0 -167
  177. data/ext/zsv/vendor/zsv-1.3.0/app/sheet/sheet_internal.h +0 -36
  178. data/ext/zsv/vendor/zsv-1.3.0/app/sheet/sqlfilter.c +0 -153
  179. data/ext/zsv/vendor/zsv-1.3.0/app/sheet/terminfo.c +0 -32
  180. data/ext/zsv/vendor/zsv-1.3.0/app/sheet/transformation.c +0 -312
  181. data/ext/zsv/vendor/zsv-1.3.0/app/sheet/transformation.h +0 -29
  182. data/ext/zsv/vendor/zsv-1.3.0/app/sheet/ui_buffer.c +0 -266
  183. data/ext/zsv/vendor/zsv-1.3.0/app/sheet/usage.c +0 -9
  184. data/ext/zsv/vendor/zsv-1.3.0/app/sheet/utf8-width.c +0 -60
  185. data/ext/zsv/vendor/zsv-1.3.0/app/sheet.c +0 -1007
  186. data/ext/zsv/vendor/zsv-1.3.0/app/sql.c +0 -453
  187. data/ext/zsv/vendor/zsv-1.3.0/app/sql_internal.c +0 -101
  188. data/ext/zsv/vendor/zsv-1.3.0/app/sql_internal.h +0 -49
  189. data/ext/zsv/vendor/zsv-1.3.0/app/stack.c +0 -393
  190. data/ext/zsv/vendor/zsv-1.3.0/app/utils/arg.c +0 -322
  191. data/ext/zsv/vendor/zsv-1.3.0/app/utils/cache.c +0 -228
  192. data/ext/zsv/vendor/zsv-1.3.0/app/utils/cat.c +0 -91
  193. data/ext/zsv/vendor/zsv-1.3.0/app/utils/chunk.c +0 -240
  194. data/ext/zsv/vendor/zsv-1.3.0/app/utils/chunk.h +0 -63
  195. data/ext/zsv/vendor/zsv-1.3.0/app/utils/clock.c +0 -57
  196. data/ext/zsv/vendor/zsv-1.3.0/app/utils/db.c +0 -148
  197. data/ext/zsv/vendor/zsv-1.3.0/app/utils/dirs-no-jq.c +0 -2
  198. data/ext/zsv/vendor/zsv-1.3.0/app/utils/dirs.c +0 -427
  199. data/ext/zsv/vendor/zsv-1.3.0/app/utils/dirs_from_json.c +0 -253
  200. data/ext/zsv/vendor/zsv-1.3.0/app/utils/dirs_to_json.c +0 -121
  201. data/ext/zsv/vendor/zsv-1.3.0/app/utils/dl.c +0 -20
  202. data/ext/zsv/vendor/zsv-1.3.0/app/utils/emcc/fs_api.c +0 -159
  203. data/ext/zsv/vendor/zsv-1.3.0/app/utils/err.c +0 -24
  204. data/ext/zsv/vendor/zsv-1.3.0/app/utils/file-mem.c +0 -180
  205. data/ext/zsv/vendor/zsv-1.3.0/app/utils/file.c +0 -256
  206. data/ext/zsv/vendor/zsv-1.3.0/app/utils/index.c +0 -197
  207. data/ext/zsv/vendor/zsv-1.3.0/app/utils/index.h +0 -49
  208. data/ext/zsv/vendor/zsv-1.3.0/app/utils/jq.c +0 -400
  209. data/ext/zsv/vendor/zsv-1.3.0/app/utils/json.c +0 -120
  210. data/ext/zsv/vendor/zsv-1.3.0/app/utils/mem.c +0 -18
  211. data/ext/zsv/vendor/zsv-1.3.0/app/utils/memmem.c +0 -132
  212. data/ext/zsv/vendor/zsv-1.3.0/app/utils/os.c +0 -178
  213. data/ext/zsv/vendor/zsv-1.3.0/app/utils/overwrite.c +0 -258
  214. data/ext/zsv/vendor/zsv-1.3.0/app/utils/overwrite_writer.c +0 -246
  215. data/ext/zsv/vendor/zsv-1.3.0/app/utils/pcre2-8/pcre2-8-test.c +0 -123
  216. data/ext/zsv/vendor/zsv-1.3.0/app/utils/pcre2-8/pcre2-8.c +0 -153
  217. data/ext/zsv/vendor/zsv-1.3.0/app/utils/pcre2-8/pcre2-8.h +0 -54
  218. data/ext/zsv/vendor/zsv-1.3.0/app/utils/prop.c +0 -267
  219. data/ext/zsv/vendor/zsv-1.3.0/app/utils/signal.c +0 -53
  220. data/ext/zsv/vendor/zsv-1.3.0/app/utils/string.c +0 -357
  221. data/ext/zsv/vendor/zsv-1.3.0/app/utils/win/dir_exists_longpath.c +0 -83
  222. data/ext/zsv/vendor/zsv-1.3.0/app/utils/win/dl.c +0 -33
  223. data/ext/zsv/vendor/zsv-1.3.0/app/utils/win/fopen_longpath.c +0 -184
  224. data/ext/zsv/vendor/zsv-1.3.0/app/utils/win/foreach_dirent_longpath.c +0 -292
  225. data/ext/zsv/vendor/zsv-1.3.0/app/utils/win/io.c +0 -259
  226. data/ext/zsv/vendor/zsv-1.3.0/app/utils/win/io.h +0 -13
  227. data/ext/zsv/vendor/zsv-1.3.0/app/utils/win/mkdir_longpath.c +0 -255
  228. data/ext/zsv/vendor/zsv-1.3.0/app/utils/win/remove_longpath.c +0 -96
  229. data/ext/zsv/vendor/zsv-1.3.0/app/utils/writer.c +0 -361
  230. data/ext/zsv/vendor/zsv-1.3.0/app/zsv_command.h +0 -40
  231. data/ext/zsv/vendor/zsv-1.3.0/app/zsv_command_standalone.c +0 -16
  232. data/ext/zsv/vendor/zsv-1.3.0/app/zsv_main.h +0 -44
  233. data/ext/zsv/vendor/zsv-1.3.0/examples/js/zsv_parser_api_dummy.c +0 -3
  234. data/ext/zsv/vendor/zsv-1.3.0/examples/lib/parse_by_chunk.c +0 -100
  235. data/ext/zsv/vendor/zsv-1.3.0/examples/lib/print_my_column.c +0 -143
  236. data/ext/zsv/vendor/zsv-1.3.0/examples/lib/pull.c +0 -89
  237. data/ext/zsv/vendor/zsv-1.3.0/examples/lib/simple.c +0 -123
  238. data/ext/zsv/vendor/zsv-1.3.0/fuzz/fuzz.c +0 -16
  239. data/ext/zsv/vendor/zsv-1.3.0/include/zsv/utils/os.h +0 -54
  240. data/ext/zsv/vendor/zsv-1.3.0/include/zsv/utils/sql.h +0 -11
  241. data/ext/zsv/vendor/zsv-1.3.0/src/zsv_strencode.c +0 -51
  242. /data/ext/zsv/vendor/{zsv-1.3.0 → zsv}/include/zsv/ext/implementation.h +0 -0
  243. /data/ext/zsv/vendor/{zsv-1.3.0 → zsv}/include/zsv/ext/implementation_private.h +0 -0
  244. /data/ext/zsv/vendor/{zsv-1.3.0 → zsv}/include/zsv/utils/cache.h +0 -0
  245. /data/ext/zsv/vendor/{zsv-1.3.0 → zsv}/include/zsv/utils/clock.h +0 -0
  246. /data/ext/zsv/vendor/{zsv-1.3.0 → zsv}/include/zsv/utils/db.h +0 -0
  247. /data/ext/zsv/vendor/{zsv-1.3.0 → zsv}/include/zsv/utils/dl.h +0 -0
  248. /data/ext/zsv/vendor/{zsv-1.3.0 → zsv}/include/zsv/utils/emcc/fs_api.h +0 -0
  249. /data/ext/zsv/vendor/{zsv-1.3.0 → zsv}/include/zsv/utils/err.h +0 -0
  250. /data/ext/zsv/vendor/{zsv-1.3.0 → zsv}/include/zsv/utils/jq.h +0 -0
  251. /data/ext/zsv/vendor/{zsv-1.3.0 → zsv}/include/zsv/utils/json.h +0 -0
  252. /data/ext/zsv/vendor/{zsv-1.3.0 → zsv}/include/zsv/utils/mem.h +0 -0
  253. /data/ext/zsv/vendor/{zsv-1.3.0 → zsv}/include/zsv/utils/memmem.h +0 -0
  254. /data/ext/zsv/vendor/{zsv-1.3.0 → zsv}/include/zsv/utils/overwrite.h +0 -0
  255. /data/ext/zsv/vendor/{zsv-1.3.0 → zsv}/include/zsv/utils/utf8.h +0 -0
  256. /data/ext/zsv/vendor/{zsv-1.3.0 → zsv}/include/zsv/utils/win/dl.h +0 -0
  257. /data/ext/zsv/vendor/{zsv-1.3.0 → zsv}/src/vector_delim.c +0 -0
  258. /data/ext/zsv/vendor/{zsv-1.3.0 → zsv}/src/zsv_scan_delim.c +0 -0
  259. /data/ext/zsv/vendor/{zsv-1.3.0 → zsv}/src/zsv_scan_fixed.c +0 -0
@@ -1,167 +0,0 @@
1
- static zsvsheet_status zsv_sqlite3_to_csv(zsvsheet_proc_context_t pctx, struct zsv_sqlite3_db *zdb, const char *sql,
2
- const char **err_msg, void *ctx,
3
- void (*on_header_cell)(void *, size_t, const char *),
4
- void (*on_data_cell)(void *, size_t, const char *, size_t len)) {
5
- zsvsheet_status zst = zsvsheet_status_error;
6
- sqlite3_stmt *stmt = NULL;
7
- char have_data = 0;
8
- if ((zdb->rc = sqlite3_prepare_v2(zdb->db, sql, -1, &stmt, NULL)) == SQLITE_OK) {
9
- char *tmp_fn = zsv_get_temp_filename("zsv_mysheet_ext_XXXXXXXX");
10
- struct zsv_csv_writer_options writer_opts = zsv_writer_get_default_opts();
11
- zsv_csv_writer cw = NULL;
12
- if (!tmp_fn)
13
- zst = zsvsheet_status_memory;
14
- else if (!(writer_opts.stream = fopen(tmp_fn, "wb"))) {
15
- zst = zsvsheet_status_error;
16
- *err_msg = strerror(errno);
17
- } else if (!(cw = zsv_writer_new(&writer_opts)))
18
- zst = zsvsheet_status_memory;
19
- else {
20
- zst = zsvsheet_status_ok;
21
- unsigned char cw_buff[1024];
22
- zsv_writer_set_temp_buff(cw, cw_buff, sizeof(cw_buff));
23
-
24
- int col_count = sqlite3_column_count(stmt);
25
- // write header row
26
- for (int i = 0; i < col_count; i++) {
27
- const char *colname = sqlite3_column_name(stmt, i);
28
- zsv_writer_cell(cw, !i, (const unsigned char *)colname, colname ? strlen(colname) : 0, 1);
29
- if (on_header_cell)
30
- on_header_cell(ctx, i, colname);
31
- }
32
-
33
- // write sql results
34
- while (sqlite3_step(stmt) == SQLITE_ROW) {
35
- for (int i = 0; i < col_count; i++) {
36
- const unsigned char *text = sqlite3_column_text(stmt, i);
37
- int len = text ? sqlite3_column_bytes(stmt, i) : 0;
38
- zsv_writer_cell(cw, !i, text, len, 1);
39
- have_data = 1;
40
- if (on_data_cell)
41
- on_data_cell(ctx, i, (const char *)text, len);
42
- }
43
- }
44
- }
45
- if (cw)
46
- zsv_writer_delete(cw);
47
- if (writer_opts.stream)
48
- fclose(writer_opts.stream);
49
-
50
- if (tmp_fn && zsv_file_exists(tmp_fn) && have_data) {
51
- struct zsvsheet_ui_buffer_opts uibopts = {0};
52
- uibopts.data_filename = tmp_fn;
53
- zst = zsvsheet_open_file_opts(pctx, &uibopts);
54
- } else {
55
- if (zst == zsvsheet_status_ok) {
56
- if (!have_data)
57
- zst = zsvsheet_status_no_data;
58
- if (!*err_msg && zdb && zdb->rc != SQLITE_OK) {
59
- zst = zsvsheet_status_error; // to do: make this more specific
60
- *err_msg = sqlite3_errmsg(zdb->db);
61
- }
62
- }
63
- if (tmp_fn && zsv_file_exists(tmp_fn))
64
- unlink(tmp_fn);
65
- }
66
- free(tmp_fn);
67
- }
68
- if (stmt)
69
- sqlite3_finalize(stmt);
70
-
71
- return zst;
72
- }
73
-
74
- static int is_constant_expression(sqlite3 *db, const char *expr, int *err) {
75
- sqlite3_stmt *stmt = NULL;
76
- // We try to prepare "SELECT [expr]". If this succeeds, the expression
77
- // does not depend on any columns and is therefore constant.
78
- char *sql_const_test = sqlite3_mprintf("SELECT %s", expr);
79
- if (!sql_const_test) {
80
- *err = errno;
81
- return 0;
82
- }
83
-
84
- int rc = sqlite3_prepare_v2(db, sql_const_test, -1, &stmt, NULL);
85
- if (stmt) {
86
- sqlite3_finalize(stmt);
87
- stmt = NULL;
88
- }
89
- sqlite3_free(sql_const_test);
90
-
91
- int is_constant = (rc == SQLITE_OK);
92
- return is_constant;
93
- }
94
-
95
- enum check_select_expression_result {
96
- zsv_select_sql_expression_valid = 0,
97
- zsv_select_sql_expression_invalid,
98
- zsv_select_sql_expression_multiple_statements,
99
- zsv_select_sql_expression_multiple_expressions,
100
- zsv_select_sql_expression_other
101
- };
102
-
103
- const char *check_select_expression_result_str(enum check_select_expression_result rc) {
104
- switch (rc) {
105
- case zsv_select_sql_expression_valid:
106
- return NULL;
107
- case zsv_select_sql_expression_invalid:
108
- return "Invalid SQL";
109
- case zsv_select_sql_expression_multiple_statements:
110
- return "Please enter only a single expression";
111
- case zsv_select_sql_expression_multiple_expressions:
112
- return "Please enter only a single expression";
113
- case zsv_select_sql_expression_other:
114
- return "Unknown error";
115
- }
116
- return NULL;
117
- }
118
-
119
- static int is_str_empty(const char *s) {
120
- if (!s)
121
- return 1;
122
- while (*s) {
123
- if (!isspace((unsigned char)*s)) {
124
- return 0;
125
- }
126
- s++;
127
- }
128
- return 1;
129
- }
130
-
131
- static enum check_select_expression_result check_select_expression(sqlite3 *db, const char *expr, int *err) {
132
- sqlite3_stmt *stmt = NULL;
133
-
134
- // Prepare "SELECT [expr] FROM data" to see if it's valid in the
135
- // context of the 'data' table.
136
- char *sql_valid_test = sqlite3_mprintf("SELECT %s FROM data LIMIT 0", expr);
137
- if (!sql_valid_test) {
138
- *err = errno;
139
- return zsv_select_sql_expression_other;
140
- }
141
-
142
- const char *pzTail = NULL;
143
- int rc = sqlite3_prepare_v2(db, sql_valid_test, -1, &stmt, &pzTail);
144
- sqlite3_free(sql_valid_test); // Free the string immediately
145
- if (rc != SQLITE_OK) {
146
- if (stmt)
147
- sqlite3_finalize(stmt);
148
- return zsv_select_sql_expression_invalid;
149
- }
150
-
151
- // if we are here, the expression is valid. check it is a single statement
152
- // pzTail points to the start of the *next* statement. If it's not
153
- // empty or just whitespace, the user tried to inject a second command.
154
- if (!is_str_empty(pzTail)) {
155
- sqlite3_finalize(stmt);
156
- return zsv_select_sql_expression_multiple_statements;
157
- }
158
-
159
- // Check that we have a single expression (e.g., not "myvalue, 123")
160
- int col_count = sqlite3_column_count(stmt);
161
-
162
- sqlite3_finalize(stmt);
163
- if (col_count != 1)
164
- return zsv_select_sql_expression_multiple_expressions;
165
-
166
- return zsv_select_sql_expression_valid;
167
- }
@@ -1,36 +0,0 @@
1
- #ifndef ZSVSHEET_INTERNAL_H
2
- #define ZSVSHEET_INTERNAL_H
3
-
4
- #define ZSVSHEET_ROWNUM_HEADER "Row #"
5
- #define ZSVSHEET_ROWNUM_HEADER_LEN strlen(ZSVSHEET_ROWNUM_HEADER)
6
-
7
- enum zsvsheet_priv_status {
8
- zsvsheet_priv_status_ok = 0,
9
- zsvsheet_priv_status_memory,
10
- zsvsheet_priv_status_error, // generic error
11
- zsvsheet_priv_status_utf8,
12
- zsvsheet_priv_status_continue // ignore / continue
13
- // zsvsheet_priv_status_duplicate
14
- };
15
-
16
- struct zsvsheet_input_dimensions {
17
- size_t col_count;
18
- size_t row_count;
19
- };
20
-
21
- struct zsvsheet_display_dimensions {
22
- size_t rows;
23
- size_t columns;
24
- size_t header_span;
25
- size_t footer_span;
26
- };
27
-
28
- struct zsvsheet_buffer_info_internal {
29
- unsigned char index_started : 1;
30
- unsigned char index_ready : 1;
31
- unsigned char write_in_progress : 1;
32
- unsigned char write_done : 1;
33
- unsigned char _ : 4;
34
- };
35
-
36
- #endif
@@ -1,153 +0,0 @@
1
- /*
2
- * Copyright (C) 2021 Liquidaty and zsv contributors. All rights
3
- * reserved. This file is part of zsv/lib, distributed under the
4
- * license defined at https://opensource.org/licenses/MIT
5
- */
6
-
7
- #include <assert.h>
8
- #include <errno.h>
9
- #include "../external/sqlite3/sqlite3.h"
10
- #include <zsv/ext/implementation.h>
11
- #include <zsv/ext/sheet.h>
12
- #include <zsv/utils/writer.h>
13
- #include <zsv/utils/file.h>
14
- #include <zsv/utils/prop.h>
15
- #include "file.h"
16
- #include "handlers_internal.h"
17
- #include "./curses.h"
18
- #include "../sql_internal.h"
19
-
20
- struct sqlfilter_data {
21
- char *value_sql; // the sql expression entered by the user e.g. City
22
- char *data_filename;
23
- };
24
-
25
- static void sqlfilter_data_delete(void *h) {
26
- struct sqlfilter_data *pd = h;
27
- if (pd) {
28
- free(pd->value_sql);
29
- free(pd->data_filename);
30
- free(pd);
31
- }
32
- }
33
-
34
- static struct sqlfilter_data *sqlfilter_data_new(const char *data_filename, const char *value_sql) {
35
- struct sqlfilter_data *pd = calloc(1, sizeof(*pd));
36
- if (pd && (pd->value_sql = strdup(value_sql)) && (pd->data_filename = strdup(data_filename)))
37
- return pd;
38
- sqlfilter_data_delete(pd);
39
- return NULL;
40
- }
41
-
42
- static void zsvsheet_check_buffer_worker_updates(struct zsvsheet_ui_buffer *ub,
43
- struct zsvsheet_display_dimensions *display_dims,
44
- struct zsvsheet_sheet_context *handler_state);
45
-
46
- /**
47
- * Here we define a custom command for the zsv `sheet` feature
48
- */
49
- static zsvsheet_status zsvsheet_sqlfilter_handler(struct zsvsheet_proc_context *ctx) {
50
- char result_buffer[256] = {0};
51
- const char *expr;
52
- zsvsheet_buffer_t buff = zsvsheet_buffer_current(ctx);
53
- struct zsvsheet_buffer_data bd = zsvsheet_buffer_info(buff);
54
- const char add_row_num = !bd.has_row_num;
55
- const char *data_filename = NULL;
56
- if (buff)
57
- data_filename = zsvsheet_buffer_data_filename(buff);
58
-
59
- if (!data_filename) { // TO DO: check that the underlying data is a tabular file and we know how to parse
60
- zsvsheet_ui_buffer_set_status(buff, "SQL filter only available for tabular data buffers");
61
- return zsvsheet_status_ok;
62
- }
63
-
64
- char *selected_cell_str_dup = NULL;
65
- switch (ctx->proc_id) {
66
- case zsvsheet_builtin_proc_sqlfilter:
67
- zsvsheet_ext_prompt(ctx, result_buffer, sizeof(result_buffer), "Enter SQL expr to filter by");
68
- if (*result_buffer == '\0')
69
- return zsvsheet_status_ok;
70
- expr = result_buffer;
71
- break;
72
- default:
73
- assert(0);
74
- return zsvsheet_status_error;
75
- }
76
-
77
- enum zsvsheet_status zst = zsvsheet_status_ok;
78
- struct zsv_sqlite3_dbopts dbopts = {0};
79
- struct zsv_opts zopts = zsvsheet_buffer_get_zsv_opts(buff);
80
- struct zsv_sqlite3_db *zdb = zsv_sqlite3_db_new(&dbopts);
81
- sqlite3_str *sql_str = NULL;
82
- struct sqlfilter_data *sqlfd = NULL;
83
- if (!zdb || !(sql_str = sqlite3_str_new(zdb->db)))
84
- zst = zsvsheet_status_memory;
85
- else if (zdb->rc == SQLITE_OK && zsv_sqlite3_add_csv_no_dq(zdb, data_filename, &zopts, NULL) == SQLITE_OK) {
86
- int ok = 0;
87
- const char *err_msg = NULL;
88
- int err = 0;
89
- if (is_constant_expression(zdb->db, expr, &err))
90
- err_msg = "Please enter an expression that is not a constant";
91
- else {
92
- enum check_select_expression_result expr_rc = check_select_expression(zdb->db, expr, &err);
93
- if (expr_rc != zsv_select_sql_expression_valid)
94
- err_msg = check_select_expression_result_str(expr_rc);
95
- else if (!err)
96
- ok = 1;
97
- }
98
- if (!ok) {
99
- if (err)
100
- zsvsheet_ui_buffer_set_status(buff, strerror(err));
101
- else if (err_msg)
102
- zsvsheet_ui_buffer_set_status(buff, err_msg);
103
- else
104
- zsvsheet_ui_buffer_set_status(buff, "Unknown error");
105
- } else {
106
- if (add_row_num)
107
- sqlite3_str_appendf(sql_str, "select ROWID as %Q, * from data where %s", ZSVSHEET_ROWNUM_HEADER, expr);
108
- else
109
- sqlite3_str_appendf(sql_str, "select * from data where %s", expr);
110
-
111
- if (!(sqlfd = sqlfilter_data_new(data_filename, expr)))
112
- zst = zsvsheet_status_memory;
113
- else {
114
- zst = zsv_sqlite3_to_csv(ctx, zdb, sqlite3_str_value(sql_str), &err_msg, sqlfd, NULL, NULL);
115
- if (zst != zsvsheet_status_ok) {
116
- if (zst == zsvsheet_status_no_data)
117
- zsvsheet_ui_buffer_set_status(buff, "No results returned");
118
- else
119
- zsvsheet_ui_buffer_set_status(buff, err_msg ? err_msg : "Unexpected error preparing SQL");
120
- } else {
121
- buff = zsvsheet_buffer_current(ctx);
122
- zsvsheet_buffer_set_ctx(buff, sqlfd, sqlfilter_data_delete);
123
- sqlfd = NULL; // so that it isn't cleaned up below
124
-
125
- while (!zsvsheet_ui_buffer_index_ready(buff, 0))
126
- napms(200); // sleep for 200ms, then check index again
127
- // TO DO: fix this if there is no data!
128
-
129
- if (selected_cell_str_dup) {
130
- struct zsvsheet_sheet_context *state = (struct zsvsheet_sheet_context *)ctx->subcommand_context;
131
- struct zsvsheet_display_info *di = &state->display_info;
132
- zsvsheet_check_buffer_worker_updates(buff, di->dimensions, NULL);
133
- zsvsheet_handle_find_next(di, buff, selected_cell_str_dup,
134
- 1, // find value in first column
135
- 1, // exact
136
- 1, // header_span
137
- di->dimensions, &di->update_buffer, NULL);
138
- }
139
- }
140
- }
141
- }
142
- // TO DO: add param to ext_sheet_open_file to set filename vs data_filename, and set buffer type or proc owner
143
- // TO DO: add way to attach custom context, and custom context destructor, to the new buffer
144
- // TO DO: add cell highlighting
145
- }
146
-
147
- zsv_sqlite3_db_delete(zdb);
148
- if (sql_str)
149
- sqlite3_free(sqlite3_str_finish(sql_str));
150
- sqlfilter_data_delete(sqlfd);
151
- free(selected_cell_str_dup);
152
- return zst;
153
- }
@@ -1,32 +0,0 @@
1
- #include <stdio.h>
2
- #include <stdlib.h>
3
- #include <string.h>
4
- #include <unistd.h>
5
-
6
- // check_or_set_terminfo: return 1 if ok, 0 if not
7
- static int terminfo_ok(void) {
8
- // Check if TERMINFO environment variable is set
9
- char *terminfo_env = getenv("TERMINFO");
10
- if (terminfo_env != NULL)
11
- return 1; // ok
12
-
13
- // Check some default locations
14
- const char *default_paths[] = {
15
- "/usr/share/terminfo", "/lib/terminfo", "/usr/lib/terminfo", "/etc/terminfo", "/usr/local/share/terminfo", NULL};
16
-
17
- for (int i = 0; default_paths[i] != NULL; i++) {
18
- if (access(default_paths[i], R_OK) == 0) {
19
- // Set the TERMINFO environment variable
20
- if (setenv("TERMINFO", default_paths[i], 1) == 0)
21
- return 1; // ok
22
- else {
23
- perror("Failed to set TERMINFO environment variable");
24
- return 0;
25
- }
26
- }
27
- }
28
- #if defined(WIN32) || defined(_WIN32)
29
- return 1; // ok
30
- #endif
31
- return 0;
32
- }
@@ -1,312 +0,0 @@
1
- #include <stdlib.h>
2
- #include <string.h>
3
- #include <pthread.h>
4
- #include <zsv/utils/file.h>
5
- #include <zsv/utils/prop.h>
6
-
7
- #include "handlers_internal.h"
8
- #include "transformation.h"
9
- #include "../utils/index.h"
10
-
11
- struct zsvsheet_transformation {
12
- zsv_parser parser;
13
- zsv_csv_writer writer;
14
- char *output_filename;
15
- FILE *output_stream;
16
- unsigned char *output_buffer;
17
- int output_fileno;
18
- char writer_wrote;
19
- struct zsvsheet_transformation_opts opts;
20
- void *user_context;
21
-
22
- struct zsvsheet_ui_buffer *ui_buffer;
23
- char *default_status;
24
- void (*on_done)(zsvsheet_transformation trn);
25
- };
26
-
27
- static size_t transformation_write(const void *restrict ptr, size_t size, size_t nitems, void *restrict stream) {
28
- struct zsvsheet_transformation *trn = stream;
29
-
30
- const size_t count = fwrite(ptr, size, nitems, trn->output_stream);
31
- trn->writer_wrote = count > 0;
32
-
33
- return count > 0 ? count : 0;
34
- }
35
-
36
- struct transformation_writer_index_ctx {
37
- void *index;
38
- zsv_csv_writer writer;
39
- };
40
-
41
- static void transformation_writer_index_on_row(void *p) {
42
- struct transformation_writer_index_ctx *ctx = p;
43
- uint64_t written = zsv_writer_cum_bytes_written(ctx->writer);
44
- zsv_index_add_row(ctx->index, written);
45
- }
46
-
47
- static void transformation_writer_index_delete(void *p) {
48
- struct transformation_writer_index_ctx *ctx = p;
49
- uint64_t written = zsv_writer_cum_bytes_written(ctx->writer);
50
- if (written)
51
- zsv_index_add_row(ctx->index, written);
52
- free(ctx);
53
- }
54
-
55
- enum zsv_status zsvsheet_transformation_new(struct zsvsheet_transformation_opts opts, zsvsheet_transformation *out) {
56
- unsigned char *temp_buff = NULL;
57
- char *temp_filename = NULL;
58
- FILE *temp_f = NULL;
59
- zsv_csv_writer temp_file_writer = NULL;
60
- struct transformation_writer_index_ctx *ctx = NULL;
61
- enum zsv_status zst = zsv_status_memory;
62
-
63
- struct zsvsheet_transformation *trn = calloc(1, sizeof(*trn));
64
- if (trn == NULL)
65
- return zst;
66
-
67
- trn->on_done = opts.on_done;
68
- trn->ui_buffer = opts.ui_buffer;
69
- zst = zsv_status_error;
70
-
71
- temp_filename = zsv_get_temp_filename("zsvsheet_filter_XXXXXXXX");
72
- if (!temp_filename)
73
- goto free;
74
- trn->output_filename = temp_filename;
75
-
76
- if (!(temp_f = fopen(temp_filename, "w+")))
77
- goto free;
78
- if (setvbuf(temp_f, NULL, _IONBF, 0))
79
- goto free;
80
- trn->output_stream = temp_f;
81
-
82
- ctx = calloc(1, sizeof(*ctx));
83
- if (!ctx)
84
- goto free;
85
- ctx->index = opts.index;
86
-
87
- struct zsv_csv_writer_options writer_opts = {
88
- .with_bom = 0,
89
- .write = transformation_write,
90
- .stream = trn,
91
- .table_init = NULL,
92
- .table_init_ctx = NULL,
93
- .on_row = transformation_writer_index_on_row,
94
- .on_row_ctx = ctx,
95
- .on_delete = transformation_writer_index_delete,
96
- .on_delete_ctx = ctx,
97
- };
98
- if (!(temp_file_writer = zsv_writer_new(&writer_opts)))
99
- goto free;
100
-
101
- ctx->writer = temp_file_writer;
102
- const size_t temp_buff_size = 2 * 1024 * 1024;
103
- temp_buff = malloc(temp_buff_size);
104
- if (!temp_buff)
105
- goto free;
106
- trn->output_buffer = temp_buff;
107
- zsv_writer_set_temp_buff(temp_file_writer, temp_buff, temp_buff_size);
108
- trn->writer = temp_file_writer;
109
-
110
- trn->user_context = opts.zsv_opts.ctx;
111
- opts.zsv_opts.ctx = trn;
112
-
113
- zst = zsv_new_with_properties(&opts.zsv_opts, opts.custom_prop_handler, opts.input_filename, &trn->parser);
114
- if (zst != zsv_status_ok)
115
- goto free;
116
-
117
- *out = trn;
118
- return zst;
119
-
120
- free:
121
- if (trn)
122
- free(trn);
123
- if (temp_filename)
124
- free(temp_filename);
125
- if (temp_f)
126
- fclose(temp_f);
127
- if (temp_file_writer)
128
- zsv_writer_delete(temp_file_writer);
129
- if (temp_buff)
130
- free(temp_buff);
131
- if (ctx)
132
- transformation_writer_index_delete(ctx);
133
-
134
- return zst;
135
- }
136
-
137
- void zsvsheet_transformation_delete(zsvsheet_transformation trn) {
138
- zsv_writer_delete(trn->writer);
139
- zsv_delete(trn->parser);
140
- free(trn->output_filename);
141
- fclose(trn->output_stream);
142
- free(trn->output_buffer);
143
- free(trn);
144
- }
145
-
146
- zsv_parser zsvsheet_transformation_parser(zsvsheet_transformation trn) {
147
- return trn->parser;
148
- }
149
-
150
- zsv_csv_writer zsvsheet_transformation_writer(zsvsheet_transformation trn) {
151
- return trn->writer;
152
- }
153
-
154
- const char *zsvsheet_transformation_filename(zsvsheet_transformation trn) {
155
- return trn->output_filename;
156
- }
157
-
158
- void *zsvsheet_transformation_user_context(zsvsheet_transformation trn) {
159
- return trn->user_context;
160
- }
161
-
162
- static void *zsvsheet_run_buffer_transformation(void *arg) {
163
- struct zsvsheet_transformation *trn = arg;
164
- struct zsvsheet_ui_buffer *uib = trn->ui_buffer;
165
- zsv_parser parser = trn->parser;
166
- pthread_mutex_t *mutex = &uib->mutex;
167
- enum zsv_status zst;
168
- char *default_status = trn->default_status;
169
-
170
- char cancelled = 0;
171
- while (!cancelled && (zst = zsv_parse_more(parser)) == zsv_status_ok) {
172
- pthread_mutex_lock(mutex);
173
- cancelled = uib->worker_cancelled;
174
- if (trn->writer_wrote) {
175
- trn->writer_wrote = 0;
176
- zsv_index_commit_rows(uib->index);
177
- uib->index_ready = 1;
178
- }
179
- pthread_mutex_unlock(mutex);
180
- }
181
-
182
- if (zst == zsv_status_no_more_input || zst == zsv_status_cancelled)
183
- zsv_finish(parser);
184
-
185
- if (trn->on_done)
186
- trn->on_done(trn);
187
-
188
- if (trn->user_context)
189
- free(trn->user_context);
190
-
191
- zsvsheet_transformation_delete(trn);
192
-
193
- pthread_mutex_lock(mutex);
194
- char *buff_status_old = uib->status;
195
- uib->write_done = 1;
196
- zsv_index_commit_rows(uib->index);
197
- uib->index_ready = 1;
198
- if (buff_status_old == default_status)
199
- uib->status = NULL;
200
- pthread_mutex_unlock(mutex);
201
-
202
- if (buff_status_old == default_status)
203
- free(buff_status_old);
204
-
205
- return NULL;
206
- }
207
-
208
- enum zsvsheet_status zsvsheet_push_transformation(zsvsheet_proc_context_t ctx,
209
- struct zsvsheet_buffer_transformation_opts opts) {
210
- zsvsheet_buffer_t buff = zsvsheet_buffer_current(ctx);
211
- const char *filename = zsvsheet_buffer_data_filename(buff);
212
- enum zsvsheet_status stat = zsvsheet_status_error;
213
- struct zsvsheet_buffer_info_internal info = zsvsheet_buffer_info_internal(buff);
214
- struct zsv_index *index = NULL;
215
-
216
- // TODO: Starting a second transformation before the first ends works, but if the second is faster
217
- // than the first then it can end prematurely and read a partially written row.
218
- // We could override the input stream reader to wait for more data when it sees EOF
219
- if (info.write_in_progress && !info.write_done)
220
- return zsvsheet_status_busy;
221
-
222
- if (!(index = zsv_index_new()))
223
- return zsvsheet_status_memory;
224
-
225
- // TODO: custom_prop_handler is not passed to extensions?
226
- struct zsvsheet_transformation_opts trn_opts = {
227
- .custom_prop_handler = NULL,
228
- .input_filename = filename,
229
- .on_done = opts.on_done,
230
- .ui_buffer = NULL,
231
- .index = index,
232
- };
233
- zsvsheet_transformation trn = NULL;
234
- struct zsv_opts zopts = zsvsheet_buffer_get_zsv_opts(buff);
235
-
236
- zopts.ctx = opts.user_context;
237
- zopts.row_handler = (void (*)(void *))opts.row_handler;
238
- zopts.stream = fopen(filename, "rb");
239
- zopts.buffsize = 2 * 1024 * 1024;
240
-
241
- if (!zopts.stream)
242
- goto error;
243
-
244
- trn_opts.zsv_opts = zopts;
245
-
246
- enum zsv_status zst = zsvsheet_transformation_new(trn_opts, &trn);
247
- if (zst != zsv_status_ok)
248
- goto error;
249
-
250
- // Transform part of the file to initially populate the UI buffer
251
- // TODO: If the transformation is a reduction that doesn't output for some time this will caus a pause
252
- zsv_parser parser = zsvsheet_transformation_parser(trn);
253
- while ((zst = zsv_parse_more(parser)) == zsv_status_ok) {
254
- if (trn->writer_wrote)
255
- break;
256
- }
257
- trn->writer_wrote = 0;
258
-
259
- switch (zst) {
260
- case zsv_status_no_more_input:
261
- case zsv_status_cancelled:
262
- if (zsv_finish(parser) != zsv_status_ok)
263
- goto error;
264
- zsv_writer_flush(trn->writer);
265
- break;
266
- case zsv_status_ok:
267
- break;
268
- default:
269
- goto error;
270
- }
271
-
272
- struct zsvsheet_ui_buffer_opts uibopts = {0};
273
-
274
- uibopts.data_filename = zsvsheet_transformation_filename(trn);
275
- uibopts.write_after_open = 1;
276
-
277
- stat = zsvsheet_open_file_opts(ctx, &uibopts);
278
- if (stat != zsvsheet_status_ok)
279
- goto error;
280
-
281
- struct zsvsheet_ui_buffer *nbuff = zsvsheet_buffer_current(ctx);
282
- trn->ui_buffer = nbuff;
283
- zsv_index_commit_rows(index);
284
- nbuff->index_started = 1;
285
- nbuff->index = index;
286
-
287
- if (zst != zsv_status_ok) {
288
- nbuff->write_done = 1;
289
- nbuff->index_ready = 1;
290
- if (trn->on_done)
291
- opts.on_done(trn);
292
- zsvsheet_transformation_delete(trn);
293
- zsv_index_commit_rows(index);
294
- return stat;
295
- }
296
-
297
- asprintf(&trn->default_status, "(working) Press ESC to cancel ");
298
- nbuff->status = trn->default_status;
299
-
300
- zsvsheet_ui_buffer_create_worker(nbuff, zsvsheet_run_buffer_transformation, trn);
301
- return stat;
302
-
303
- error:
304
- zsv_index_delete(index);
305
-
306
- if (trn && trn->on_done)
307
- opts.on_done(trn);
308
- if (trn)
309
- zsvsheet_transformation_delete(trn);
310
-
311
- return stat;
312
- }