|
| 1 | +/* |
| 2 | + Copyright (C) 2021 David Maisonave |
| 3 | + The RegexAssistant source code is free software. You can redistribute it and/or modify it under the terms of the GNU General Public License. |
| 4 | + This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. |
| 5 | +*/ |
| 6 | +#pragma once |
| 7 | +#include <string> |
| 8 | +#include <memory> |
| 9 | +#include <mutex> |
| 10 | +#include <algorithm> |
| 11 | +#include <cctype> |
| 12 | +#include <locale> |
| 13 | + |
| 14 | +namespace Common |
| 15 | +{ |
| 16 | + std::wstring ToWString(std::string src); |
| 17 | + inline std::wstring ToWString( const std::wstring &src ) { return src; } |
| 18 | + inline std::wstring ToWString( const wchar_t* src ) { return std::wstring( src ); } |
| 19 | + |
| 20 | + std::string ToString( std::wstring src); |
| 21 | + inline std::string ToString( const std::string &src ) { return src; } |
| 22 | + inline std::string ToString( const wchar_t* src ) { return ToString( std::wstring(src) ); } |
| 23 | + |
| 24 | + inline std::string ToString( int src ) { return std::to_string( src ); } |
| 25 | + inline std::string ToString( long src ) { return std::to_string( src ); } |
| 26 | + inline std::string ToString( long long src ) { return std::to_string( src ); } |
| 27 | + inline std::string ToString( unsigned src ) { return std::to_string( src ); } |
| 28 | + inline std::string ToString( unsigned long src ) { return std::to_string( src ); } |
| 29 | + inline std::string ToString( unsigned long long src ) { return std::to_string( src ); } |
| 30 | + inline std::string ToString( float src ) { return std::to_string( src ); } |
| 31 | + inline std::string ToString( double src ) { return std::to_string( src ); } |
| 32 | + inline std::string ToString( long double src ) { return std::to_string( src ); } |
| 33 | + |
| 34 | + inline std::wstring ToWString( int src ) { return std::to_wstring( src ); } |
| 35 | + inline std::wstring ToWString( long src ) { return std::to_wstring( src ); } |
| 36 | + inline std::wstring ToWString( long long src ) { return std::to_wstring( src ); } |
| 37 | + inline std::wstring ToWString( unsigned src ) { return std::to_wstring( src ); } |
| 38 | + inline std::wstring ToWString( unsigned long src ) { return std::to_wstring( src ); } |
| 39 | + inline std::wstring ToWString( unsigned long long src ) { return std::to_wstring( src ); } |
| 40 | + inline std::wstring ToWString( float src ) { return std::to_wstring( src ); } |
| 41 | + inline std::wstring ToWString( double src ) { return std::to_wstring( src ); } |
| 42 | + inline std::wstring ToWString( long double src ) { return std::to_wstring( src ); } |
| 43 | + |
| 44 | + template<class T> |
| 45 | + std::wstring ToStr( std::wstring dest, T src ) |
| 46 | + { |
| 47 | + dest = std::to_wstring( src ); |
| 48 | + return dest; |
| 49 | + } |
| 50 | + template<class T> |
| 51 | + std::string ToStr( std::string dest, T src ) |
| 52 | + { |
| 53 | + dest = std::to_string( src ); |
| 54 | + return dest; |
| 55 | + } |
| 56 | + |
| 57 | + // trim from start (in place) |
| 58 | + inline void ltrim(std::string &s) { |
| 59 | + s.erase(s.begin(), std::find_if(s.begin(), s.end(), [](unsigned char ch) { |
| 60 | + return !std::isspace(ch); |
| 61 | + })); |
| 62 | + } |
| 63 | + |
| 64 | + // trim from end (in place) |
| 65 | + inline void rtrim(std::string &s) { |
| 66 | + s.erase(std::find_if(s.rbegin(), s.rend(), [](unsigned char ch) { |
| 67 | + return !std::isspace(ch); |
| 68 | + }).base(), s.end()); |
| 69 | + } |
| 70 | + |
| 71 | + // trim from both ends (in place) |
| 72 | + inline void trim(std::string &s) { |
| 73 | + ltrim(s); |
| 74 | + rtrim(s); |
| 75 | + } |
| 76 | + |
| 77 | + // trim from start (copying) |
| 78 | + inline std::string ltrim_copy(std::string s) { |
| 79 | + ltrim(s); |
| 80 | + return s; |
| 81 | + } |
| 82 | + |
| 83 | + // trim from end (copying) |
| 84 | + inline std::string rtrim_copy(std::string s) { |
| 85 | + rtrim(s); |
| 86 | + return s; |
| 87 | + } |
| 88 | + |
| 89 | + // trim from both ends (copying) |
| 90 | + inline std::string trim_copy(std::string s) { |
| 91 | + trim(s); |
| 92 | + return s; |
| 93 | + } |
| 94 | + |
| 95 | + // trim from end of string (right) |
| 96 | + inline std::string& rtrim(std::string& s, const char* t_str) |
| 97 | + { |
| 98 | + s.erase(s.find_last_not_of(t_str) + 1); |
| 99 | + return s; |
| 100 | + } |
| 101 | + |
| 102 | + // trim from beginning of string (left) |
| 103 | + inline std::string& ltrim(std::string& s, const char* t_str) |
| 104 | + { |
| 105 | + s.erase(0, s.find_first_not_of(t_str)); |
| 106 | + return s; |
| 107 | + } |
| 108 | + |
| 109 | + // trim from both ends of string (right then left) |
| 110 | + inline std::string& trim(std::string& s, const char* t_str) |
| 111 | + { |
| 112 | + return ltrim(rtrim(s, t_str), t_str); |
| 113 | + } |
| 114 | +#ifdef _AFX |
| 115 | + inline CString ToCString(const char * src) { return ToWString(src).c_str(); } |
| 116 | + inline CString ToCString(const wchar_t * src) { return src; } |
| 117 | + |
| 118 | + inline CString ToCString( int src ) { return std::to_wstring( src ).c_str(); } |
| 119 | + inline CString ToCString( long src ) { return std::to_wstring( src ).c_str(); } |
| 120 | + inline CString ToCString( long long src ) { return std::to_wstring( src ).c_str(); } |
| 121 | + inline CString ToCString( unsigned src ) { return std::to_wstring( src ).c_str(); } |
| 122 | + inline CString ToCString( unsigned long src ) { return std::to_wstring( src ).c_str(); } |
| 123 | + inline CString ToCString( unsigned long long src ) { return std::to_wstring( src ).c_str(); } |
| 124 | + inline CString ToCString( float src ) { return std::to_wstring( src ).c_str(); } |
| 125 | + inline CString ToCString( double src ) { return std::to_wstring( src ).c_str(); } |
| 126 | + inline CString ToCString( long double src ) { return std::to_wstring( src ).c_str(); } |
| 127 | + inline size_t StrLen(const CString &str) { return static_cast <size_t>(str.GetLength()); } |
| 128 | + inline DWORD StrDwLen( const CString &str ) { return static_cast<DWORD>(str.GetLength()); } |
| 129 | + void TraceDiagnosticInfo( std::string msg ); |
| 130 | +#endif //_AFX |
| 131 | + |
| 132 | +#ifndef EXCLUDE_BOOST_CODE |
| 133 | + std::string ReplaceInCopy(const std::string &src, const std::string &searchStr, const std::string &ReplacementStr); |
| 134 | + std::string Replace(std::string &src, const std::string &searchStr, const std::string &ReplacementStr); |
| 135 | + void ReplaceAll( std::string &src, const std::string &searchStr, const std::string &ReplacementStr ); |
| 136 | +#endif //!EXCLUDE_BOOST_CODE |
| 137 | + |
| 138 | + template <class strType1, class strType2> bool MatchesNoCase(strType1 str1, strType2 str2) |
| 139 | + { |
| 140 | + std::wstring wstr1 = ToWString(str1); |
| 141 | + std::wstring wstr2 = ToWString(str2); |
| 142 | + return (0 == _wcsicmp(wstr1.c_str(), wstr2.c_str())); |
| 143 | + } |
| 144 | + |
| 145 | + inline size_t StrLen(const char * str) { return strlen(str); } |
| 146 | + inline size_t StrLen(const wchar_t * str) { return wcslen(str); } |
| 147 | + |
| 148 | +#ifdef _WINBASE_ |
| 149 | +#define GETLASTERROR__ GetLastError() |
| 150 | +#else |
| 151 | +#include <cstddef> |
| 152 | +#define GETLASTERROR__ GetErrNo() |
| 153 | + int GetErrNo() |
| 154 | + { |
| 155 | + int Err = 0; |
| 156 | + _get_errno(&Err); |
| 157 | + return Err; |
| 158 | + } |
| 159 | + using DWORD = unsigned long; |
| 160 | +#endif |
| 161 | + |
| 162 | + inline DWORD StrDwLen( const char * str ) { return static_cast<DWORD>(strlen( str )); } |
| 163 | + inline DWORD StrDwLen( const wchar_t * str ) { return static_cast<DWORD>(wcslen( str )); } |
| 164 | + |
| 165 | + class errno_exception |
| 166 | + { |
| 167 | + int m_errno; |
| 168 | + std::string m_what; |
| 169 | + std::exception *m_exception; |
| 170 | + std::string m_name; |
| 171 | + public: |
| 172 | + errno_exception( const char *name, std::exception &Exception, int no = GETLASTERROR__, std::string s = "" ) :m_errno( no ), m_what( s.size() ? s : Exception.what() ), m_exception( &Exception ), m_name( name ) {} |
| 173 | + errno_exception( const char *s = NULL, int no = GETLASTERROR__ ) :m_errno( no ), m_what( (s == NULL) ? "Unknown Exception" : s ), m_exception( NULL ), m_name( (s == NULL) ? "Unknown Exception" : "Exception" ) {} |
| 174 | + errno_exception( std::string &s, int no = GETLASTERROR__ ) :m_errno( no ), m_what( s ), m_exception( NULL ), m_name( "Exception" ) {} |
| 175 | + const char* what() { return (m_exception == NULL) ? m_what.c_str() : m_exception->what(); } |
| 176 | + int code() { return m_errno; } |
| 177 | + const char* name() { return m_name.c_str(); } |
| 178 | + }; |
| 179 | + |
| 180 | + class auto_lock |
| 181 | + { |
| 182 | + std::mutex mtx; |
| 183 | + public: |
| 184 | + auto_lock(){ mtx.lock(); } |
| 185 | + ~auto_lock(){ mtx.unlock(); } |
| 186 | + }; |
| 187 | + |
| 188 | + |
| 189 | +} |
| 190 | + |
| 191 | +#define REPORT_ERR_AND_EXIT(exit_err_no, msg_format, ...) {CString msg; msg.Format(_T("Error: " msg_format " Performming early exit due to error."),__VA_ARGS__);AfxMessageBox(msg);exit(exit_err_no);} |
| 192 | +#define ERR_LOG(msg_format, ... ) {Common::Logging logging(__FILE__, __LINE__, __FUNCTION__, Common::Verbosity::ERR, msg_format __VA_OPT__(,) __VA_ARGS__);} |
| 193 | + |
0 commit comments