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


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