diff --git a/CHANGELOG.md b/CHANGELOG.md index 0b7d299..105e53e 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,6 +4,10 @@ All notable changes to this project will be documented in this file. ## Next Release +### File + +- add new (very thin) type `File` for type safe string handling + ## [0.2.6](https://github.com/repo/owner/releases/tag/0.2.6) - 2026-09-23 diff --git a/include/mstd/file.hpp b/include/mstd/file.hpp new file mode 100644 index 0000000..6e6be6e --- /dev/null +++ b/include/mstd/file.hpp @@ -0,0 +1,28 @@ +/***************************************************************************** + + + mstd library + Copyright (C) 2025-now Jakob Gamper + + This program is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program. If not, see . + + +******************************************************************************/ + +#ifndef __MSTD__FILE_HPP__ +#define __MSTD__FILE_HPP__ + +#include "file/file.hpp" // IWYU pragma: export + +#endif // __MSTD__FILE_HPP__ diff --git a/include/mstd/file/file.hpp b/include/mstd/file/file.hpp new file mode 100644 index 0000000..6839199 --- /dev/null +++ b/include/mstd/file/file.hpp @@ -0,0 +1,56 @@ +/***************************************************************************** + + + mstd library + Copyright (C) 2025-now Jakob Gamper + + This program is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program. If not, see . + + +******************************************************************************/ + +#ifndef __MSTD__FILE__FILE_HPP__ +#define __MSTD__FILE__FILE_HPP__ + +#include + +namespace mstd +{ + /** + * @brief Represents a file in the filesystem. + */ + class File + { + private: + std::string _fileName; + + public: + explicit File(std::string fileName); + + [[nodiscard]] + bool exists() const; + + [[nodiscard]] + std::string fileName() const; + + [[nodiscard]] + constexpr bool operator==(const File &other) const = default; + }; +} // namespace mstd + +#ifndef __MSTD__FILE__FILE_IMPL__HPP__ +#include "file_impl.hpp" // IWYU pragma: export +#endif + +#endif // __MSTD__FILE__FILE_HPP__ diff --git a/include/mstd/file/file_impl.hpp b/include/mstd/file/file_impl.hpp new file mode 100644 index 0000000..31dab8c --- /dev/null +++ b/include/mstd/file/file_impl.hpp @@ -0,0 +1,64 @@ +/***************************************************************************** + + + mstd library + Copyright (C) 2025-now Jakob Gamper + + This program is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program. If not, see . + + +******************************************************************************/ + +#ifndef __MSTD__FILE__FILE_IMPL_HPP__ +#define __MSTD__FILE__FILE_IMPL_HPP__ + +#include +#include +#include + +#include "file.hpp" + +namespace mstd +{ + /** + * @brief constructs a File object with the given file name + * + * @param fileName the name of the file + */ + inline File::File(std::string fileName) : _fileName(std::move(fileName)) {} + + /** + * @brief checks if a file exists and can be opened + * + * @return true if file exists and can be opened + * @return false if file does not exist or cannot be opened + */ + inline bool File::exists() const + { + if (!std::filesystem::is_regular_file(_fileName)) + return false; + + std::ifstream file(_fileName); + return file.good(); + } + + /** + * @brief returns the file name of the File object + * + * @return the file name + */ + inline std::string File::fileName() const { return _fileName; } +} // namespace mstd + +#endif // __MSTD__FILE__FILE_IMPL_HPP__