rexylib/include/rexy/filerd.hpp

85 lines
2.3 KiB
C++

/**
This file is a part of rexy's general purpose library
Copyright (C) 2020 rexy712
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/>.
*/
#ifndef REXY_FILERD_HPP
#define REXY_FILERD_HPP
#include <cstdio> //FILE
#include <cstddef> //size_t
#include <type_traits> //is_nothrow_constructible
#include "string.hpp"
#include "steal.hpp"
#include "utility.hpp"
#include "rexy.hpp"
#include "buffer.hpp"
#ifndef LIBREXY_HEADER_ONLY
namespace rexy{
//RAII wrapper for FILE*
class filerd
{
private:
FILE* m_fp = nullptr;
public:
constexpr filerd(void)noexcept = default;
filerd(const char* f, const char* mode = "r")noexcept;
filerd(const filerd&) = delete;
constexpr filerd(filerd&& f)noexcept:
m_fp(exchange(f.m_fp, nullptr)){}
~filerd(void)noexcept;
filerd& operator=(const filerd&) = delete;
constexpr filerd& operator=(filerd&& f)noexcept{
swap(m_fp, f.m_fp);
return *this;
}
void reset(FILE* fp = nullptr)noexcept;
FILE* release(void)noexcept;
size_t length(void)noexcept;
size_t position(void)const noexcept;
void rewind(size_t pos = 0)noexcept;
operator FILE*(void)noexcept;
operator const FILE*(void)const noexcept;
FILE* get(void)noexcept;
const FILE* get(void)const noexcept;
operator bool(void)const noexcept;
size_t read(char* dest, size_t bytes)noexcept;
rexy::string read(size_t bytes)noexcept;
rexy::string readln(size_t max = 0)noexcept;
rexy::buffer<char> read_bin(size_t bytes)noexcept(std::is_nothrow_constructible<rexy::buffer<char>, char*, size_t>::value);
size_t write(const char* c, size_t bytes)noexcept;
size_t write(const rexy::string_base<char>& s)noexcept;
};
}
#else //LIBREXY_HEADER_ONLY
#error "rexy::filerd is not available when built with header only support"
#endif //LIBREXY_HEADER_ONLY
#endif