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