Skip to content

Commit f321c98

Browse files
authored
Update FilePath::getFilesInDirectory to allow getting all files (AcademySoftwareFoundation#2633)
While working on some other internal projects I found myself using the `FilePath` object, and bumped up against the fact that `getFilesInDirectory` is unable to query all the files in the provided directory. This PR provides a simple extension to the existing code to allow accessing all the files in a directory if no extension is provided to match against.
1 parent 3585b1a commit f321c98

3 files changed

Lines changed: 53 additions & 4 deletions

File tree

source/MaterialXFormat/File.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -185,8 +185,8 @@ FilePathVec FilePath::getFilesInDirectory(const string& extension) const
185185

186186
#if defined(_WIN32)
187187
WIN32_FIND_DATAA fd;
188-
string wildcard = "*." + extension;
189-
HANDLE hFind = FindFirstFileA((*this / wildcard).asString().c_str(), &fd);
188+
FilePath query = extension.empty() ? *this : (*this / ("*." + extension));
189+
HANDLE hFind = FindFirstFileA(query.asString().c_str(), &fd);
190190
if (hFind != INVALID_HANDLE_VALUE)
191191
{
192192
do
@@ -204,7 +204,7 @@ FilePathVec FilePath::getFilesInDirectory(const string& extension) const
204204
{
205205
while (struct dirent* entry = readdir(dir))
206206
{
207-
if (entry->d_type != DT_DIR && FilePath(entry->d_name).getExtension() == extension)
207+
if (entry->d_type != DT_DIR && (extension.empty() || FilePath(entry->d_name).getExtension() == extension))
208208
{
209209
files.push_back(FilePath(entry->d_name));
210210
}

source/MaterialXFormat/File.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -188,7 +188,8 @@ class MX_FORMAT_API FilePath
188188
bool isDirectory() const;
189189

190190
/// Return a vector of all files in the given directory with the given extension.
191-
FilePathVec getFilesInDirectory(const string& extension) const;
191+
/// If extension is empty all files are returned.
192+
FilePathVec getFilesInDirectory(const string& extension = EMPTY_STRING) const;
192193

193194
/// Return a vector of all directories at or beneath the given path.
194195
FilePathVec getSubDirectories() const;

source/MaterialXTest/MaterialXFormat/File.cpp

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -154,3 +154,51 @@ TEST_CASE("Path normalization test", "[file]")
154154
REQUIRE((REFERENCE_ABS_PREFIX / path).getNormalized() == (REFERENCE_ABS_PREFIX / REFERENCE_REL_PATH));
155155
}
156156
}
157+
158+
TEST_CASE("Get all files in directory", "[file]")
159+
{
160+
mx::FileSearchPath searchPath = mx::getDefaultDataSearchPath();
161+
162+
mx::FilePath lightsDir = searchPath.find("resources/Lights");
163+
164+
mx::FilePathVec mtlxFilenames =
165+
{
166+
"environment_map.mtlx",
167+
"goegap_split.mtlx",
168+
"san_giuseppe_bridge_split.mtlx",
169+
"table_mountain_split.mtlx"
170+
};
171+
172+
mx::FilePathVec hdrFilenames =
173+
{
174+
"goegap.hdr",
175+
"goegap_split.hdr",
176+
"san_giuseppe_bridge.hdr",
177+
"san_giuseppe_bridge_split.hdr",
178+
"table_mountain.hdr",
179+
"table_mountain_split.hdr",
180+
};
181+
182+
mx::FilePathVec allFilesnames = hdrFilenames;
183+
allFilesnames.insert(allFilesnames.begin(), mtlxFilenames.begin(), mtlxFilenames.end());
184+
185+
mx::FilePathVec results;
186+
187+
results = lightsDir.getFilesInDirectory("mtlx");
188+
for (const mx::FilePath& filename : results)
189+
{
190+
REQUIRE(std::find(mtlxFilenames.begin(), mtlxFilenames.end(), filename) != mtlxFilenames.end());
191+
}
192+
193+
results = lightsDir.getFilesInDirectory("hdr");
194+
for (const mx::FilePath& filename : results)
195+
{
196+
REQUIRE(std::find(hdrFilenames.begin(), hdrFilenames.end(), filename) != hdrFilenames.end());
197+
}
198+
199+
results = lightsDir.getFilesInDirectory();
200+
for (const mx::FilePath& filename : results)
201+
{
202+
REQUIRE(std::find(allFilesnames.begin(), allFilesnames.end(), filename) != allFilesnames.end());
203+
}
204+
}

0 commit comments

Comments
 (0)