Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

<!-- insertion marker -->
## [0.2.6](https://github.com/repo/owner/releases/tag/0.2.6) - 2026-09-23

Expand Down
28 changes: 28 additions & 0 deletions include/mstd/file.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
/*****************************************************************************
<GPL_HEADER>

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 <http://www.gnu.org/licenses/>.

<GPL_HEADER>
******************************************************************************/

#ifndef __MSTD__FILE_HPP__
#define __MSTD__FILE_HPP__

#include "file/file.hpp" // IWYU pragma: export

#endif // __MSTD__FILE_HPP__
56 changes: 56 additions & 0 deletions include/mstd/file/file.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
/*****************************************************************************
<GPL_HEADER>

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 <http://www.gnu.org/licenses/>.

<GPL_HEADER>
******************************************************************************/

#ifndef __MSTD__FILE__FILE_HPP__
#define __MSTD__FILE__FILE_HPP__

#include <string>

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__
64 changes: 64 additions & 0 deletions include/mstd/file/file_impl.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
/*****************************************************************************
<GPL_HEADER>

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 <http://www.gnu.org/licenses/>.

<GPL_HEADER>
******************************************************************************/

#ifndef __MSTD__FILE__FILE_IMPL_HPP__
#define __MSTD__FILE__FILE_IMPL_HPP__

#include <filesystem>
#include <fstream>
#include <string>

#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__
Loading