diff options
| author | Martial Simon <msimon_fr@hotmail.com> | 2025-09-15 01:08:27 +0200 |
|---|---|---|
| committer | Martial Simon <msimon_fr@hotmail.com> | 2025-09-15 01:08:27 +0200 |
| commit | c9b6b9a5ca082fe7c1b6f58d7713f785a9eb6a5c (patch) | |
| tree | 3e4f42f93c7ae89a364e4d51fff6e5cec4e55fa9 /graphs/cpp/directories_infos | |
add: graphs et rushs
Diffstat (limited to 'graphs/cpp/directories_infos')
| -rw-r--r-- | graphs/cpp/directories_infos/directory_info.cc | 34 | ||||
| -rw-r--r-- | graphs/cpp/directories_infos/directory_info.hh | 24 | ||||
| -rw-r--r-- | graphs/cpp/directories_infos/main_example.cc | 32 | ||||
| -rw-r--r-- | graphs/cpp/directories_infos/read_info.cc | 39 | ||||
| -rw-r--r-- | graphs/cpp/directories_infos/read_info.hh | 7 |
5 files changed, 136 insertions, 0 deletions
diff --git a/graphs/cpp/directories_infos/directory_info.cc b/graphs/cpp/directories_infos/directory_info.cc new file mode 100644 index 0000000..2c28150 --- /dev/null +++ b/graphs/cpp/directories_infos/directory_info.cc @@ -0,0 +1,34 @@ +// +// Created by martial.simon on 2/24/25. +// +#include "directory_info.hh" + +DirectoryInfo::DirectoryInfo(const std::string& name, size_t size, + uint16_t rights, const std::string& owner) + : name_{ name } + , size_{ size } + , rights_{ rights } + , owner_{ owner } +{ + is_valid_ = true; +} +const std::string& DirectoryInfo::get_name() const +{ + return name_; +} +const std::string& DirectoryInfo::get_owner() const +{ + return owner_; +} +size_t DirectoryInfo::get_size() const +{ + return size_; +} +uint16_t DirectoryInfo::get_rights() const +{ + return rights_; +} +bool DirectoryInfo::is_valid() const +{ + return is_valid_; +} diff --git a/graphs/cpp/directories_infos/directory_info.hh b/graphs/cpp/directories_infos/directory_info.hh new file mode 100644 index 0000000..eaf7c62 --- /dev/null +++ b/graphs/cpp/directories_infos/directory_info.hh @@ -0,0 +1,24 @@ +#pragma once + +#include <cstdint> +#include <string> + +class DirectoryInfo +{ +public: + DirectoryInfo() = default; + DirectoryInfo(const std::string& name, size_t size, uint16_t rights, + const std::string& owner); + const std::string& get_name() const; + const std::string& get_owner() const; + size_t get_size() const; + uint16_t get_rights() const; + bool is_valid() const; + +private: + std::string name_; + size_t size_; + uint16_t rights_; + std::string owner_; + bool is_valid_ = false; +}; diff --git a/graphs/cpp/directories_infos/main_example.cc b/graphs/cpp/directories_infos/main_example.cc new file mode 100644 index 0000000..db4bfa0 --- /dev/null +++ b/graphs/cpp/directories_infos/main_example.cc @@ -0,0 +1,32 @@ +#include <fstream> +#include <iomanip> +#include <iostream> + +#include "directory_info.hh" +#include "read_info.hh" + +int main(int argc, char** argv) +{ + if (argc < 2) + return 1; + + auto file = std::ifstream(argv[1]); + + DirectoryInfo dir_info; + + while ((dir_info = read_info(file)).is_valid()) + { + std::stringstream str_stream; + str_stream << dir_info.get_name() << ' ' << dir_info.get_size() << ' ' + << std::oct << dir_info.get_rights() << std::dec << ' ' + << dir_info.get_owner() << '\n'; + + dir_info = read_info(str_stream); + if (!dir_info.is_valid()) + break; + + std::cout << dir_info.get_name() << '|' << dir_info.get_size() << '|' + << std::oct << dir_info.get_rights() << std::dec << '|' + << dir_info.get_owner() << '\n'; + } +} diff --git a/graphs/cpp/directories_infos/read_info.cc b/graphs/cpp/directories_infos/read_info.cc new file mode 100644 index 0000000..327d004 --- /dev/null +++ b/graphs/cpp/directories_infos/read_info.cc @@ -0,0 +1,39 @@ +#include "read_info.hh" + +#include <sstream> + +#include "directory_info.hh" + +DirectoryInfo read_info(std::istream& stream) +{ + if (stream.eof()) + { + return DirectoryInfo{}; + } + std::string l; + std::getline(stream, l); + std::istringstream line{ l }; + std::string name; + size_t size; + uint16_t rights; + std::string owner; + if (!(line >> name)) + { + return DirectoryInfo{}; + } + if (!(line >> size)) + { + return DirectoryInfo{}; + } + if (!(line >> std::oct >> rights >> std::dec)) + { + return DirectoryInfo{}; + } + if (!(line >> owner)) + { + return DirectoryInfo{}; + } + if (line.eof()) + return DirectoryInfo{ name, size, rights, owner }; + return DirectoryInfo{}; +}
\ No newline at end of file diff --git a/graphs/cpp/directories_infos/read_info.hh b/graphs/cpp/directories_infos/read_info.hh new file mode 100644 index 0000000..72f64b4 --- /dev/null +++ b/graphs/cpp/directories_infos/read_info.hh @@ -0,0 +1,7 @@ +#pragma once +#include <iostream> + +#include "directory_info.hh" + +// use ref to istream +DirectoryInfo read_info(std::istream& stream);
\ No newline at end of file |
