Skip to content

Commit

Permalink
Added directory iterators.
Browse files Browse the repository at this point in the history
Stream types may provide their own directory iterators. Currently
implemented for both file and resource streams.
  • Loading branch information
akb825 committed Jan 4, 2025
1 parent 10093cd commit 6332ad0
Show file tree
Hide file tree
Showing 19 changed files with 604 additions and 95 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -156,8 +156,8 @@ jobs:
libs: win32
lib_type: Shared
cmake_args: "-DDEEPSEA_SHARED=ON"
- arch: x64
libs: win64
- arch: Win32
libs: win32
lib_type: Single-Shared
cmake_args: "-DDEEPSEA_SINGLE_SHARED=ON"
- arch: x64
Expand Down
17 changes: 1 addition & 16 deletions modules/Core/Core/include/DeepSea/Core/Streams/Endian.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,15 +33,7 @@ extern "C"

#if DS_MSC
// Assume always little endian.

/**
* @brief Macro set to 1 for big-endian CPUs, 0 for little-endian CPUs.
*/
#define DS_BIG_ENDIAN 0

/**
* @brief Macro set to 1 for big-endian CPUs, 0 for little-endian CPUs.
*/
#define DS_LITTLE_ENDIAN 1
#else
#if !defined(__BYTE_ORDER__)
Expand All @@ -58,18 +50,11 @@ extern "C"
#define DS_BIG_ENDIAN 1

/**
* @brief Macro set to 1 for big-endian CPUs, 0 for little-endian CPUs.
* @brief Macro set to 1 for little-endian CPUs, 0 for big-endian CPUs.
*/
#define DS_LITTLE_ENDIAN 0
#elif __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__
/**
* @brief Macro set to 1 for big-endian CPUs, 0 for little-endian CPUs.
*/
#define DS_BIG_ENDIAN 0

/**
* @brief Macro set to 1 for big-endian CPUs, 0 for little-endian CPUs.
*/
#define DS_LITTLE_ENDIAN 1
#else
#error Unsupported byte ordering.
Expand Down
29 changes: 29 additions & 0 deletions modules/Core/Core/include/DeepSea/Core/Streams/FileStream.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,35 @@ extern "C"
* @see dsFileStream
*/

/**
* @brief Starts iterating over a directory on the filesystem.
* @remark errno will be set on failure.
* @param path The path to the directory.
* @return The directory iterator or NULL if the directory cannot be iterated.
*/
DS_CORE_EXPORT dsDirectoryIterator dsFileStream_openDirectory(const char* path);

/**
* @brief Gets the next entry in a directory.
*
* The . and .. entries will be implicitly skipped.
*
* @remark errno will be set on failure.
* @param[out] outEntry The entry to populate.
* @param iterator The iterator to get the next entry with.
* @return The result of getting the next entry.
*/
DS_CORE_EXPORT dsDirectoryEntryResult dsFileStream_nextDirectoryEntry(
dsDirectoryEntry* outEntry, dsDirectoryIterator iterator);

/**
* @brief Closes a directory.
* @remark errno will be set on failure.
* @param iterator The directory iterator to close.
* @return False if the directory couldn't be closed.
*/
DS_CORE_EXPORT bool dsFileStream_closeDirectory(dsDirectoryIterator iterator);

/**
* @brief Opens a file stream with a file path.
* @remark errno will be set on failure.
Expand Down
45 changes: 39 additions & 6 deletions modules/Core/Core/include/DeepSea/Core/Streams/ResourceStream.h
Original file line number Diff line number Diff line change
Expand Up @@ -45,37 +45,37 @@ DS_CORE_EXPORT bool dsResourceStream_setContext(void* globalContext, void* appli
* @brief Gets the directory for embedded resources.
* @return The embedded directory.
*/
DS_CORE_EXPORT const char* dsResourceStream_getEmbeddedDir(void);
DS_CORE_EXPORT const char* dsResourceStream_getEmbeddedDirectory(void);

/**
* @brief Sets the directory for embedded resources.
* @param dir The embedded directory.
*/
DS_CORE_EXPORT void dsResourceStream_setEmbeddedDir(const char* dir);
DS_CORE_EXPORT void dsResourceStream_setEmbeddedDirectory(const char* dir);

/**
* @brief Gets the directory for local resources.
* @return The local directory.
*/
DS_CORE_EXPORT const char* dsResourceStream_getLocalDir(void);
DS_CORE_EXPORT const char* dsResourceStream_getLocalDirectory(void);

/**
* @brief Sets the directory for local resources.
* @param dir The local directory.
*/
DS_CORE_EXPORT void dsResourceStream_setLocalDir(const char* dir);
DS_CORE_EXPORT void dsResourceStream_setLocalDirectory(const char* dir);

