Rtos API example

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers File.cpp Source File

File.cpp

00001 /* mbed Microcontroller Library
00002  * Copyright (c) 2015 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 "File.h"
00018 #include "mbed.h"
00019 #include <errno.h>
00020 
00021 
00022 File::File()
00023     : _fs(0), _file(0)
00024 {
00025 }
00026 
00027 File::File(FileSystem *fs, const char *path, int flags)
00028     : _fs(0), _file(0)
00029 {
00030     open(fs, path, flags);
00031 }
00032 
00033 File::~File()
00034 {
00035     if (_fs) {
00036         close();
00037     }
00038 }
00039 
00040 int File::open(FileSystem *fs, const char *path, int flags)
00041 {
00042     if (_fs) {
00043         return -EINVAL;
00044     }
00045 
00046     int err = fs->file_open(&_file, path, flags);
00047     if (!err) {
00048         _fs = fs;
00049     }
00050 
00051     return err;
00052 }
00053 
00054 int File::close()
00055 {
00056     if (!_fs) {
00057         return -EINVAL;
00058     }
00059 
00060     int err = _fs->file_close(_file);
00061     _fs = 0;
00062     return err;
00063 }
00064 
00065 ssize_t File::read(void *buffer, size_t len)
00066 {
00067     MBED_ASSERT(_fs);
00068     return _fs->file_read(_file, buffer, len);
00069 }
00070 
00071 ssize_t File::write(const void *buffer, size_t len)
00072 {
00073     MBED_ASSERT(_fs);
00074     return _fs->file_write(_file, buffer, len);
00075 }
00076 
00077 int File::sync()
00078 {
00079     MBED_ASSERT(_fs);
00080     return _fs->file_sync(_file);
00081 }
00082 
00083 int File::isatty()
00084 {
00085     MBED_ASSERT(_fs);
00086     return _fs->file_isatty(_file);
00087 }
00088 
00089 off_t File::seek(off_t offset, int whence)
00090 {
00091     MBED_ASSERT(_fs);
00092     return _fs->file_seek(_file, offset, whence);
00093 }
00094 
00095 off_t File::tell()
00096 {
00097     MBED_ASSERT(_fs);
00098     return _fs->file_tell(_file);
00099 }
00100 
00101 void File::rewind()
00102 {
00103     MBED_ASSERT(_fs);
00104     return _fs->file_rewind(_file);
00105 }
00106 
00107 off_t File::size()
00108 {
00109     MBED_ASSERT(_fs);
00110     return _fs->file_size(_file);
00111 }
00112