-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutil.h
More file actions
51 lines (44 loc) · 1.45 KB
/
util.h
File metadata and controls
51 lines (44 loc) · 1.45 KB
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
#pragma once
#include<fstream>
#include<string>
class Util{
public:
static std::ifstream getStream(std::string path);
static std::string convertTime(long int input_seconds);
static std::string getProgressBar(std::string percent);
};
std::ifstream Util::getStream(std::string path){
std::ifstream stream(path);
if(!stream){
throw std::runtime_error("Non -existing PID");
}
return stream;
}
std::string Util::convertTime(long int input_seconds){
long hours = input_seconds / 3600;
long minutes = (input_seconds % 3600) / 60;
long seconds = int(input_seconds%60);
std::string result = std::to_string(hours) + ":" + std::to_string(minutes)+":"+std::to_string(seconds);
return result;
}
std::string Util::getProgressBar(std::string percent){
try {
float percentValue = stof(percent);
if(percentValue < 0) percentValue = 0;
if(percentValue > 100) percentValue = 100;
char buffer[16];
snprintf(buffer, sizeof(buffer), "%.1f", percentValue);
std::string result = std::string(buffer) + "% ";
int _size = 50;
int boundaries = (percentValue / 100.0) * _size;
for(int i=0; i<_size; ++i){
if(i < boundaries)
result += "|";
else
result += " ";
}
return result;
} catch(...) {
return "0% [ERROR]";
}
}