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#include <cstdarg>
29
30// define max size of file path
31#ifdef PATH_MAX
32 #define OM_PATH_MAX (FILENAME_MAX < PATH_MAX ? FILENAME_MAX : PATH_MAX)
33#endif
34#ifndef OM_PATH_MAX
35 #ifdef _MAX_PATH
36 #define OM_PATH_MAX (FILENAME_MAX < _MAX_PATH ? FILENAME_MAX : _MAX_PATH)
37 #else
38 #define OM_PATH_MAX (FILENAME_MAX < 4096 ? FILENAME_MAX : 4096)
39 #endif
40#endif
41
42#define OM_STRLEN_MAX INT_MAX
43#define OM_STR_DB_MAX 32000
44#define OM_CODE_DB_MAX 32
45#define OM_NAME_DB_MAX 255
46#define OM_DESCR_DB_MAX 255
47#define OM_OPTION_DB_MAX 32000
48#define OM_SQL_EXPR_DB_MAX 2048
49#define OM_MAX_BUILTIN_TYPE_ID 100
51#define OM_STATE_SAVE_TIME 1811L
52#define OM_STATE_BEAT_TIME 1823L
54namespace openm
55{
57 extern void toLower(std::string & io_str, const std::locale & i_locale = std::locale(""));
58
60 extern void toLower(char * io_str, const std::locale & i_locale = std::locale(""));
61
63 extern void toUpper(std::string & io_str, const std::locale & i_locale = std::locale(""));
64
66 extern void toUpper(char * io_str, const std::locale & i_locale = std::locale(""));
67
69 extern int compareNoCase(const char * i_left, const char * i_right, size_t i_length = 0);
70
72 extern bool equalNoCase(const char * i_left, const char * i_right, size_t i_length = 0);
73
75 extern bool startWithNoCase(const std::string & i_str, const char * i_start);
76
78 extern bool endWithNoCase(const std::string & i_str, const char * i_end);
79
81 extern std::string trimLeft(const std::string & i_str, const std::locale & i_locale = std::locale(""));
82
84 extern std::string trimRight(const std::string & i_str, const std::locale & i_locale = std::locale(""));
85
87 extern std::string trim(const std::string & i_str, const std::locale & i_locale = std::locale(""));
88
90 extern void blankCrLf(std::string & io_str);
91
93 extern const std::string toQuoted(const std::string & i_str, char i_quote = '\'');
94
96 extern const std::string toUnQuoted(const std::string & i_str, char i_quote = '\'', const std::locale & i_locale = std::locale(""));
97
99 extern const std::string toString(double i_val);
100
102 extern const std::string toAlphaNumeric(const std::string & i_str, int i_maxSize = 0);
103
105 extern const std::string cleanPathChars(const std::string & i_str, int i_maxSize = 0);
106
108 extern const std::string replaceAll(const std::string & i_src, const char * i_oldValue, const char * i_newValue);
109
111 extern bool isBoolType(const char * i_typeName);
112
114 extern bool isStringType(const char * i_typeName);
115
117 extern bool isBigIntType(const char * i_typeName);
118
122 extern bool isIntType(const char * i_typeName, int i_typeId);
123
125 extern bool isBuiltInType(int i_typeId);
126
128 extern bool isFloatType(const char * i_typeName);
129
131 extern bool isTimeType(const char * i_typeName);
132
134 extern bool isIntValid(const char* i_value);
135
137 extern bool isFloatValid(const char* i_value);
138
140 extern bool isBoolTrue(const char* i_str);
141
143 extern bool isBoolValid(const char* i_str);
144
151 extern int boolStringToInt(const char * i_value);
152
154 ptrdiff_t memCopyTo(uint8_t * io_dst, ptrdiff_t i_offset, const void * i_src, size_t i_size);
155
157 extern const std::string makeDateTime(const std::chrono::system_clock::time_point & i_time);
158
160 extern const std::string makeTimeStamp(const std::chrono::system_clock::time_point & i_time);
161
163 extern const std::string toDateTimeString(const std::string & i_timestamp);
164
166 extern void formatTo(size_t i_size, char * io_buffer, const char * i_format, va_list io_args);
167
169 extern std::string formatToString(const char * i_format, ...);
170
172 extern const char * elliptString(const char * i_src, size_t i_size, char * io_buffer);
173
175 extern const std::string normalizeLanguageName(const std::string & i_srcLanguage);
176
178 extern const std::list<std::string> splitLanguageName(const std::string & i_srcLanguage);
179
190 extern std::list<std::string> splitCsv(
191 const std::string & i_values, const char * i_delimiters = ",", bool i_isUnquote = false, char i_quote = '\''
192 );
193
206 extern void splitCsv(
207 const std::string & i_values,
208 std::list<std::string> & io_resultLst,
209 const char * i_delimiters = ",",
210 bool i_isUnquote = false,
211 char i_quote = '\'',
212 const std::locale & i_locale = std::locale(""));
213
216 {
217 bool operator() (const std::string & i_left, const std::string & i_right) const
218 {
219 return compareNoCase(i_left.c_str(), i_right.c_str()) < 0;
220 }
221 };
222
224 typedef std::map<std::string, std::string, LessNoCase> NoCaseMap;
225
227 typedef std::set<std::string, LessNoCase> NoCaseSet;
228
230 struct IRowBase
231 {
232 virtual ~IRowBase(void) noexcept = 0;
233 };
234
236 typedef std::unique_ptr<IRowBase> IRowBaseUptr;
237
239 typedef std::vector<IRowBaseUptr> IRowBaseVec;
240
242 typedef std::list<IRowBaseUptr> IRowBaseList;
243
246 {
247 virtual ~IFinalState(void) noexcept = 0;
248
250 virtual bool isFinal(void) = 0;
251
253 virtual bool isError(void) = 0;
254 };
255
257 template <class ResourceHolder>
259 {
260 public:
261
263 typedef void(ResourceHolder::*CleanupHandler)(void);
264
266 exit_guard(ResourceHolder * const i_holder, CleanupHandler i_cleanupFnc) :
267 isHold(false),
268 holder(i_holder),
269 cleanup(i_cleanupFnc)
270 { }
271
273 ~exit_guard(void) noexcept
274 {
275 try {
276 if (!isHold) (holder->*cleanup)();
277 }
278 catch (...) { }
279 }
280
282 void hold(void) noexcept { isHold = true; }
283
284 private:
285 bool isHold; // if true then hold resource
286 ResourceHolder * const holder; // resource holder class
287 CleanupHandler cleanup; // resource release method
288
289 private:
290 exit_guard(const exit_guard & i_guard) = delete;
291 exit_guard & operator=(const exit_guard & i_guard) = delete;
292 };
293}
294
295#endif // OM_H_HELPER_H
simple resource exit guard implementation
Definition: omHelper.h:259
void hold(void) noexcept
hold resource on exit instead of relesing it
Definition: omHelper.h:282
void(ResourceHolder::* CleanupHandler)(void)
resource release method of resource holder class
Definition: omHelper.h:263
~exit_guard(void) noexcept
release resource if exit guard hold() was not called
Definition: omHelper.h:273
exit_guard(ResourceHolder *const i_holder, CleanupHandler i_cleanupFnc)
create resource guard to release resource on block exit
Definition: omHelper.h:266
openM++ namespace
Definition: log.h:32
std::string formatToString(const char *i_format,...)
format message into string result using vsnprintf()
Definition: helper.cpp:421
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:440
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
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).
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:242
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:236
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...
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 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:224
std::string trim(const std::string &i_str, const std::locale &i_locale=std::locale(""))
trim leading and trailing blank and space characters
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....
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:239
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:227
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:246
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:231
case neutral less comparator for strings
Definition: omHelper.h:216