Created
November 23, 2014 00:50
-
-
Save lighth7015/ae22ab1cf81e44c8654b to your computer and use it in GitHub Desktop.
FindFile API (Doesn't work.)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include "stdafx.h" | |
/* So it will compile, enjoy. */ | |
class IBaseTool { | |
}; | |
struct ToolInfo | |
{ | |
HMODULE hModule; | |
std::vector<IBaseTool> | |
m_ToolCollection; | |
}; | |
class FindFileIterator; | |
class FindFile | |
{ | |
std::vector<ToolInfo> m_ToolCollection; | |
friend class FindFileIterator; | |
public: | |
FindFile(); | |
typedef FindFileIterator iterator; | |
void load(FindFileIterator it); | |
FindFileIterator begin(); | |
HANDLE end(); | |
}; | |
class FindFileIterator | |
{ | |
char directory[MAX_PATH]; | |
HANDLE m_findHandle; | |
WIN32_FIND_DATA data; | |
friend class FindFile; | |
typedef char directory_t[MAX_PATH]; | |
typedef FindFile* tool_factory; | |
FindFile& m_Factory; | |
bool success(); | |
public: | |
FindFileIterator(tool_factory const, directory_t directory); | |
FindFileIterator& operator++(int); | |
bool operator==(HANDLE); | |
bool operator!=(HANDLE); | |
}; | |
void DisplayErrorMsg() | |
{ | |
LPCWSTR ErrorMsg; | |
FormatMessageW( | |
// use system message tables to retrieve error text | |
FORMAT_MESSAGE_FROM_SYSTEM | |
// allocate buffer on local heap for error text | |
| FORMAT_MESSAGE_ALLOCATE_BUFFER | |
// ACHTUNG!! This will fail if you don't FORMAT_MESSAGE_IGNORE_INSERTS, | |
// since we can't pass insertion parameters. | |
| FORMAT_MESSAGE_IGNORE_INSERTS, | |
nullptr, | |
GetLastError(), | |
MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), | |
(LPWSTR)&ErrorMsg, | |
0, nullptr); | |
wchar_t application_title[256] = { 0 }; | |
LoadStringW(hInstance, IDS_APPFRAME_TITLE, application_title, sizeof(application_title) / sizeof(TCHAR)); | |
MessageBoxW( | |
nullptr | |
, ErrorMsg | |
, application_title | |
, MB_OK); | |
} | |
FindFile::FindFile(){ | |
} | |
FindFileIterator FindFile::begin(){ | |
char directory[MAX_PATH], tool_directory[MAX_PATH]; | |
::GetModuleFileName(NULL, directory, MAX_PATH); | |
// Surgically remove the filename from the directory part. | |
char* p = ::strrchr(directory, '\\'); *(p + 1) = 0; | |
::strcpy_s(tool_directory, MAX_PATH, directory); | |
::strcat_s(tool_directory, MAX_PATH, "tools\\*.tool"); | |
return FindFileIterator(this, tool_directory); | |
} | |
HANDLE FindFile::end(){ return INVALID_HANDLE_VALUE; } | |
void FindFile::load(FindFileIterator it){ | |
} | |
FindFileIterator& FindFileIterator::operator++(int) | |
{ | |
if (!(::FindNextFile(m_findHandle, &data))) { | |
DisplayErrorMsg(); | |
m_findHandle = m_Factory.end(); | |
} | |
return *this; | |
} | |
bool FindFileIterator::operator!=(HANDLE h){ return !(m_findHandle != h); } | |
bool FindFileIterator::operator==(HANDLE h){ return (m_findHandle == h); } | |
// | |
// FindFileIterator()- | |
// ------------------------------------------------------------------------------- | |
// | |
// Initializes a ToolFactory iterator to walk the filesystem, looking for InterVUE | |
// tools to initialize. | |
// ------------------------------------------------------------------------------- | |
// | |
FindFileIterator::FindFileIterator(tool_factory const factory, directory_t directory) | |
: m_Factory(*factory), m_findHandle(::FindFirstFile(directory, &data)) | |
{ | |
if (m_findHandle == m_Factory.end()) | |
DisplayErrorMsg(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment