mbed-os for GR-LYCHEE

Dependents:   mbed-os-example-blinky-gr-lychee GR-Boads_Camera_sample GR-Boards_Audio_Recoder GR-Boads_Camera_DisplayApp ... more

Committer:
dkato
Date:
Fri Feb 02 05:42:23 2018 +0000
Revision:
0:f782d9c66c49
mbed-os for GR-LYCHEE

Who changed what in which revision?

UserRevisionLine numberNew contents of line
dkato 0:f782d9c66c49 1 /* mbed Microcontroller Library
dkato 0:f782d9c66c49 2 * Copyright (c) 2015 ARM Limited
dkato 0:f782d9c66c49 3 *
dkato 0:f782d9c66c49 4 * Licensed under the Apache License, Version 2.0 (the "License");
dkato 0:f782d9c66c49 5 * you may not use this file except in compliance with the License.
dkato 0:f782d9c66c49 6 * You may obtain a copy of the License at
dkato 0:f782d9c66c49 7 *
dkato 0:f782d9c66c49 8 * http://www.apache.org/licenses/LICENSE-2.0
dkato 0:f782d9c66c49 9 *
dkato 0:f782d9c66c49 10 * Unless required by applicable law or agreed to in writing, software
dkato 0:f782d9c66c49 11 * distributed under the License is distributed on an "AS IS" BASIS,
dkato 0:f782d9c66c49 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
dkato 0:f782d9c66c49 13 * See the License for the specific language governing permissions and
dkato 0:f782d9c66c49 14 * limitations under the License.
dkato 0:f782d9c66c49 15 */
dkato 0:f782d9c66c49 16
dkato 0:f782d9c66c49 17 #include "File.h"
dkato 0:f782d9c66c49 18 #include "mbed.h"
dkato 0:f782d9c66c49 19 #include <errno.h>
dkato 0:f782d9c66c49 20
dkato 0:f782d9c66c49 21
dkato 0:f782d9c66c49 22 File::File()
dkato 0:f782d9c66c49 23 : _fs(0), _file(0)
dkato 0:f782d9c66c49 24 {
dkato 0:f782d9c66c49 25 }
dkato 0:f782d9c66c49 26
dkato 0:f782d9c66c49 27 File::File(FileSystem *fs, const char *path, int flags)
dkato 0:f782d9c66c49 28 : FileLike(path), _fs(0), _file(0)
dkato 0:f782d9c66c49 29 {
dkato 0:f782d9c66c49 30 open(fs, path, flags);
dkato 0:f782d9c66c49 31 }
dkato 0:f782d9c66c49 32
dkato 0:f782d9c66c49 33 File::~File()
dkato 0:f782d9c66c49 34 {
dkato 0:f782d9c66c49 35 if (_fs) {
dkato 0:f782d9c66c49 36 close();
dkato 0:f782d9c66c49 37 }
dkato 0:f782d9c66c49 38 }
dkato 0:f782d9c66c49 39
dkato 0:f782d9c66c49 40 int File::open(FileSystem *fs, const char *path, int flags)
dkato 0:f782d9c66c49 41 {
dkato 0:f782d9c66c49 42 if (_fs) {
dkato 0:f782d9c66c49 43 return -EINVAL;
dkato 0:f782d9c66c49 44 }
dkato 0:f782d9c66c49 45
dkato 0:f782d9c66c49 46 int err = fs->file_open(&_file, path, flags);
dkato 0:f782d9c66c49 47 if (!err) {
dkato 0:f782d9c66c49 48 _fs = fs;
dkato 0:f782d9c66c49 49 }
dkato 0:f782d9c66c49 50
dkato 0:f782d9c66c49 51 return err;
dkato 0:f782d9c66c49 52 }
dkato 0:f782d9c66c49 53
dkato 0:f782d9c66c49 54 int File::close()
dkato 0:f782d9c66c49 55 {
dkato 0:f782d9c66c49 56 if (!_fs) {
dkato 0:f782d9c66c49 57 return -EINVAL;
dkato 0:f782d9c66c49 58 }
dkato 0:f782d9c66c49 59
dkato 0:f782d9c66c49 60 int err = _fs->file_close(_file);
dkato 0:f782d9c66c49 61 _fs = 0;
dkato 0:f782d9c66c49 62 return err;
dkato 0:f782d9c66c49 63 }
dkato 0:f782d9c66c49 64
dkato 0:f782d9c66c49 65 ssize_t File::read(void *buffer, size_t len)
dkato 0:f782d9c66c49 66 {
dkato 0:f782d9c66c49 67 MBED_ASSERT(_fs);
dkato 0:f782d9c66c49 68 return _fs->file_read(_file, buffer, len);
dkato 0:f782d9c66c49 69 }
dkato 0:f782d9c66c49 70
dkato 0:f782d9c66c49 71 ssize_t File::write(const void *buffer, size_t len)
dkato 0:f782d9c66c49 72 {
dkato 0:f782d9c66c49 73 MBED_ASSERT(_fs);
dkato 0:f782d9c66c49 74 return _fs->file_write(_file, buffer, len);
dkato 0:f782d9c66c49 75 }
dkato 0:f782d9c66c49 76
dkato 0:f782d9c66c49 77 int File::sync()
dkato 0:f782d9c66c49 78 {
dkato 0:f782d9c66c49 79 MBED_ASSERT(_fs);
dkato 0:f782d9c66c49 80 return _fs->file_sync(_file);
dkato 0:f782d9c66c49 81 }
dkato 0:f782d9c66c49 82
dkato 0:f782d9c66c49 83 int File::isatty()
dkato 0:f782d9c66c49 84 {
dkato 0:f782d9c66c49 85 MBED_ASSERT(_fs);
dkato 0:f782d9c66c49 86 return _fs->file_isatty(_file);
dkato 0:f782d9c66c49 87 }
dkato 0:f782d9c66c49 88
dkato 0:f782d9c66c49 89 off_t File::seek(off_t offset, int whence)
dkato 0:f782d9c66c49 90 {
dkato 0:f782d9c66c49 91 MBED_ASSERT(_fs);
dkato 0:f782d9c66c49 92 return _fs->file_seek(_file, offset, whence);
dkato 0:f782d9c66c49 93 }
dkato 0:f782d9c66c49 94
dkato 0:f782d9c66c49 95 off_t File::tell()
dkato 0:f782d9c66c49 96 {
dkato 0:f782d9c66c49 97 MBED_ASSERT(_fs);
dkato 0:f782d9c66c49 98 return _fs->file_tell(_file);
dkato 0:f782d9c66c49 99 }
dkato 0:f782d9c66c49 100
dkato 0:f782d9c66c49 101 void File::rewind()
dkato 0:f782d9c66c49 102 {
dkato 0:f782d9c66c49 103 MBED_ASSERT(_fs);
dkato 0:f782d9c66c49 104 return _fs->file_rewind(_file);
dkato 0:f782d9c66c49 105 }
dkato 0:f782d9c66c49 106
dkato 0:f782d9c66c49 107 size_t File::size()
dkato 0:f782d9c66c49 108 {
dkato 0:f782d9c66c49 109 MBED_ASSERT(_fs);
dkato 0:f782d9c66c49 110 return _fs->file_size(_file);
dkato 0:f782d9c66c49 111 }
dkato 0:f782d9c66c49 112