From 699ef6737c38cbeb96e20c57bdba9f26f8c84295 Mon Sep 17 00:00:00 2001 From: Jakob Gamper <97gamjak@gmail.com> Date: Fri, 25 Sep 2026 23:04:32 +0200 Subject: [PATCH 1/2] add simple file wrapper Fixes #118 --- CHANGELOG.md | 4 +++ include/mstd/file.hpp | 28 +++++++++++++++ include/mstd/file/file.hpp | 56 +++++++++++++++++++++++++++++ include/mstd/file/file_impl.hpp | 64 +++++++++++++++++++++++++++++++++ 4 files changed, 152 insertions(+) create mode 100644 include/mstd/file.hpp create mode 100644 include/mstd/file/file.hpp create mode 100644 include/mstd/file/file_impl.hpp 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..785e240 --- /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__ From cffe030143043ec1fd3a451dfd6621be8668e9f1 Mon Sep 17 00:00:00 2001 From: Jakob Gamper <97gamjak@gmail.com> Date: Sat, 26 Sep 2026 09:56:33 +0200 Subject: [PATCH 2/2] add simple file wrapper Fixes #118 --- include/mstd/file/file_impl.hpp | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/include/mstd/file/file_impl.hpp b/include/mstd/file/file_impl.hpp index 785e240..31dab8c 100644 --- a/include/mstd/file/file_impl.hpp +++ b/include/mstd/file/file_impl.hpp @@ -20,8 +20,8 @@ ******************************************************************************/ -#ifndef __MSTD__FILE__FILE_IMPL__HPP__ -#define __MSTD__FILE__FILE_IMPL__HPP__ +#ifndef __MSTD__FILE__FILE_IMPL_HPP__ +#define __MSTD__FILE__FILE_IMPL_HPP__ #include #include @@ -61,4 +61,4 @@ namespace mstd inline std::string File::fileName() const { return _fileName; } } // namespace mstd -#endif // __MSTD__FILE__FILE_IMPL__HPP__ +#endif // __MSTD__FILE__FILE_IMPL_HPP__