37 #ifndef FIX8_UTILS_HPP_
38 #define FIX8_UTILS_HPP_
46 #include <Poco/DateTime.h>
47 #include <Poco/Net/SocketAddress.h>
50 # include <sys/ioctl.h>
56 #if (FIX8_REGEX_SYSTEM == FIX8_REGEX_REGEX_H)
58 #elif (FIX8_REGEX_SYSTEM == FIX8_REGEX_POCO)
59 #include <Poco/RegularExpression.h>
60 #include <Poco/Exception.h>
66 #define STRINGOF(x) #x
67 #define STRINGIFY(x) STRINGOF(x)
68 #define FILE_LINE __FILE__ ":" STRINGIFY(__LINE__)
119 F8API const std::string&
GetTimeAsStringMS(std::string& result,
const class Tickval *tv=0,
const unsigned dplaces=6,
bool use_gm=
false);
153 inline std::string
trim(
const std::string& source,
const std::string& ws=
" \t")
155 const size_t bgstr(source.find_first_not_of(ws));
156 return bgstr == std::string::npos
157 ? source : source.substr(bgstr, source.find_last_not_of(ws) - bgstr + 1);
164 inline const std::string&
trim(std::string& source,
const std::string& ws=
" \t")
166 const size_t bgstr(source.find_first_not_of(ws));
167 return bgstr == std::string::npos
168 ? source : source = source.substr(bgstr, source.find_last_not_of(ws) - bgstr + 1);
203 inline T
rotl(
const T val,
const int times) {
return val << times | val >> (
sizeof(T) * 8 - times); }
211 inline T
rotr(
const T val,
const int times) {
return val >> times | val << (
sizeof(T) * 8 - times); }
218 unsigned int hash(0);
220 for (
const auto& pp : str)
223 hash -=
rotl(hash, 13);
230 inline unsigned rothash(
unsigned result,
unsigned value)
233 return result ^= (result >> 2) ^ (result << 5) ^ (result << 13) ^ value ^ 0x80001801;
244 template<
typename _CharT,
typename _Traits,
typename _Alloc>
245 inline bool operator% (
const std::basic_string<_CharT, _Traits, _Alloc>& __lhs,
246 const std::basic_string<_CharT, _Traits, _Alloc>& __rhs)
249 return _stricmp(__lhs.c_str(), __rhs.c_str()) == 0;
251 return strcasecmp(__lhs.c_str(), __rhs.c_str()) == 0;
262 template<
typename _CharT,
typename _Traits,
typename _Alloc>
263 inline bool operator% (
const _CharT* __lhs,
const std::basic_string<_CharT, _Traits, _Alloc>& __rhs)
264 {
return strcasecmp(__lhs, __rhs.c_str()) == 0; }
273 template<
typename _CharT,
typename _Traits,
typename _Alloc>
274 inline bool operator% (
const std::basic_string<_CharT, _Traits, _Alloc>& __lhs,
const _CharT* __rhs)
277 return _stricmp(__lhs.c_str(), __rhs) == 0;
279 return strcasecmp(__lhs.c_str(), __rhs) == 0;
290 template<
typename _CharT,
typename _Traits,
typename _Alloc>
291 inline bool operator^ (
const std::basic_string<_CharT, _Traits, _Alloc>& __lhs,
292 const std::basic_string<_CharT, _Traits, _Alloc>& __rhs)
295 return _stricmp(__lhs.c_str(), __rhs.c_str()) < 0;
297 return strcasecmp(__lhs.c_str(), __rhs.c_str()) < 0;
310 #if FIX8_REGEX_SYSTEM == FIX8_REGEX_REGEX_H
315 #elif FIX8_REGEX_SYSTEM == FIX8_REGEX_POCO
316 Poco::RegularExpression::MatchVec _matchVec;
322 #if FIX8_REGEX_SYSTEM == FIX8_REGEX_REGEX_H
334 #if FIX8_REGEX_SYSTEM == FIX8_REGEX_REGEX_H
336 #elif FIX8_REGEX_SYSTEM == FIX8_REGEX_POCO
337 return static_cast<unsigned>(_matchVec.size());
346 #if FIX8_REGEX_SYSTEM == FIX8_REGEX_REGEX_H
347 return which < subCnt_ ? subexprs_[which].rm_eo - subexprs_[which].rm_so : -1;
348 #elif FIX8_REGEX_SYSTEM == FIX8_REGEX_POCO
349 return which < static_cast<int>(_matchVec.size()) ? _matchVec[which].length : -1;
358 #if FIX8_REGEX_SYSTEM == FIX8_REGEX_REGEX_H
359 return which < subCnt_ ? subexprs_[which].rm_so : -1;
360 #elif FIX8_REGEX_SYSTEM == FIX8_REGEX_POCO
361 return which < static_cast<int>(_matchVec.size()) ? static_cast<int>(_matchVec[which].offset) : -1;
373 #if FIX8_REGEX_SYSTEM == FIX8_REGEX_REGEX_H
378 #elif FIX8_REGEX_SYSTEM == FIX8_REGEX_POCO
379 Poco::RegularExpression * _regexp;
388 RegExp(
const char *pattern,
const int flags=0)
389 #if FIX8_REGEX_SYSTEM == FIX8_REGEX_REGEX_H
392 if ((errCode_ = regcomp(®_, pattern_.c_str(), REG_EXTENDED|flags)) != 0)
399 #elif FIX8_REGEX_SYSTEM == FIX8_REGEX_POCO
404 _regexp =
new Poco::RegularExpression(pattern, flags,
true);
406 catch(
const Poco::RegularExpressionException& ex)
408 errCode_ = ex.code();
409 errString = ex.message();
417 #if FIX8_REGEX_SYSTEM == FIX8_REGEX_REGEX_H
420 #elif FIX8_REGEX_SYSTEM == FIX8_REGEX_POCO
433 #if FIX8_REGEX_SYSTEM == FIX8_REGEX_REGEX_H
439 #elif FIX8_REGEX_SYSTEM == FIX8_REGEX_POCO
440 match._matchVec.clear();
441 return _regexp ? _regexp->match(source, offset, match._matchVec) : 0;
452 static std::string&
SubExpr(
RegMatch& match,
const std::string& source, std::string& target,
const int offset=0,
const int num=0)
454 #if FIX8_REGEX_SYSTEM == FIX8_REGEX_REGEX_H
457 #elif FIX8_REGEX_SYSTEM == FIX8_REGEX_POCO
458 if (num < static_cast<int>(match.
SubCnt()))
459 target = source.substr(offset + match.
SubPos(num), match.
SubSize(num));
471 static std::string&
Erase(
RegMatch& match, std::string& source,
const int num=0)
473 #if FIX8_REGEX_SYSTEM == FIX8_REGEX_REGEX_H
476 #elif FIX8_REGEX_SYSTEM == FIX8_REGEX_POCO
477 if (num < static_cast<int>(match.
SubCnt()))
489 static std::string&
Replace(
RegMatch& match, std::string& source,
const std::string& with,
const int num=0)
491 #if FIX8_REGEX_SYSTEM == FIX8_REGEX_REGEX_H
494 #elif FIX8_REGEX_SYSTEM == FIX8_REGEX_POCO
495 if (num < static_cast<int>(match.
SubCnt()))
507 static std::string&
Replace(
RegMatch& match, std::string& source,
const char with,
const int num=0)
509 #if FIX8_REGEX_SYSTEM == FIX8_REGEX_REGEX_H
512 #elif FIX8_REGEX_SYSTEM == FIX8_REGEX_POCO
513 if (num < static_cast<int>(match.
SubCnt()))
514 source.replace(match.
SubPos(num), match.
SubSize(num), 1, with);
533 operator void*() {
return errCode_ ? 0 :
this; }
540 bool operator()(
const std::string *a,
const std::string *b)
const
547 bool operator()(
const std::string *a,
const std::string *b)
const
554 bool operator()(
const std::string& a,
const std::string& b)
const
566 std::istringstream istr(source);
577 inline unsigned get_value(
const std::string& source) {
return std::stoul(source); }
584 inline int get_value(
const std::string& source) {
return std::stoi(source); }
591 inline double get_value(
const std::string& source) {
return std::stod(source); }
598 inline float get_value(
const std::string& source) {
return std::stof(source); }
609 #if !defined FIX8_XMLENTITY_STRICT_BOOL
610 return source %
"true" || source %
"yes" || source %
"y" || source ==
"1";
613 std::istringstream istr(source);
614 istr >> std::boolalpha >> result;
629 for (; *str != term; ++str)
630 retval = (retval << 3) + (retval << 1) + *str -
'0';
647 inline size_t itoa(T value,
char *result,
int base)
650 if (base < 2 || base > 36)
656 char *ptr(result), *ptr1(result);
663 *ptr++ =
"zyxwvutsrqponmlkjihgfedcba9876543210123456789abcdefghijklmnopqrstuvwxyz" [35 + (tmp_value - value * base)];
673 const char tmp_char(*ptr);
677 return ::strlen(result);
690 if (base < 2 || base > 36)
696 char *ptr(result), *ptr1(result);
697 unsigned int tmp_value;
703 *ptr++ =
"zyxwvutsrqponmlkjihgfedcba9876543210123456789abcdefghijklmnopqrstuvwxyz" [35 + (tmp_value - value * base)];
710 const char tmp_char(*ptr);
714 return ::strlen(result);
730 fp_type sign(1.), value(0.), scale(1.);
747 value = value * 10. + (*p -
'0');
758 value += (*p -
'0') / mpow10;
765 if (toupper(*p) ==
'E')
767 unsigned int expon(0);
782 expon = expon * 10 + (*p -
'0');
785 #if defined FIX8_USE_SINGLE_PRECISION
813 return sign * (frac ? (value / scale) : (value * scale));
821 extern "C" {
size_t modp_dtoa(
double value,
char* str,
int prec); }
825 constexpr
unsigned bitsum() {
return 0; }
832 template <
typename T,
typename... Args>
833 constexpr
unsigned bitsum(T value, Args... args)
834 {
return 1 << value |
bitsum(args...); }
841 template<
typename T,
typename B=
unsigned int>
861 explicit ebitset(
const T sbit) : a_((1 << sbit) - 1) {}
886 void set(
const T sbit,
bool on=
true) {
if (on) a_ |= 1 << sbit;
else a_ &= ~(1 << sbit); }
898 int set(
const std::vector<std::string>& sset,
const std::string& what,
bool ignorecase,
bool on=
true)
900 auto itr(sset.cbegin());
901 for (; itr != sset.cend(); ++itr)
902 if (ignorecase ?
trim(*itr) % what :
trim(*itr) == what)
904 if (itr == sset.cend())
906 const int dist(std::distance(sset.cbegin(), itr));
907 set(static_cast<T>(dist), on);
913 void clear(
const T sbit) { a_ &= ~(1 << sbit); }
920 void setall(
const T sbit) { a_ = (1 << sbit) - 1; }
940 template<
typename T,
typename B=
unsigned int>
960 explicit ebitset_r(
const T sbit) { a_ = (1 << sbit) - 1; }
985 void set(
const T sbit,
bool on=
true) {
if (on) a_ |= 1 << sbit;
else a_ &= ~(1 << sbit); }
993 bool set(
const std::vector<std::string>& sset,
const std::string& what,
bool ignorecase=
false,
bool on=
true)
995 auto itr(sset.cbegin());
996 for (; itr != sset.cend(); ++itr)
997 if (ignorecase ?
trim(*itr) % what :
trim(*itr) == what)
999 if (itr == sset.cend())
1001 set(static_cast<T>(std::distance(sset.cbegin(), itr)), on);
1018 void setall(
const T sbit) { a_ = (1 << sbit) - 1; }
1047 template<
typename T>
1048 T
enum_str_get(
const std::vector<std::string>& sset,
const std::string& what,
const T def,
bool ignorecase=
false)
1052 auto itr(sset.cbegin());
1053 for (; itr != sset.cend(); ++itr)
1054 if (ignorecase ? *itr % what : *itr == what)
1056 return itr == sset.cend() ? def :
static_cast<T
>(std::distance(sset.begin(), itr));
1068 inline bool exist(
const std::string& fname)
1071 return _access(fname.c_str(), 0) == 0;
1073 return access(fname.c_str(), F_OK) == 0;
1084 std::string::size_type slpos(source.find_last_of(
"/\\"));
1085 if (slpos == std::string::npos)
1089 filepart.assign(source.substr(slpos + 1));
1090 dirpart.assign(source.substr(0, slpos));
1100 inline char *
CopyString(
const std::string& src,
char *target,
unsigned limit=0)
1104 const unsigned sz(limit && static_cast<unsigned>(src.size()) > limit ? limit : (
unsigned)src.size() + 1);
1105 src.copy(target, sz - 1);
1114 template <
typename T>
1146 if (gptr() < egptr())
1148 int put_back_cnt(gptr() - eback());
1151 memcpy(_buffer + (
_back_limit - put_back_cnt), gptr() - put_back_cnt, put_back_cnt);
1202 if (ioctl(_fd, TIOCSETA, &_tty_state) < 0)
1204 if (ioctl(_fd, TCSETA, &_tty_state) < 0)
1206 std::cerr <<
Str_error(errno,
"Cannot reset ioctl") << std::endl;
1219 if (ioctl(_fd, TIOCGETA, &_tty_state) < 0)
1221 if (ioctl(_fd, TCGETA, &_tty_state) < 0)
1224 std::cerr <<
Str_error(errno,
"Cannot get ioctl") << std::endl;
1228 termios tty_state(_tty_state);
1230 termio tty_state(_tty_state);
1232 tty_state.c_lflag = 0;
1233 tty_state.c_cc[VTIME] = 0;
1234 tty_state.c_cc[VMIN] = 1;
1236 if (ioctl(_fd, TIOCSETA, &tty_state) < 0)
1238 if (ioctl(_fd, TCSETA, &tty_state) < 0)
1240 std::cerr <<
Str_error(errno,
"Cannot reset ioctl") << std::endl;
1256 filestdin(std::istream *ifs,
bool nodel=
false) : ifs_(ifs), nodel_(nodel) {}
1263 #if defined POCO_VERSION && POCO_VERSION <= 0x01040100
1264 inline bool operator==(
const Poco::Net::SocketAddress &a,
const Poco::Net::SocketAddress &b)
1266 return a.host() == b.host() && a.port() == b.port();
1269 inline bool operator!=(
const Poco::Net::SocketAddress &a,
const Poco::Net::SocketAddress &b)
1278 #endif // _F8_UTILS_HPP_
bool set(const std::vector< std::string > &sset, const std::string &what, bool ignorecase=false, bool on=true)
int set(const std::vector< std::string > &sset, const std::string &what, bool ignorecase, bool on=true)
unsigned SubPos(const int which=0) const
F8API const std::string & GetTimeAsStringMini(std::string &result, const Tickval *tv)
size_t SubSize(const int which=0) const
filestdin(std::istream *ifs, bool nodel=false)
static std::string & SubExpr(RegMatch &match, const std::string &source, std::string &target, const int offset=0, const int num=0)
F8API std::string Str_error(const int err, const char *str=0)
size_t modp_dtoa(double value, char *str, int prec)
Convert double to ascii.
POSIX regex wrapper class.
regmatch_t subexprs_[SubLimit_]
size_t itoa(T value, char *result, int base)
Fast itoa.
const string & filepart(const string &source, string &where)
F8API std::string & InPlaceReplaceInSet(const std::string &iset, std::string &src, const char repl='_')
std::string trim(const std::string &source, const std::string &ws=" \t")
Singleton & operator=(const Singleton &)=delete
void operator|=(const T sbit)
F8API void create_path(const std::string &path)
const std::string & ErrString() const
fp_type fast_atof(const char *p)
ebitset_r(const ebitset_r< T, B > &from)
Create a streambuf from an open file descriptor.
char _buffer[_buffer_size]
std::istream & operator()()
F8API std::string StrToLower(const std::string &src)
bool operator^(const std::basic_string< _CharT, _Traits, _Alloc > &__lhs, const std::basic_string< _CharT, _Traits, _Alloc > &__rhs)
unsigned rothash(unsigned result, unsigned value)
void setall(const T sbit)
virtual int_type underflow()
void operator|=(const T sbit)
static std::string & Replace(RegMatch &match, std::string &source, const char with, const int num=0)
tty_save_state & operator=(const tty_save_state &from)
constexpr unsigned bitsum()
empty argument version
static std::string & Replace(RegMatch &match, std::string &source, const std::string &with, const int num=0)
Case-insensitive string comparison.
virtual ~Singleton()
Dtor.
ebitset_r(const integral_type a)
unsigned short integral_type
const std::string pattern_
integral_type operator&(const T sbit) const
void set(const integral_type bset)
F8API f8String find_package_info_string(const f8String &what)
ebitset< T, B > & operator=(const ebitset< T, B > &that)
F8API int decode_dow(const std::string &from)
T fast_atoi(const char *str, const char term='\0')
Case-sensitive string comparison, pointer version.
bool operator()(const std::string &a, const std::string &b) const
F8API const Package_info & package_info()
T rotr(const T val, const int times)
void set(const T sbit, bool on=true)
void operator&=(const T sbit)
T rotl(const T val, const int times)
integral_type has(const T sbit) const
bool operator()(const std::string *a, const std::string *b) const
RegExp(const char *pattern, const int flags=0)
F8API const std::string & GetTimeAsStringMS(std::string &result, const class Tickval *tv=0, const unsigned dplaces=6, bool use_gm=false)
int SearchString(RegMatch &match, const std::string &source, const int subExpr, const int offset=0) const
void clearall()
Clear all bits.
virtual ~RegExp()
Dtor. Destroys internal compiled expression.
T enum_str_get(const std::vector< std::string > &sset, const std::string &what, const T def, bool ignorecase=false)
std::atomic< T > f8_atomic
T get_value(const std::string &source)
ebitset_r & operator<<(const T sbit)
char * CopyString(const std::string &src, char *target, unsigned limit=0)
static std::string & Erase(RegMatch &match, std::string &source, const int num=0)
unsigned int integral_type
ebitset & operator<<(const T sbit)
F8API std::string & InPlaceStrToUpper(std::string &src)
F8API std::string & InPlaceStrToLower(std::string &src)
void split_path(const std::string &source, std::string &filepart, std::string &dirpart)
const std::string & GetPattern() const
bool operator%(const std::basic_string< _CharT, _Traits, _Alloc > &__lhs, const std::basic_string< _CharT, _Traits, _Alloc > &__rhs)
F8API std::string & CheckAddTrailingSlash(std::string &source)
ebitset(const integral_type a)
integral_type has(const T sbit) const
Abstract file or stdin input.
A class to contain regex matches using RegExp.
void set(const integral_type bset)
void clearall()
Clear all bits.
f8_atomic< integral_type > a_
size_t itoa< unsigned int >(unsigned int value, char *result, int base)
Fast itoa - unsigned int specialisation.
void setall(const T sbit)
void set(const T sbit, bool on=true)
ebitset_r< T, B > & operator=(const ebitset_r< T, B > &that)
bool exist(const std::string &fname)
unsigned ROT13Hash(const std::string &str)
bool operator()(const std::string *a, const std::string *b) const
std::map< f8String, f8String > Package_info
ebitset(const ebitset< T, B > &from)
Case-insensitive string comparison, pointer version.
integral_type operator&(const T sbit) const