Skip to content

Commit

Permalink
meshconvert: added wildcard support
Browse files Browse the repository at this point in the history
  • Loading branch information
walbourn committed Sep 13, 2016
1 parent 43c0594 commit 1d92f76
Showing 1 changed file with 95 additions and 1 deletion.
96 changes: 95 additions & 1 deletion Meshconvert/Meshconvert.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,8 @@ using namespace DirectX;

enum OPTIONS
{
OPT_NORMALS = 1,
OPT_RECURSIVE = 1,
OPT_NORMALS,
OPT_WEIGHT_BY_AREA,
OPT_WEIGHT_BY_EQUAL,
OPT_TANGENTS,
Expand Down Expand Up @@ -77,6 +78,7 @@ struct SValue

SValue g_pOptions[] =
{
{ L"r", OPT_RECURSIVE },
{ L"n", OPT_NORMALS },
{ L"na", OPT_WEIGHT_BY_AREA },
{ L"ne", OPT_WEIGHT_BY_EQUAL },
Expand Down Expand Up @@ -107,6 +109,12 @@ SValue g_pOptions[] =

namespace
{
inline HANDLE safe_handle(HANDLE h) { return (h == INVALID_HANDLE_VALUE) ? 0 : h; }

struct find_closer { void operator()(HANDLE h) { assert(h != INVALID_HANDLE_VALUE); if (h) FindClose(h); } };

typedef public std::unique_ptr<void, find_closer> ScopedFindHandle;

#pragma prefast(disable : 26018, "Only used with static internal arrays")

DWORD LookupByName(const wchar_t *pName, const SValue *pArray)
Expand Down Expand Up @@ -137,6 +145,81 @@ namespace
}


void SearchForFiles(const wchar_t* path, std::list<SConversion>& files, bool recursive)
{
// Process files
WIN32_FIND_DATA findData = {};
ScopedFindHandle hFile(safe_handle(FindFirstFileExW(path,
FindExInfoBasic, &findData,
FindExSearchNameMatch, nullptr,
FIND_FIRST_EX_LARGE_FETCH)));
if (hFile)
{
for (;;)
{
if (!(findData.dwFileAttributes & (FILE_ATTRIBUTE_HIDDEN | FILE_ATTRIBUTE_SYSTEM | FILE_ATTRIBUTE_DIRECTORY)))
{
wchar_t drive[_MAX_DRIVE] = {};
wchar_t dir[_MAX_DIR] = {};
_wsplitpath_s(path, drive, _MAX_DRIVE, dir, _MAX_DIR, nullptr, 0, nullptr, 0);

SConversion conv;
_wmakepath_s(conv.szSrc, drive, dir, findData.cFileName, nullptr);
files.push_back(conv);
}

if (!FindNextFile(hFile.get(), &findData))
break;
}
}

// Process directories
if (recursive)
{
wchar_t searchDir[MAX_PATH] = {};
{
wchar_t drive[_MAX_DRIVE] = {};
wchar_t dir[_MAX_DIR] = {};
_wsplitpath_s(path, drive, _MAX_DRIVE, dir, _MAX_DIR, nullptr, 0, nullptr, 0);
_wmakepath_s(searchDir, drive, dir, L"*", nullptr);
}

hFile.reset(safe_handle(FindFirstFileExW(searchDir,
FindExInfoBasic, &findData,
FindExSearchLimitToDirectories, nullptr,
FIND_FIRST_EX_LARGE_FETCH)));
if (!hFile)
return;

for (;;)
{
if (findData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
{
if (findData.cFileName[0] != L'.')
{
wchar_t subdir[MAX_PATH] = {};

{
wchar_t drive[_MAX_DRIVE] = {};
wchar_t dir[_MAX_DIR] = {};
wchar_t fname[_MAX_FNAME] = {};
wchar_t ext[_MAX_FNAME] = {};
_wsplitpath_s(path, drive, dir, fname, ext);
wcscat_s(dir, findData.cFileName);
_wmakepath_s(subdir, drive, dir, fname, ext);
}

SearchForFiles(subdir, files, recursive);
}
}

if (!FindNextFile(hFile.get(), &findData))
break;
}
}
}


void PrintLogo()
{
wprintf(L"Microsoft (R) MeshConvert Command-line Tool\n");
Expand All @@ -151,6 +234,7 @@ namespace

wprintf(L"Usage: meshconvert <options> <files>\n");
wprintf(L"\n");
wprintf(L" -r wildcard filename search is recursive\n");
wprintf(L" -n | -na | -ne generate normals weighted by angle/area/equal\n");
wprintf(L" -t generate tangents\n");
wprintf(L" -tb generate tangents & bi-tangents\n");
Expand Down Expand Up @@ -401,6 +485,16 @@ int __cdecl wmain(_In_ int argc, _In_z_count_(argc) wchar_t* argv[])
break;
}
}
else if (wcspbrk(pArg, L"?*") != nullptr)
{
size_t count = conversion.size();
SearchForFiles(pArg, conversion, (dwOptions & (1 << OPT_RECURSIVE)) != 0);
if (conversion.size() <= count)
{
wprintf(L"No matching files found for %ls\n", pArg);
return 1;
}
}
else
{
SConversion conv;
Expand Down

0 comments on commit 1d92f76

Please sign in to comment.