fw-string-formatter.cpp
Go to the documentation of this file.
1 // License: Apache 2.0. See LICENSE file in root directory.
2 // Copyright(c) 2019 Intel Corporation. All Rights Reserved.
3 #include "fw-string-formatter.h"
5 #include <regex>
6 #include <sstream>
7 #include <iomanip>
8 #include <iostream>
9 
10 using namespace std;
11 
12 namespace librealsense
13 {
14  namespace fw_logs
15  {
16  fw_string_formatter::fw_string_formatter(std::unordered_map<std::string, std::vector<kvp>> enums)
17  :_enums(enums)
18  {
19  }
20 
21 
23  {
24  }
25 
26  bool fw_string_formatter::generate_message(const string& source, size_t num_of_params, const uint32_t* params, string* dest)
27  {
28  map<string, string> exp_replace_map;
29  map<string, int> enum_replace_map;
30 
31  if (params == nullptr && num_of_params > 0) return false;
32 
33  for (size_t i = 0; i < num_of_params; i++)
34  {
35  string regular_exp[4];
36  string replacement[4];
37  stringstream st_regular_exp[4];
38  stringstream st_replacement[4];
39 
40  st_regular_exp[0] << "\\{\\b(" << i << ")\\}";
41  regular_exp[0] = st_regular_exp[0].str();
42 
43  st_replacement[0] << params[i];
44  replacement[0] = st_replacement[0].str();
45 
46  exp_replace_map[regular_exp[0]] = replacement[0];
47 
48 
49  st_regular_exp[1] << "\\{\\b(" << i << "):x\\}";
50  regular_exp[1] = st_regular_exp[1].str();
51 
52  st_replacement[1] << hex << setw(2) << setfill('0') << params[i];
53  replacement[1] = st_replacement[1].str();
54 
55  exp_replace_map[regular_exp[1]] = replacement[1];
56 
57  st_regular_exp[2] << "\\{\\b(" << i << "):f\\}";
58  regular_exp[2] = st_regular_exp[2].str();
59  st_replacement[2] << params[i];
60  replacement[2] = st_replacement[2].str();
61  exp_replace_map[regular_exp[2]] = replacement[2];
62 
63 
64  st_regular_exp[3] << "\\{\\b(" << i << "),[a-zA-Z]+\\}";
65  regular_exp[3] = st_regular_exp[3].str();
66 
67  enum_replace_map[regular_exp[3]] = params[i];
68  }
69 
70  return replace_params(source, exp_replace_map, enum_replace_map, dest);
71  }
72 
73  bool fw_string_formatter::replace_params(const string& source, const map<string, string>& exp_replace_map, const map<string, int>& enum_replace_map, string* dest)
74  {
75  string source_temp(source);
76 
77  for (auto exp_replace_it = exp_replace_map.begin(); exp_replace_it != exp_replace_map.end(); exp_replace_it++)
78  {
79  string destTemp;
80  regex e(exp_replace_it->first);
81  auto res = regex_replace(back_inserter(destTemp), source_temp.begin(), source_temp.end(), e, exp_replace_it->second);
82  source_temp = destTemp;
83  }
84 
85  for (auto exp_replace_it = enum_replace_map.begin(); exp_replace_it != enum_replace_map.end(); exp_replace_it++)
86  {
87  string destTemp;
88  regex e(exp_replace_it->first);
89  std::smatch m;
90  std::regex_search(source_temp, m, std::regex(e));
91 
92  string enum_name;
93 
94  string st_regular_exp = "[a-zA-Z]+";
95  regex e1(st_regular_exp);
96 
97  for (size_t exp = 0; exp < m.size(); exp++)
98  {
99  string str = m[exp];
100 
101  std::smatch m1;
102 
103  regex e2 = e1;
104  std::regex_search(str, m1, std::regex(e2));
105 
106  for (size_t exp = 0; exp < m1.size(); exp++)
107  {
108  enum_name = m1[exp];
109  if (_enums.size() > 0 && _enums.find(enum_name) != _enums.end())
110  {
111  auto vec = _enums[enum_name];
112  regex e3 = e;
113  // Verify user's input is within the enumerated range
114  int val = exp_replace_it->second;
115  auto it = std::find_if(vec.begin(), vec.end(), [val](kvp& entry) { return entry.first == val; });
116  if (it != vec.end())
117  {
118  regex_replace(back_inserter(destTemp), source_temp.begin(), source_temp.end(), e3, it->second);
119  }
120  else
121  {
122  stringstream s;
123  s << "Protocol Error recognized!\nImproper log message received: " << source_temp
124  << ", invalid parameter: " << exp_replace_it->second << ".\n The range of supported values is \n";
125  for_each(vec.begin(), vec.end(), [&s](kvp& entry) { s << entry.first << ":" << entry.second << " ,"; });
126  std::cout << s.str().c_str() << std::endl;;
127  }
128  source_temp = destTemp;
129  }
130  }
131  }
132  }
133 
134  *dest = source_temp;
135  return true;
136  }
137  }
138 }
std::unordered_map< std::string, std::vector< std::pair< int, std::string > > > _enums
GLdouble s
const GLfloat * m
Definition: glext.h:6814
GLsizei const GLchar *const * string
GLuint entry
Definition: glext.h:10991
e
Definition: rmse.py:177
GLuint GLfloat * val
Definition: parser.hpp:154
std::ostream & cout()
unsigned int uint32_t
Definition: stdint.h:80
GLenum const GLfloat * params
bool generate_message(const std::string &source, size_t num_of_params, const uint32_t *params, std::string *dest)
static auto it
GLsizei GLsizei GLchar * source
LZ4LIB_API char * dest
Definition: lz4.h:438
int i
Definition: enums.py:1
GLuint res
Definition: glext.h:8856
bool replace_params(const std::string &source, const std::map< std::string, std::string > &exp_replace_map, const std::map< std::string, int > &enum_replace_map, std::string *dest)


librealsense2
Author(s): Sergey Dorodnicov , Doron Hirshberg , Mark Horn , Reagan Lopez , Itay Carpis
autogenerated on Mon May 3 2021 02:47:15