/* 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 g++ -Wall -O2 -s -o detsv detsv.cpp */ #include #include #include #include #include #include using namespace std; const string info = "" "detsv [file...]\n" "\n" "Turn TSV tables into JSON data.\n" ""; void de_bom(string &s) { // s.starts_with("\xef\xbb\xbf") if (s.size() >= 3 && s[0] == '\xef' && s[1] == '\xbb' && s[2] == '\xbf') { s.erase(0, 3); } } void de_cr(string &s) { s.erase(remove(s.begin(), s.end(), '\r'), s.end()); } void tab_split(const string& line, vector& items) { size_t start = 0; size_t end = line.size(); for (size_t i = 0; i < end; i++) { if (line[i] == '\t') { items.push_back(line.substr(start, i - start)); start = i + 1; } } if (start < end) { items.push_back(line.substr(start, end - start + 1)); } } void tab_split_view(const string& line, vector& items) { size_t start = 0; size_t end = line.size(); for (size_t i = 0; i < end; i++) { if (line[i] == '\t') { items.push_back(string_view(line).substr(start, i - start)); start = i + 1; } } if (start < end) { items.push_back(string_view(line).substr(start, end - start + 1)); } } void emit_json_string(const string& s) { cout << '"'; for (auto c : s) { switch (c) { case '"': case '\\': cout << '\\' << c; break; default: cout << c; break; } } cout << '"'; } void emit_json_string_view(const string_view& s) { cout << '"'; for (auto c : s) { switch (c) { case '"': case '\\': cout << '\\' << c; break; default: cout << c; break; } } cout << '"'; } // seems_json_number detects only a subset of valid json numbers for now bool seems_json_number(const string_view& s) { size_t dots = 0; size_t digits = 0; for (auto c : s) { if ('0' <= c && c <= '9') { digits++; continue; } if (c == '-' && digits > 0) { return false; } if (c == '.') { if (digits == 0 || dots > 0) { return false; } dots++; digits = 0; // effectively demand digits after a dot continue; } return false; } return digits > 0; } void emit_json_value(const string_view& s) { // if (s == "") { // cout << "null"; // return; // } // if (s == "null") { // cout << s; // return; // } // if (s == "true" || s == "false") { // cout << s; // return; // } // recognize numbers to avoid quoting them // if (seems_json_number(s)) { // cout << s; // return; // } emit_json_string_view(s); } // size_t count(string& s, char what) { // size_t count = 0; // for (auto c : s) { // if (c == what) { // count++; // } // } // return count; // } bool handle_input(istream& in, string& line) { if (!getline(in, line)) { return true; } de_bom(line); de_cr(line); vector keys; keys.reserve(count_if(line.begin(), line.end(), [](char c) { return c == '\t'; }) + 1); // keys.reserve(count(line, '\t') + 1); tab_split(line, keys); size_t n = keys.size(); size_t i = 0; vector values; values.reserve(n); for (i = 0; !cout.eof() && getline(in, line); i++) { de_cr(line); if (i == 0) { cout << '[' << endl; } else { cout << ',' << endl; } values.clear(); tab_split_view(line, values); size_t got = values.size(); if (got > n) { cerr << "\x1b[31mexpected up to " << n << " items, but got "; cerr << got << " instead\x1b[0m" << endl; return false; } cout << " {"; for (size_t j = 0; j < got; j++) { if (j > 0) { cout << ", "; } emit_json_string(keys[j]); cout << ": "; emit_json_value(values[j]); } for (size_t j = got; j < n; j++) { cout << ", "; emit_json_string(keys[j]); cout << ": null"; } cout << '}'; } if (i > 0) { cout << endl; cout << ']' << endl; } else { cout << "[]" << endl; } return true; } bool handle_file(const char* path, string& line) { ifstream f(path); if (!f.is_open()) { const auto msg = "can't open file named"; cerr << "\x1b[31m" << msg << " '" << path << "'\x1b[0m" << endl; return false; } return handle_input(f, line); } int main(int argc, char** argv) { string line; if (argc > 1) { if ( strcmp(argv[1], "-h") == 0 || strcmp(argv[1], "-help") == 0 || strcmp(argv[1], "--h") == 0 || strcmp(argv[1], "--help") == 0 ) { cout << info; return 0; } } if (argc > 2) { cerr << "\x1b[31mcan't use more than 1 input file\x1b[0m" << endl; return 1; } cin.tie(NULL); ios_base::sync_with_stdio(false); if (argc < 2 || strcmp(argv[1], "-") == 0) { return handle_input(cin, line) ? 0 : 1; } return handle_file(argv[1], line) ? 0 : 1; }