[136] | 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 |
|
---|
| 28 | class File : public Stream {
|
---|
| 29 |
|
---|
| 30 | public:
|
---|
| 31 | File(BridgeClass &b = Bridge);
|
---|
| 32 | File(const char *_filename, uint8_t _mode, BridgeClass &b = Bridge);
|
---|
| 33 | ~File();
|
---|
| 34 |
|
---|
| 35 | virtual size_t write(uint8_t);
|
---|
| 36 | virtual size_t write(const uint8_t *buf, size_t size);
|
---|
| 37 | virtual int read();
|
---|
| 38 | virtual int peek();
|
---|
| 39 | virtual int available();
|
---|
| 40 | virtual void flush();
|
---|
| 41 | int read(void *buf, uint16_t nbyte);
|
---|
| 42 | boolean seek(uint32_t pos);
|
---|
| 43 | uint32_t position();
|
---|
| 44 | uint32_t size();
|
---|
| 45 | void close();
|
---|
| 46 | operator bool();
|
---|
| 47 | const char * name();
|
---|
| 48 | boolean isDirectory();
|
---|
| 49 | File openNextFile(uint8_t mode = FILE_READ);
|
---|
| 50 | void rewindDirectory(void);
|
---|
| 51 |
|
---|
| 52 | //using Print::write;
|
---|
| 53 |
|
---|
| 54 | private:
|
---|
| 55 | void doBuffer();
|
---|
| 56 | uint8_t buffered;
|
---|
| 57 | uint8_t readPos;
|
---|
| 58 | uint16_t dirPosition;
|
---|
| 59 | static const int BUFFER_SIZE = 64;
|
---|
| 60 | uint8_t buffer[BUFFER_SIZE];
|
---|
| 61 |
|
---|
| 62 |
|
---|
| 63 | private:
|
---|
| 64 | BridgeClass &bridge;
|
---|
| 65 | String filename;
|
---|
| 66 | uint8_t mode;
|
---|
| 67 | uint8_t handle;
|
---|
| 68 |
|
---|
| 69 | };
|
---|
| 70 |
|
---|
| 71 | class FileSystemClass {
|
---|
| 72 | public:
|
---|
| 73 | FileSystemClass() : bridge(Bridge) { }
|
---|
| 74 | FileSystemClass(BridgeClass &_b) : bridge(_b) { }
|
---|
| 75 |
|
---|
| 76 | boolean begin();
|
---|
| 77 |
|
---|
| 78 | // Open the specified file/directory with the supplied mode (e.g. read or
|
---|
| 79 | // write, etc). Returns a File object for interacting with the file.
|
---|
| 80 | // Note that currently only one file can be open at a time.
|
---|
| 81 | File open(const char *filename, uint8_t mode = FILE_READ);
|
---|
| 82 |
|
---|
| 83 | // Methods to determine if the requested file path exists.
|
---|
| 84 | boolean exists(const char *filepath);
|
---|
| 85 |
|
---|
| 86 | // Create the requested directory hierarchy--if intermediate directories
|
---|
| 87 | // do not exist they will be created.
|
---|
| 88 | boolean mkdir(const char *filepath);
|
---|
| 89 |
|
---|
| 90 | // Delete the file.
|
---|
| 91 | boolean remove(const char *filepath);
|
---|
| 92 |
|
---|
| 93 | boolean rmdir(const char *filepath);
|
---|
| 94 |
|
---|
| 95 | private:
|
---|
| 96 | friend class File;
|
---|
| 97 |
|
---|
| 98 | BridgeClass &bridge;
|
---|
| 99 | };
|
---|
| 100 |
|
---|
| 101 | extern FileSystemClass FileSystem;
|
---|
| 102 |
|
---|
| 103 | #endif
|
---|