Skip to content

Commit

Permalink
feat: refactoring bonus task
Browse files Browse the repository at this point in the history
  • Loading branch information
stevejaxon committed May 3, 2024
1 parent 96dfd20 commit da4f14f
Showing 1 changed file with 4 additions and 8 deletions.
12 changes: 4 additions & 8 deletions src/linux_parser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -98,23 +98,19 @@ string LinuxParser::Kernel(const std::filesystem::path &filePath) {
return kernel;
}

// BONUS: Update this to use std::filesystem
vector<int> LinuxParser::Pids(const std::string &dirPath) {
vector<int> pids;
DIR *directory = opendir(dirPath.c_str());
struct dirent *file;
while ((file = readdir(directory)) != nullptr) {
// Is this a directory?
if (file->d_type == DT_DIR) {
const std::filesystem::path directory{dirPath};
for (auto const& dir_entry : std::filesystem::directory_iterator{directory}) {
if (dir_entry.is_directory()) {
// Is every character of the name a digit?
string filename(file->d_name);
string filename(dir_entry.path().filename());
if (std::all_of(filename.begin(), filename.end(), isdigit)) {
int pid = stoi(filename);
pids.push_back(pid);
}
}
}
closedir(directory);
return pids;
}

Expand Down

0 comments on commit da4f14f

Please sign in to comment.