Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <io.h>
- #include <windows.h>
- #include "scandir.h"
- using namespace std;
- const int NOTFOUND = -1;
- //========================================================================================
- // Returns true, if directory exists
- //========================================================================================
- bool IsAValidDirectory(const char* directory)
- {
- if ( directory )
- {
- DWORD dw;
- dw = GetFileAttributes(directory);
- return ((dw & FILE_ATTRIBUTE_DIRECTORY) != 0) &&
- (dw != 0xFFFFFFFF);
- }
- else
- {
- return false;
- }
- }
- // Overloaded string version
- inline const char* AppendBackslash(std::string& currentdir)
- {
- if ( (currentdir.length()) && (currentdir[currentdir.length() - 1] != '\\') )
- {
- currentdir = currentdir + "\\";
- }
- return currentdir.c_str();
- }
- //=============================================================================
- // Append to a list of files based the mask "*.*". Store
- // full names for files in a std::list. DOES NOT CLEAR LIST TO ZERO
- // ELEMENTS TO BEGIN WITH
- // RETURNS: Number of files appended to std::list
- //=============================================================================
- int _ScanForFiles(STRING_LIST& stringList, const char* Path, const char* Filemask, bool IncludeSubDirs /* = false */)
- {
- struct _finddata_t fileinfo;
- long filespec;
- int result = 0;
- bool normal;
- bool archive;
- bool readonly;
- bool directory;
- string searchStr;
- string path;
- string subdir;
- if (!Path || !*Path)
- {
- Path = "";
- }
- //============
- // Initialize
- //============
- memset(&fileinfo,sizeof(struct _finddata_t),0);
- path = Path;
- searchStr = AppendBackslash(path) + string(Filemask);
- //====================
- // Search Directory
- //====================
- filespec = _findfirst(searchStr.c_str(),&fileinfo);
- while ((filespec != NOTFOUND) && (result != NOTFOUND))
- {
- normal = fileinfo.attrib == _A_NORMAL;
- archive = ( (fileinfo.attrib & _A_ARCH) == _A_ARCH);
- readonly = ( (fileinfo.attrib & _A_RDONLY) == _A_RDONLY);
- directory = ( (fileinfo.attrib & _A_SUBDIR) == _A_SUBDIR);
- if ( (normal || archive || readonly) && ( !directory ) )
- {
- searchStr = path + fileinfo.name;
- stringList.push_back(searchStr);
- }
- else if ( (directory) && ( strcmp(".", fileinfo.name) != 0 ) && ( strcmp("..", fileinfo.name) != 0 ) && IncludeSubDirs)
- {
- subdir = AppendBackslash(path) + string(fileinfo.name);
- _ScanForFiles(stringList, subdir.c_str(), Filemask, IncludeSubDirs);
- }
- result = _findnext(filespec,&fileinfo);
- } // while
- _findclose(filespec);
- return stringList.size();
- }
- int ScanForFiles(STRING_LIST& stringList, const char* Path, const char* Filemask, bool IncludeSubDirs /* = false */)
- {
- STRING_LIST directory_list;
- STRING_LIST::iterator i;
- int retval = 0;
- if ( (strcmp(Filemask, "*.*") == 0 ) || (!IncludeSubDirs) )
- {
- retval = _ScanForFiles(stringList, Path, Filemask, IncludeSubDirs);
- }
- else
- {
- ScanForDirectories(directory_list, Path);
- for (i = directory_list.begin(); i != directory_list.end(); i++)
- {
- retval = ScanForFiles(stringList, i->c_str(), Filemask, false);
- }
- }
- return retval;
- }
- //=============================================================================
- // Append to a list of subdirectories for a directory
- // Store names for in a std::list. DOES NOT CLEAR ARRAY TO ZERO
- // ELEMENTS TO BEGIN WITH
- // RETURNS: Number of directory names append to list
- //=============================================================================
- int ScanForDirectories(STRING_LIST& stringList, const char* Path)
- {
- struct _finddata_t fileinfo;
- long filespec;
- int result;
- string searchStr;
- string subdir;
- if (!Path || !*Path)
- {
- Path = "";
- }
- memset(&fileinfo,sizeof(struct _finddata_t),0);
- //================================
- // Add directory to list
- //================================
- if ( IsAValidDirectory(Path) )
- {
- stringList.push_back(Path);
- }
- searchStr = AppendBackslash(string(Path)) + string("*.*");
- //================================
- // Search for sub directories
- //================================
- result = (NOTFOUND + 1);
- filespec = _findfirst(searchStr.c_str(),&fileinfo);
- while ((filespec != NOTFOUND) && (result != NOTFOUND))
- {
- if ( ( (fileinfo.attrib & _A_SUBDIR) == _A_SUBDIR) && ( strcmp(".", fileinfo.name) != 0 ) && ( strcmp("..", fileinfo.name) != 0 ) )
- {
- subdir = AppendBackslash(string(Path)) + string(fileinfo.name);
- ScanForDirectories(stringList, subdir.c_str());
- }
- result = _findnext(filespec,&fileinfo);
- } // while
- _findclose(filespec);
- return stringList.size();
- }
Add Comment
Please, Sign In to add comment