Skip to content

Commit 8f46ccd

Browse files
textanalyticsmantextanalyticsman
textanalyticsman
authored and
textanalyticsman
committed
Fixing recommendations from coding review Udacity
1 parent dd6e87e commit 8f46ccd

File tree

3 files changed

+39
-25
lines changed

3 files changed

+39
-25
lines changed

src/format.cpp

+9-3
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
#include <string>
2+
#include <iomanip>
23

34
#include "format.h"
45

@@ -19,8 +20,13 @@ string Format::ElapsedTime(long seconds)
1920
seconds %= 60;
2021

2122
long int tseconds{ seconds };
22-
23-
string result { std::to_string(tdays) + ":" + std::to_string(thours) + ":" + std::to_string(tminutes) + ":" + std::to_string(tseconds)};
2423

25-
return result;
24+
// I am using this answer https://knowledge.udacity.com/questions/155686
25+
std::ostringstream stream;
26+
stream << std::setw(2) << std::setfill('0') << tdays << ":"
27+
<<std::setw(2) << std::setfill('0') << thours << ":"
28+
<< std::setw(2) << std::setfill('0') << tminutes << ":"
29+
<< std::setw(2) << std::setfill('0') << tseconds;
30+
31+
return stream.str();
2632
}

src/linux_parser.cpp

+29-21
Original file line numberDiff line numberDiff line change
@@ -372,34 +372,42 @@ string LinuxParser::User(int pid)
372372
}
373373

374374
// Read and return the uptime of a process
375+
// This implementation only works with kernel > 2.6
376+
// Otherwise it will retur zero.
375377
long LinuxParser::UpTime(int pid)
376-
{
378+
{
379+
std::string kernel{ LinuxParser::Kernel() };
380+
float kernel_version{ std::stof(kernel.substr(0,3)) };
377381
long int uptime{ 0 };
378-
std::string line;
379382

380-
std::ifstream filestream(kProcDirectory + std::to_string(pid) + kStatFilename);
381-
382-
if (filestream.is_open())
383-
std::getline(filestream, line);
384-
385-
// Sometimes there are process that dies and then the pseudo file disappears, which causes errors.
386-
// Thus, if no data is detected, the analysis is by passed
387-
if(line != "")
383+
if (kernel_version > 2.6)
388384
{
389-
std::istringstream linestream{ line };
390-
std::string tmp;
391-
int index{0};
385+
std::string line;
392386

393-
while (linestream >> tmp)
394-
{
395-
++index;
396-
if(index == 22)
397-
break;
398-
}
387+
std::ifstream filestream(kProcDirectory + std::to_string(pid) + kStatFilename);
399388

400-
long int hertz{ sysconf(_SC_CLK_TCK) };
389+
if (filestream.is_open())
390+
std::getline(filestream, line);
391+
392+
// Sometimes there are process that dies and then the pseudo file disappears, which causes errors.
393+
// Thus, if no data is detected, the analysis is by passed
394+
if(line != "")
395+
{
396+
std::istringstream linestream{ line };
397+
std::string tmp;
398+
int index{0};
401399

402-
uptime = std::stol(tmp) / hertz;
400+
while (linestream >> tmp)
401+
{
402+
++index;
403+
if(index == 22)
404+
break;
405+
}
406+
407+
long int hertz{ sysconf(_SC_CLK_TCK) };
408+
409+
uptime = UpTime() - std::stol(tmp) / hertz;
410+
}
403411
}
404412

405413
return uptime;

src/ncurses_display.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ void NCursesDisplay::DisplayProcesses(std::vector<Process>& processes,
6060
int const cpu_column{19};
6161
int const ram_column{29};
6262
int const time_column{39};
63-
int const command_column{49};
63+
int const command_column{54};
6464
wattron(window, COLOR_PAIR(2));
6565
mvwprintw(window, ++row, pid_column, "PID");
6666
mvwprintw(window, row, user_column, "USER");

0 commit comments

Comments
 (0)