/* The MIT License (MIT) Copyright © 2020-2025 pacman64 Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ /* You can build this command-line app by running cc -Wall -s -O2 -o ./jsons ./jsons.c To use empty strings for missing trailing cells, you can build it using cc -Wall -s -O2 -D PURE_JSONS -o ./jsons ./jsons.c */ #include #include #include #include #ifdef _WIN32 #include #include #endif #ifndef PURE_JSONS #define NULL_TRAILS #endif #ifdef NULL_TRAILS const char* info = "" "jsons [filename...]\n" "\n" "Turn a TSV table into JSON Strings, which is a JSON array with objects of\n" "string values. The only non-string values are nulls, which are used for any\n" "missing trailing fields.\n" ""; #else const char* info = "" "jsons [filename...]\n" "\n" "Turn a TSV table into JSON Strings, which is a JSON array with objects of\n" "string values.\n" ""; #endif const char* no_line_memory_msg = "can't get enough memory to read lines"; // span is a region of bytes in memory typedef struct span { // ptr is the starting place of the region unsigned char* ptr; // len is how many bytes are in the region size_t len; } span; // slice is a growable region of bytes in memory typedef struct slice { // ptr is the starting place of the region unsigned char* ptr; // len is how many bytes are currently being used size_t len; // cap is how many bytes the memory region has available size_t cap; } slice; bool starts_with_bom(const unsigned char* b, const size_t n) { return (n >= 3 && b[0] == 0xef && b[1] == 0xbb && b[2] == 0xbf); } // emit_json_item emits JSON strings for the TSV items given: such items are // strings without line-feeds, carriage-returns, or tabs void emit_json_item(FILE* w, const unsigned char* ptr, size_t len) { putc('"', w); for (size_t i = 0; i < len; i++) { const unsigned char b = ptr[i]; if (b == '"' || b == '\\') { putc('\\', w); } putc(b, w); } putc('"', w); } // emit_json_item_str is like function emit_json_item, but taking c-strings void emit_json_item_str(FILE* w, const char* s) { putc('"', w); for (; *s != 0; s++) { const unsigned char b = *s; if (b == '"' || b == '\\') { putc('\\', w); } putc(b, w); } putc('"', w); } void handle_row(FILE* w, char** keys, size_t num_keys, span line) { size_t start = 0; size_t len = 0; size_t got = 0; putc(' ', w); putc(' ', w); putc('{', w); for (size_t i = 0; i < line.len; i++) { if (line.ptr[i] == '\t') { if (got > 0) { fprintf(w, ", "); } emit_json_item_str(w, keys[got]); fprintf(w, ": "); emit_json_item(w, line.ptr + start, len); start = i + 1; len = 0; got++; } else { len++; } } if (start < line.len) { if (got > 0) { fprintf(w, ", "); } emit_json_item_str(w, keys[got]); fprintf(w, ": "); emit_json_item(w, line.ptr + start, len); got++; } for (size_t i = got; i < num_keys; i++) { if (i > 0) { fprintf(w, ", "); } emit_json_item_str(w, keys[got]); #ifdef NULL_TRAILS fprintf(w, ": null"); #else fprintf(w, ": \"\""); #endif } putc('}', w); } void show_error(FILE* w, const char* msg) { putc('\n', w); fprintf(stderr, "\x1b[31m%s\x1b[0m\n", msg); } // handle_reader skips leading UTF-8 BOMs (byte-order marks), and turns all // CR-LF pairs into single LF bytes bool handle_reader(FILE* w, FILE* r, slice* line) { span trimmed; char** keys = NULL; size_t num_keys = 0; size_t i = 0; for (i = 0; !feof(w); i++) { ssize_t len = getline((char**)&line->ptr, &line->cap, r); if (len < 0) { break; } if (line->ptr == NULL) { show_error(w, no_line_memory_msg); exit(1); } line->len = len; trimmed.ptr = line->ptr; trimmed.len = line->len; // get rid of leading UTF-8 BOM (byte-order mark) if 1st line has it if (i == 0 && starts_with_bom(trimmed.ptr, trimmed.len)) { trimmed.ptr += 3; trimmed.len -= 3; len = trimmed.len; } const unsigned char* p = trimmed.ptr; // get rid of trailing line-feeds and CRLF end-of-line byte-pairs if (len >= 2 && p[len - 2] == '\r' && p[len - 1] == '\n') { trimmed.len -= 2; } else if (len >= 1 && p[len - 1] == '\n') { trimmed.len--; } if (i == 0) { for (size_t j = 0; j < trimmed.len; j++) { if (trimmed.ptr[j] == '\t') { num_keys++; } } num_keys++; keys = malloc(sizeof(char*) * num_keys); if (keys == NULL) { show_error(w, no_line_memory_msg); exit(1); } keys[0] = malloc(trimmed.len + 1); if (keys[0] == NULL) { show_error(w, no_line_memory_msg); exit(1); } char* copy = keys[0]; memcpy(copy, trimmed.ptr, trimmed.len); copy[trimmed.len] = 0; for (size_t j = 0, k = 1; j < trimmed.len; j++) { if (copy[j] == '\t') { copy[j] = 0; keys[k] = copy + j + 1; k++; } } continue; } if (i == 1) { putc('[', w); } else { putc(',', w); } putc('\n', w); fflush(w); handle_row(w, keys, num_keys, trimmed); } if (i > 0) { fprintf(w, "\n]\n"); } else { fprintf(w, "[]\n"); } if (keys != NULL) { if (keys[0] != NULL) { free(keys[0]); } free(keys); } fflush(w); return true; } // handle_file handles data from the filename given; returns false only when // the file can't be opened bool handle_file(FILE* w, const char* fname, slice* line) { FILE* f = fopen(fname, "rb"); if (f == NULL) { fprintf(stderr, "\x1b[31mcan't open file named %s\x1b[0m\n", fname); return false; } const bool ok = handle_reader(w, f, line); fclose(f); return ok; } bool run(FILE* w, const char* fname) { slice line; line.len = 0; line.cap = 32 * 1024; line.ptr = malloc(line.cap); if (line.ptr == NULL) { fprintf(stderr, "\x1b[31m%s\x1b[0m\n", no_line_memory_msg); return false; } // filename `-` means use the standard input if (fname[0] == '-' && fname[1] == 0) { const bool ok = handle_reader(w, stdin, &line); free(line.ptr); return ok; } const bool ok = handle_file(w, fname, &line); free(line.ptr); return ok; } int main(int argc, char** argv) { #ifdef _WIN32 setmode(fileno(stdin), O_BINARY); // ensure output lines end in LF instead of CRLF on windows setmode(fileno(stdout), O_BINARY); setmode(fileno(stderr), O_BINARY); #endif if (argc > 1) { if ( strcmp(argv[1], "-h") == 0 || strcmp(argv[1], "-help") == 0 || strcmp(argv[1], "--h") == 0 || strcmp(argv[1], "--help") == 0 ) { fprintf(stdout, "%s", info); return 0; } } if (argc > 2) { const char* msg = "can't use more than 1 named input"; fprintf(stderr, "\x1b[31m%s\x1b[0m\n", msg); return 1; } return run(stdout, (argc > 1) ? argv[1] : "-") ? 0 : 1; }