-
Notifications
You must be signed in to change notification settings - Fork 0
/
util.h
58 lines (48 loc) · 1.3 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
#pragma once
#include <string>
#include <fstream>
class Util {
public:
static std::string convertToTime ( long int input_seconds );
static std::string getProgressBar(std::string percent);
static std::ifstream getStream(std::string path);
};
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::string result = std::to_string(hours) + ":" + std::to_string(minutes) + ":" + std::to_string(seconds);
return result;
}
// 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;
}
std::ifstream Util::getStream(std::string path){
std::ifstream stream(path);
if (!stream){
stream.close();
throw std::runtime_error("Non - existing PID");
}
return stream;
}