2013-02-18 15:32:11 +00:00
|
|
|
/* mbed Microcontroller Library
|
|
|
|
* Copyright (c) 2006-2013 ARM Limited
|
|
|
|
*
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
*/
|
|
|
|
#ifndef MBED_DIRHANDLE_H
|
|
|
|
#define MBED_DIRHANDLE_H
|
|
|
|
|
2017-02-06 20:31:01 +00:00
|
|
|
#include <stdint.h>
|
2017-02-13 19:19:57 +00:00
|
|
|
#include "platform/platform.h"
|
2017-03-07 17:11:20 +00:00
|
|
|
#include "platform/FileHandle.h"
|
2013-02-18 15:32:11 +00:00
|
|
|
|
|
|
|
namespace mbed {
|
2017-04-04 19:21:53 +00:00
|
|
|
/** \addtogroup platform */
|
2013-02-18 15:32:11 +00:00
|
|
|
|
2017-03-07 17:11:20 +00:00
|
|
|
|
2013-02-18 15:32:11 +00:00
|
|
|
/** Represents a directory stream. Objects of this type are returned
|
2017-03-07 17:11:20 +00:00
|
|
|
* by an opendir function. The core functions are read and seek,
|
|
|
|
* but only a subset needs to be provided.
|
2013-02-18 15:32:11 +00:00
|
|
|
*
|
|
|
|
* If a FileSystemLike class defines the opendir method, then the
|
|
|
|
* directories of an object of that type can be accessed by
|
|
|
|
* DIR *d = opendir("/example/directory") (or opendir("/example")
|
|
|
|
* to open the root of the filesystem), and then using readdir(d) etc.
|
|
|
|
*
|
2017-03-07 17:11:20 +00:00
|
|
|
* The root directory is considered to contain all FileHandle and
|
|
|
|
* FileSystem objects, so the DIR* returned by opendir("/") will
|
2013-02-18 15:32:11 +00:00
|
|
|
* reflect this.
|
2016-06-08 12:52:14 +00:00
|
|
|
*
|
2017-03-07 17:11:20 +00:00
|
|
|
* @note to create a directory, @see Dir
|
2017-04-25 19:37:08 +00:00
|
|
|
* @note Synchronization level: Set by subclass
|
2017-04-04 19:21:53 +00:00
|
|
|
* @ingroup platform
|
2013-02-18 15:32:11 +00:00
|
|
|
*/
|
|
|
|
class DirHandle {
|
|
|
|
public:
|
2017-03-07 17:11:20 +00:00
|
|
|
virtual ~DirHandle() {}
|
|
|
|
|
|
|
|
/** Read the next directory entry
|
|
|
|
*
|
|
|
|
* @param ent The directory entry to fill out
|
|
|
|
* @return 1 on reading a filename, 0 at end of directory, negative error on failure
|
|
|
|
*/
|
|
|
|
virtual ssize_t read(struct dirent *ent) = 0;
|
|
|
|
|
|
|
|
/** Close a directory
|
|
|
|
*
|
|
|
|
* return 0 on success, negative error code on failure
|
|
|
|
*/
|
|
|
|
virtual int close() = 0;
|
|
|
|
|
|
|
|
/** Set the current position of the directory
|
|
|
|
*
|
|
|
|
* @param offset Offset of the location to seek to,
|
|
|
|
* must be a value returned from tell
|
|
|
|
*/
|
|
|
|
virtual void seek(off_t offset) = 0;
|
|
|
|
|
|
|
|
/** Get the current position of the directory
|
|
|
|
*
|
|
|
|
* @return Position of the directory that can be passed to rewind
|
|
|
|
*/
|
|
|
|
virtual off_t tell() = 0;
|
|
|
|
|
|
|
|
/** Rewind the current position to the beginning of the directory
|
|
|
|
*/
|
|
|
|
virtual void rewind() = 0;
|
|
|
|
|
|
|
|
/** Get the sizeof the directory
|
|
|
|
*
|
|
|
|
* @return Number of files in the directory
|
|
|
|
*/
|
|
|
|
virtual size_t size()
|
|
|
|
{
|
|
|
|
off_t off = tell();
|
|
|
|
size_t size = 0;
|
|
|
|
struct dirent *ent = new struct dirent;
|
|
|
|
|
|
|
|
rewind();
|
|
|
|
while (read(ent) > 0) {
|
|
|
|
size += 1;
|
|
|
|
}
|
|
|
|
seek(off);
|
|
|
|
|
|
|
|
delete ent;
|
|
|
|
return size;
|
|
|
|
}
|
2017-02-13 19:19:57 +00:00
|
|
|
|
2013-02-18 15:32:11 +00:00
|
|
|
/** Closes the directory.
|
|
|
|
*
|
|
|
|
* @returns
|
|
|
|
* 0 on success,
|
|
|
|
* -1 on error.
|
|
|
|
*/
|
2017-03-07 17:11:20 +00:00
|
|
|
MBED_DEPRECATED_SINCE("mbed-os-5.4", "Replaced by DirHandle::close")
|
|
|
|
virtual int closedir() { return close(); };
|
2013-02-18 15:32:11 +00:00
|
|
|
|
|
|
|
/** Return the directory entry at the current position, and
|
|
|
|
* advances the position to the next entry.
|
|
|
|
*
|
|
|
|
* @returns
|
|
|
|
* A pointer to a dirent structure representing the
|
|
|
|
* directory entry at the current position, or NULL on reaching
|
|
|
|
* end of directory or error.
|
|
|
|
*/
|
2017-03-07 17:11:20 +00:00
|
|
|
MBED_DEPRECATED_SINCE("mbed-os-5.4", "Replaced by DirHandle::read")
|
|
|
|
virtual struct dirent *readdir()
|
|
|
|
{
|
|
|
|
static struct dirent ent;
|
|
|
|
return (read(&ent) > 0) ? &ent : NULL;
|
|
|
|
}
|
2013-02-18 15:32:11 +00:00
|
|
|
|
|
|
|
/** Resets the position to the beginning of the directory.
|
|
|
|
*/
|
2017-03-07 17:11:20 +00:00
|
|
|
MBED_DEPRECATED_SINCE("mbed-os-5.4", "Replaced by DirHandle::rewind")
|
|
|
|
virtual void rewinddir() { rewind(); }
|
2013-02-18 15:32:11 +00:00
|
|
|
|
|
|
|
/** Returns the current position of the DirHandle.
|
|
|
|
*
|
|
|
|
* @returns
|
|
|
|
* the current position,
|
|
|
|
* -1 on error.
|
|
|
|
*/
|
2017-03-07 17:11:20 +00:00
|
|
|
MBED_DEPRECATED_SINCE("mbed-os-5.4", "Replaced by DirHandle::tell")
|
|
|
|
virtual off_t telldir() { return tell(); }
|
2013-02-18 15:32:11 +00:00
|
|
|
|
|
|
|
/** Sets the position of the DirHandle.
|
|
|
|
*
|
|
|
|
* @param location The location to seek to. Must be a value returned by telldir.
|
|
|
|
*/
|
2017-03-07 17:11:20 +00:00
|
|
|
MBED_DEPRECATED_SINCE("mbed-os-5.4", "Replaced by DirHandle::seek")
|
|
|
|
virtual void seekdir(off_t location) { seek(location); }
|
2013-02-18 15:32:11 +00:00
|
|
|
};
|
|
|
|
|
2017-03-07 17:11:20 +00:00
|
|
|
|
2013-02-18 15:32:11 +00:00
|
|
|
} // namespace mbed
|
|
|
|
|
|
|
|
#endif /* MBED_DIRHANDLE_H */
|