libraries / Bridge / src / FileIO.hon commit Added link to project report (97a3ba0)
   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
  32  public:
  33    File(BridgeClass &b = Bridge);
  34    File(const char *_filename, uint8_t _mode, BridgeClass &b = Bridge);
  35    ~File();
  36
  37    virtual size_t write(uint8_t);
  38    virtual size_t write(const uint8_t *buf, size_t size);
  39    virtual int read();
  40    virtual int peek();
  41    virtual int available();
  42    virtual void flush();
  43    int read(void *buf, uint16_t nbyte);
  44    boolean seek(uint32_t pos);
  45    uint32_t position();
  46    uint32_t size();
  47    void close();
  48    operator bool();
  49    const char * name();
  50    boolean isDirectory();
  51    File openNextFile(uint8_t mode = FILE_READ);
  52    void rewindDirectory(void);
  53
  54    //using Print::write;
  55
  56  private:
  57    void doBuffer();
  58    uint8_t buffered;
  59    uint8_t readPos;
  60    uint16_t dirPosition;
  61    static const int BUFFER_SIZE = 64;
  62    uint8_t buffer[BUFFER_SIZE];
  63
  64
  65  private:
  66    BridgeClass &bridge;
  67    String filename;
  68    uint8_t mode;
  69    uint8_t handle;
  70
  71};
  72
  73class FileSystemClass {
  74  public:
  75    FileSystemClass() : bridge(Bridge) { }
  76    FileSystemClass(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
  97  private:
  98    friend 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)
 116typedef BridgeLib::File            BridgeFile;
 117typedef BridgeLib::FileSystemClass BridgeFileSystemClass;
 118#define BridgeFileSystem           BridgeLib::FileSystem
 119
 120#endif