|
| 1 | +/* |
| 2 | + FS.cpp - file system wrapper |
| 3 | + Copyright (c) 2015 Ivan Grokhotkov. All rights reserved. |
| 4 | + This file is part of the esp8266 core for Arduino environment. |
| 5 | +
|
| 6 | + This library is free software; you can redistribute it and/or |
| 7 | + modify it under the terms of the GNU Lesser General Public |
| 8 | + License as published by the Free Software Foundation; either |
| 9 | + version 2.1 of the License, or (at your option) any later version. |
| 10 | +
|
| 11 | + This library is distributed in the hope that it will be useful, |
| 12 | + but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 14 | + Lesser General Public License for more details. |
| 15 | +
|
| 16 | + You should have received a copy of the GNU Lesser General Public |
| 17 | + License along with this library; if not, write to the Free Software |
| 18 | + Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA |
| 19 | + */ |
| 20 | + |
| 21 | +#include "FS.h" |
| 22 | +#include "FSImpl.h" |
| 23 | + |
| 24 | +using namespace fs; |
| 25 | + |
| 26 | +size_t File::write(uint8_t c) |
| 27 | +{ |
| 28 | + if (!*this) { |
| 29 | + return 0; |
| 30 | + } |
| 31 | + |
| 32 | + return _p->write(&c, 1); |
| 33 | +} |
| 34 | + |
| 35 | +time_t File::getLastWrite() |
| 36 | +{ |
| 37 | + if (!*this) { |
| 38 | + return 0; |
| 39 | + } |
| 40 | + |
| 41 | + return _p->getLastWrite(); |
| 42 | +} |
| 43 | + |
| 44 | +size_t File::write(const uint8_t *buf, size_t size) |
| 45 | +{ |
| 46 | + if (!*this) { |
| 47 | + return 0; |
| 48 | + } |
| 49 | + |
| 50 | + return _p->write(buf, size); |
| 51 | +} |
| 52 | + |
| 53 | +int File::available() |
| 54 | +{ |
| 55 | + if (!*this) { |
| 56 | + return false; |
| 57 | + } |
| 58 | + |
| 59 | + return _p->size() - _p->position(); |
| 60 | +} |
| 61 | + |
| 62 | +int File::read() |
| 63 | +{ |
| 64 | + if (!*this) { |
| 65 | + return -1; |
| 66 | + } |
| 67 | + |
| 68 | + uint8_t result; |
| 69 | + if (_p->read(&result, 1) != 1) { |
| 70 | + return -1; |
| 71 | + } |
| 72 | + |
| 73 | + return result; |
| 74 | +} |
| 75 | + |
| 76 | +size_t File::read(uint8_t* buf, size_t size) |
| 77 | +{ |
| 78 | + if (!*this) { |
| 79 | + return -1; |
| 80 | + } |
| 81 | + |
| 82 | + return _p->read(buf, size); |
| 83 | +} |
| 84 | + |
| 85 | +int File::peek() |
| 86 | +{ |
| 87 | + if (!*this) { |
| 88 | + return -1; |
| 89 | + } |
| 90 | + |
| 91 | + size_t curPos = _p->position(); |
| 92 | + int result = read(); |
| 93 | + seek(curPos, SeekSet); |
| 94 | + return result; |
| 95 | +} |
| 96 | + |
| 97 | +void File::flush() |
| 98 | +{ |
| 99 | + if (!*this) { |
| 100 | + return; |
| 101 | + } |
| 102 | + |
| 103 | + _p->flush(); |
| 104 | +} |
| 105 | + |
| 106 | +bool File::seek(uint32_t pos, SeekMode mode) |
| 107 | +{ |
| 108 | + if (!*this) { |
| 109 | + return false; |
| 110 | + } |
| 111 | + |
| 112 | + return _p->seek(pos, mode); |
| 113 | +} |
| 114 | + |
| 115 | +size_t File::position() const |
| 116 | +{ |
| 117 | + if (!*this) { |
| 118 | + return 0; |
| 119 | + } |
| 120 | + |
| 121 | + return _p->position(); |
| 122 | +} |
| 123 | + |
| 124 | +size_t File::size() const |
| 125 | +{ |
| 126 | + if (!*this) { |
| 127 | + return 0; |
| 128 | + } |
| 129 | + |
| 130 | + return _p->size(); |
| 131 | +} |
| 132 | + |
| 133 | +bool File::setBufferSize(size_t size) |
| 134 | +{ |
| 135 | + if (!*this) { |
| 136 | + return 0; |
| 137 | + } |
| 138 | + |
| 139 | + return _p->setBufferSize(size); |
| 140 | +} |
| 141 | + |
| 142 | +void File::close() |
| 143 | +{ |
| 144 | + if (_p) { |
| 145 | + _p->close(); |
| 146 | + _p = nullptr; |
| 147 | + } |
| 148 | +} |
| 149 | + |
| 150 | +File::operator bool() const |
| 151 | +{ |
| 152 | + return _p != nullptr && *_p != false; |
| 153 | +} |
| 154 | + |
| 155 | +const char* File::path() const |
| 156 | +{ |
| 157 | + if (!*this) { |
| 158 | + return nullptr; |
| 159 | + } |
| 160 | + |
| 161 | + return _p->path(); |
| 162 | +} |
| 163 | + |
| 164 | +const char* File::name() const |
| 165 | +{ |
| 166 | + if (!*this) { |
| 167 | + return nullptr; |
| 168 | + } |
| 169 | + |
| 170 | + return _p->name(); |
| 171 | +} |
| 172 | + |
| 173 | +//to implement |
| 174 | +boolean File::isDirectory(void) |
| 175 | +{ |
| 176 | + if (!*this) { |
| 177 | + return false; |
| 178 | + } |
| 179 | + return _p->isDirectory(); |
| 180 | +} |
| 181 | + |
| 182 | +File File::openNextFile(const char* mode) |
| 183 | +{ |
| 184 | + if (!*this) { |
| 185 | + return File(); |
| 186 | + } |
| 187 | + return _p->openNextFile(mode); |
| 188 | +} |
| 189 | + |
| 190 | +boolean File::seekDir(long position){ |
| 191 | + if(!_p){ |
| 192 | + return false; |
| 193 | + } |
| 194 | + return _p->seekDir(position); |
| 195 | +} |
| 196 | + |
| 197 | +String File::getNextFileName(void) |
| 198 | +{ |
| 199 | + if (!_p) { |
| 200 | + return ""; |
| 201 | + } |
| 202 | + return _p->getNextFileName(); |
| 203 | + |
| 204 | +} |
| 205 | + |
| 206 | + |
| 207 | +void File::rewindDirectory(void) |
| 208 | +{ |
| 209 | + if (!*this) { |
| 210 | + return; |
| 211 | + } |
| 212 | + _p->rewindDirectory(); |
| 213 | +} |
| 214 | + |
| 215 | +File FS::open(const String& path, const char* mode, const bool create) |
| 216 | +{ |
| 217 | + return open(path.c_str(), mode, create); |
| 218 | +} |
| 219 | + |
| 220 | +File FS::open(const char* path, const char* mode, const bool create) |
| 221 | +{ |
| 222 | + if (!_impl) { |
| 223 | + return File(); |
| 224 | + } |
| 225 | + |
| 226 | + return File(_impl->open(path, mode, create)); |
| 227 | +} |
| 228 | + |
| 229 | +bool FS::exists(const char* path) |
| 230 | +{ |
| 231 | + if (!_impl) { |
| 232 | + return false; |
| 233 | + } |
| 234 | + return _impl->exists(path); |
| 235 | +} |
| 236 | + |
| 237 | +bool FS::exists(const String& path) |
| 238 | +{ |
| 239 | + return exists(path.c_str()); |
| 240 | +} |
| 241 | + |
| 242 | +bool FS::remove(const char* path) |
| 243 | +{ |
| 244 | + if (!_impl) { |
| 245 | + return false; |
| 246 | + } |
| 247 | + return _impl->remove(path); |
| 248 | +} |
| 249 | + |
| 250 | +bool FS::remove(const String& path) |
| 251 | +{ |
| 252 | + return remove(path.c_str()); |
| 253 | +} |
| 254 | + |
| 255 | +bool FS::rename(const char* pathFrom, const char* pathTo) |
| 256 | +{ |
| 257 | + if (!_impl) { |
| 258 | + return false; |
| 259 | + } |
| 260 | + return _impl->rename(pathFrom, pathTo); |
| 261 | +} |
| 262 | + |
| 263 | +bool FS::rename(const String& pathFrom, const String& pathTo) |
| 264 | +{ |
| 265 | + return rename(pathFrom.c_str(), pathTo.c_str()); |
| 266 | +} |
| 267 | + |
| 268 | + |
| 269 | +bool FS::mkdir(const char *path) |
| 270 | +{ |
| 271 | + if (!_impl) { |
| 272 | + return false; |
| 273 | + } |
| 274 | + return _impl->mkdir(path); |
| 275 | +} |
| 276 | + |
| 277 | +bool FS::mkdir(const String &path) |
| 278 | +{ |
| 279 | + return mkdir(path.c_str()); |
| 280 | +} |
| 281 | + |
| 282 | +bool FS::rmdir(const char *path) |
| 283 | +{ |
| 284 | + if (!_impl) { |
| 285 | + return false; |
| 286 | + } |
| 287 | + return _impl->rmdir(path); |
| 288 | +} |
| 289 | + |
| 290 | +bool FS::rmdir(const String &path) |
| 291 | +{ |
| 292 | + return rmdir(path.c_str()); |
| 293 | +} |
| 294 | + |
| 295 | + |
| 296 | +void FSImpl::mountpoint(const char * mp) |
| 297 | +{ |
| 298 | + _mountpoint = mp; |
| 299 | +} |
| 300 | + |
| 301 | +const char * FSImpl::mountpoint() |
| 302 | +{ |
| 303 | + return _mountpoint; |
| 304 | +} |
0 commit comments