Etterna 0.74.4
Loading...
Searching...
No Matches
RageUtil_FileDB.h
1#ifndef RAGE_UTIL_FILEDB
2#define RAGE_UTIL_FILEDB
3
4#include "RageUtil/File/RageFileManager.h"
5#include "RageUtil/Misc/RageThreads.h"
6#include "RageUtil/Misc/RageTimer.h"
7#include "RageUtil/Utils/RageUtil.h"
8
9#include <map>
10#include <set>
11
12struct FileSet;
13struct File
14{
15 std::string name;
16 std::string lname;
17
18 void SetName(const std::string& fn)
19 {
20 name = fn;
21 lname = name;
22 MakeLower(lname);
23 }
24
25 bool dir;
26 int size;
27 /* Modification time of the file. The contents of this is undefined, except
28 * that when the file has been modified, this value will change. */
29 int hash;
30
31 /* Private data, for RageFileDrivers. */
32 void* priv;
33
34 /* If this is non-NULL, and dir is true, this is a pointer to the FileSet
35 * containing the directory contents. (This is a cache; it isn't always
36 * set.) */
37 const FileSet* dirp;
38
39 File()
40 {
41 dir = false;
42 dirp = nullptr;
43 size = -1;
44 hash = -1;
45 priv = nullptr;
46 }
47 File(const std::string& fn)
48 {
49 SetName(fn);
50 dir = false;
51 size = -1;
52 hash = -1;
53 priv = nullptr;
54 dirp = nullptr;
55 }
56
57 bool operator<(const File& rhs) const { return lname < rhs.lname; }
58
59 bool equal(const File& rhs) const { return lname == rhs.lname; }
60 bool equal(const std::string& rhs) const
61 {
62 std::string l = make_lower(rhs);
63 return lname == l;
64 }
65};
66
67inline bool
68operator==(File const& lhs, File const& rhs)
69{
70 return lhs.lname == rhs.lname;
71}
72inline bool
73operator!=(File const& lhs, File const& rhs)
74{
75 return !operator==(lhs, rhs);
76}
77
79struct FileSet
80{
81 std::set<File> files;
82 RageTimer age;
83
84 /*
85 * If m_bFilled is false, this FileSet hasn't completed being filled in yet;
86 * it's owned by the thread filling it in. Wait on FilenameDB::m_Mutex and
87 * retry until it becomes true.
88 */
89 bool m_bFilled;
90
91 FileSet() { m_bFilled = true; }
92
93 void GetFilesMatching(const std::string& sBeginning,
94 const std::string& sContaining,
95 const std::string& sEnding,
96 std::vector<std::string>& asOut,
97 DirListingReturnFilter returnFilter) const;
98 void GetFilesEqualTo(const std::string& pat,
99 std::vector<std::string>& out,
100 DirListingReturnFilter returnFilter) const;
101
102 RageFileManager::FileType GetFileType(const std::string& sPath) const;
103 int GetFileSize(const std::string& sPath) const;
104 int GetFileHash(const std::string& sPath) const;
105};
108{
109 public:
110 FilenameDB()
111 : m_Mutex("FilenameDB")
112 {
113 }
114 virtual ~FilenameDB() { FlushDirCache(); }
115
116 void AddFile(const std::string& sPath,
117 int iSize,
118 int iHash,
119 void* pPriv = nullptr);
120 void DelFile(const std::string& sPath);
121 void* GetFilePriv(const std::string& sPath);
122
123 /* This handles at most two * wildcards. If we need anything more
124 * complicated, we'll need to use fnmatch or regex. */
125 void GetFilesSimpleMatch(const std::string& sDir,
126 const std::string& sFile,
127 std::vector<std::string>& asOut,
128 DirListingReturnFilter returnFilter);
129
130 /* Search for "path" case-insensitively and replace it with the correct
131 * case. If only a portion of the path exists, resolve as much as possible.
132 * Return true if the entire path was matched. */
133 bool ResolvePath(std::string& sPath);
134
135 RageFileManager::FileType GetFileType(const std::string& sPath);
136 int GetFileSize(const std::string& sPath);
137 int GetFileHash(const std::string& sFilePath);
138 void GetDirListing(const std::string& sPath,
139 std::vector<std::string>& asAddTo,
140 DirListingReturnFilter returnFilter,
141 bool bReturnPathToo);
142
143 void FlushDirCache(const std::string& sDir = std::string());
144
145 void GetFileSetCopy(const std::string& dir, FileSet& out);
146 /* Probably slow, so override it. */
147 virtual void CacheFile(const std::string& sPath);
148
149 protected:
150 RageEvent m_Mutex;
151
152 const File* GetFile(const std::string& sPath);
153 FileSet* GetFileSet(const std::string& sDir, bool create = true);
154
155 /* Directories we have cached: */
156 std::map<std::string, FileSet*> dirs;
157
158 int ExpireSeconds{ -1 };
159
160 void GetFilesEqualTo(const std::string& sDir,
161 const std::string& sName,
162 std::vector<std::string>& asOut,
163 DirListingReturnFilter returnFilter);
164
165 void GetFilesMatching(const std::string& sDir,
166 const std::string& sBeginning,
167 const std::string& sContaining,
168 const std::string& sEnding,
169 std::vector<std::string>& asOut,
170 DirListingReturnFilter returnFilter);
171
172 void DelFileSet(std::map<std::string, FileSet*>::iterator dir);
173
174 /* The given path wasn't cached. Cache it. */
175 virtual void PopulateFileSet(FileSet& /* fs */,
176 const std::string& /* sPath */)
177 {
178 }
179};
180
181/* This FilenameDB must be populated in advance. */
183{
184 public:
185 NullFilenameDB() { ExpireSeconds = -1; }
186 void CacheFile(const std::string& /* sPath */) override {}
187};
188
189#endif
A container for a file listing.
Definition RageUtil_FileDB.h:108
Definition RageUtil_FileDB.h:183
Definition RageThreads.h:309
Definition RageTimer.h:9
This represents a directory.
Definition RageUtil_FileDB.h:80
Definition RageUtil_FileDB.h:14