ParamKit
A small library helping to parse commandline parameters (for Windows).
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
pk_util.cpp
Go to the documentation of this file.
1 #include "pk_util.h"
2 #include "strings_util.h"
3 
4 bool paramkit::is_hex(const char *buf, size_t len)
5 {
6  if (!buf || len == 0) return false;
7  for (size_t i = 0; i < len; i++) {
8  if (buf[i] >= '0' && buf[i] <= '9') continue;
9  if (buf[i] >= 'A' && buf[i] <= 'F') continue;
10  if (buf[i] >= 'a' && buf[i] <= 'f') continue;
11  return false;
12  }
13  return true;
14 }
15 
16 bool paramkit::is_dec(const char *buf, size_t len)
17 {
18  if (!buf || len == 0) return false;
19  for (size_t i = 0; i < len; i++) {
20  if (buf[i] >= '0' && buf[i] <= '9') continue;
21  return false;
22  }
23  return true;
24 }
25 
26 bool paramkit::is_hex_with_prefix(const char *my_buf)
27 {
28  if (!my_buf) return false;
29 
30  const char hex_pattern[] = "0x";
31  size_t hex_pattern_len = strlen(hex_pattern);
32 
33  const size_t len = strlen(my_buf);
34  if (len == 0) return false;
35 
36  if (len > hex_pattern_len) {
37  if (util::is_cstr_equal(my_buf, hex_pattern, hex_pattern_len)) {
38  if (!is_hex(my_buf + hex_pattern_len, len - hex_pattern_len)) return false;
39  return true;
40  }
41  }
42  return false;
43 }
44 
45 bool paramkit::is_number(const char* my_buf)
46 {
47  if (!my_buf) return false;
48 
49  const size_t len = strlen(my_buf);
50  if (is_hex_with_prefix(my_buf)) return true;
51  if (is_dec(my_buf, len)) return true;
52  return false;
53 }
54 
55 long paramkit::get_number(const char *my_buf)
56 {
57  if (!my_buf) return false;
58 
59  const char hex_pattern[] = "0x";
60  size_t hex_pattern_len = strlen(hex_pattern);
61 
62  const size_t len = strlen(my_buf);
63  if (len == 0) return 0;
64 
65  long out = 0;
66  const size_t min_length = 1; //tolerate number with at least 1 character
67  if (len > hex_pattern_len) {
68  if (util::is_cstr_equal(my_buf, hex_pattern, hex_pattern_len)) {
69  if (!is_hex(my_buf + hex_pattern_len, min_length)) return 0;
70 
71  std::stringstream ss;
72  ss << std::hex << my_buf;
73  ss >> out;
74  return out;
75  }
76  }
77  if (!is_dec(my_buf, min_length)) return 0;
78 
79  std::stringstream ss;
80  ss << std::dec << my_buf;
81  ss >> out;
82  return out;
83 }
84 
85 bool paramkit::get_console_color(HANDLE hConsole, int& color) {
86  CONSOLE_SCREEN_BUFFER_INFO info;
87  if (!GetConsoleScreenBufferInfo(hConsole, &info))
88  return false;
89  color = info.wAttributes;
90  return true;
91 }
92 
93 void paramkit::print_in_color(int color, const std::string &text)
94 {
95  HANDLE hConsole = GetStdHandle(STD_OUTPUT_HANDLE);
96  int prev = 7;
97  get_console_color(hConsole, prev); // get the previous color
98 
99  FlushConsoleInputBuffer(hConsole);
100  SetConsoleTextAttribute(hConsole, color); // back to default color
101  std::cout << text;
102  FlushConsoleInputBuffer(hConsole);
103 
104  SetConsoleTextAttribute(hConsole, prev); // back to previous color
105 }
106 
107 namespace paramkit {
108  std::string& ltrim(std::string& str, const std::string& chars = "\t\n\v\f\r ")
109  {
110  str.erase(0, str.find_first_not_of(chars));
111  return str;
112  }
113 
114  std::string& rtrim(std::string& str, const std::string& chars = "\t\n\v\f\r ")
115  {
116  str.erase(str.find_last_not_of(chars) + 1);
117  return str;
118  }
119 };
120 
121 std::string& paramkit::trim(std::string& str, const std::string& chars)
122 {
123  return ltrim(rtrim(str, chars), chars);
124 }
125 
126 size_t paramkit::strip_to_list(IN std::string s, IN std::string delim, OUT std::set<std::string> &elements_list)
127 {
128  size_t start = 0;
129  size_t end = s.find(delim);
130  while (end != std::string::npos)
131  {
132  std::string next_str = s.substr(start, end - start);
133  trim(next_str);
134  if (next_str.length() > 0) {
135  elements_list.insert(next_str);
136  }
137  start = end + delim.length();
138  end = s.find(delim, start);
139  }
140  std::string next_str = s.substr(start, end);
141  trim(next_str);
142  if (next_str.length() > 0) {
143  elements_list.insert(next_str);
144  }
145  return elements_list.size();
146 }
bool is_cstr_equal(char const *a, char const *b, const size_t max_len, bool ignoreCase=true)
long get_number(const char *my_buf)
Definition: pk_util.cpp:55
bool is_dec(const char *buf, size_t len)
Definition: pk_util.cpp:16
std::string & rtrim(std::string &str, const std::string &chars="\t\n\v\f\r ")
Definition: pk_util.cpp:114
void print_in_color(int color, const std::string &text)
Definition: pk_util.cpp:93
bool is_number(const char *my_buf)
Definition: pk_util.cpp:45
std::string & ltrim(std::string &str, const std::string &chars="\t\n\v\f\r ")
Definition: pk_util.cpp:108
bool get_console_color(HANDLE hConsole, int &color)
Definition: pk_util.cpp:85
size_t strip_to_list(IN std::string s, IN std::string delim, OUT std::set< std::string > &elements_list)
Definition: pk_util.cpp:126
bool is_hex(const char *buf, size_t len)
Definition: pk_util.cpp:4
std::string & trim(std::string &str, const std::string &chars="\t\n\v\f\r ")
Definition: pk_util.cpp:121
bool is_hex_with_prefix(const char *buf)
Definition: pk_util.cpp:26
The set of utility functions used by the ParamKit.
The set of utility functions related with string processing, and finding similarity between strings.