Platform_Win32_FileSystem_Directory.cpp
Go to the documentation of this file.00001 //*** Platform_Win32_FileSystem_Directory.cpp *** 00002 00003 #include "Platform_OS.h" 00004 00005 #define WIN32_LEAN_AND_MEAN 00006 #define VC_EXTRALEAN 00007 #include <windows.h> 00008 #include <string.h> 00009 #include <malloc.h> 00010 00011 #include "Platform_Win32_FileSystem_Directory.h" 00012 00013 00014 00015 //*** Constructor *** 00016 00017 Platform_Win32_FileSystem_Directory::Platform_Win32_FileSystem_Directory(const char* path): 00018 path_(0), 00019 subdirectoryCount_(0), 00020 subdirectories_(0), 00021 fileCount_(0), 00022 files_(0) 00023 { 00024 if (!path) 00025 { 00026 Platform::GetPlatform_OS()->OutputDebugText("No path specified!\n"); 00027 } 00028 00029 if (path) 00030 { 00031 path_=strdup(path); 00032 } 00033 00034 Rescan(); 00035 } 00036 00037 00038 //*** Destructor *** 00039 00040 Platform_Win32_FileSystem_Directory::~Platform_Win32_FileSystem_Directory() 00041 { 00042 if (path_) 00043 { 00044 free(path_); 00045 } 00046 00047 EmptySubdirectoryList(); 00048 if (subdirectories_) 00049 { 00050 free(subdirectories_); 00051 } 00052 00053 EmptyFileList(); 00054 if (files_) 00055 { 00056 free(files_); 00057 } 00058 } 00059 00060 00061 //*** GetPath *** 00062 00063 const char* Platform_Win32_FileSystem_Directory::GetPath() 00064 { 00065 return path_; 00066 } 00067 00068 00069 //*** Exists *** 00070 00071 bool Platform_Win32_FileSystem_Directory::Exists() 00072 { 00073 if (!path_) 00074 { 00075 return false; 00076 } 00077 00078 WIN32_FIND_DATA findFileData; 00079 HANDLE hFind = FindFirstFile(GetPath(), &findFileData); 00080 if (hFind!=INVALID_HANDLE_VALUE && (findFileData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)) 00081 { 00082 FindClose(hFind); 00083 return true; 00084 } 00085 00086 FindClose(hFind); 00087 return false; 00088 } 00089 00090 00091 //*** Create *** 00092 00093 void Platform_Win32_FileSystem_Directory::Create() 00094 { 00095 if (path_) 00096 { 00097 CreateDirectory(path_,0); 00098 } 00099 } 00100 00101 00102 //*** Delete *** 00103 00104 void Platform_Win32_FileSystem_Directory::Delete() 00105 { 00106 if (path_) 00107 { 00108 RemoveDirectory(path_); 00109 } 00110 } 00111 00112 00113 //*** Rescan *** 00114 00115 void Platform_Win32_FileSystem_Directory::Rescan() 00116 { 00117 if (!path_) 00118 { 00119 return; 00120 } 00121 00122 // Get rid of any existing subdirectories 00123 EmptySubdirectoryList(); 00124 00125 // Create search string 00126 const char* searchPattern="*"; 00127 char* absoluteSearchPattern=new char[strlen(path_) + strlen(searchPattern)+2]; 00128 strcpy(absoluteSearchPattern, path_); 00129 strcat(absoluteSearchPattern, "/"); 00130 strcat(absoluteSearchPattern, searchPattern); 00131 00132 WIN32_FIND_DATA findFileData; 00133 HANDLE hFind = FindFirstFile(absoluteSearchPattern, &findFileData); 00134 if (hFind!=INVALID_HANDLE_VALUE) 00135 { 00136 // List files and subdirs 00137 BOOL loop=true; 00138 while (loop) 00139 { 00140 if (findFileData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) 00141 { 00142 if ((stricmp(findFileData.cFileName,".")!=0) && (stricmp(findFileData.cFileName,"..")!=0)) 00143 { 00144 if (!subdirectories_) 00145 { 00146 subdirectoryMaxCount_=64; 00147 subdirectories_=static_cast<const char**>(malloc(subdirectoryMaxCount_*sizeof(const char*))); 00148 } 00149 else if (subdirectoryCount_>=subdirectoryMaxCount_) 00150 { 00151 subdirectoryMaxCount_*=2; 00152 subdirectories_=static_cast<const char**>(realloc(subdirectories_,subdirectoryMaxCount_*sizeof(const char*))); 00153 00154 } 00155 subdirectories_[subdirectoryCount_]=strdup(findFileData.cFileName); 00156 subdirectoryCount_++; 00157 } 00158 } 00159 else 00160 { 00161 if (!files_) 00162 { 00163 fileMaxCount_=64; 00164 files_=static_cast<const char**>(malloc(fileMaxCount_*sizeof(const char*))); 00165 } 00166 else if (fileCount_>=fileMaxCount_) 00167 { 00168 fileMaxCount_*=2; 00169 files_=static_cast<const char**>(realloc(files_,fileMaxCount_*sizeof(const char*))); 00170 00171 } 00172 files_[fileCount_]=strdup(findFileData.cFileName); 00173 fileCount_++; 00174 } 00175 00176 loop=FindNextFile(hFind,&findFileData); 00177 } 00178 } 00179 00180 delete absoluteSearchPattern; 00181 FindClose(hFind); 00182 } 00183 00184 00185 00186 //*** EmptySubdirectoryList *** 00187 00188 void Platform_Win32_FileSystem_Directory::EmptySubdirectoryList() 00189 { 00190 for (int i=0; i<subdirectoryCount_; i++) 00191 { 00192 char* name=const_cast<char*>(subdirectories_[i]); 00193 free(name); 00194 } 00195 subdirectoryCount_=0; 00196 } 00197 00198 00199 //*** EmptyFileList *** 00200 00201 void Platform_Win32_FileSystem_Directory::EmptyFileList() 00202 { 00203 for (int i=0; i<fileCount_; i++) 00204 { 00205 char* name=const_cast<char*>(files_[i]); 00206 free(name); 00207 } 00208 fileCount_=0; 00209 } 00210 00211 00212 //*** GetSubdirectoryCount *** 00213 00214 int Platform_Win32_FileSystem_Directory::GetSubdirectoryCount() 00215 { 00216 return subdirectoryCount_; 00217 } 00218 00219 00220 //*** GetSubdirectory *** 00221 00222 const char* Platform_Win32_FileSystem_Directory::GetSubdirectory(int index) 00223 { 00224 if (index<0 || index>=subdirectoryCount_) 00225 { 00226 return 0; 00227 } 00228 00229 return subdirectories_[index]; 00230 } 00231 00232 00233 //*** GetFileCount *** 00234 00235 int Platform_Win32_FileSystem_Directory::GetFileCount() 00236 { 00237 return fileCount_; 00238 } 00239 00240 00241 //*** GetFile*** 00242 00243 const char* Platform_Win32_FileSystem_Directory::GetFile(int index) 00244 { 00245 if (index<0 || index>=fileCount_) 00246 { 00247 return 0; 00248 } 00249 00250 return files_[index]; 00251 }
Reproduction/republishing of any material on this site without permission is strictly prohibited.
