Jpp 20.0.0-195-g190c9e876
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"
#include "csv2csh.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 19 of file csv2code.cc.

19 {
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}
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 47 of file csv2code.cc.

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