SDCard version

Fork of gr-peach-opencv-project-sd-card by the do

Committer:
thedo
Date:
Fri Jul 21 01:26:54 2017 +0000
Revision:
167:2ee3e82cb6f5
Parent:
166:240bc5a0f42a
gr-peach-opencv-project-sd-card

Who changed what in which revision?

UserRevisionLine numberNew contents of line
thedo 166:240bc5a0f42a 1 /* mbed Microcontroller Library
thedo 166:240bc5a0f42a 2 * Copyright (c) 2006-2013 ARM Limited
thedo 166:240bc5a0f42a 3 *
thedo 166:240bc5a0f42a 4 * Licensed under the Apache License, Version 2.0 (the "License");
thedo 166:240bc5a0f42a 5 * you may not use this file except in compliance with the License.
thedo 166:240bc5a0f42a 6 * You may obtain a copy of the License at
thedo 166:240bc5a0f42a 7 *
thedo 166:240bc5a0f42a 8 * http://www.apache.org/licenses/LICENSE-2.0
thedo 166:240bc5a0f42a 9 *
thedo 166:240bc5a0f42a 10 * Unless required by applicable law or agreed to in writing, software
thedo 166:240bc5a0f42a 11 * distributed under the License is distributed on an "AS IS" BASIS,
thedo 166:240bc5a0f42a 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
thedo 166:240bc5a0f42a 13 * See the License for the specific language governing permissions and
thedo 166:240bc5a0f42a 14 * limitations under the License.
thedo 166:240bc5a0f42a 15 */
thedo 166:240bc5a0f42a 16
thedo 166:240bc5a0f42a 17 #include "mbed.h"
thedo 166:240bc5a0f42a 18 #include "filesystem/FileSystem.h"
thedo 166:240bc5a0f42a 19 #include <errno.h>
thedo 166:240bc5a0f42a 20
thedo 166:240bc5a0f42a 21
thedo 166:240bc5a0f42a 22 FileSystem::FileSystem(const char *name)
thedo 166:240bc5a0f42a 23 : FileSystemLike(name)
thedo 166:240bc5a0f42a 24 {
thedo 166:240bc5a0f42a 25 }
thedo 166:240bc5a0f42a 26
thedo 166:240bc5a0f42a 27 int FileSystem::remove(const char *path)
thedo 166:240bc5a0f42a 28 {
thedo 166:240bc5a0f42a 29 return -ENOSYS;
thedo 166:240bc5a0f42a 30 }
thedo 166:240bc5a0f42a 31
thedo 166:240bc5a0f42a 32 int FileSystem::rename(const char *path, const char *newpath)
thedo 166:240bc5a0f42a 33 {
thedo 166:240bc5a0f42a 34 return -ENOSYS;
thedo 166:240bc5a0f42a 35 }
thedo 166:240bc5a0f42a 36
thedo 166:240bc5a0f42a 37 int FileSystem::stat(const char *path, struct stat *st)
thedo 166:240bc5a0f42a 38 {
thedo 166:240bc5a0f42a 39 return -ENOSYS;
thedo 166:240bc5a0f42a 40 }
thedo 166:240bc5a0f42a 41
thedo 166:240bc5a0f42a 42 int FileSystem::mkdir(const char *path, mode_t mode)
thedo 166:240bc5a0f42a 43 {
thedo 166:240bc5a0f42a 44 return -ENOSYS;
thedo 166:240bc5a0f42a 45 }
thedo 166:240bc5a0f42a 46
thedo 166:240bc5a0f42a 47 int FileSystem::file_sync(fs_file_t file)
thedo 166:240bc5a0f42a 48 {
thedo 166:240bc5a0f42a 49 return 0;
thedo 166:240bc5a0f42a 50 }
thedo 166:240bc5a0f42a 51
thedo 166:240bc5a0f42a 52 int FileSystem::file_isatty(fs_file_t file)
thedo 166:240bc5a0f42a 53 {
thedo 166:240bc5a0f42a 54 return false;
thedo 166:240bc5a0f42a 55 }
thedo 166:240bc5a0f42a 56
thedo 166:240bc5a0f42a 57 off_t FileSystem::file_tell(fs_file_t file)
thedo 166:240bc5a0f42a 58 {
thedo 166:240bc5a0f42a 59 return file_seek(file, 0, SEEK_CUR);
thedo 166:240bc5a0f42a 60 }
thedo 166:240bc5a0f42a 61
thedo 166:240bc5a0f42a 62 void FileSystem::file_rewind(fs_file_t file)
thedo 166:240bc5a0f42a 63 {
thedo 166:240bc5a0f42a 64 file_seek(file, 0, SEEK_SET);
thedo 166:240bc5a0f42a 65 }
thedo 166:240bc5a0f42a 66
thedo 166:240bc5a0f42a 67 off_t FileSystem::file_size(fs_file_t file)
thedo 166:240bc5a0f42a 68 {
thedo 166:240bc5a0f42a 69 off_t off = file_tell(file);
thedo 166:240bc5a0f42a 70 off_t size = file_seek(file, 0, SEEK_END);
thedo 166:240bc5a0f42a 71 file_seek(file, off, SEEK_SET);
thedo 166:240bc5a0f42a 72 return size;
thedo 166:240bc5a0f42a 73 }
thedo 166:240bc5a0f42a 74
thedo 166:240bc5a0f42a 75 int FileSystem::dir_open(fs_dir_t *dir, const char *path)
thedo 166:240bc5a0f42a 76 {
thedo 166:240bc5a0f42a 77 return -ENOSYS;
thedo 166:240bc5a0f42a 78 }
thedo 166:240bc5a0f42a 79
thedo 166:240bc5a0f42a 80 int FileSystem::dir_close(fs_dir_t dir)
thedo 166:240bc5a0f42a 81 {
thedo 166:240bc5a0f42a 82 return -ENOSYS;
thedo 166:240bc5a0f42a 83 }
thedo 166:240bc5a0f42a 84
thedo 166:240bc5a0f42a 85 ssize_t FileSystem::dir_read(fs_dir_t dir, struct dirent *ent)
thedo 166:240bc5a0f42a 86 {
thedo 166:240bc5a0f42a 87 return -ENOSYS;
thedo 166:240bc5a0f42a 88 }
thedo 166:240bc5a0f42a 89
thedo 166:240bc5a0f42a 90 void FileSystem::dir_seek(fs_dir_t dir, off_t offset)
thedo 166:240bc5a0f42a 91 {
thedo 166:240bc5a0f42a 92 }
thedo 166:240bc5a0f42a 93
thedo 166:240bc5a0f42a 94 off_t FileSystem::dir_tell(fs_dir_t dir)
thedo 166:240bc5a0f42a 95 {
thedo 166:240bc5a0f42a 96 return 0;
thedo 166:240bc5a0f42a 97 }
thedo 166:240bc5a0f42a 98
thedo 166:240bc5a0f42a 99 void FileSystem::dir_rewind(fs_dir_t dir)
thedo 166:240bc5a0f42a 100 {
thedo 166:240bc5a0f42a 101 // Note, the may not satisfy rewind on all filesystems
thedo 166:240bc5a0f42a 102 dir_seek(dir, 0);
thedo 166:240bc5a0f42a 103 }
thedo 166:240bc5a0f42a 104
thedo 166:240bc5a0f42a 105 size_t FileSystem::dir_size(fs_dir_t dir)
thedo 166:240bc5a0f42a 106 {
thedo 166:240bc5a0f42a 107 off_t off = dir_tell(dir);
thedo 166:240bc5a0f42a 108 size_t size = 0;
thedo 166:240bc5a0f42a 109 struct dirent *ent = new struct dirent;
thedo 166:240bc5a0f42a 110
thedo 166:240bc5a0f42a 111 dir_rewind(dir);
thedo 166:240bc5a0f42a 112 while (true) {
thedo 166:240bc5a0f42a 113 int res = dir_read(dir, ent);
thedo 166:240bc5a0f42a 114 if (res <= 0) {
thedo 166:240bc5a0f42a 115 break;
thedo 166:240bc5a0f42a 116 }
thedo 166:240bc5a0f42a 117
thedo 166:240bc5a0f42a 118 size += 1;
thedo 166:240bc5a0f42a 119 }
thedo 166:240bc5a0f42a 120 dir_seek(dir, off);
thedo 166:240bc5a0f42a 121
thedo 166:240bc5a0f42a 122 delete ent;
thedo 166:240bc5a0f42a 123 return size;
thedo 166:240bc5a0f42a 124 }
thedo 166:240bc5a0f42a 125
thedo 166:240bc5a0f42a 126 // Internally used file wrapper that manages memory on close
thedo 166:240bc5a0f42a 127 template <typename F>
thedo 166:240bc5a0f42a 128 class Managed : public F {
thedo 166:240bc5a0f42a 129 public:
thedo 166:240bc5a0f42a 130 virtual int close() {
thedo 166:240bc5a0f42a 131 int err = F::close();
thedo 166:240bc5a0f42a 132 delete this;
thedo 166:240bc5a0f42a 133 return err;
thedo 166:240bc5a0f42a 134 }
thedo 166:240bc5a0f42a 135 };
thedo 166:240bc5a0f42a 136
thedo 166:240bc5a0f42a 137 int FileSystem::open(FileHandle **file, const char *path, int flags)
thedo 166:240bc5a0f42a 138 {
thedo 166:240bc5a0f42a 139 File *f = new Managed<File>;
thedo 166:240bc5a0f42a 140 int err = f->open(this, path, flags);
thedo 166:240bc5a0f42a 141 if (err) {
thedo 166:240bc5a0f42a 142 delete f;
thedo 166:240bc5a0f42a 143 return err;
thedo 166:240bc5a0f42a 144 }
thedo 166:240bc5a0f42a 145
thedo 166:240bc5a0f42a 146 *file = f;
thedo 166:240bc5a0f42a 147 return 0;
thedo 166:240bc5a0f42a 148 }
thedo 166:240bc5a0f42a 149
thedo 166:240bc5a0f42a 150 int FileSystem::open(DirHandle **dir, const char *path) {
thedo 166:240bc5a0f42a 151 Dir *d = new Managed<Dir>;
thedo 166:240bc5a0f42a 152 int err = d->open(this, path);
thedo 166:240bc5a0f42a 153 if (err) {
thedo 166:240bc5a0f42a 154 delete d;
thedo 166:240bc5a0f42a 155 return err;
thedo 166:240bc5a0f42a 156 }
thedo 166:240bc5a0f42a 157
thedo 166:240bc5a0f42a 158 *dir = d;
thedo 166:240bc5a0f42a 159 return 0;
thedo 166:240bc5a0f42a 160 }
thedo 166:240bc5a0f42a 161