-
Notifications
You must be signed in to change notification settings - Fork 0
/
control.cpp
133 lines (112 loc) · 3.04 KB
/
control.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
#include <stdio.h>
#include <iostream>
#include <cstdarg>
#include <boost/property_tree/json_parser.hpp>
#include <boost/date_time/posix_time/posix_time.hpp>
#include <boost/algorithm/string/replace.hpp>
#include <boost/algorithm/string.hpp>
#include <boost/filesystem.hpp>
#include <boost/asio.hpp>
#ifdef WIN32
#else
#include <pthread.h>
#endif
#include <teoslib/config.h>
#include <teoslib/control.hpp>
std::string formatUsage(std::string unixUsage) {
#ifdef WIN32
std::string windowsCmndUsage = unixUsage;
boost::replace_all(windowsCmndUsage, "\"", "\"\"\"");
boost::replace_all(windowsCmndUsage, "'", "\"");
return windowsCmndUsage;
#else
return unixUsage;
#endif
}
namespace teos
{
using namespace std;
using namespace boost::property_tree;
#define SHARP "# "
#define INDENT "15"
boost::format output(string label, string format) {
string header = (boost::format(SHARP "%1$" INDENT "s: ") % label).str();
return boost::format(header + format);
}
void output(const char* label, const char* format, ...) {
printf(SHARP "%" INDENT "s: ", label);
string f(format);
f += "\n";
va_list argptr;
va_start(argptr, format);
vprintf(f.c_str(), argptr);
va_end(argptr);
}
void output(const char* text, ...) {
printf(SHARP "%s\n", text);
}
ostream& sharp() {
cout << SHARP;
return cout;
}
void output(string text){
text = SHARP + text;
boost::replace_all<string>(text, "\n", "\n" SHARP);
cout << text << endl;
}
string TeosControl::requestToString(bool isRaw) const {
stringstream ss;
json_parser::
write_json(ss, reqJson_, !isRaw);
string temp = ss.str();
boost::replace_all(temp, "\\/", "/");
return temp;
}
string TeosControl::responseToString(bool isRaw) const {
stringstream ss;
json_parser::
write_json(ss, respJson_, !isRaw);
string temp = ss.str();
boost::replace_all(temp, "\\/", "/");
return temp;
}
string TeosControl::executable = "";
string TeosControl::getConfigJson(){
string configJson
= (boost::filesystem::path(TeosControl::executable).parent_path()
/ CONFIG_JSON).string();
return configJson;
}
ptree TeosControl::getConfig(TeosControl* teosControl) {
ptree config;
try
{
read_json(getConfigJson(), config);
}
catch (exception& e) {
if(teosControl) {
teosControl->putError(e.what());
} else {
cout << teos_ERROR << endl << e.what() << endl;
}
}
return config;
}
void TeosControl::errorRespJson(string sender, string message)
{
if(respJson_.count(teos_ERROR) != 0) {
return;
}
if(!sender.empty()) {
string senderEntry = "\"sender\":\"" + sender + "\"";
string msgEntry = "\"message\":{" + message + "}";
respJson_.put(teos_ERROR, "{" + senderEntry + ", " + msgEntry + "}");
} else {
respJson_.put(teos_ERROR, message);
}
isError_ = true;
}
void TeosControl::putError(string message, string sender) {
errorRespJson(sender, message);
}
}