-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathutility.cpp
More file actions
125 lines (114 loc) · 3.26 KB
/
utility.cpp
File metadata and controls
125 lines (114 loc) · 3.26 KB
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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
/*******************************************
A Docile Sloth 2016 (adocilesloth@gmail.com)
*******************************************/
#include "utility.hpp"
#if _WIN32
#include <Windows.h>
#else
#endif
//Returns count of non-overlapping occurrences of 'sub' in 'str'
int countSubstring(const std::string& str, const std::string& sub)
{
if (sub.length() == 0 || str.length() < sub.length())
{
return 0;
}
int count = 0;
for (size_t offset = str.find(sub); offset != std::string::npos; offset = str.find(sub, offset + sub.length()))
{
count++;
}
return count;
}
int wcountSubstring(const std::wstring& str, const std::wstring& sub)
{
if (sub.length() == 0 || str.length() < sub.length())
{
return 0;
}
int count = 0;
for (size_t offset = str.find(sub); offset != std::string::npos; offset = str.find(sub, offset + sub.length()))
{
count++;
}
return count;
}
//Replace instances of 'from' in 'str' to 'to'
void ReplaceAll(std::string &str, const std::string& from, const std::string& to)
{
size_t start_pos = 0;
while((start_pos = str.find(from, start_pos)) != std::string::npos)
{
str.replace(start_pos, from.length(), to);
start_pos += to.length(); // Handles case where 'to' is a substring of 'from'
}
return;
}
void wReplaceAll(std::wstring &str, const std::wstring& from, const std::wstring& to)
{
size_t start_pos = 0;
while((start_pos = str.find(from, start_pos)) != std::string::npos)
{
str.replace(start_pos, from.length(), to);
start_pos += to.length(); // Handles case where 'to' is a substring of 'from'
}
return;
}
//Convert string to wstring and vice versa
#if _WIN32
std::wstring s2ws(const std::string& str)
{
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 ws2s(const std::wstring& wstr)
{
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;
}
#else
#include <cstring>
std::wstring s2ws(const std::string& str)
{
std::string curLocale = setlocale(LC_ALL, "");
const char* _Source = str.c_str();
size_t _Dsize = mbstowcs(NULL, _Source, 0) + 1;
wchar_t *_Dest = new wchar_t[_Dsize];
wmemset(_Dest, 0, _Dsize);
mbstowcs(_Dest,_Source,_Dsize);
std::wstring strTo = _Dest;
delete []_Dest;
setlocale(LC_ALL, curLocale.c_str());
return strTo;
}
std::string ws2s(const std::wstring& wstr)
{
std::string curLocale = setlocale(LC_ALL, "");
const wchar_t* _Source = wstr.c_str();
size_t _Dsize = wcstombs(NULL, _Source, 0) + 1;
char *_Dest = new char[_Dsize];
memset(_Dest, 0, _Dsize);
wcstombs(_Dest,_Source,_Dsize);
std::string strTo = _Dest;
delete []_Dest;
setlocale(LC_ALL, curLocale.c_str());
return strTo;
}
#endif
//Sleep function
#ifdef _WIN32
void psleep(unsigned milliseconds)
{
Sleep(milliseconds);
}
#else
#include <unistd.h>
void psleep(unsigned milliseconds)
{
usleep(milliseconds * 1000); // takes microseconds
}
#endif