-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfunctions.cpp
181 lines (139 loc) · 3.64 KB
/
functions.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
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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
#include <stdio.h>
#include <iostream>
#include <string>
#include <vector>
#include <sstream>
#include <fstream>
#ifdef _WIN32
#include <windows.h>
#elif MACOS
#include <sys/param.h>
#include <sys/sysctl.h>
#else
#include <unistd.h>
#endif
#include <boost/thread.hpp>
#include "functions.h"
// ---------------------------------------------------------------------
int substr_count(std::string source, std::string substring)
{
int count = 0;
for (size_t pos = 0; pos < source.size(); pos += substring.size())
{
pos = source.find(substring, pos);
if (pos != std::string::npos)
{
++count;
}
else
{
break;
}
}
return count;
}
// ---------------------------------------------------------------------
std::string trim(std::string& str)
{
str.erase(0, str.find_first_not_of(' ')); //prefixing spaces
str.erase(str.find_last_not_of(' ') + 1); //surfixing spaces
return str;
}
// ---------------------------------------------------------------------
void fast_exec(std::string command)
{
boost::thread bthrd(boost::bind(exec, command));
}
// ---------------------------------------------------------------------
std::string exec(std::string command)
{
std::string excmd;
#ifdef _WIN32
FILE * f = _popen( &command[0], "r" );
#else
FILE * f = popen( &command[0], "r" );
#endif
if ( f == 0 ) {
return "";
}
const int BUFSIZE = 1000;
char buf[ BUFSIZE ];
while( fgets( buf, BUFSIZE, f ) ) {
excmd = excmd + buf;
}
#ifdef _WIN32
_pclose( f );
#else
pclose( f );
#endif
return excmd;
}
// ---------------------------------------------------------------------
std::vector<std::string> explode(std::string delimiter, std::string inputstring){
std::vector<std::string> explodes;
inputstring.append(delimiter);
while(inputstring.find(delimiter)!=std::string::npos){
explodes.push_back(inputstring.substr(0, inputstring.find(delimiter)));
inputstring.erase(inputstring.begin(), inputstring.begin()+inputstring.find(delimiter)+delimiter.size());
}
return explodes;
}
// ---------------------------------------------------------------------
std::string implode(std::string delimiter, std::vector<std::string> & elements)
{
std::string full;
for (std::vector<std::string>::iterator it = elements.begin(); it != elements.end(); ++it)
{
full += (*it);
if (it != elements.end()-1)
full += delimiter;
}
return full;
}
// ---------------------------------------------------------------------
bool file_exists(std::string file_name)
{
std::ifstream f(file_name.c_str());
if (f.good()) {
f.close();
return true;
} else {
f.close();
return false;
}
}
// ---------------------------------------------------------------------
/**
* Ïîëó÷àåò êîëè÷åñòâî ÿäåð
*/
int get_cores_count() {
#ifdef WIN32
SYSTEM_INFO sysinfo;
GetSystemInfo(&sysinfo);
return sysinfo.dwNumberOfProcessors;
#elif MACOS
int nm[2];
size_t len = 4;
uint32_t count;
nm[0] = CTL_HW; nm[1] = HW_AVAILCPU;
sysctl(nm, 2, &count, &len, NULL, 0);
if(count < 1) {
nm[1] = HW_NCPU;
sysctl(nm, 2, &count, &len, NULL, 0);
if(count < 1) { count = 1; }
}
return count;
#else
return sysconf(_SC_NPROCESSORS_ONLN);
#endif
}
// ---------------------------------------------------------------------
bool in_array(const std::string &needle, const std::vector< std::string > &haystack)
{
int max=haystack.size();
if (max==0) return false;
for(int i=0; i<max; i++)
if (haystack[i]==needle)
return true;
return false;
}