trainings/AdvancedCppV2/Presentation/exercises/files/main.cpp
2024-04-25 15:00:24 +02:00

52 lines
No EOL
2 KiB
C++

#include <filesystem>
#include <iostream>
namespace fs = std::filesystem;
void printPerms(fs::perms p) {
std::cout << ((p & fs::perms::owner_read) != fs::perms::none ? "r" : "-")
<< ((p & fs::perms::owner_write) != fs::perms::none ? "w" : "-")
<< ((p & fs::perms::owner_exec) != fs::perms::none ? "x" : "-")
<< ((p & fs::perms::group_read) != fs::perms::none ? "r" : "-")
<< ((p & fs::perms::group_write) != fs::perms::none ? "w" : "-")
<< ((p & fs::perms::group_exec) != fs::perms::none ? "x" : "-")
<< ((p & fs::perms::others_read) != fs::perms::none ? "r" : "-")
<< ((p & fs::perms::others_write) != fs::perms::none ? "w" : "-")
<< ((p & fs::perms::others_exec) != fs::perms::none ? "x" : "-")
<< '\n';
}
void printPermissionsForDir() {
for (const auto& entry : fs::directory_iterator(fs::current_path().string() + "/project")) {
if (fs::is_regular_file(entry.path())) {
std::cout << "File: "
<< std::left << std::setw(20)
<< entry.path().filename()
<< " | Permissions: ";
printPerms(fs::status(entry.path()).permissions());
}
}
std::cout << std::string(80, '_') << "\n\n";
}
void changePermissions() {
}
int main() {
printPermissionsForDir();
changePermissions();
printPermissionsForDir();
// Part 2 unncoment later
/*
auto print_last_write_time = [](std::filesystem::file_time_type const& ftime) {
std::time_t time = std::chrono::system_clock::to_time_t(
std::chrono::file_clock::to_sys(ftime));
std::cout << "File write time is " << std::asctime(std::localtime(&time));
};
std::cout << "Last modified file is: " << *last_modify << '\n';
print_last_write_time(std::filesystem::last_write_time(*last_modify));
std::cout << "files which were longest unmodified: \n";
// Print it here
*/
}