forked from IntelRealSense/librealsense
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstring-formatter.cpp
70 lines (52 loc) · 2.04 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
// License: Apache 2.0. See LICENSE file in root directory.
// Copyright(c) 2019 Intel Corporation. All Rights Reserved.
#include "string-formatter.h"
#include <regex>
#include <sstream>
#include <iomanip>
using namespace std;
namespace fw_logger
{
string_formatter::string_formatter(void)
{
}
string_formatter::~string_formatter(void)
{
}
bool string_formatter::generate_message(const string& source, int num_of_params, const uint32_t* params, string* dest)
{
map<string, string> exp_replace_map;
if (params == nullptr && num_of_params > 0) return false;
for (int i = 0; i < num_of_params; i++)
{
string regular_exp[2];
string replacement[2];
stringstream st_regular_exp[2];
stringstream st_replacement[2];
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];
}
return replace_params(source, exp_replace_map, dest);
}
bool string_formatter::replace_params(const string& source, const map<string, string>& exp_replace_map, string* dest) const
{
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);
regex_replace(back_inserter(destTemp), source_temp.begin(), source_temp.end(), e, exp_replace_it->second);
source_temp = destTemp;
}
*dest = source_temp;
return true;
}
}