2016-01-17 17:20:02 -05:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2015 Jared Boone, ShareBrained Technology, Inc.
|
|
|
|
*
|
|
|
|
* This file is part of PortaPack.
|
|
|
|
*
|
|
|
|
* 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 2, 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; see the file COPYING. If not, write to
|
|
|
|
* the Free Software Foundation, Inc., 51 Franklin Street,
|
|
|
|
* Boston, MA 02110-1301, USA.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef __FILE_H__
|
|
|
|
#define __FILE_H__
|
|
|
|
|
|
|
|
#include "ff.h"
|
|
|
|
|
2016-05-16 17:01:44 -04:00
|
|
|
#include "optional.hpp"
|
|
|
|
|
2016-01-17 17:20:02 -05:00
|
|
|
#include <cstddef>
|
2016-05-11 13:58:57 -04:00
|
|
|
#include <cstdint>
|
2016-01-17 17:20:02 -05:00
|
|
|
#include <string>
|
2016-02-19 00:34:03 -05:00
|
|
|
#include <array>
|
2016-04-20 12:56:35 -04:00
|
|
|
#include <memory>
|
|
|
|
#include <iterator>
|
2016-01-17 17:20:02 -05:00
|
|
|
|
2016-04-29 14:27:12 -04:00
|
|
|
std::string next_filename_stem_matching_pattern(const std::string& filename_stem_pattern);
|
2016-04-20 12:57:17 -04:00
|
|
|
|
2016-04-20 12:56:35 -04:00
|
|
|
namespace std {
|
|
|
|
namespace filesystem {
|
|
|
|
|
2016-05-12 21:19:28 -04:00
|
|
|
struct filesystem_error {
|
2016-05-16 17:01:44 -04:00
|
|
|
constexpr filesystem_error(
|
|
|
|
) : err { FR_OK }
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
constexpr filesystem_error(
|
|
|
|
FRESULT fatfs_error
|
|
|
|
) : err { fatfs_error }
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
constexpr filesystem_error(
|
|
|
|
unsigned int other_error
|
|
|
|
) : err { other_error }
|
|
|
|
{
|
|
|
|
}
|
2016-05-12 21:19:28 -04:00
|
|
|
|
2016-06-21 13:54:13 -04:00
|
|
|
uint32_t code() const {
|
|
|
|
return err;
|
|
|
|
}
|
|
|
|
|
2016-05-12 21:19:28 -04:00
|
|
|
std::string what() const;
|
2016-05-16 17:01:44 -04:00
|
|
|
|
|
|
|
private:
|
|
|
|
uint32_t err;
|
2016-05-12 21:19:28 -04:00
|
|
|
};
|
|
|
|
|
2016-05-11 13:58:57 -04:00
|
|
|
using path = std::string;
|
2016-04-20 12:56:35 -04:00
|
|
|
using file_status = BYTE;
|
|
|
|
|
2016-05-11 13:58:57 -04:00
|
|
|
struct space_info {
|
|
|
|
static_assert(sizeof(std::uintmax_t) >= 8, "std::uintmax_t too small (<uint64_t)");
|
|
|
|
|
|
|
|
std::uintmax_t capacity;
|
|
|
|
std::uintmax_t free;
|
|
|
|
std::uintmax_t available;
|
|
|
|
};
|
|
|
|
|
2016-04-20 12:56:35 -04:00
|
|
|
struct directory_entry : public FILINFO {
|
|
|
|
file_status status() const {
|
|
|
|
return fattrib;
|
|
|
|
}
|
|
|
|
|
|
|
|
const std::string path() const noexcept { return fname; };
|
|
|
|
};
|
|
|
|
|
|
|
|
class directory_iterator {
|
|
|
|
struct Impl {
|
|
|
|
DIR dir;
|
|
|
|
directory_entry filinfo;
|
|
|
|
|
|
|
|
~Impl() {
|
|
|
|
f_closedir(&dir);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
std::shared_ptr<Impl> impl;
|
|
|
|
|
|
|
|
friend bool operator!=(const directory_iterator& lhs, const directory_iterator& rhs);
|
|
|
|
|
|
|
|
public:
|
|
|
|
using difference_type = std::ptrdiff_t;
|
|
|
|
using value_type = directory_entry;
|
|
|
|
using pointer = const directory_entry*;
|
|
|
|
using reference = const directory_entry&;
|
|
|
|
using iterator_category = std::input_iterator_tag;
|
|
|
|
|
|
|
|
directory_iterator() noexcept { };
|
|
|
|
directory_iterator(const char* path, const char* wild);
|
|
|
|
|
|
|
|
~directory_iterator() { }
|
|
|
|
|
|
|
|
directory_iterator& operator++();
|
|
|
|
|
|
|
|
reference operator*() const {
|
|
|
|
// TODO: Exception or assert if impl == nullptr.
|
|
|
|
return impl->filinfo;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
inline const directory_iterator& begin(const directory_iterator& iter) noexcept { return iter; };
|
|
|
|
inline directory_iterator end(const directory_iterator&) noexcept { return { }; };
|
|
|
|
|
|
|
|
inline bool operator!=(const directory_iterator& lhs, const directory_iterator& rhs) { return lhs.impl != rhs.impl; };
|
|
|
|
|
|
|
|
bool is_regular_file(const file_status s);
|
|
|
|
|
2016-05-11 13:58:57 -04:00
|
|
|
space_info space(const path& p);
|
|
|
|
|
2016-04-20 12:56:35 -04:00
|
|
|
} /* namespace filesystem */
|
|
|
|
} /* namespace std */
|
|
|
|
|
2016-05-13 00:44:37 -04:00
|
|
|
class File {
|
|
|
|
public:
|
2016-05-16 17:01:44 -04:00
|
|
|
using Error = std::filesystem::filesystem_error;
|
|
|
|
|
|
|
|
template<typename T>
|
|
|
|
struct Result {
|
|
|
|
enum class Type {
|
|
|
|
Success,
|
|
|
|
Error,
|
|
|
|
} type;
|
|
|
|
union {
|
|
|
|
T value_;
|
|
|
|
Error error_;
|
|
|
|
};
|
|
|
|
|
|
|
|
bool is_ok() const {
|
|
|
|
return type == Type::Success;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool is_error() const {
|
|
|
|
return type == Type::Error;
|
|
|
|
}
|
|
|
|
|
2016-07-24 22:12:09 -04:00
|
|
|
const T& value() const {
|
2016-05-16 17:01:44 -04:00
|
|
|
return value_;
|
|
|
|
}
|
|
|
|
|
|
|
|
Error error() const {
|
|
|
|
return error_;
|
|
|
|
}
|
|
|
|
|
|
|
|
Result() = delete;
|
|
|
|
|
|
|
|
constexpr Result(
|
|
|
|
T value
|
|
|
|
) : type { Type::Success },
|
|
|
|
value_ { value }
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
constexpr Result(
|
|
|
|
Error error
|
|
|
|
) : type { Type::Error },
|
|
|
|
error_ { error }
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
~Result() {
|
|
|
|
if( type == Type::Success ) {
|
|
|
|
value_.~T();
|
|
|
|
}
|
|
|
|
}
|
2016-05-13 00:44:37 -04:00
|
|
|
};
|
|
|
|
|
2016-05-16 17:01:44 -04:00
|
|
|
File() { };
|
2016-05-13 00:44:37 -04:00
|
|
|
~File();
|
|
|
|
|
2016-05-16 17:01:44 -04:00
|
|
|
/* Prevent copies */
|
|
|
|
File(const File&) = delete;
|
|
|
|
File& operator=(const File&) = delete;
|
2016-05-13 00:55:39 -04:00
|
|
|
|
2016-05-16 17:01:44 -04:00
|
|
|
// TODO: Return Result<>.
|
|
|
|
Optional<Error> open(const std::string& filename);
|
|
|
|
Optional<Error> append(const std::string& filename);
|
|
|
|
Optional<Error> create(const std::string& filename);
|
2016-05-13 00:55:39 -04:00
|
|
|
|
2016-05-16 17:01:44 -04:00
|
|
|
Result<size_t> read(void* const data, const size_t bytes_to_read);
|
|
|
|
Result<size_t> write(const void* const data, const size_t bytes_to_write);
|
2016-05-13 00:44:37 -04:00
|
|
|
|
2016-05-16 17:01:44 -04:00
|
|
|
Result<uint64_t> seek(const uint64_t new_position);
|
2016-05-13 00:44:37 -04:00
|
|
|
|
|
|
|
template<size_t N>
|
2016-05-16 17:01:44 -04:00
|
|
|
Result<size_t> write(const std::array<uint8_t, N>& data) {
|
2016-05-13 00:44:37 -04:00
|
|
|
return write(data.data(), N);
|
|
|
|
}
|
|
|
|
|
2016-07-24 23:39:21 -04:00
|
|
|
Optional<Error> write_line(const std::string& s);
|
2016-05-13 00:44:37 -04:00
|
|
|
|
2016-05-16 17:01:44 -04:00
|
|
|
// TODO: Return Result<>.
|
|
|
|
Optional<Error> sync();
|
2016-05-13 00:44:37 -04:00
|
|
|
|
|
|
|
private:
|
|
|
|
FIL f;
|
|
|
|
|
2016-05-16 17:01:44 -04:00
|
|
|
Optional<Error> open_fatfs(const std::string& filename, BYTE mode);
|
|
|
|
};
|
2016-05-13 00:44:37 -04:00
|
|
|
|
2016-01-17 17:20:02 -05:00
|
|
|
#endif/*__FILE_H__*/
|