Renesas GR-PEACH OpenCV Development / gr-peach-opencv-project-sd-card_update

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

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers FileSystem.cpp Source File

FileSystem.cpp

00001 /* mbed Microcontroller Library
00002  * Copyright (c) 2006-2013 ARM Limited
00003  *
00004  * Licensed under the Apache License, Version 2.0 (the "License");
00005  * you may not use this file except in compliance with the License.
00006  * You may obtain a copy of the License at
00007  *
00008  *     http://www.apache.org/licenses/LICENSE-2.0
00009  *
00010  * Unless required by applicable law or agreed to in writing, software
00011  * distributed under the License is distributed on an "AS IS" BASIS,
00012  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00013  * See the License for the specific language governing permissions and
00014  * limitations under the License.
00015  */
00016 
00017 #include "mbed.h"
00018 #include "filesystem/FileSystem.h"
00019 #include <errno.h>
00020 
00021 
00022 FileSystem::FileSystem(const char *name)
00023     : FileSystemLike(name)
00024 {
00025 }
00026 
00027 int FileSystem::remove(const char *path)
00028 {
00029     return -ENOSYS;
00030 }
00031 
00032 int FileSystem::rename(const char *path, const char *newpath)
00033 {
00034     return -ENOSYS;
00035 }
00036 
00037 int FileSystem::stat(const char *path, struct stat *st)
00038 {
00039     return -ENOSYS;
00040 }
00041 
00042 int FileSystem::mkdir(const char *path, mode_t mode)
00043 {
00044     return -ENOSYS;
00045 }
00046 
00047 int FileSystem::file_sync(fs_file_t file)
00048 {
00049     return 0;
00050 }
00051 
00052 int FileSystem::file_isatty(fs_file_t file)
00053 {
00054     return false;
00055 }
00056 
00057 off_t FileSystem::file_tell(fs_file_t file)
00058 {
00059     return file_seek(file, 0, SEEK_CUR);
00060 }
00061 
00062 void FileSystem::file_rewind(fs_file_t file)
00063 {
00064     file_seek(file, 0, SEEK_SET);
00065 }
00066 
00067 off_t FileSystem::file_size(fs_file_t file)
00068 {
00069     off_t off = file_tell(file);
00070     off_t size = file_seek(file, 0, SEEK_END);
00071     file_seek(file, off, SEEK_SET);
00072     return size;
00073 }
00074 
00075 int FileSystem::dir_open(fs_dir_t *dir, const char *path)
00076 {
00077     return -ENOSYS;
00078 }
00079 
00080 int FileSystem::dir_close(fs_dir_t dir)
00081 {
00082     return -ENOSYS;
00083 }
00084 
00085 ssize_t FileSystem::dir_read(fs_dir_t dir, struct dirent *ent)
00086 {
00087     return -ENOSYS;
00088 }
00089 
00090 void FileSystem::dir_seek(fs_dir_t dir, off_t offset)
00091 {
00092 }
00093 
00094 off_t FileSystem::dir_tell(fs_dir_t dir)
00095 {
00096     return 0;
00097 }
00098 
00099 void FileSystem::dir_rewind(fs_dir_t dir)
00100 {
00101     // Note, the may not satisfy rewind on all filesystems
00102     dir_seek(dir, 0);
00103 }
00104 
00105 size_t FileSystem::dir_size(fs_dir_t dir)
00106 {
00107     off_t off = dir_tell(dir);
00108     size_t size = 0;
00109     struct dirent *ent = new struct dirent;
00110 
00111     dir_rewind(dir);
00112     while (true) {
00113         int res = dir_read(dir, ent);
00114         if (res <= 0) {
00115             break;
00116         }
00117 
00118         size += 1;
00119     }
00120     dir_seek(dir, off);
00121 
00122     delete ent;
00123     return size;
00124 }
00125 
00126 // Internally used file wrapper that manages memory on close
00127 template <typename F>
00128 class Managed : public F {
00129 public:
00130     virtual int close() {
00131         int err = F::close();
00132         delete this;
00133         return err;
00134     }
00135 };
00136 
00137 int FileSystem::open(FileHandle **file, const char *path, int flags)
00138 {
00139     File *f = new Managed<File>;
00140     int err = f->open(this, path, flags);
00141     if (err) {
00142         delete f;
00143         return err;
00144     }
00145 
00146     *file = f;
00147     return 0;
00148 }
00149 
00150 int FileSystem::open(DirHandle **dir, const char *path) {
00151     Dir *d = new Managed<Dir>;
00152     int err = d->open(this, path);
00153     if (err) {
00154         delete d;
00155         return err;
00156     }
00157 
00158     *dir = d;
00159     return 0;
00160 }
00161