Guest User

Untitled

a guest
Dec 7th, 2018
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.67 KB | None | 0 0
  1.  
  2. #include <io.h>
  3. #include <windows.h>
  4. #include "scandir.h"
  5.  
  6. using namespace std;
  7.  
  8. const int NOTFOUND = -1;
  9.  
  10. //========================================================================================
  11. // Returns true, if directory exists
  12. //========================================================================================
  13. bool IsAValidDirectory(const char* directory)
  14. {
  15.  
  16. if ( directory )
  17. {
  18. DWORD dw;
  19. dw = GetFileAttributes(directory);
  20. return ((dw & FILE_ATTRIBUTE_DIRECTORY) != 0) &&
  21. (dw != 0xFFFFFFFF);
  22. }
  23. else
  24. {
  25. return false;
  26. }
  27. }
  28.  
  29. // Overloaded string version
  30. inline const char* AppendBackslash(std::string& currentdir)
  31. {
  32. if ( (currentdir.length()) && (currentdir[currentdir.length() - 1] != '\\') )
  33. {
  34. currentdir = currentdir + "\\";
  35. }
  36.  
  37. return currentdir.c_str();
  38.  
  39. }
  40.  
  41. //=============================================================================
  42. // Append to a list of files based the mask "*.*". Store
  43. // full names for files in a std::list. DOES NOT CLEAR LIST TO ZERO
  44. // ELEMENTS TO BEGIN WITH
  45. // RETURNS: Number of files appended to std::list
  46. //=============================================================================
  47. int _ScanForFiles(STRING_LIST& stringList, const char* Path, const char* Filemask, bool IncludeSubDirs /* = false */)
  48. {
  49. struct _finddata_t fileinfo;
  50. long filespec;
  51. int result = 0;
  52.  
  53. bool normal;
  54. bool archive;
  55. bool readonly;
  56. bool directory;
  57.  
  58. string searchStr;
  59. string path;
  60. string subdir;
  61.  
  62. if (!Path || !*Path)
  63. {
  64. Path = "";
  65. }
  66.  
  67. //============
  68. // Initialize
  69. //============
  70. memset(&fileinfo,sizeof(struct _finddata_t),0);
  71. path = Path;
  72.  
  73. searchStr = AppendBackslash(path) + string(Filemask);
  74.  
  75. //====================
  76. // Search Directory
  77. //====================
  78. filespec = _findfirst(searchStr.c_str(),&fileinfo);
  79. while ((filespec != NOTFOUND) && (result != NOTFOUND))
  80. {
  81.  
  82. normal = fileinfo.attrib == _A_NORMAL;
  83. archive = ( (fileinfo.attrib & _A_ARCH) == _A_ARCH);
  84. readonly = ( (fileinfo.attrib & _A_RDONLY) == _A_RDONLY);
  85. directory = ( (fileinfo.attrib & _A_SUBDIR) == _A_SUBDIR);
  86.  
  87. if ( (normal || archive || readonly) && ( !directory ) )
  88. {
  89. searchStr = path + fileinfo.name;
  90. stringList.push_back(searchStr);
  91. }
  92. else if ( (directory) && ( strcmp(".", fileinfo.name) != 0 ) && ( strcmp("..", fileinfo.name) != 0 ) && IncludeSubDirs)
  93. {
  94. subdir = AppendBackslash(path) + string(fileinfo.name);
  95. _ScanForFiles(stringList, subdir.c_str(), Filemask, IncludeSubDirs);
  96. }
  97. result = _findnext(filespec,&fileinfo);
  98. } // while
  99. _findclose(filespec);
  100.  
  101. return stringList.size();
  102.  
  103. }
  104.  
  105. int ScanForFiles(STRING_LIST& stringList, const char* Path, const char* Filemask, bool IncludeSubDirs /* = false */)
  106. {
  107. STRING_LIST directory_list;
  108. STRING_LIST::iterator i;
  109. int retval = 0;
  110.  
  111. if ( (strcmp(Filemask, "*.*") == 0 ) || (!IncludeSubDirs) )
  112. {
  113. retval = _ScanForFiles(stringList, Path, Filemask, IncludeSubDirs);
  114. }
  115. else
  116. {
  117. ScanForDirectories(directory_list, Path);
  118. for (i = directory_list.begin(); i != directory_list.end(); i++)
  119. {
  120. retval = ScanForFiles(stringList, i->c_str(), Filemask, false);
  121. }
  122. }
  123.  
  124. return retval;
  125.  
  126. }
  127.  
  128. //=============================================================================
  129. // Append to a list of subdirectories for a directory
  130. // Store names for in a std::list. DOES NOT CLEAR ARRAY TO ZERO
  131. // ELEMENTS TO BEGIN WITH
  132. // RETURNS: Number of directory names append to list
  133. //=============================================================================
  134. int ScanForDirectories(STRING_LIST& stringList, const char* Path)
  135. {
  136. struct _finddata_t fileinfo;
  137.  
  138. long filespec;
  139. int result;
  140.  
  141. string searchStr;
  142. string subdir;
  143.  
  144. if (!Path || !*Path)
  145. {
  146. Path = "";
  147. }
  148.  
  149. memset(&fileinfo,sizeof(struct _finddata_t),0);
  150.  
  151. //================================
  152. // Add directory to list
  153. //================================
  154. if ( IsAValidDirectory(Path) )
  155. {
  156. stringList.push_back(Path);
  157. }
  158.  
  159. searchStr = AppendBackslash(string(Path)) + string("*.*");
  160.  
  161. //================================
  162. // Search for sub directories
  163. //================================
  164. result = (NOTFOUND + 1);
  165. filespec = _findfirst(searchStr.c_str(),&fileinfo);
  166. while ((filespec != NOTFOUND) && (result != NOTFOUND))
  167. {
  168. if ( ( (fileinfo.attrib & _A_SUBDIR) == _A_SUBDIR) && ( strcmp(".", fileinfo.name) != 0 ) && ( strcmp("..", fileinfo.name) != 0 ) )
  169. {
  170. subdir = AppendBackslash(string(Path)) + string(fileinfo.name);
  171. ScanForDirectories(stringList, subdir.c_str());
  172. }
  173. result = _findnext(filespec,&fileinfo);
  174. } // while
  175.  
  176. _findclose(filespec);
  177.  
  178. return stringList.size();
  179.  
  180. }
Add Comment
Please, Sign In to add comment