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.h
Go to the documentation of this file.
1 
6 #pragma once
7 
8 #include <windows.h>
9 
10 #include <iostream>
11 #include <string>
12 #include <sstream>
13 #include <map>
14 #include <set>
15 
16 #include "strings_util.h"
17 
18 #define GETNAME(x) (#x)
19 
20 namespace paramkit {
21 
22  bool is_hex(const char *buf, size_t len);
23  bool is_hex_with_prefix(const char *buf);
24  bool is_dec(const char *buf, size_t len);
25  bool is_number(const char* my_buf);
26  long get_number(const char *my_buf);
27 
28  size_t strip_to_list(IN std::string s, IN std::string delim, OUT std::set<std::string> &elements_list);
29  std::string& trim(std::string& str, const std::string& chars = "\t\n\v\f\r ");
30 
31  bool get_console_color(HANDLE hConsole, int& color);
32  void print_in_color(int color, const std::string &text);
33  //--
34 
35  template <typename T_CHAR>
36  std::string to_string(T_CHAR *str1)
37  {
38  if (str1 == nullptr) return "";
39  std::string val;
40 
41  for (size_t i = 0; ; i++) {
42  if (str1[i] == 0) break;
43  val.push_back((char)str1[i]);
44  }
45  return val;
46  }
47 
48  template <typename T_CHAR>
49  int loadInt(const T_CHAR *str1, bool isHex = false)
50  {
51  std::string str = to_string(str1);
52  int intVal = 0;
53  std::stringstream ss;
54  if (isHex) {
55  ss << std::hex << str;
56  }
57  else {
58  ss << std::dec << str;
59  }
60  ss >> intVal;
61  return intVal;
62  }
63 
64  template <typename T_CHAR>
65  bool loadBoolean(IN const T_CHAR *str1, OUT bool &value)
66  {
67  std::string str = to_string(str1);
68  if (util::strequals(str, "True") || util::strequals(str, "on") || util::strequals(str, "yes")) {
69  value = true;
70  return true;
71  }
72  if (util::strequals(str, "False") || util::strequals(str, "off") || util::strequals(str, "no")) {
73  value = false;
74  return true;
75  }
76  if (!is_dec(str.c_str(), str.length())) {
77  return false;
78  }
79  const int val = loadInt(str.c_str(), false);
80  if (val == 0) {
81  value = false;
82  return true;
83  }
84  if (val == 1) {
85  value = true;
86  return true;
87  }
88  return false;
89  }
90 
92  template <typename T_STR, typename T_CHAR>
93  size_t copy_to_cstr(T_STR value, T_CHAR *buf, size_t buf_count)
94  {
95  size_t val_len = value.length() + 1;
96  if (val_len > buf_count) {
97  val_len = buf_count;
98  }
99  memcpy(buf, value.c_str(), val_len * sizeof(T_CHAR));
100  buf[val_len - 1] = '\0';
101  return val_len;
102  }
103 
104 };
bool strequals(const std::string &a, const std::string &b, bool ignoreCase=true)
bool loadBoolean(IN const T_CHAR *str1, OUT bool &value)
Definition: pk_util.h:65
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
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 to_string(T_CHAR *str1)
Definition: pk_util.h:36
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
size_t copy_to_cstr(T_STR value, T_CHAR *buf, size_t buf_count)
Copy the std::string/std::wstring value into an buffer of a given character count.
Definition: pk_util.h:93
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
int loadInt(const T_CHAR *str1, bool isHex=false)
Definition: pk_util.h:49
bool is_hex_with_prefix(const char *buf)
Definition: pk_util.cpp:26
The set of utility functions related with string processing, and finding similarity between strings.