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"
|
|
|
|
|
|
|
|
#include <cstddef>
|
|
|
|
#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
|
|
|
|
|
|
|
class File {
|
|
|
|
public:
|
|
|
|
~File();
|
|
|
|
|
2016-04-10 18:55:01 -04:00
|
|
|
bool open_for_writing(const std::string& file_path);
|
2016-04-10 18:56:34 -04:00
|
|
|
bool open_for_reading(const std::string& file_path);
|
2016-02-06 19:23:20 -05:00
|
|
|
bool open_for_append(const std::string& file_path);
|
2016-01-17 17:20:02 -05:00
|
|
|
bool close();
|
|
|
|
|
|
|
|
bool is_ready();
|
|
|
|
|
|
|
|
bool read(void* const data, const size_t bytes_to_read);
|
|
|
|
bool write(const void* const data, const size_t bytes_to_write);
|
|
|
|
|
2016-02-19 00:34:03 -05:00
|
|
|
template<size_t N>
|
|
|
|
bool write(const std::array<uint8_t, N>& data) {
|
|
|
|
return write(data.data(), N);
|
|
|
|
}
|
|
|
|
|
2016-02-06 19:23:20 -05:00
|
|
|
bool puts(const std::string& string);
|
2016-01-17 17:20:02 -05:00
|
|
|
|
|
|
|
bool sync();
|
|
|
|
|
|
|
|
private:
|
|
|
|
FIL f;
|
|
|
|
};
|
|
|
|
|
2016-04-20 12:57:17 -04:00
|
|
|
std::string next_filename_matching_pattern(const std::string& filename_pattern);
|
|
|
|
|
2016-04-20 12:56:35 -04:00
|
|
|
namespace std {
|
|
|
|
namespace filesystem {
|
|
|
|
|
|
|
|
using file_status = BYTE;
|
|
|
|
|
|
|
|
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);
|
|
|
|
|
|
|
|
} /* namespace filesystem */
|
|
|
|
} /* namespace std */
|
|
|
|
|
2016-01-17 17:20:02 -05:00
|
|
|
#endif/*__FILE_H__*/
|