1/* 2 Copyright (c) 2013 Arduino LLC. All right reserved. 3 4 This library is free software; you can redistribute it and/or 5 modify it under the terms of the GNU Lesser General Public 6 License as published by the Free Software Foundation; either 7 version 2.1 of the License, or (at your option) any later version. 8 9 This library is distributed in the hope that it will be useful, 10 but WITHOUT ANY WARRANTY; without even the implied warranty of 11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 12 Lesser General Public License for more details. 13 14 You should have received a copy of the GNU Lesser General Public 15 License along with this library; if not, write to the Free Software 16 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA 17*/ 18 19#ifndef __FILEIO_H__ 20#define __FILEIO_H__ 21 22#include <Process.h> 23 24#define FILE_READ 0 25#define FILE_WRITE 1 26#define FILE_APPEND 2 27 28namespace BridgeLib { 29 30class File :public Stream { 31 32public: 33File(BridgeClass &b = Bridge); 34File(const char*_filename,uint8_t _mode, BridgeClass &b = Bridge); 35~File(); 36 37virtualsize_twrite(uint8_t); 38virtualsize_twrite(const uint8_t*buf,size_t size); 39virtualintread(); 40virtualintpeek(); 41virtualintavailable(); 42virtualvoidflush(); 43intread(void*buf,uint16_t nbyte); 44 boolean seek(uint32_t pos); 45uint32_tposition(); 46uint32_tsize(); 47voidclose(); 48operatorbool(); 49const char*name(); 50 boolean isDirectory(); 51 File openNextFile(uint8_t mode = FILE_READ); 52voidrewindDirectory(void); 53 54//using Print::write; 55 56private: 57voiddoBuffer(); 58uint8_t buffered; 59uint8_t readPos; 60uint16_t dirPosition; 61static const int BUFFER_SIZE =64; 62uint8_t buffer[BUFFER_SIZE]; 63 64 65private: 66 BridgeClass &bridge; 67 String filename; 68uint8_t mode; 69uint8_t handle; 70 71}; 72 73class FileSystemClass { 74public: 75FileSystemClass() :bridge(Bridge) { } 76FileSystemClass(BridgeClass &_b) :bridge(_b) { } 77 78 boolean begin(); 79 80// Open the specified file/directory with the supplied mode (e.g. read or 81// write, etc). Returns a File object for interacting with the file. 82// Note that currently only one file can be open at a time. 83 File open(const char*filename,uint8_t mode = FILE_READ); 84 85// Methods to determine if the requested file path exists. 86 boolean exists(const char*filepath); 87 88// Create the requested directory hierarchy--if intermediate directories 89// do not exist they will be created. 90 boolean mkdir(const char*filepath); 91 92// Delete the file. 93 boolean remove(const char*filepath); 94 95 boolean rmdir(const char*filepath); 96 97private: 98friend class File; 99 100 BridgeClass &bridge; 101}; 102 103extern FileSystemClass FileSystem; 104 105}; 106 107// We enclose File and FileSystem classes in namespace BridgeLib to avoid 108// conflicts with legacy SD library. 109 110// This ensure compatibility with older sketches that uses only Bridge lib 111// (the user can still use File instead of BridgeFile) 112using namespace BridgeLib; 113 114// This allows sketches to use BridgeLib::File together with SD library 115// (you must use BridgeFile instead of File when needed to disambiguate) 116typedefBridgeLib::File BridgeFile; 117typedefBridgeLib::FileSystemClass BridgeFileSystemClass; 118#define BridgeFileSystem BridgeLib::FileSystem 119 120#endif