forked from IntelRealSense/librealsense
-
Notifications
You must be signed in to change notification settings - Fork 0
/
string-formatter.cpp
135 lines (106 loc) · 4.81 KB
/
string-formatter.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
134
135
// License: Apache 2.0. See LICENSE file in root directory.
// Copyright(c) 2019 Intel Corporation. All Rights Reserved.
#include "string-formatter.h"
#include "fw-logs-formating-options.h"
#include <regex>
#include <sstream>
#include <iomanip>
#include <iostream>
using namespace std;
namespace fw_logger
{
string_formatter::string_formatter(std::unordered_map<std::string, std::vector<kvp>> enums)
:_enums(enums)
{
}
string_formatter::~string_formatter(void)
{
}
bool string_formatter::generate_message(const string& source, size_t num_of_params, const uint32_t* params, string* dest)
{
map<string, string> exp_replace_map;
map<string, int> enum_replace_map;
if (params == nullptr && num_of_params > 0) return false;
for (size_t i = 0; i < num_of_params; i++)
{
string regular_exp[4];
string replacement[4];
stringstream st_regular_exp[4];
stringstream st_replacement[4];
st_regular_exp[0] << "\\{\\b(" << i << ")\\}";
regular_exp[0] = st_regular_exp[0].str();
st_replacement[0] << params[i];
replacement[0] = st_replacement[0].str();
exp_replace_map[regular_exp[0]] = replacement[0];
st_regular_exp[1] << "\\{\\b(" << i << "):x\\}";
regular_exp[1] = st_regular_exp[1].str();
st_replacement[1] << hex << setw(2) << setfill('0') << params[i];
replacement[1] = st_replacement[1].str();
exp_replace_map[regular_exp[1]] = replacement[1];
st_regular_exp[2] << "\\{\\b(" << i << "):f\\}";
regular_exp[2] = st_regular_exp[2].str();
st_replacement[2] << params[i];
replacement[2] = st_replacement[2].str();
exp_replace_map[regular_exp[2]] = replacement[2];
st_regular_exp[3] << "\\{\\b(" << i << "),[a-zA-Z]+\\}";
regular_exp[3] = st_regular_exp[3].str();
enum_replace_map[regular_exp[3]] = params[i];
}
return replace_params(source, exp_replace_map, enum_replace_map, dest);
}
bool string_formatter::replace_params(const string& source, const map<string, string>& exp_replace_map, const map<string, int>& enum_replace_map, string* dest)
{
string source_temp(source);
for (auto exp_replace_it = exp_replace_map.begin(); exp_replace_it != exp_replace_map.end(); exp_replace_it++)
{
string destTemp;
regex e(exp_replace_it->first);
auto res = regex_replace(back_inserter(destTemp), source_temp.begin(), source_temp.end(), e, exp_replace_it->second);
source_temp = destTemp;
}
for (auto exp_replace_it = enum_replace_map.begin(); exp_replace_it != enum_replace_map.end(); exp_replace_it++)
{
string destTemp;
regex e(exp_replace_it->first);
std::smatch m;
std::regex_search(source_temp, m, std::regex(e));
string enum_name;
string st_regular_exp = "[a-zA-Z]+";
regex e1(st_regular_exp);
for(size_t exp = 0; exp<m.size(); exp++)
{
string str = m[exp];
std::smatch m1;
regex e2 = e1;
std::regex_search(str, m1, std::regex(e2));
for (size_t exp = 0; exp < m1.size(); exp++)
{
enum_name = m1[exp];
if (_enums.size()>0 && _enums.find(enum_name) != _enums.end())
{
auto vec = _enums[enum_name];
regex e3 = e;
// Verify user's input is within the enumerated range
int val = exp_replace_it->second;
auto it = std::find_if(vec.begin(), vec.end(), [val](kvp& entry){ return entry.first == val; });
if (it != vec.end())
{
regex_replace(back_inserter(destTemp), source_temp.begin(), source_temp.end(), e3, it->second);
}
else
{
stringstream s;
s << "Protocol Error recognized!\nImproper log message received: " << source_temp
<< ", invalid parameter: " << exp_replace_it->second << ".\n The range of supported values is \n";
for_each(vec.begin(), vec.end(), [&s](kvp& entry) { s << entry.first << ":" << entry.second << " ,"; });
std::cout << s.str().c_str() << std::endl;;
}
source_temp = destTemp;
}
}
}
}
*dest = source_temp;
return true;
}
}