forked from udacity/CppND-System-Monitor
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathProcessContainer.h
61 lines (58 loc) · 1.71 KB
/
ProcessContainer.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
#pragma once
#include "Process.h"
#include <vector>
class ProcessContainer{
private:
std::vector<Process>_list;
public:
ProcessContainer(){
this->refreshList();
}
void refreshList();
std::string printList();
std::vector<std::vector<std::string>> getList();
};
void ProcessContainer::refreshList(){
std::vector<int> pidList = ProcessParser::getPidList();
this->_list.clear();
for(int i=0;i<pidList.size();i++){
try{
Process proc(to_string(pidList[i]));
string cmd = proc.getCmd();
if (cmd.empty())
continue;
else
this->_list.push_back(proc);
}
catch (std::runtime_error){
continue;
}
}
}
std::string ProcessContainer::printList(){
std::string result="";
for(int i=0;i<this->_list.size();i++){
result += this->_list[i].getProcess();
}
return result;
}
std::vector<std::vector<std::string> > ProcessContainer::getList(){
std::vector<std::vector<std::string>> values;
std::vector<std::string> stringifiedList;
for(int i=0; i<ProcessContainer::_list.size(); i++){
stringifiedList.push_back(ProcessContainer::_list[i].getProcess());
}
int lastIndex = 0;
for (int i=0; i<stringifiedList.size();i++){
if(i %25 == 0 && i > 0){
std::vector<std::string> sub(&stringifiedList[i-25], &stringifiedList[i]);
values.push_back(sub);
lastIndex = i;
}
if(i == (ProcessContainer::_list.size() - 1) && (i-lastIndex)<25){
std::vector<std::string> sub(&stringifiedList[lastIndex],&stringifiedList[i+1]);
values.push_back(sub);
}
}
return values;
}