Jpp test-rotations-new
the software that should make you happy
Loading...
Searching...
No Matches
csv2code.cc File Reference
#include "csv2code.hh"
#include <fstream>
#include <iomanip>
#include <iostream>
#include <sstream>
#include <stdexcept>
#include <string>
#include <vector>
#include "csv2cpp.hh"
#include "csv2julia.hh"
#include "csv2python.hh"
#include "csv2shell.hh"

Go to the source code of this file.

Functions

CSV readCSV (istream &in)
 
int main (int argc, char **argv)
 Helper program to convert an input CSV file, with a format of #type,name,number,comment.
 

Function Documentation

◆ readCSV()

CSV readCSV ( istream & in)

Definition at line 18 of file csv2code.cc.

18 {
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}
std::istream & getline(std::istream &in, JString &object)
Read string from input stream until end of line.
Definition JString.hh:478
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

◆ main()

int main ( int argc,
char ** argv )

Helper program to convert an input CSV file, with a format of #type,name,number,comment.

into a source code file of the requested language.

Definition at line 46 of file csv2code.cc.

46 {
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}
CSV readCSV(istream &in)
Definition csv2code.cc:18
void convert(std::ostream &out, const Header &header, const CSV &csv)
Definition csv2code.hh:29