LCOV - code coverage report
Current view: top level - source/lib/file/vfs - vfs_util.h (source / functions) Hit Total Coverage
Test: 0 A.D. test coverage report Lines: 1 1 100.0 %
Date: 2026-12-03 01:19:39 Functions: 1 1 100.0 %

          Line data    Source code
       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             : 
      30             : #include "lib/file/file_system.h"
      31             : #include "lib/file/vfs/vfs.h"
      32             : #include "lib/file/vfs/vfs_path.h"
      33             : #include "lib/status.h"
      34             : 
      35             : #include <cstddef>
      36             : #include <cstdint>
      37             : #include <queue>
      38             : 
      39             : namespace vfs
      40             : {
      41             : 
      42             : extern 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             :  **/
      56             : typedef 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             :  **/
      68             : typedef Status (*DirCallback)(const VfsPath& pathname, const uintptr_t cbData);
      69             : 
      70             : enum DirFlags
      71             : {
      72             :     DIR_RECURSIVE = 1
      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             :  **/
      89             : Status 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             : 
      91             : struct ForEachFileContext
      92             : {
      93             :     // (declare here to avoid reallocations)
      94             :     CFileInfos files;
      95             :     DirectoryNames subdirectoryNames;
      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          81 :     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.
     106             : Status 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             :  **/
     121             : void NextNumberedFilename(const PIVFS& fs, const VfsPath& pathnameFormat, size_t& nextNumber, VfsPath& nextPathname);
     122             : 
     123             : }   // namespace vfs
     124             : 
     125             : #endif   // #ifndef INCLUDED_VFS_UTIL

Generated by: LCOV version 1.16