-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCommonFunction.cpp
72 lines (63 loc) · 2.55 KB
/
CommonFunction.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
/*
Copyright (C) 2021 David Maisonave
The RegexAssistant source code is free software. You can redistribute it and/or modify it under the terms of the GNU General Public License.
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.
*/
#include "pch.h"
#include <windows.h>
#include "CommonFunctions.h"
#ifndef EXCLUDE_BOOST_CODE
#include <boost\algorithm\string\replace.hpp>
#endif //!EXCLUDE_BOOST_CODE
namespace Common
{
std::wstring ToWString(std::string str )
{
if ( str.empty() ) return std::wstring();
int size_needed = MultiByteToWideChar( CP_UTF8, 0, &str[0], (int)str.size(), NULL, 0 );
std::wstring wstrTo( size_needed, 0 );
MultiByteToWideChar( CP_UTF8, 0, &str[0], (int)str.size(), &wstrTo[0], size_needed );
return wstrTo;
}
std::string ToString(std::wstring wstr )
{
if ( wstr.empty() ) return std::string();
int size_needed = WideCharToMultiByte( CP_UTF8, 0, &wstr[0], (int)wstr.size(), NULL, 0, NULL, NULL );
std::string strTo( size_needed, 0 );
WideCharToMultiByte( CP_UTF8, 0, &wstr[0], (int)wstr.size(), &strTo[0], size_needed, NULL, NULL );
return strTo;
}
#ifndef EXCLUDE_BOOST_CODE
void ReplaceAll( std::string &src, const std::string &searchStr, const std::string &ReplacementStr )
{
boost::replace_all( src, searchStr, ReplacementStr );
}
std::string Replace( std::string &src, const std::string &searchStr, const std::string &ReplacementStr )
{
ReplaceAll( src, searchStr, ReplacementStr );
return src;
}
std::string ReplaceInCopy(const std::string &org, const std::string &searchStr, const std::string &ReplacementStr)
{
std::string str = org;
ReplaceAll(str, searchStr, ReplacementStr);
return str;
}
#endif //!EXCLUDE_BOOST_CODE
#ifdef _AFX
void TraceDiagnosticInfo( std::string msg )
{// AfxTrace has a max buffer size of 512 bytes, which is 256 wchar_t's. Need to make sure the diagnostic info doesn't overrun the buffer by sending it MaxSectionSize characters at a time
const int MaxSectionSize = 100;
const int strsize = static_cast<int>(msg.size());
if ( strsize > MaxSectionSize )
{
#ifndef EXCLUDE_BOOST_CODE
Common::Replace( msg, "\n", " " );
Common::Replace( msg, "\r", " " );
#endif //!EXCLUDE_BOOST_CODE
}
for ( int i = 0; (i * MaxSectionSize) < strsize; ++i )
AfxTrace( i ? L"\t\t\t\t%S\n" : L"Exception Info:\t%S\n", msg.substr( i * MaxSectionSize, (i + 1) * MaxSectionSize ).c_str() );
}
#endif //_AFX
}