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 : #include "precompiled.h"
28 :
29 : #include "vfs_util.h"
30 :
31 : #include "lib/code_annotation.h"
32 : #include "lib/path.h"
33 : #include "lib/regex.h"
34 : #include "lib/secure_crt.h"
35 :
36 : #include <algorithm>
37 : #include <climits>
38 : #include <queue>
39 : #include <string>
40 : #include <vector>
41 :
42 : namespace vfs {
43 :
44 305 : Status GetPathnames(const PIVFS& fs, const VfsPath& path, const wchar_t* filter, VfsPaths& pathnames)
45 : {
46 305 : std::vector<CFileInfo> files;
47 305 : RETURN_STATUS_IF_ERR(fs->GetDirectoryEntries(path, &files, 0));
48 :
49 252 : pathnames.reserve(files.size());
50 :
51 3161 : for(size_t i = 0; i < files.size(); i++)
52 : {
53 2909 : if(match_wildcard(files[i].Name().string().c_str(), filter))
54 5780 : pathnames.push_back(path / files[i].Name());
55 : }
56 :
57 : return INFO::OK;
58 305 : }
59 :
60 :
61 27 : 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)
62 : {
63 27 : ForEachFileContext context{startPath};
64 267 : while(!context.pendingDirectories.empty())
65 : {
66 267 : RETURN_STATUS_IF_ERR(ForEachFileNext(context, fs, cb, cbData, pattern, flags, dircb, dircbData));
67 : }
68 : return INFO::OK;
69 27 : }
70 :
71 240 : Status ForEachFileNext(
72 : ForEachFileContext& context, const PIVFS& fs, FileCallback cb,
73 : uintptr_t cbData, const wchar_t* pattern, size_t flags,
74 : DirCallback dircb, uintptr_t dircbData)
75 : {
76 240 : const VfsPath path{std::move(context.pendingDirectories.front())};
77 240 : context.pendingDirectories.pop();
78 :
79 240 : RETURN_STATUS_IF_ERR(fs->GetDirectoryEntries(path, &context.files, &context.subdirectoryNames));
80 :
81 240 : if(dircb)
82 0 : RETURN_STATUS_IF_ERR(dircb(path, dircbData));
83 :
84 9392 : for (const CFileInfo& fileInfo : context.files)
85 : {
86 9152 : if(!match_wildcard(fileInfo.Name().string().c_str(), pattern))
87 1 : continue;
88 :
89 9151 : const VfsPath pathname(path / fileInfo.Name()); // (CFileInfo only stores the name)
90 9151 : RETURN_STATUS_IF_ERR(cb(pathname, fileInfo, cbData));
91 9151 : }
92 :
93 240 : if(!(flags & DIR_RECURSIVE))
94 : return INFO::OK;
95 :
96 436 : for(const OsPath& subdirectoryName : context.subdirectoryNames)
97 639 : context.pendingDirectories.push(path / subdirectoryName / "");
98 :
99 : return INFO::OK;
100 240 : }
101 :
102 :
103 0 : void NextNumberedFilename(const PIVFS& fs, const VfsPath& pathnameFormat, size_t& nextNumber, VfsPath& nextPathname)
104 : {
105 : // (first call only:) scan directory and set nextNumber according to
106 : // highest matching filename found. this avoids filling "holes" in
107 : // the number series due to deleted files, which could be confusing.
108 : // example: add 1st and 2nd; [exit] delete 1st; [restart]
109 : // add 3rd -> without this measure it would get number 1, not 3.
110 0 : if(nextNumber == 0)
111 : {
112 0 : const VfsPath nameFormat = pathnameFormat.Filename();
113 0 : const VfsPath path = pathnameFormat.Parent()/"";
114 :
115 0 : size_t maxNumber = 0;
116 0 : CFileInfos files;
117 0 : fs->GetDirectoryEntries(path, &files, 0);
118 0 : for(size_t i = 0; i < files.size(); i++)
119 : {
120 0 : int number;
121 0 : if(swscanf_s(files[i].Name().string().c_str(), nameFormat.string().c_str(), &number) == 1)
122 0 : maxNumber = std::max(size_t(number), maxNumber);
123 : }
124 :
125 0 : nextNumber = maxNumber+1;
126 0 : }
127 :
128 : // now increment number until that file doesn't yet exist.
129 : // this is fairly slow, but typically only happens once due
130 : // to scan loop above. (we still need to provide for looping since
131 : // someone may have added files in the meantime)
132 : // we don't bother with binary search - this isn't a bottleneck.
133 0 : do
134 : {
135 0 : wchar_t pathnameBuf[PATH_MAX];
136 0 : swprintf_s(pathnameBuf, ARRAY_SIZE(pathnameBuf), pathnameFormat.string().c_str(), nextNumber++);
137 0 : nextPathname = pathnameBuf;
138 : }
139 0 : while(fs->GetFileInfo(nextPathname, 0) == INFO::OK);
140 0 : }
141 :
142 : } // namespace vfs
|