Program Listing for File disk_io.hpp

Return to documentation for file (disk_io.hpp)

#ifndef FELIX_DISKIO_H_
#define FELIX_DISKIO_H_

#include <string>
#include <string_view>

std::string errno_msg(int errn);

class DiskIO
{
    public:
        static constexpr std::string_view FREAD = "r";
        static constexpr std::string_view FWRITE = "w+";

        DiskIO(const std::string& filename, size_t block_size, std::string_view op, bool is_fifo);

        DiskIO(const DiskIO &) = delete;
        DiskIO &operator=(const DiskIO &) = delete;

        ~DiskIO();

        size_t block_read(void *to_read, size_t max_blocks);

        size_t block_write(const void *to_write, size_t max_blocks);

        size_t byte_read(void* to_read, size_t bytes);

        size_t byte_write(void* to_write, size_t bytes);

        bool is_eof();

        int reset_position();

    private:
        std::string m_filename{};
        size_t m_block_size{0};
        int m_fd{-1};
        FILE* m_fstream{nullptr};

        void open_file(std::string_view op);
        void open_fifo(std::string_view op);
};


#endif /* FELIX_DISKIO_H_ */