forked from udacity/CppND-System-Monitor
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutil.h
111 lines (96 loc) · 3.02 KB
/
util.h
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
#pragma once
#include <string>
#include <fstream>
#include <sstream>
#include <vector>
// Classic helper function
class Util {
public:
static std::string convertToTime ( long int input_seconds );
static std::string getProgressBar(std::string percent);
static void getStream(std::string path, std::ifstream& stream);
template <typename T>
static T getItemFromStream(std::ifstream &s, std::string field, char sep=':'); //(mine) done
static std::string getStringFromStream(std::ifstream &s, std::string field, char sep=':'); //(mine) done
template <typename T>
static std::vector<T> getSpacedList(std::istringstream &line); //(mine) done
};
std::string Util::convertToTime (long int input_seconds){
long minutes = input_seconds / 60;
long hours = minutes / 60;
long seconds = int(input_seconds%60);
minutes = int(minutes%60);
std::stringstream result;
result << std::setfill('0') <<std::setw(2) << hours << ":"
<< std::setw(2) << minutes << ":"
<< std::setw(2) << seconds;
return result.str();
}
// constructing string for given percentage
// 50 bars is uniformly streched 0 - 100 %
// meaning: every 2% is one bar(|)
std::string Util::getProgressBar(std::string percent){
std::string result = "0% ";
int _size= 50;
int boundaries;
try {
boundaries = (stof(percent)/100)*_size;
} catch (...){
boundaries = 0;
}
for(int i=0;i<_size;i++){
if(i<=boundaries){
result +="|";
}
else{
result +=" ";
}
}
result +=" " + percent.substr(0,5) + "/100%";
return result;
}
// wrapper for creating streams
void Util::getStream(std::string path, std::ifstream& stream){
stream.open (path, std::ifstream::in);
if (!stream && !stream.is_open()){
stream.close();
throw std::runtime_error("Non - existing PID");
}
//return stream;
}
template <typename T>
T Util::getItemFromStream(std::ifstream &s, std::string field, char sep){
std::string line, key, value;
while(getline(s, line)){
std::istringstream ss(line);
getline(ss, key, sep);
if (key == field){
T value;
ss >> value;
return value;
}
}
}
// the function below could probably be avoided but I could not get the branching for std::string
// to use getline instead of cin to work with the above template function.
std::string Util::getStringFromStream(std::ifstream &s, std::string field, char sep){
std::string line, key, value;
while(getline(s, line)){
std::istringstream ss(line);
getline(ss, key, sep);
if (key == field){
std::string value;
getline(ss, value);
return value;
}
}
}
template <typename T>
std::vector<T> Util::getSpacedList(std::istringstream &line){
std::vector<T> v;
T val;
while (line >> val){
v.push_back(val);
}
return v;
}