-
Notifications
You must be signed in to change notification settings - Fork 0
/
CommonFunctions.cpp
153 lines (133 loc) · 3.5 KB
/
CommonFunctions.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
/*
* CommonFunctions.cpp
*
* Created on: Feb 5, 2014
* Author: edwinrietmeijer
*/
#include "CommonFunctions.h"
namespace common {
bool isDouble( const std::string & fString ) {
std::string fromString = std::string( fString );
double f = 0.0;
std::string s = "";
std::stringstream ss;
ss << fromString;
ss >> f;
ss.clear();
ss << f;
ss >> s;
if ( f != static_cast<int>( f ) ) {
while ( fromString.back() == '0' ) {
fromString.pop_back();
}
if ( fromString.back() == '.' ) {
fromString.pop_back();
}
}
// std::cout << "fromstring: " << fromString << " f: " << f << " abs( f ): " << abs( f ) << " s: " << s << " ( fromString == s ): " << ( fromString == s ) << std::endl;
return ( fromString == s );
}
bool isInt( const std::string & fromString ) {
int i = 0;
std::string s = "";
std::stringstream ss;
ss << fromString;
ss >> i;
ss.clear();
ss << i;
ss >> s;
return ( fromString == s );
}
double stringToDouble( const std::string & fromString ) {
double f = 0.0;
std::stringstream ss;
ss << fromString;
ss >> f;
return f;
}
int stringToInt( const std::string & fromString ) {
int i = 0;
std::stringstream ss;
ss << fromString;
ss >> i;
return i;
}
std::string lower( std::string toLowerCase ) {
std::transform( toLowerCase.begin(), toLowerCase.end(), toLowerCase.begin(), ::tolower );
return toLowerCase;
}
void strVectorToLower( std::vector<std::string> & vecToConvert ) {
std::vector<std::string>:: iterator pos;
for( pos = vecToConvert.begin(); pos != vecToConvert.end(); ++pos ) {
std::transform( (*pos).begin(), (*pos).end(), (*pos).begin(), ::tolower);
}
}
std::vector<double> strVecToDoubleVec( std::vector<std::string> inVector ) {
std::vector<double> outVector;
std::vector<std::string>::iterator pos;
for( pos = inVector.begin(); pos != inVector.end(); ++pos ) {
if ( isDouble( *pos ) ) {
outVector.push_back( stringToDouble( *pos ) );
}
}
return outVector;
}
std::vector<int> strVecToIntVec( std::vector<std::string> inVector ) {
std::vector<int> outVector;
std::vector<std::string>::iterator pos;
for( pos = inVector.begin(); pos != inVector.end(); ++pos ) {
if ( isInt( *pos ) ) {
outVector.push_back( stringToInt( *pos ) );
}
}
return outVector;
}
long getFileMutateDate( std::string fileName ) {
struct stat buf;
stat( fileName.c_str() ,&buf);
// std::cout << buf.st_dev << std::endl;
// std::cout << buf.st_ino << std::endl;
// std::cout << buf.st_mode << std::endl;
// std::cout << buf.st_nlink << std::endl;
// std::cout << buf.st_uid << std::endl;
// std::cout << buf.st_gid << std::endl;
// std::cout << buf.st_rdev << std::endl;
// std::cout << buf.st_size << std::endl;
// std::cout << buf.st_blksize << std::endl;
// std::cout << buf.st_blocks << std::endl;
// std::cout << buf.st_atime << std::endl;
return buf.st_mtime;
// std::cout << buf.st_ctime << std::endl;
}
int indexMod( int index, int size ) {
size = ( int ) size;
index = ( int ) index;
int diff = 0;
if ( index < 0 ) {
diff = ( ( abs( static_cast<int> ( index ) ) ) / size ) + 1;
}
index = ( index + ( size * diff ) ) % size;
return index;
}
} // namespace common
void toConsole( std::string aString ) {
std::cout << aString << std::endl;
}
void toConsole( int anInt ) {
std::cout << anInt << std::endl;
}
void toConsole( double aDouble ) {
std::cout << aDouble << std::endl;
}
void boolToConsole( bool aBool ) {
if ( aBool ) {
std::cout << "true " << std::endl;
}
else
{
std::cout << "false " << std::endl;
}
}
void endline() {
std::cout << std::endl;
}