Win32LocalFileSystem.cpp 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  1. /*
  2. ** Command & Conquer Generals Zero Hour(tm)
  3. ** Copyright 2025 Electronic Arts Inc.
  4. **
  5. ** This program is free software: you can redistribute it and/or modify
  6. ** it under the terms of the GNU General Public License as published by
  7. ** the Free Software Foundation, either version 3 of the License, or
  8. ** (at your option) any later version.
  9. **
  10. ** This program is distributed in the hope that it will be useful,
  11. ** but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. ** GNU General Public License for more details.
  14. **
  15. ** You should have received a copy of the GNU General Public License
  16. ** along with this program. If not, see <http://www.gnu.org/licenses/>.
  17. */
  18. ////////////////////////////////////////////////////////////////////////////////
  19. // //
  20. // (c) 2001-2003 Electronic Arts Inc. //
  21. // //
  22. ////////////////////////////////////////////////////////////////////////////////
  23. ///////// Win32LocalFileSystem.cpp /////////////////////////
  24. // Bryan Cleveland, August 2002
  25. ////////////////////////////////////////////////////////////
  26. #include <windows.h>
  27. #include "Common/AsciiString.h"
  28. #include "Common/GameMemory.h"
  29. #include "Common/PerfTimer.h"
  30. #include "Win32Device/Common/Win32LocalFileSystem.h"
  31. #include "Win32Device/Common/Win32LocalFile.h"
  32. #include <io.h>
  33. Win32LocalFileSystem::Win32LocalFileSystem() : LocalFileSystem()
  34. {
  35. }
  36. Win32LocalFileSystem::~Win32LocalFileSystem() {
  37. }
  38. //DECLARE_PERF_TIMER(Win32LocalFileSystem_openFile)
  39. File * Win32LocalFileSystem::openFile(const Char *filename, Int access /* = 0 */)
  40. {
  41. //USE_PERF_TIMER(Win32LocalFileSystem_openFile)
  42. Win32LocalFile *file = newInstance( Win32LocalFile );
  43. // sanity check
  44. if (strlen(filename) <= 0) {
  45. return NULL;
  46. }
  47. if (access & File::WRITE) {
  48. // if opening the file for writing, we need to make sure the directory is there
  49. // before we try to create the file.
  50. AsciiString string;
  51. string = filename;
  52. AsciiString token;
  53. AsciiString dirName;
  54. string.nextToken(&token, "\\/");
  55. dirName = token;
  56. while ((token.find('.') == NULL) || (string.find('.') != NULL)) {
  57. createDirectory(dirName);
  58. string.nextToken(&token, "\\/");
  59. dirName.concat('\\');
  60. dirName.concat(token);
  61. }
  62. }
  63. if (file->open(filename, access) == FALSE) {
  64. file->close();
  65. file->deleteInstance();
  66. file = NULL;
  67. } else {
  68. file->deleteOnClose();
  69. }
  70. // this will also need to play nice with the STREAMING type that I added, if we ever enable this
  71. // srj sez: this speeds up INI loading, but makes BIG files unusable.
  72. // don't enable it without further tweaking.
  73. //
  74. // unless you like running really slowly.
  75. // if (!(access&File::WRITE)) {
  76. // // Return a ramfile.
  77. // RAMFile *ramFile = newInstance( RAMFile );
  78. // if (ramFile->open(file)) {
  79. // file->close(); // is deleteonclose, so should delete.
  80. // ramFile->deleteOnClose();
  81. // return ramFile;
  82. // } else {
  83. // ramFile->close();
  84. // ramFile->deleteInstance();
  85. // }
  86. // }
  87. return file;
  88. }
  89. void Win32LocalFileSystem::update()
  90. {
  91. }
  92. void Win32LocalFileSystem::init()
  93. {
  94. }
  95. void Win32LocalFileSystem::reset()
  96. {
  97. }
  98. //DECLARE_PERF_TIMER(Win32LocalFileSystem_doesFileExist)
  99. Bool Win32LocalFileSystem::doesFileExist(const Char *filename) const
  100. {
  101. //USE_PERF_TIMER(Win32LocalFileSystem_doesFileExist)
  102. if (_access(filename, 0) == 0) {
  103. return TRUE;
  104. }
  105. return FALSE;
  106. }
  107. void Win32LocalFileSystem::getFileListInDirectory(const AsciiString& currentDirectory, const AsciiString& originalDirectory, const AsciiString& searchName, FilenameList & filenameList, Bool searchSubdirectories) const
  108. {
  109. HANDLE fileHandle = NULL;
  110. WIN32_FIND_DATA findData;
  111. char search[_MAX_PATH];
  112. AsciiString asciisearch;
  113. asciisearch = originalDirectory;
  114. asciisearch.concat(currentDirectory);
  115. asciisearch.concat(searchName);
  116. strcpy(search, asciisearch.str());
  117. Bool done = FALSE;
  118. fileHandle = FindFirstFile(search, &findData);
  119. done = (fileHandle == INVALID_HANDLE_VALUE);
  120. while (!done) {
  121. if (!(findData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) &&
  122. (strcmp(findData.cFileName, ".") && strcmp(findData.cFileName, ".."))) {
  123. // if we haven't already, add this filename to the list.
  124. // a stl set should only allow one copy of each filename
  125. AsciiString newFilename;
  126. newFilename = originalDirectory;
  127. newFilename.concat(currentDirectory);
  128. newFilename.concat(findData.cFileName);
  129. if (filenameList.find(newFilename) == filenameList.end()) {
  130. filenameList.insert(newFilename);
  131. }
  132. }
  133. done = (FindNextFile(fileHandle, &findData) == 0);
  134. }
  135. FindClose(fileHandle);
  136. if (searchSubdirectories) {
  137. AsciiString subdirsearch;
  138. subdirsearch = originalDirectory;
  139. subdirsearch.concat(currentDirectory);
  140. subdirsearch.concat("*.");
  141. fileHandle = FindFirstFile(subdirsearch.str(), &findData);
  142. done = fileHandle == INVALID_HANDLE_VALUE;
  143. while (!done) {
  144. if ((findData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) &&
  145. (strcmp(findData.cFileName, ".") && strcmp(findData.cFileName, ".."))) {
  146. AsciiString tempsearchstr;
  147. tempsearchstr.concat(currentDirectory);
  148. tempsearchstr.concat(findData.cFileName);
  149. tempsearchstr.concat('\\');
  150. // recursively add files in subdirectories if required.
  151. getFileListInDirectory(tempsearchstr, originalDirectory, searchName, filenameList, searchSubdirectories);
  152. }
  153. done = (FindNextFile(fileHandle, &findData) == 0);
  154. }
  155. FindClose(fileHandle);
  156. }
  157. }
  158. Bool Win32LocalFileSystem::getFileInfo(const AsciiString& filename, FileInfo *fileInfo) const
  159. {
  160. WIN32_FIND_DATA findData;
  161. HANDLE findHandle = NULL;
  162. findHandle = FindFirstFile(filename.str(), &findData);
  163. if (findHandle == INVALID_HANDLE_VALUE) {
  164. return FALSE;
  165. }
  166. fileInfo->timestampHigh = findData.ftLastWriteTime.dwHighDateTime;
  167. fileInfo->timestampLow = findData.ftLastWriteTime.dwLowDateTime;
  168. fileInfo->sizeHigh = findData.nFileSizeHigh;
  169. fileInfo->sizeLow = findData.nFileSizeLow;
  170. FindClose(findHandle);
  171. return TRUE;
  172. }
  173. Bool Win32LocalFileSystem::createDirectory(AsciiString directory)
  174. {
  175. if ((directory.getLength() > 0) && (directory.getLength() < _MAX_DIR)) {
  176. return (CreateDirectory(directory.str(), NULL) != 0);
  177. }
  178. return FALSE;
  179. }