/* 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 -O3 -march=native -mtune=native -flto -o ./dessv ./dessv.c */ #include #include #include #include #include #ifdef _WIN32 #include #include #endif #ifdef RED_ERRORS #define ERROR_STYLE "\x1b[38;2;204;0;0m" #ifdef __APPLE__ #define ERROR_STYLE "\x1b[31m" #endif #define RESET_STYLE "\x1b[0m" #else #define ERROR_STYLE #define RESET_STYLE #endif #define ERROR_LINE(MSG) (ERROR_STYLE MSG RESET_STYLE "\n") const char* info = "" "dessv [filenames...]\n" "\n" "Turn Space(s)-Separated Values (SSV) into Tab-Separated Values (TSV), where\n" "both leading and trailing spaces from input lines are ignored.\n" ""; // bufwriter is, as the name implies, a buffered-writer: when it's aimed at // stdout, it considerably speeds up this app, as intended typedef struct bufwriter { // buf is the buffer proper unsigned char* buf; // len is how many bytes of the buffer are currently being used size_t len; // cap is the capacity of the buffer, or the most bytes it can hold size_t cap; // out is the destination of all that's written into the buffer FILE* out; } bufwriter; void init_bufwriter(bufwriter* w, FILE* out, unsigned char* b, size_t cap) { w->buf = b; w->len = 0; w->cap = cap; w->out = out; } void write_byte(bufwriter* w, unsigned char b) { if (w->len < w->cap) { w->buf[w->len++] = b; return; } fwrite(w->buf, w->cap, 1, w->out); w->buf[0] = b; w->len = 1; } // write_bytes does as it says, minimizing the number of calls to fwrite void write_bytes(bufwriter* w, const unsigned char* src, size_t len) { for (size_t i = 0; i < len; i++) { write_byte(w, src[i]); } } void flush(bufwriter* w) { if (w->len > 0) { fwrite(w->buf, w->len, 1, w->out); } w->len = 0; fflush(w->out); } // 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); } bool has_tabs(const unsigned char* b, const size_t n) { for (size_t i = 0; i < n; i++) { if (b[i] == '\t') { return true; } } return false; } size_t count_tabs(const unsigned char* b, const size_t n) { size_t tabs = 0; for (size_t i = 0; i < n; i++) { tabs += (b[i] == '\t'); } return tabs; } // write_tsv_line returns the number of tab-separated values emitted; current // line isn't ended with a line-feed, which must be emitted separately size_t write_tsv_line(bufwriter* w, const unsigned char* p, size_t len) { // ignore trailing CRLF byte-pairs, or trailing line-feed bytes if (len >= 2 && p[len - 2] == '\r' && p[len - 1] == '\n') { len -= 2; } else if (len >= 1 && p[len - 1] == '\n') { len--; } write_bytes(w, p, len); return count_tabs(p, len) + 1; } // write_ssv_line returns the number of tab-separated values emitted; current // line isn't ended with a line-feed, which must be emitted separately size_t write_ssv_line(bufwriter* w, const unsigned char* p, size_t len) { // ignore trailing CRLF byte-pairs, or trailing line-feed bytes if (len >= 2 && p[len - 2] == '\r' && p[len - 1] == '\n') { len -= 2; } else if (len >= 1 && p[len - 1] == '\n') { len--; } // ignore leading spaces while (len > 0 && p[0] == ' ') { p++; len--; } // trailing spaces are inconsequential, since there's nothing to follow // them, which in turn prevents their normal single-tab substitutes from // being emitted size_t items = 0; bool space = false; for (size_t i = 0; i < len; i++) { unsigned char b = p[i]; if (b == ' ') { space = true; continue; } if (space) { write_byte(w, '\t'); items++; space = false; } write_byte(w, b); } return items; } // handle_reader skips leading UTF-8 BOMs (byte-order marks), and turns all // CR-LF pairs into single LF bytes bool handle_reader(bufwriter* w, FILE* r, slice* line, bool live_lines) { size_t (*write_items)(bufwriter*, const unsigned char*, size_t) = NULL; size_t items = 0; for (size_t i = 0; !feof(w->out); i++) { ssize_t len = getline((char**)&line->ptr, &line->cap, r); if (len < 0) { break; } if (line->ptr == NULL) { fprintf(stderr, ERROR_LINE("out of memory")); return false; } unsigned char* ptr = line->ptr; // turn trailing carriage-returns into line-feeds if (len >= 1 && ptr[len - 1] == '\r') { ptr[len - 1] = '\n'; } // get rid of carriage-returns preceding line-feeds if (len >= 2 && ptr[len - 2] == '\r' && ptr[len - 1] == '\n') { ptr[len - 2] = '\n'; len--; } // 1st line: figure out if lines are already TSV, remember item-count, // and ignore UTF-8 byte-order marks if (i == 0) { if (starts_with_bom(ptr, len)) { ptr += 3; len -= 3; } const bool tsv = has_tabs(ptr, len); write_items = tsv ? write_tsv_line : write_ssv_line; items = write_items(w, ptr, len); write_byte(w, '\n'); if (live_lines) { flush(w); } continue; } // write normal data lines size_t got = write_items(w, ptr, len); // add empty fields, when missing trailing ones for (size_t j = got; j < items; j++) { write_byte(w, '\t'); } write_byte(w, '\n'); if (live_lines) { flush(w); } } return true; } // handle_file handles data from the filename given; returns false only when // the file can't be opened bool handle_file(bufwriter* w, const char* path, slice* line, bool live_lines) { FILE* f = fopen(path, "rb"); if (f == NULL) { fprintf(stderr, ERROR_LINE("can't open file named '%s'"), path); return false; } const bool ok = handle_reader(w, f, line, live_lines); fclose(f); return ok; } // run returns the number of errors int run(int argc, char** argv, FILE* w, bool live_lines) { unsigned char outbuf[8 * 1024]; bufwriter bw; size_t dashes = 0; for (int i = 1; i < argc; i++) { if (argv[i][0] == '-' && argv[i][1] == 0) { dashes++; } } if (dashes > 1) { const char* m = "can't use the standard input (dash) more than once"; fprintf(stderr, ERROR_LINE("%s"), m); return 1; } slice line; line.len = 0; line.cap = 32 * 1024; line.ptr = malloc(line.cap); if (live_lines) { if (line.ptr == NULL) { fprintf(stderr, ERROR_LINE("out of memory")); return 1; } } init_bufwriter(&bw, w, outbuf, sizeof(outbuf)); size_t errors = 0; for (int i = 1; i < argc && !feof(w) && line.ptr != NULL; i++) { if (live_lines && line.ptr == NULL) { break; } if (argv[i][0] == '-' && argv[i][1] == 0) { if (!handle_reader(&bw, stdin, &line, live_lines)) { errors++; } continue; } if (!handle_file(&bw, argv[i], &line, live_lines)) { errors++; } } // use stdin when not given any filepaths if (argc <= 1) { if (!handle_reader(&bw, stdin, &line, live_lines)) { errors++; } } free(line.ptr); flush(&bw); return errors; } 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; } } const bool live_lines = lseek(fileno(stdout), 0, SEEK_CUR) != 0; if (!live_lines) { setvbuf(stdout, NULL, _IOFBF, 0); } return run(argc, argv, stdout, live_lines) == 0 ? 0 : 1; }