Skip to content

File system #256

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Mar 10, 2017
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
9 changes: 9 additions & 0 deletions libraries/FS/library.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
name=FS
version=1.0
author=Hristo Gochkov, Ivan Grokhtkov
maintainer=Hristo Gochkov <hristo@espressif.com>
sentence=ESP32 File System
paragraph=
category=Data Storage
url=
architectures=esp32
260 changes: 260 additions & 0 deletions libraries/FS/src/FS.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,260 @@
/*
FS.cpp - file system wrapper
Copyright (c) 2015 Ivan Grokhotkov. All rights reserved.
This file is part of the esp8266 core for Arduino environment.

This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.

This library 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
Lesser General Public License for more details.

You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/

#include "FS.h"
#include "FSImpl.h"

using namespace fs;

size_t File::write(uint8_t c)
{
if (!_p) {
return 0;
}

return _p->write(&c, 1);
}

size_t File::write(const uint8_t *buf, size_t size)
{
if (!_p) {
return 0;
}

return _p->write(buf, size);
}

int File::available()
{
if (!_p) {
return false;
}

return _p->size() - _p->position();
}

int File::read()
{
if (!_p) {
return -1;
}

uint8_t result;
if (_p->read(&result, 1) != 1) {
return -1;
}

return result;
}

size_t File::read(uint8_t* buf, size_t size)
{
if (!_p) {
return -1;
}

return _p->read(buf, size);
}

int File::peek()
{
if (!_p) {
return -1;
}

size_t curPos = _p->position();
int result = read();
seek(curPos, SeekSet);
return result;
}

void File::flush()
{
if (!_p) {
return;
}

_p->flush();
}

bool File::seek(uint32_t pos, SeekMode mode)
{
if (!_p) {
return false;
}

return _p->seek(pos, mode);
}

size_t File::position() const
{
if (!_p) {
return 0;
}

return _p->position();
}

size_t File::size() const
{
if (!_p) {
return 0;
}

return _p->size();
}

void File::close()
{
if (_p) {
_p->close();
_p = nullptr;
}
}

File::operator bool() const
{
return !!_p;
}

const char* File::name() const
{
if (!_p) {
return nullptr;
}

return _p->name();
}

//to implement
boolean File::isDirectory(void)
{
if (!_p) {
return false;
}
return _p->isDirectory();
}

File File::openNextFile(const char* mode)
{
if (!_p) {
return File();
}
return _p->openNextFile(mode);
}

void File::rewindDirectory(void)
{
if (!_p) {
return;
}
_p->rewindDirectory();
}

File FS::open(const String& path, const char* mode)
{
return open(path.c_str(), mode);
}

File FS::open(const char* path, const char* mode)
{
if (!_impl) {
return File();
}

return File(_impl->open(path, mode));
}

bool FS::exists(const char* path)
{
if (!_impl) {
return false;
}
return _impl->exists(path);
}

bool FS::exists(const String& path)
{
return exists(path.c_str());
}

bool FS::remove(const char* path)
{
if (!_impl) {
return false;
}
return _impl->remove(path);
}

bool FS::remove(const String& path)
{
return remove(path.c_str());
}

bool FS::rename(const char* pathFrom, const char* pathTo)
{
if (!_impl) {
return false;
}
return _impl->rename(pathFrom, pathTo);
}

bool FS::rename(const String& pathFrom, const String& pathTo)
{
return rename(pathFrom.c_str(), pathTo.c_str());
}


bool FS::mkdir(const char *path)
{
if (!_impl) {
return false;
}
return _impl->mkdir(path);
}

bool FS::mkdir(const String &path)
{
return mkdir(path.c_str());
}

bool FS::rmdir(const char *path)
{
if (!_impl) {
return false;
}
return _impl->rmdir(path);
}

bool FS::rmdir(const String &path)
{
return rmdir(path.c_str());
}


void FSImpl::mountpoint(const char * mp)
{
_mountpoint = mp;
}

const char * FSImpl::mountpoint()
{
return _mountpoint;
}
122 changes: 122 additions & 0 deletions libraries/FS/src/FS.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@
/*
FS.h - file system wrapper
Copyright (c) 2015 Ivan Grokhotkov. All rights reserved.
This file is part of the esp8266 core for Arduino environment.

This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.

This library 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
Lesser General Public License for more details.

You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/

#ifndef FS_H
#define FS_H

#include <memory>
#include <Arduino.h>

namespace fs
{

#define FILE_READ "r"
#define FILE_WRITE "w"
#define FILE_APPEND "a"

class File;

class FileImpl;
typedef std::shared_ptr<FileImpl> FileImplPtr;
class FSImpl;
typedef std::shared_ptr<FSImpl> FSImplPtr;

enum SeekMode {
SeekSet = 0,
SeekCur = 1,
SeekEnd = 2
};

class File : public Stream
{
public:
File(FileImplPtr p = FileImplPtr()) : _p(p) {}

size_t write(uint8_t) override;
size_t write(const uint8_t *buf, size_t size) override;
int available() override;
int read() override;
int peek() override;
void flush() override;
size_t read(uint8_t* buf, size_t size);
size_t readBytes(char *buffer, size_t length)
{
return read((uint8_t*)buffer, length);
}

bool seek(uint32_t pos, SeekMode mode);
bool seek(uint32_t pos)
{
return seek(pos, SeekSet);
}
size_t position() const;
size_t size() const;
void close();
operator bool() const;
const char* name() const;

boolean isDirectory(void);
File openNextFile(const char* mode = FILE_READ);
void rewindDirectory(void);

protected:
FileImplPtr _p;
};

class FS
{
public:
FS(FSImplPtr impl) : _impl(impl) { }

File open(const char* path, const char* mode = FILE_READ);
File open(const String& path, const char* mode = FILE_READ);

bool exists(const char* path);
bool exists(const String& path);

bool remove(const char* path);
bool remove(const String& path);

bool rename(const char* pathFrom, const char* pathTo);
bool rename(const String& pathFrom, const String& pathTo);

bool mkdir(const char *path);
bool mkdir(const String &path);

bool rmdir(const char *path);
bool rmdir(const String &path);


protected:
FSImplPtr _impl;
};

} // namespace fs

#ifndef FS_NO_GLOBALS
using fs::FS;
using fs::File;
using fs::SeekMode;
using fs::SeekSet;
using fs::SeekCur;
using fs::SeekEnd;
#endif //FS_NO_GLOBALS

#endif //FS_H
Loading