Jpp
21.0.0-rc.1-88-g0130508c4
the software that should make you happy
Loading...
Searching...
No Matches
externals
km3net-dataformat
definitions
csv2code
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
17
using namespace
std
;
18
19
CSV
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
*/
47
int
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
}
main
int main()
Definition
codemeta-consumer/main.cc:3
readCSV
CSV readCSV(istream &in)
Definition
csv2code.cc:19
csv2code.hh
convert
void convert(std::ostream &out, const Header &header, const CSV &csv)
Definition
csv2code.hh:29
csv2cpp.hh
csv2csh.hh
csv2julia.hh
csv2python.hh
csv2shell.hh
std
Definition
JSTDTypes.hh:14
CSV
Definition
csv2code.hh:15
CSV::numbers
std::vector< std::string > numbers
Definition
csv2code.hh:18
CSV::types
std::vector< std::string > types
Definition
csv2code.hh:16
CSV::names
std::vector< std::string > names
Definition
csv2code.hh:17
CSV::comments
std::vector< std::string > comments
Definition
csv2code.hh:19
Header
Definition
csv2code.hh:8
Generated by
1.12.0