Pyrogenesis HEAD
Pyrogenesis, a RTS Engine
vfs_util.h
Go to the documentation of this file.
1/* Copyright (C) 2025 Wildfire Games.
2 *
3 * Permission is hereby granted, free of charge, to any person obtaining
4 * a copy of this software and associated documentation files (the
5 * "Software"), to deal in the Software without restriction, including
6 * without limitation the rights to use, copy, modify, merge, publish,
7 * distribute, sublicense, and/or sell copies of the Software, and to
8 * permit persons to whom the Software is furnished to do so, subject to
9 * the following conditions:
10 *
11 * The above copyright notice and this permission notice shall be included
12 * in all copies or substantial portions of the Software.
13 *
14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
17 * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
18 * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
19 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
20 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
21 */
22
23/*
24 * helper functions for directory access
25 */
26
27#ifndef INCLUDED_VFS_UTIL
28#define INCLUDED_VFS_UTIL
29
31#include "lib/file/vfs/vfs.h"
33#include "lib/status.h"
34
35#include <cstddef>
36#include <cstdint>
37#include <queue>
38
39namespace vfs
40{
41
42extern Status GetPathnames(const PIVFS& fs, const VfsPath& path, const wchar_t* filter, VfsPaths& pathnames);
43
44/**
45 * called for files in a directory.
46 *
47 * @param pathname full pathname (since CFileInfo only gives the name).
48 * @param fileInfo file information
49 * @param cbData user-specified context
50 * @return INFO::OK on success; any other value will immediately
51 * be returned to the caller (no more calls will be forthcoming).
52 *
53 * CAVEAT: pathname and fileInfo are only valid until the function
54 * returns!
55 **/
56typedef Status (*FileCallback)(const VfsPath& pathname, const CFileInfo& fileInfo, const uintptr_t cbData);
57
58/**
59 * called for directories in a directory.
60 *
61 * @param pathname full pathname
62 * @param cbData user-specified context
63 * @return INFO::OK on success; any other value will immediately
64 * be returned to the caller (no more calls will be forthcoming).
65 *
66 * CAVEAT: pathname only valid until the function returns!
67 **/
68typedef Status (*DirCallback)(const VfsPath& pathname, const uintptr_t cbData);
69
71{
73};
74
75/**
76 * call back for each file in a directory tree, and optionally each directory.
77 *
78 * @param fs
79 * @param path
80 * @param cb @ref FileCallback
81 * @param cbData
82 * @param pattern that file names must match. '*' and '&' wildcards
83 * are allowed. 0 matches everything.
84 * @param flags @ref DirFlags
85 * @param dircb @ref DirCallback
86 * @param dircbData
87 * @return Status
88 **/
89Status ForEachFile(const PIVFS& fs, const VfsPath& path, FileCallback cb, uintptr_t cbData, const wchar_t* pattern = 0, size_t flags = 0, DirCallback dircb = NULL, uintptr_t dircbData = 0);
90
92{
93 // (declare here to avoid reallocations)
96
97 // (a FIFO queue is more efficient than recursion because it uses less
98 // stack space and avoids seeks due to breadth-first traversal.)
99 std::queue<VfsPath> pendingDirectories;
100
101 ForEachFileContext(const VfsPath& startPath) { pendingDirectories.push(startPath / ""); }
102
103 bool empty() const { return pendingDirectories.empty(); }
104};
105// The same ForEachFile but allows to pause the iteration.
106Status ForEachFileNext(ForEachFileContext& context, const PIVFS& fs, FileCallback cb, uintptr_t cbData, const wchar_t* pattern = 0, size_t flags = 0, DirCallback dircb = NULL, uintptr_t dircbData = 0);
107
108/**
109 * Determine the next available pathname with a given format.
110 * This is useful when creating new files without overwriting the previous
111 * ones (screenshots are a good example).
112 *
113 * @param fs
114 * @param pathnameFormat Format string for the pathname; must contain one
115 * format specifier for an integer.
116 * Example: "screenshots/screenshot%04d.png"
117 * @param nextNumber in: the first number to try; out: the next number.
118 * If 0, numbers corresponding to existing files are skipped.
119 * @param nextPathname receives the output.
120 **/
121void NextNumberedFilename(const PIVFS& fs, const VfsPath& pathnameFormat, size_t& nextNumber, VfsPath& nextPathname);
122
123} // namespace vfs
124
125#endif // #ifndef INCLUDED_VFS_UTIL
Definition: file_system.h:41
Definition: path.h:84
std::vector< OsPath > DirectoryNames
Definition: file_system.h:76
std::vector< CFileInfo > CFileInfos
Definition: file_system.h:75
Definition: vfs_util.cpp:42
DirFlags
Definition: vfs_util.h:71
@ DIR_RECURSIVE
Definition: vfs_util.h:72
Status GetPathnames(const PIVFS &fs, const VfsPath &path, const wchar_t *filter, VfsPaths &pathnames)
Definition: vfs_util.cpp:44
void NextNumberedFilename(const PIVFS &fs, const VfsPath &pathnameFormat, size_t &nextNumber, VfsPath &nextPathname)
Determine the next available pathname with a given format.
Definition: vfs_util.cpp:103
Status ForEachFile(const PIVFS &fs, const VfsPath &startPath, FileCallback cb, uintptr_t cbData, const wchar_t *pattern, size_t flags, DirCallback dircb, uintptr_t dircbData)
call back for each file in a directory tree, and optionally each directory.
Definition: vfs_util.cpp:61
Status(* DirCallback)(const VfsPath &pathname, const uintptr_t cbData)
called for directories in a directory.
Definition: vfs_util.h:68
Status ForEachFileNext(ForEachFileContext &context, const PIVFS &fs, FileCallback cb, uintptr_t cbData, const wchar_t *pattern, size_t flags, DirCallback dircb, uintptr_t dircbData)
Definition: vfs_util.cpp:71
Status(* FileCallback)(const VfsPath &pathname, const CFileInfo &fileInfo, const uintptr_t cbData)
called for files in a directory.
Definition: vfs_util.h:56
std::int64_t Status
Error handling system.
Definition: status.h:174
Definition: vfs_util.h:92
CFileInfos files
Definition: vfs_util.h:94
ForEachFileContext(const VfsPath &startPath)
Definition: vfs_util.h:101
bool empty() const
Definition: vfs_util.h:103
std::queue< VfsPath > pendingDirectories
Definition: vfs_util.h:99
DirectoryNames subdirectoryNames
Definition: vfs_util.h:95
std::shared_ptr< IVFS > PIVFS
Definition: vfs.h:240
std::vector< VfsPath > VfsPaths
Definition: vfs_path.h:45