/**
* @brief Gets the directory for dynamic resources.
* @return The dynamic directory.
*/
DS_CORE_EXPORT const char* dsResourceStream_getDynamicDir(void);
DS_CORE_EXPORT const char* dsResourceStream_getDynamicDirectory(void);

/**
* @brief Sets the directory for dynamic resources.
* @param dir The dynamic directory.
*/
DS_CORE_EXPORT void dsResourceStream_setDynamicDir(const char* dir);
DS_CORE_EXPORT void dsResourceStream_setDynamicDirectory(const char* dir);

/**
* @brief Gets whether or not a resource type will be a file.
Expand Down Expand Up @@ -103,6 +103,39 @@ DS_CORE_EXPORT const char* dsResourceStream_getDirectory(dsFileResourceType type
DS_CORE_EXPORT bool dsResourceStream_getPath(char* outResult, size_t resultSize,
dsFileResourceType type, const char* path);

/**
* @brief Starts iterating over a directory from a resource.
* @remark errno will be set on failure.
* @param type The resource type.
* @param path The path to the directory.
* @return The directory iterator or NULL if the directory cannot be iterated.
*/
DS_CORE_EXPORT dsDirectoryIterator dsResourceStream_openDirectory(
dsFileResourceType type, const char* path);

/**
* @brief Gets the next entry in a directory.
*
* The . and .. entries will be implicitly skipped.
*
* @remark When listing a directory for an embedded path on Android, due to limitations of the
* NDK only file entries will be listed.
* @remark errno will be set on failure.
* @param[out] outEntry The entry to populate.
* @param iterator The iterator to get the next entry with.
* @return The result of getting the next entry.
*/
DS_CORE_EXPORT dsDirectoryEntryResult dsResourceStream_nextDirectoryEntry(
dsDirectoryEntry* outEntry, dsDirectoryIterator iterator);

/**
* @brief Closes a directory.
* @remark errno will be set on failure.
* @param iterator The directory iterator to close.
* @return False if the directory couldn't be closed.
*/
DS_CORE_EXPORT bool dsResourceStream_closeDirectory(dsDirectoryIterator iterator);

/**
* @brief Opens a stream for a resource.
* @remark errno will be set on failure.
Expand Down
47 changes: 46 additions & 1 deletion modules/Core/Core/include/DeepSea/Core/Streams/Types.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
#include <stddef.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>

#if DS_LINUX
#include <linux/limits.h>
Expand All @@ -40,19 +41,30 @@ extern "C"

#if DS_WINDOWS
#define DS_PATH_MAX (_MAX_PATH + 1)
#define DS_FILE_NAME_MAX DS_PATH_MAX
#define DS_PATH_SEPARATOR '\\'
#define DS_PATH_ALT_SEPARATOR '/'
#else
/**
* @brief Define for the typical maximum length of a path.
*
* There are cases on some filesystems where the length can exceed this path, but this should be
* There are cases on some filesystems where the length can exceed this limit, but this should be
* sufficient for typical cases.
*
* This includes space for the NULL terminator at the end of the string.
*/
#define DS_PATH_MAX (PATH_MAX + 1)

/**
* @brief Define for the typical maximum length of a file name.
*
* There are cases on some filesystems where the length can exceed this limit, but this should be
* sufficient for typical cases.
*
* This includes space for the NULL terminator at the end of the string.
*/
#define DS_FILE_NAME_MAX (NAME_MAX + 1)

/**
* @brief The main path separator for the current platform.
*/
Expand Down Expand Up @@ -112,6 +124,16 @@ typedef enum dsFileStatus
dsFileStatus_ExistsDirectory ///< File exists as a directory.
} dsFileStatus;

/**
* @brief Enum for the result of retrieving a directory entry.
*/
typedef enum dsDirectoryEntryResult
{
dsDirectoryEntryResult_Success, ///< The directory entry was successfully retrieved.
dsDirectoryEntryResult_End, ///< The end of the directory was reached.
dsDirectoryEntryResult_Error ///< An error occurred when getting the entry.
} dsDirectoryEntryResult;

/**
* @brief Function for reading from a stream.
* @param stream The stream to read from.
Expand Down Expand Up @@ -341,6 +363,29 @@ typedef struct dsResourceStream
bool isFile;
} dsResourceStream;

/**
* @brief Opque type for an iterator over a directory.
*
* It is only valid to use a dsDirectoryIterator instance with the stream type that created it.
*/
typedef void* dsDirectoryIterator;

/**
* @brief Structure that defines an entry within a directory.
*/
typedef struct dsDirectoryEntry
{
/**
* @brief Whether this entry is itself a directory.
*/
bool isDirectory;

/**
* @brief The name of the entry.
*/
char name[DS_FILE_NAME_MAX];
} dsDirectoryEntry;

#ifdef __cplusplus
}
#endif
Loading

0 comments on commit 6332ad0

Please sign in to comment.