Last active
December 2, 2020 18:24
-
-
Save shipof123/b74887e8a43c465e7e03107b4a757168 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <sstream> | |
#include <iostream> | |
#include <fstream> | |
#include <string> | |
#include <vector> | |
#include <map> | |
#include <utility> | |
/* | |
* Small, generalized utility for converting *-sv (*-seperated value) files to html tables | |
* C++11 or higher | |
*/ | |
template <char delim> | |
struct SV { | |
SV(std::string filename, std::map<std::string, std::string> opts) { | |
std::ifstream in(filename); | |
std::string opt_string = ""; | |
for(const std::pair<std::string, std::string>& p : opts) { | |
opt_string += p.first + "=" + p.second + ' '; | |
} | |
std::cout << "<table " + opt_string + ">\n"; | |
std::string line, value; | |
while(getline(in, line)) { | |
auto linestream = std::istringstream(line); | |
std::cout << "<tr>\t"; | |
while(getline(linestream, value, delim)) { | |
std::cout << "<td>" << value << "</td>\t"; | |
} | |
std::cout << "</tr>\n"; | |
} | |
std::cout << "</table>" << std::endl; | |
} | |
}; | |
int main(int argc, char* argv[]){ | |
std::map<std::string, std::string> opts = { | |
{"border", "1"} | |
}; | |
auto tsv = SV<'\t'>("cr.tsv", opts); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment