OpenM++ runtime library (libopenm)
omHelper.h
Go to the documentation of this file.
1 
5 // Copyright (c) 2013-2015 OpenM++
6 // This code is licensed under the MIT license (see LICENSE.txt for details)
7 
8 #ifndef OM_H_HELPER_H
9 #define OM_H_HELPER_H
10 
11 #include <algorithm>
12 #include <cstring>
13 #include <ctime>
14 #include <chrono>
15 #include <functional>
16 #include <list>
17 #include <locale>
18 #include <map>
19 #include <memory>
20 #include <set>
21 #include <string>
22 #include <stdexcept>
23 #include <thread>
24 #include <iomanip>
25 #include <sstream>
26 #include <climits>
27 #include <vector>
28 
29 // define max size of file path
30 #ifdef PATH_MAX
31  #define OM_PATH_MAX (FILENAME_MAX < PATH_MAX ? FILENAME_MAX : PATH_MAX)
32 #endif
33 #ifndef OM_PATH_MAX
34  #ifdef _MAX_PATH
35  #define OM_PATH_MAX (FILENAME_MAX < _MAX_PATH ? FILENAME_MAX : _MAX_PATH)
36  #else
37  #define OM_PATH_MAX (FILENAME_MAX < 4096 ? FILENAME_MAX : 4096)
38  #endif
39 #endif
40 
41 #define OM_STRLEN_MAX INT_MAX
42 #define OM_STR_DB_MAX 32000
43 #define OM_CODE_DB_MAX 32
44 #define OM_NAME_DB_MAX 255
45 #define OM_DESCR_DB_MAX 255
46 #define OM_OPTION_DB_MAX 32000
47 #define OM_SQL_EXPR_DB_MAX 2048
48 #define OM_MAX_BUILTIN_TYPE_ID 100
50 #define OM_STATE_SAVE_TIME 1811L
51 #define OM_STATE_BEAT_TIME 1823L
53 namespace openm
54 {
56  extern void toLower(std::string & io_str, const std::locale & i_locale = std::locale(""));
57 
59  extern void toLower(char * io_str, const std::locale & i_locale = std::locale(""));
60 
62  extern void toUpper(std::string & io_str, const std::locale & i_locale = std::locale(""));
63 
65  extern void toUpper(char * io_str, const std::locale & i_locale = std::locale(""));
66 
68  extern int compareNoCase(const char * i_left, const char * i_right, size_t i_length = 0);
69 
71  extern bool equalNoCase(const char * i_left, const char * i_right, size_t i_length = 0);
72 
74  extern bool startWithNoCase(const std::string & i_str, const char * i_start);
75 
77  extern bool endWithNoCase(const std::string & i_str, const char * i_end);
78 
80  extern std::string trimLeft(const std::string & i_str, const std::locale & i_locale = std::locale(""));
81 
83  extern std::string trimRight(const std::string & i_str, const std::locale & i_locale = std::locale(""));
84 
86  extern std::string trim(const std::string & i_str, const std::locale & i_locale = std::locale(""));
87 
89  extern void blankCrLf(std::string & io_str);
90 
92  extern const std::string toQuoted(const std::string & i_str, char i_quote = '\'');
93 
95  extern const std::string toUnQuoted(const std::string & i_str, char i_quote = '\'', const std::locale & i_locale = std::locale(""));
96 
98  extern const std::string toString(double i_val);
99 
101  extern const std::string toAlphaNumeric(const std::string & i_str, int i_maxSize = 0);
102 
104  extern const std::string cleanPathChars(const std::string & i_str, int i_maxSize = 0);
105 
107  extern const std::string replaceAll(const std::string & i_src, const char * i_oldValue, const char * i_newValue);
108 
110  extern bool isBoolType(const char * i_typeName);
111 
113  extern bool isStringType(const char * i_typeName);
114 
116  extern bool isBigIntType(const char * i_typeName);
117 
121  extern bool isIntType(const char * i_typeName, int i_typeId);
122 
124  extern bool isBuiltInType(int i_typeId);
125 
127  extern bool isFloatType(const char * i_typeName);
128 
130  extern bool isTimeType(const char * i_typeName);
131 
133  extern bool isIntValid(const char* i_value);
134 
136  extern bool isFloatValid(const char* i_value);
137 
139  extern bool isBoolTrue(const char* i_str);
140 
142  extern bool isBoolValid(const char* i_str);
143 
150  extern int boolStringToInt(const char * i_value);
151 
153  ptrdiff_t memCopyTo(uint8_t * io_dst, ptrdiff_t i_offset, const void * i_src, size_t i_size);
154 
156  extern const std::string makeDateTime(const std::chrono::system_clock::time_point & i_time);
157 
159  extern const std::string makeTimeStamp(const std::chrono::system_clock::time_point & i_time);
160 
162  extern const std::string toDateTimeString(const std::string & i_timestamp);
163 
165  extern void formatTo(size_t i_size, char * io_buffer, const char * i_format, va_list io_args);
166 
168  extern const char * elliptString(const char * i_src, size_t i_size, char * io_buffer);
169 
171  extern const std::string normalizeLanguageName(const std::string & i_srcLanguage);
172 
174  extern const std::list<std::string> splitLanguageName(const std::string & i_srcLanguage);
175 
186  extern std::list<std::string> splitCsv(
187  const std::string & i_values, const char * i_delimiters = ",", bool i_isUnquote = false, char i_quote = '\''
188  );
189 
202  extern void splitCsv(
203  const std::string & i_values,
204  std::list<std::string> & io_resultLst,
205  const char * i_delimiters = ",",
206  bool i_isUnquote = false,
207  char i_quote = '\'',
208  const std::locale & i_locale = std::locale(""));
209 
211  struct LessNoCase
212  {
213  bool operator() (const std::string & i_left, const std::string & i_right) const
214  {
215  return compareNoCase(i_left.c_str(), i_right.c_str()) < 0;
216  }
217  };
218 
220  typedef std::map<std::string, std::string, LessNoCase> NoCaseMap;
221 
223  typedef std::set<std::string, LessNoCase> NoCaseSet;
224 
226  struct IRowBase
227  {
228  virtual ~IRowBase(void) noexcept = 0;
229  };
230 
232  typedef std::unique_ptr<IRowBase> IRowBaseUptr;
233 
235  typedef std::vector<IRowBaseUptr> IRowBaseVec;
236 
238  typedef std::list<IRowBaseUptr> IRowBaseList;
239 
241  struct IFinalState
242  {
243  virtual ~IFinalState(void) noexcept = 0;
244 
246  virtual bool isFinal(void) = 0;
247 
249  virtual bool isError(void) = 0;
250  };
251 
253  template <class ResourceHolder>
255  {
256  public:
257 
259  typedef void(ResourceHolder::*CleanupHandler)(void);
260 
262  exit_guard(ResourceHolder * const i_holder, CleanupHandler i_cleanupFnc) :
263  isHold(false),
264  holder(i_holder),
265  cleanup(i_cleanupFnc)
266  { }
267 
269  ~exit_guard(void) noexcept
270  {
271  try {
272  if (!isHold) (holder->*cleanup)();
273  }
274  catch (...) { }
275  }
276 
278  void hold(void) noexcept { isHold = true; }
279 
280  private:
281  bool isHold; // if true then hold resource
282  ResourceHolder * const holder; // resource holder class
283  CleanupHandler cleanup; // resource release method
284 
285  private:
286  exit_guard(const exit_guard & i_guard) = delete;
287  exit_guard & operator=(const exit_guard & i_guard) = delete;
288  };
289 }
290 
291 #endif // OM_H_HELPER_H
simple resource exit guard implementation
Definition: omHelper.h:255
void hold(void) noexcept
hold resource on exit instead of relesing it
Definition: omHelper.h:278
void(ResourceHolder::* CleanupHandler)(void)
resource release method of resource holder class
Definition: omHelper.h:259
~exit_guard(void) noexcept
release resource if exit guard hold() was not called
Definition: omHelper.h:269
exit_guard(ResourceHolder *const i_holder, CleanupHandler i_cleanupFnc)
create resource guard to release resource on block exit
Definition: omHelper.h:262
openM++ namespace
Definition: log.h:32
ptrdiff_t memCopyTo(uint8_t *io_dst, ptrdiff_t i_offset, const void *i_src, size_t i_size)
copy bytes source into destination and return next destination offset
Definition: helper.cpp:347
const std::string makeDateTime(const std::chrono::system_clock::time_point &i_time)
make date-time string, ie: 2012-08-17 16:04:59.148
const char * elliptString(const char *i_src, size_t i_size, char *io_buffer)
if source string exceed max size than return ellipted copy into the buffer
Definition: helper.cpp:421
const std::string toAlphaNumeric(const std::string &i_str, int i_maxSize=0)
replace all non [A-Z,a-z,0-9] by _ underscore and remove repetitive underscores.
bool isBoolValid(const char *i_str)
return true if lower case of source string one of: "yes" "1" "true" "no" "0" "false"
Definition: helper.cpp:325
std::string trimLeft(const std::string &i_str, const std::locale &i_locale=std::locale(""))
trim leading blank and space characters
bool isIntValid(const char *i_value)
return true if i_value string represent valid integer constant
Definition: helper.cpp:308
bool isBoolTrue(const char *i_str)
return true if lower case of source string one of: "yes" "1" "true"
Definition: helper.cpp:319
std::string trimRight(const std::string &i_str, const std::locale &i_locale=std::locale(""))
trim trailing blank and space characters
bool isStringType(const char *i_typeName)
return true if model type is string (varchar)
Definition: helper.cpp:266
const std::string makeTimeStamp(const std::chrono::system_clock::time_point &i_time)
make timestamp string, ie: 2012_08_17_16_04_59_148
int boolStringToInt(const char *i_value)
convert i_value string represnting boolean option and return one of: return 1 if i_value is one of: ...
Definition: helper.cpp:336
int compareNoCase(const char *i_left, const char *i_right, size_t i_length=0)
case neutral string comparison
Definition: helper.cpp:64
bool isBigIntType(const char *i_typeName)
return true if model type is bigint (64 bit)
Definition: helper.cpp:272
void blankCrLf(std::string &io_str)
replace all <cr> or <lf> with blank space character
std::list< IRowBaseUptr > IRowBaseList
db rows: list of unique pointers to db row
Definition: omHelper.h:238
bool startWithNoCase(const std::string &i_str, const char *i_start)
check if string start with i_start, using case neutral string comparison
std::unique_ptr< IRowBase > IRowBaseUptr
unique pointer to db row
Definition: omHelper.h:232
void formatTo(size_t i_size, char *io_buffer, const char *i_format, va_list io_args)
format message into supplied buffer using vsnprintf()
Definition: helper.cpp:406
const std::string toString(double i_val)
convert float type to string: exist to fix std::to_string conversion losses.
Definition: helper.cpp:182
bool isIntType(const char *i_typeName, int i_typeId)
return true if model type is integer: not float, string, boolean, bigint (if type is not a built-in t...
Definition: helper.cpp:283
bool isFloatType(const char *i_typeName)
return true if model type is float (float, real, double or time)
Definition: helper.cpp:293
bool isFloatValid(const char *i_value)
return true if i_value string represent valid floating point constant
Definition: helper.cpp:314
const std::string toUnQuoted(const std::string &i_str, char i_quote='\'', const std::locale &i_locale=std::locale(""))
make unquoted string using sql single ' quote by default, ie: 'O''Brien' into O'Brien
const std::string normalizeLanguageName(const std::string &i_srcLanguage)
normalize language name by removing encoding part, replace _ by - and lower case: "en-ca" from "en_CA...
bool isBuiltInType(int i_typeId)
return true if model type id is built-in type id, ie: int, double, logical
Definition: helper.cpp:290
void toLower(std::string &io_str, const std::locale &i_locale=std::locale(""))
convert string to lower case
std::map< std::string, std::string, LessNoCase > NoCaseMap
map of key-value strings with case neutral key search
Definition: omHelper.h:220
std::string trim(const std::string &i_str, const std::locale &i_locale=std::locale(""))
trim leading and trailing blank and space characters
std::list< std::string > splitCsv(const std::string &i_values, const char *i_delimiters=",", bool i_isUnquote=false, char i_quote='\'')
split and trim comma-separated list of values (other delimiters can be used too, ie: semicolon).
bool isTimeType(const char *i_typeName)
return true if model type is Time
Definition: helper.cpp:302
void toUpper(std::string &io_str, const std::locale &i_locale=std::locale(""))
convert string to upper case
bool equalNoCase(const char *i_left, const char *i_right, size_t i_length=0)
case neutral string equal comparison
Definition: helper.cpp:71
const std::string toDateTimeString(const std::string &i_timestamp)
make date-time string from timestamp string, ie: 2012_08_17_16_04_59_148 => 2012-08-17 16:04:59....
const std::list< std::string > splitLanguageName(const std::string &i_srcLanguage)
normalize language name and split it into list of prefered languages: en_CA => [en-ca,...
bool endWithNoCase(const std::string &i_str, const char *i_end)
check if string end with i_end, using case neutral string comparison
const std::string toQuoted(const std::string &i_str, char i_quote='\'')
make quoted string using sql single ' quote by default, ie: 'O''Brien'
std::vector< IRowBaseUptr > IRowBaseVec
db rows: vector of unique pointers to db row
Definition: omHelper.h:235
bool isBoolType(const char *i_typeName)
return true if model type is boolean (logical)
Definition: helper.cpp:260
const std::string replaceAll(const std::string &i_src, const char *i_oldValue, const char *i_newValue)
replace all occurence of i_oldValue by i_newValue, both old and new values must be not empty
std::set< std::string, LessNoCase > NoCaseSet
set of strings with case neutral key search
Definition: omHelper.h:223
const std::string cleanPathChars(const std::string &i_str, int i_maxSize=0)
replace all non non-printable and any of "'`$}{><:|?*&^;/\ by _ underscore.
final model run state public interface: thread safe
Definition: omHelper.h:242
virtual bool isError(void)=0
return true if status is an error
virtual bool isFinal(void)=0
return true if status is one of exiting: done, exit, error
db-row abstract base
Definition: omHelper.h:227
case neutral less comparator for strings
Definition: omHelper.h:212