Entrega 3er corte - sistemas embebidos

Committer:
Bethory
Date:
Wed May 30 00:01:50 2018 +0000
Revision:
0:6ad07c9019fd
Codigo de tales para todos los pasculaes

Who changed what in which revision?

UserRevisionLine numberNew contents of line
Bethory 0:6ad07c9019fd 1 /* mbed Microcontroller Library
Bethory 0:6ad07c9019fd 2 * Copyright (c) 2015 ARM Limited
Bethory 0:6ad07c9019fd 3 *
Bethory 0:6ad07c9019fd 4 * Licensed under the Apache License, Version 2.0 (the "License");
Bethory 0:6ad07c9019fd 5 * you may not use this file except in compliance with the License.
Bethory 0:6ad07c9019fd 6 * You may obtain a copy of the License at
Bethory 0:6ad07c9019fd 7 *
Bethory 0:6ad07c9019fd 8 * http://www.apache.org/licenses/LICENSE-2.0
Bethory 0:6ad07c9019fd 9 *
Bethory 0:6ad07c9019fd 10 * Unless required by applicable law or agreed to in writing, software
Bethory 0:6ad07c9019fd 11 * distributed under the License is distributed on an "AS IS" BASIS,
Bethory 0:6ad07c9019fd 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
Bethory 0:6ad07c9019fd 13 * See the License for the specific language governing permissions and
Bethory 0:6ad07c9019fd 14 * limitations under the License.
Bethory 0:6ad07c9019fd 15 */
Bethory 0:6ad07c9019fd 16
Bethory 0:6ad07c9019fd 17 #include "Dir.h"
Bethory 0:6ad07c9019fd 18 #include "mbed.h"
Bethory 0:6ad07c9019fd 19 #include <errno.h>
Bethory 0:6ad07c9019fd 20
Bethory 0:6ad07c9019fd 21
Bethory 0:6ad07c9019fd 22 Dir::Dir()
Bethory 0:6ad07c9019fd 23 : _fs(0), _dir(0)
Bethory 0:6ad07c9019fd 24 {
Bethory 0:6ad07c9019fd 25 }
Bethory 0:6ad07c9019fd 26
Bethory 0:6ad07c9019fd 27 Dir::Dir(FileSystem *fs, const char *path)
Bethory 0:6ad07c9019fd 28 : _fs(0), _dir(0)
Bethory 0:6ad07c9019fd 29 {
Bethory 0:6ad07c9019fd 30 open(fs, path);
Bethory 0:6ad07c9019fd 31 }
Bethory 0:6ad07c9019fd 32
Bethory 0:6ad07c9019fd 33 Dir::~Dir()
Bethory 0:6ad07c9019fd 34 {
Bethory 0:6ad07c9019fd 35 if (_fs) {
Bethory 0:6ad07c9019fd 36 close();
Bethory 0:6ad07c9019fd 37 }
Bethory 0:6ad07c9019fd 38 }
Bethory 0:6ad07c9019fd 39
Bethory 0:6ad07c9019fd 40 int Dir::open(FileSystem *fs, const char *path)
Bethory 0:6ad07c9019fd 41 {
Bethory 0:6ad07c9019fd 42 if (_fs) {
Bethory 0:6ad07c9019fd 43 return -EINVAL;
Bethory 0:6ad07c9019fd 44 }
Bethory 0:6ad07c9019fd 45
Bethory 0:6ad07c9019fd 46 int err = fs->dir_open(&_dir, path);
Bethory 0:6ad07c9019fd 47 if (!err) {
Bethory 0:6ad07c9019fd 48 _fs = fs;
Bethory 0:6ad07c9019fd 49 }
Bethory 0:6ad07c9019fd 50
Bethory 0:6ad07c9019fd 51 return err;
Bethory 0:6ad07c9019fd 52 }
Bethory 0:6ad07c9019fd 53
Bethory 0:6ad07c9019fd 54 int Dir::close()
Bethory 0:6ad07c9019fd 55 {
Bethory 0:6ad07c9019fd 56 if (!_fs) {
Bethory 0:6ad07c9019fd 57 return -EINVAL;
Bethory 0:6ad07c9019fd 58 }
Bethory 0:6ad07c9019fd 59
Bethory 0:6ad07c9019fd 60 int err = _fs->dir_close(_dir);
Bethory 0:6ad07c9019fd 61 _fs = 0;
Bethory 0:6ad07c9019fd 62 return err;
Bethory 0:6ad07c9019fd 63 }
Bethory 0:6ad07c9019fd 64
Bethory 0:6ad07c9019fd 65 ssize_t Dir::read(struct dirent *ent)
Bethory 0:6ad07c9019fd 66 {
Bethory 0:6ad07c9019fd 67 MBED_ASSERT(_fs);
Bethory 0:6ad07c9019fd 68 memset(ent, 0, sizeof(struct dirent));
Bethory 0:6ad07c9019fd 69 return _fs->dir_read(_dir, ent);
Bethory 0:6ad07c9019fd 70 }
Bethory 0:6ad07c9019fd 71
Bethory 0:6ad07c9019fd 72 void Dir::seek(off_t offset)
Bethory 0:6ad07c9019fd 73 {
Bethory 0:6ad07c9019fd 74 MBED_ASSERT(_fs);
Bethory 0:6ad07c9019fd 75 return _fs->dir_seek(_dir, offset);
Bethory 0:6ad07c9019fd 76 }
Bethory 0:6ad07c9019fd 77
Bethory 0:6ad07c9019fd 78 off_t Dir::tell()
Bethory 0:6ad07c9019fd 79 {
Bethory 0:6ad07c9019fd 80 MBED_ASSERT(_fs);
Bethory 0:6ad07c9019fd 81 return _fs->dir_tell(_dir);
Bethory 0:6ad07c9019fd 82 }
Bethory 0:6ad07c9019fd 83
Bethory 0:6ad07c9019fd 84 void Dir::rewind()
Bethory 0:6ad07c9019fd 85 {
Bethory 0:6ad07c9019fd 86 MBED_ASSERT(_fs);
Bethory 0:6ad07c9019fd 87 return _fs->dir_rewind(_dir);
Bethory 0:6ad07c9019fd 88 }
Bethory 0:6ad07c9019fd 89
Bethory 0:6ad07c9019fd 90 size_t Dir::size()
Bethory 0:6ad07c9019fd 91 {
Bethory 0:6ad07c9019fd 92 MBED_ASSERT(_fs);
Bethory 0:6ad07c9019fd 93 return _fs->dir_size(_dir);
Bethory 0:6ad07c9019fd 94 }