-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathUtils.cpp
127 lines (112 loc) · 2.69 KB
/
Utils.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
#include "Utils.h"
string grepLineWith(string path, string contains){
string theLine = "none";
string tempLine;
ifstream stream(path);
int i = 0;
while(getline(stream, tempLine) && i <= 1000){
if (tempLine.find(contains) != string::npos) {
theLine = tempLine;
break;
}
i++;
}
return theLine;
}
int valueXInIntegersFile(string path, int x){
string xStr = grepValueXInSequence(path, x);
return stringToInt(xStr, false);
}
string grepValueXInSequence(string path, int x){
ifstream valuesStream(path);
//cout << "opening " << path << endl;
//valuesStream.open(path);
string temp, theValue;
//theValue = "not-found";
for(int i = 1; i < x; i++){
valuesStream >> temp;
//cout << "skiping value: " << temp << endl;
}
valuesStream >> theValue;
//cout << "theValue: " << theValue << endl;
//valuesStream.close();
return theValue;
}
vector<string> getProcDirs(){
vector<string> dirs;
tinydir_dir dir;
tinydir_open(&dir, "/proc");
while(dir.has_next){
tinydir_file file;
tinydir_readfile(&dir, &file);
if(file.is_dir){
string fileName = file.name;
string filePath = file.path;
try{
int idFound = stoi(fileName);
dirs.push_back(filePath);
}catch(...){
}
}
tinydir_next(&dir);
}
tinydir_close(&dir);
return dirs;
}
string cleanSpacesAndEtc(string s){
char chars[] = " :()-;\t";
for (unsigned int i = 0; i < strlen(chars); ++i)
{
char toErase = chars[i];
while(true){
int pos = s.find(toErase);
if(pos >= 0 && pos < s.size()){
s.erase(pos, 1);
}else{
break;
}
}
}
return s;
}
string removeSubstring(string s, string subs){
//cout << "Erasing " << subs << " from " << s;
int start_position_to_erase = s.find(subs);
if(start_position_to_erase >= 0
&& start_position_to_erase+subs.size() < s.size()){
s.erase(start_position_to_erase, subs.size());
}
//cout << ": " << s << endl;
return s;
}
int stringToInt(string s, bool formated){
try{
int x = stoi(s);
}catch(...){
if(formated){
cout << "[ERROR] Could not convert "
<< s << " to int;" << endl;
return -666;
}else{
return stringToInt(cleanSpacesAndEtc(s), true);
}
}
}
string getValueFromLineWithTagRemovingTag(string path, string contains)
{
string lineRaw = grepLineWith(path, contains);
string value = cleanSpacesAndEtc(removeSubstring(lineRaw, contains));
return value;
}
int getPageSizeInKB(){
return (sysconf(_SC_PAGE_SIZE)/1024);
}
string fillWithSpacesIfSmallerThan(string s, int x){
int diff = x - s.size();
if(diff > 0){
for(int i = 1; i <= diff; i++){
s = s + " ";
}
}
return s;
}