Jpp 20.0.0-195-g190c9e876
the software that should make you happy
Loading...
Searching...
No Matches
csv2code.cc
Go to the documentation of this file.
1#include "csv2code.hh"
2
3#include <fstream>
4#include <iomanip>
5#include <iostream>
6#include <sstream>
7#include <stdexcept>
8#include <string>
9#include <vector>
10
11#include "csv2cpp.hh"
12#include "csv2julia.hh"
13#include "csv2python.hh"
14#include "csv2shell.hh"
15#include "csv2csh.hh"
16
17using namespace std;
18
19CSV readCSV(istream& in) {
20 // format of the CSV is type,name,number,comment
21 CSV csv;
22 string line;
23 while (getline(in, line)) {
24 if (line.size() < 2 || line[0] == '#') {
25 continue;
26 }
27 string type, name, number, comment;
28 stringstream sline(line);
29 getline(sline, type, ',');
30 getline(sline, name, ',');
31 getline(sline, number, ',');
32 getline(sline, comment);
33 csv.types.emplace_back(type);
34 csv.names.emplace_back(name);
35 csv.numbers.emplace_back(number);
36 csv.comments.emplace_back(comment);
37 }
38 return csv;
39}
40
41/**
42 * Helper program to convert an input CSV file, with a format of
43 * #type,name,number,comment
44 *
45 * into a source code file of the requested language.
46 */
47int main(int argc, char** argv) {
48 if (argc < 5) {
49 throw runtime_error("need at least 4 arguments");
50 }
51 string lang = argv[1]; // language tag : hh,py,jl,sh
52 // CPP,PYTHON,JULIA,SHELL
53 string infile = argv[2]; // input file name, input content should be CSV
54 string outfile = argv[3]; // output file name
55 string base = argv[4]; // base name of the definition
56 string description =
57 argc > 5 ? argv[5] : "some description"; // optional description
58 string url = argc > 6 ? argv[6] : "some url"; // optional url
59
60 ifstream in(infile);
61 if (!in.is_open()) {
62 std::cerr << "Cannot open input file " << infile << "\n";
63 exit(1);
64 }
65 ofstream out(outfile);
66 if (!out.is_open()) {
67 std::cerr << "Cannot open output file " << outfile << "\n";
68 exit(2);
69 }
70
71 Header header{base, description, url};
72 CSV csv = readCSV(in);
73
74 if (lang == "hh") {
75 convert<CPP>(out, header, csv);
76 } else if (lang == "py") {
77 convert<PYTHON>(out, header, csv);
78 } else if (lang == "jl") {
79 convert<JULIA>(out, header, csv);
80 } else if (lang == "sh") {
81 convert<SHELL>(out, header, csv);
82 } else if (lang == "csh") {
83 convert<CSH>(out, header, csv);
84 } else {
85 throw runtime_error(
86 "unknown output language requested. I only know hh, py");
87 }
88
89 return 0;
90}
int main(int argc, char **argv)
Helper program to convert an input CSV file, with a format of #type,name,number,comment.
Definition csv2code.cc:47
CSV readCSV(istream &in)
Definition csv2code.cc:19
void convert(std::ostream &out, const Header &header, const CSV &csv)
Definition csv2code.hh:29
std::vector< std::string > numbers
Definition csv2code.hh:18
std::vector< std::string > types
Definition csv2code.hh:16
std::vector< std::string > names
Definition csv2code.hh:17
std::vector< std::string > comments
Definition csv2code.hh:19