comparison Resources/CodeGeneration/testCppHandler/main.cpp @ 495:6405435480ae bgo-commands-codegen

Fixed template to add dump capabilities + started work on an integrated TS/WASM test
author bgo-osimis
date Sat, 23 Feb 2019 14:14:32 +0100
parents fc17251477d6
children 4eccf698e52f
comparison
equal deleted inserted replaced
494:fc17251477d6 495:6405435480ae
1 #include <string> 1 #include <string>
2 #include <fstream>
2 #include <filesystem> 3 #include <filesystem>
3 #include <regex> 4 #include <regex>
4 using namespace std; 5 using namespace std;
5 namespace fs = std::filesystem; 6 namespace fs = std::filesystem;
6 7
23 str.replace(pos, oldStr.length(), newStr); 24 str.replace(pos, oldStr.length(), newStr);
24 pos += newStr.length(); 25 pos += newStr.length();
25 } 26 }
26 } 27 }
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 VsolMessages::IHandler
43 {
44 public:
45 virtual bool Handle(const VsolMessages::A& value) override
46 {
47 VsolMessages::StoneDumpValue(cout, value);
48 return true;
49 }
50 virtual bool Handle(const VsolMessages::B& value) override
51 {
52 VsolMessages::StoneDumpValue(cout, value);
53 return true;
54 }
55 virtual bool Handle(const VsolMessages::C& value) override
56 {
57 VsolMessages::StoneDumpValue(cout, value);
58 return true;
59 }
60 virtual bool Handle(const VsolMessages::Message1& value) override
61 {
62 VsolMessages::StoneDumpValue(cout, value);
63 return true;
64 }
65 virtual bool Handle(const VsolMessages::Message2& value) override
66 {
67 VsolMessages::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 VsolMessages::StoneDispatchToHandler(contents, &handler);
81 }
82
28 int main(int argc, char** argv) 83 int main(int argc, char** argv)
29 { 84 {
30 try 85 try
31 { 86 {
32 string pattern;
33 87
34 options_description desc("Allowed options"); 88 options_description desc("Allowed options");
35 desc.add_options() 89 desc.add_options()
36 // First parameter describes option name/short name 90 // First parameter describes option name/short name
37 // The second is parameter to option 91 // The second is parameter to option
38 // The third is description 92 // The third is description
39 ("help,h", "print usage message") 93 ("help,h", "print usage message")
40 ("pattern,p", value(&pattern), "pattern for input") 94 ("pattern,p", value<string>(), "pattern for input")
41 ; 95 ;
42 96
43 variables_map vm; 97 variables_map vm;
44 store(parse_command_line(argc, argv, desc), vm); 98 store(parse_command_line(argc, argv, desc), vm);
45 99
47 { 101 {
48 cout << desc << "\n"; 102 cout << desc << "\n";
49 return 0; 103 return 0;
50 } 104 }
51 105
106 notify(vm);
107
108 string pattern = vm["pattern"].as<string>();
109
52 // tranform globbing pattern into regex 110 // tranform globbing pattern into regex
53 // we should deal with -, ., *... 111 // we should deal with -, ., *...
54 string regexPatternStr = pattern; 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
55 regex regexPattern(regexPatternStr); 121 regex regexPattern(regexPatternStr);
56 122
57 for (auto& p : fs::directory_iterator(".")) 123 for (auto& p : fs::directory_iterator("."))
58 { 124 {
59 if (regex_match(p.path().string(), regexPattern)) 125 auto fileName = p.path().filename().string();
60 std::cout << "\"" << p << "\" is a match\n"; 126 if (regex_match(fileName, regexPattern))
61 else 127 {
62 std::cout << "\"" << p << "\" is *not* a match\n"; 128 ProcessPath(p);
129 }
63 } 130 }
64 return 0; 131 return 0;
65 132
66 133
67 } 134 }