Mercurial > hg > orthanc-stone
comparison Deprecated/Resources/CodeGeneration/testCppHandler/main.cpp @ 1401:f6a2d46d2b76
moved CodeGeneration into Deprecated
author | Alain Mazy <alain@mazy.be> |
---|---|
date | Wed, 29 Apr 2020 20:48:18 +0200 |
parents | Resources/CodeGeneration/testCppHandler/main.cpp@342f3e04bfa9 |
children | 828a9b4ee1b7 |
comparison
equal
deleted
inserted
replaced
1400:419d0320c344 | 1401:f6a2d46d2b76 |
---|---|
1 #include <string> | |
2 #include <fstream> | |
3 #include <filesystem> | |
4 #include <regex> | |
5 using namespace std; | |
6 namespace fs = std::filesystem; | |
7 | |
8 #include <boost/program_options.hpp> | |
9 using namespace boost::program_options; | |
10 | |
11 #include "TestStoneCodeGen_generated.hpp" | |
12 | |
13 /** | |
14 Transforms `str` by replacing occurrences of `oldStr` with `newStr`, using | |
15 plain text (*not* regular expressions.) | |
16 */ | |
17 static inline void ReplaceInString( | |
18 string& str, | |
19 const std::string& oldStr, | |
20 const std::string& newStr) | |
21 { | |
22 std::string::size_type pos = 0u; | |
23 while ((pos = str.find(oldStr, pos)) != std::string::npos) { | |
24 str.replace(pos, oldStr.length(), newStr); | |
25 pos += newStr.length(); | |
26 } | |
27 } | |
28 | |
29 string SlurpFile(const string& fileName) | |
30 { | |
31 ifstream ifs(fileName.c_str(), ios::in | ios::binary | ios::ate); | |
32 | |
33 ifstream::pos_type fileSize = ifs.tellg(); | |
34 ifs.seekg(0, ios::beg); | |
35 | |
36 vector<char> bytes(fileSize); | |
37 ifs.read(bytes.data(), fileSize); | |
38 | |
39 return string(bytes.data(), fileSize); | |
40 } | |
41 | |
42 class MyHandler : public TestStoneCodeGen::IHandler | |
43 { | |
44 public: | |
45 virtual bool Handle(const TestStoneCodeGen::A& value) override | |
46 { | |
47 TestStoneCodeGen::StoneDumpValue(cout, value); | |
48 return true; | |
49 } | |
50 virtual bool Handle(const TestStoneCodeGen::B& value) override | |
51 { | |
52 TestStoneCodeGen::StoneDumpValue(cout, value); | |
53 return true; | |
54 } | |
55 virtual bool Handle(const TestStoneCodeGen::C& value) override | |
56 { | |
57 TestStoneCodeGen::StoneDumpValue(cout, value); | |
58 return true; | |
59 } | |
60 virtual bool Handle(const TestStoneCodeGen::Message1& value) override | |
61 { | |
62 TestStoneCodeGen::StoneDumpValue(cout, value); | |
63 return true; | |
64 } | |
65 virtual bool Handle(const TestStoneCodeGen::Message2& value) override | |
66 { | |
67 TestStoneCodeGen::StoneDumpValue(cout, value); | |
68 return true; | |
69 } | |
70 }; | |
71 | |
72 template<typename T> | |
73 void ProcessPath(T filePath) | |
74 { | |
75 cout << "+--------------------------------------------+\n"; | |
76 cout << "| Processing: " << filePath.path().string() << "\n"; | |
77 cout << "+--------------------------------------------+\n"; | |
78 MyHandler handler; | |
79 auto contents = SlurpFile(filePath.path().string()); | |
80 TestStoneCodeGen::StoneDispatchToHandler(contents, &handler); | |
81 } | |
82 | |
83 int main(int argc, char** argv) | |
84 { | |
85 try | |
86 { | |
87 | |
88 options_description desc("Allowed options"); | |
89 desc.add_options() | |
90 // First parameter describes option name/short name | |
91 // The second is parameter to option | |
92 // The third is description | |
93 ("help,h", "print usage message") | |
94 ("pattern,p", value<string>(), "pattern for input") | |
95 ; | |
96 | |
97 variables_map vm; | |
98 store(parse_command_line(argc, argv, desc), vm); | |
99 | |
100 if (vm.count("help")) | |
101 { | |
102 cout << desc << "\n"; | |
103 return 0; | |
104 } | |
105 | |
106 notify(vm); | |
107 | |
108 string pattern = vm["pattern"].as<string>(); | |
109 | |
110 // tranform globbing pattern into regex | |
111 // we should deal with -, ., *... | |
112 string regexPatternStr = pattern; | |
113 cout << "Pattern is: " << regexPatternStr << endl; | |
114 ReplaceInString(regexPatternStr, "\\", "\\\\"); | |
115 ReplaceInString(regexPatternStr, "-", "\\-"); | |
116 ReplaceInString(regexPatternStr, ".", "\\."); | |
117 ReplaceInString(regexPatternStr, "*", ".*"); | |
118 ReplaceInString(regexPatternStr, "?", "."); | |
119 cout << "Corresponding regex is: " << regexPatternStr << endl; | |
120 | |
121 regex regexPattern(regexPatternStr); | |
122 | |
123 for (auto& p : fs::directory_iterator(".")) | |
124 { | |
125 auto fileName = p.path().filename().string(); | |
126 if (regex_match(fileName, regexPattern)) | |
127 { | |
128 ProcessPath(p); | |
129 } | |
130 } | |
131 return 0; | |
132 | |
133 | |
134 } | |
135 catch (exception& e) | |
136 { | |
137 cerr << e.what() << "\n"; | |
138 } | |
139 } |