Jpp  master_rocky-43-ge265d140c
the software that should make you happy
classdef2code.cc
Go to the documentation of this file.
1 #include <filesystem>
2 #include <fstream>
3 #include <iomanip>
4 #include <iostream>
5 #include <regex>
6 #include <sstream>
7 #include <stdexcept>
8 #include <string>
9 #include <vector>
10 
11 using namespace std;
12 
13 struct Includes {
17 };
18 
19 void outputTypes(std::ostream &out, const Includes &includes) {
20  out << R"(#ifndef __JDB__JDBTypes__
21 #define __JDB__JDBTypes__
22 
23 /**
24  * @file
25  * This file is automatically created by classdef2code during CMake build.
26  */
27 
28 namespace JDATABASE {
29 
30 )";
31 
32  for (auto v1 : includes.api) {
33  out << " class " << v1 << ";\n";
34  }
35 
36  out << "\n namespace APIV2 {\n\n";
37  for (auto v2 : includes.apiV2) {
38  out << " class " << v2 << ";\n";
39  }
40 
41  out << R"(
42  }
43 }
44 #endif
45 )";
46 }
47 
48 void outputIncludes(std::ostream &out, const Includes &includes) {
49  out << R"(/**
50  * @file
51  * This file is automatically created by classdef2code during CMake build.
52  */
53 
54 )";
55  for (auto i : includes.files) {
56  std::filesystem::path p(i);
57  out << "#include \"JDB/" << p.filename().string() << "\"\n";
58  }
59 }
60 
62  Includes cn;
63  for (auto infile : includes) {
64  ifstream in(infile);
65  if (!in.is_open()) {
66  std::cerr << "Cannot open input file " << infile << "\n";
67  exit(1);
68  }
69  string line;
70  std::regex classDefRegex(R"(ClassDef\s*(?:NV)?\‍(\s*(.*)\s*,\s*(\d*))");
71  std::regex apiV2Regex("namespace.*APIV2");
72  std::smatch classDefMatch;
73  bool v2{false};
74  string stype{""};
75  bool used{false};
76  while (getline(in, line)) {
77  if (std::regex_search(line, apiV2Regex)) {
78  v2 = true;
79  }
80  if (std::regex_search(line, classDefMatch, classDefRegex)) {
81  // classDefMatch[2] is the version, if needed.
82  stype = classDefMatch[1];
83  if (v2) {
84  cn.apiV2.emplace_back(stype);
85  used = true;
86  } else {
87  cn.api.emplace_back(stype);
88  used = true;
89  }
90  }
91  }
92  if (used) {
93  cn.files.emplace_back(infile);
94  }
95  }
96  return cn;
97 }
98 
99 /**
100  * Helper program to create includes files for types and includes
101  * from a list of JDB includes
102  *
103  */
104 int main(int argc, char **argv) {
105  if (argc < 5) {
106  throw runtime_error("need at least 4 arguments");
107  }
108  string outfile = argv[1]; // output file name
109  string outputType = argv[2]; // output type ("types" or "includes")
110  vector<string> classdefs; // input include files (absolute paths or relative
111  // to current dir)
112  for (int i = 3; i < argc; i++) {
113  classdefs.emplace_back(argv[i]);
114  }
115 
116  auto includes = parseIncludes(classdefs);
117 
118  ofstream out(outfile);
119  if (!out.is_open()) {
120  std::cerr << "Cannot open output file " << outfile << "\n";
121  exit(1);
122  }
123 
124  if (outputType == "types") {
125  outputTypes(out, includes);
126  } else if (outputType == "includes") {
127  outputIncludes(out, includes);
128  } else {
129  std::cerr << "don't know what to do with outputType " << outputType
130  << " : I only know 'types' or 'includes'\n";
131  exit(2);
132  }
133 
134  return 0;
135 }
Includes parseIncludes(const std::vector< std::string > &includes)
void outputIncludes(std::ostream &out, const Includes &includes)
int main(int argc, char **argv)
Helper program to create includes files for types and includes from a list of JDB includes.
void outputTypes(std::ostream &out, const Includes &includes)
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
std::vector< std::string > apiV2
std::vector< std::string > files
std::vector< std::string > api