ArchiveFile.cpp 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  1. /*
  2. ** Command & Conquer Generals(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. /////////ArchiveFile.cpp ///////////////////////
  24. // Bryan Cleveland, August 2002
  25. ////////////////////////////////////////////////
  26. #include "PreRTS.h"
  27. #include "Common/ArchiveFile.h"
  28. #include "Common/ArchiveFileSystem.h"
  29. #include "Common/File.h"
  30. #include "Common/PerfTimer.h"
  31. // checks to see if str matches searchString. Search string is done in the
  32. // using * and ? as wildcards. * is used to denote any number of characters,
  33. // and ? is used to denote a single wildcard character.
  34. static Bool SearchStringMatches(AsciiString str, AsciiString searchString)
  35. {
  36. if (str.getLength() == 0) {
  37. if (searchString.getLength() == 0) {
  38. return TRUE;
  39. }
  40. return FALSE;
  41. }
  42. if (searchString.getLength() == 0) {
  43. return FALSE;
  44. }
  45. const char *c1 = str.str();
  46. const char *c2 = searchString.str();
  47. while ((*c1 == *c2) || (*c2 == '?') || (*c2 == '*')) {
  48. if ((*c1 == *c2) || (*c2 == '?')) {
  49. ++c1;
  50. ++c2;
  51. } else if (*c2 == '*') {
  52. ++c2;
  53. if (*c2 == 0) {
  54. return TRUE;
  55. }
  56. while (*c1 != 0) {
  57. if (SearchStringMatches(AsciiString(c1), AsciiString(c2))) {
  58. return TRUE;
  59. }
  60. ++c1;
  61. }
  62. }
  63. if (*c1 == 0) {
  64. if (*c2 == 0) {
  65. return TRUE;
  66. }
  67. return FALSE;
  68. }
  69. if (*c2 == 0) {
  70. return FALSE;
  71. }
  72. }
  73. return FALSE;
  74. }
  75. ArchiveFile::~ArchiveFile()
  76. {
  77. if (m_file != NULL) {
  78. m_file->close();
  79. m_file = NULL;
  80. }
  81. }
  82. ArchiveFile::ArchiveFile()
  83. {
  84. m_rootDirectory.clear();
  85. }
  86. void ArchiveFile::addFile(const AsciiString& path, const ArchivedFileInfo *fileInfo)
  87. {
  88. AsciiString temp;
  89. temp = path;
  90. temp.toLower();
  91. AsciiString token;
  92. AsciiString debugpath;
  93. DetailedArchivedDirectoryInfo *dirInfo = &m_rootDirectory;
  94. temp.nextToken(&token, "\\/");
  95. while (token.getLength() > 0) {
  96. if (dirInfo->m_directories.find(token) == dirInfo->m_directories.end())
  97. {
  98. dirInfo->m_directories[token].clear();
  99. dirInfo->m_directories[token].m_directoryName = token;
  100. }
  101. debugpath.concat(token);
  102. debugpath.concat('\\');
  103. dirInfo = &(dirInfo->m_directories[token]);
  104. temp.nextToken(&token, "\\/");
  105. }
  106. dirInfo->m_files[fileInfo->m_filename] = *fileInfo;
  107. //path.concat(fileInfo->m_filename);
  108. }
  109. void ArchiveFile::getFileListInDirectory(const AsciiString& currentDirectory, const AsciiString& originalDirectory, const AsciiString& searchName, FilenameList &filenameList, Bool searchSubdirectories) const
  110. {
  111. AsciiString searchDir;
  112. const DetailedArchivedDirectoryInfo *dirInfo = &m_rootDirectory;
  113. searchDir = originalDirectory;
  114. searchDir.toLower();
  115. AsciiString token;
  116. searchDir.nextToken(&token, "\\/");
  117. while (token.getLength() > 0) {
  118. DetailedArchivedDirectoryInfoMap::const_iterator it = dirInfo->m_directories.find(token);
  119. if (it != dirInfo->m_directories.end())
  120. {
  121. dirInfo = &it->second;
  122. }
  123. else
  124. {
  125. // if the directory doesn't exist, then there aren't any files to be had.
  126. return;
  127. }
  128. searchDir.nextToken(&token, "\\/");
  129. }
  130. getFileListInDirectory(dirInfo, originalDirectory, searchName, filenameList, searchSubdirectories);
  131. }
  132. void ArchiveFile::getFileListInDirectory(const DetailedArchivedDirectoryInfo *dirInfo, const AsciiString& currentDirectory, const AsciiString& searchName, FilenameList &filenameList, Bool searchSubdirectories) const
  133. {
  134. DetailedArchivedDirectoryInfoMap::const_iterator diriter = dirInfo->m_directories.begin();
  135. while (diriter != dirInfo->m_directories.end()) {
  136. const DetailedArchivedDirectoryInfo *tempDirInfo = &(diriter->second);
  137. AsciiString tempdirname;
  138. tempdirname = currentDirectory;
  139. if ((tempdirname.getLength() > 0) && (!tempdirname.endsWith("\\"))) {
  140. tempdirname.concat('\\');
  141. }
  142. tempdirname.concat(tempDirInfo->m_directoryName);
  143. getFileListInDirectory(tempDirInfo, tempdirname, searchName, filenameList, searchSubdirectories);
  144. diriter++;
  145. }
  146. ArchivedFileInfoMap::const_iterator fileiter = dirInfo->m_files.begin();
  147. while (fileiter != dirInfo->m_files.end()) {
  148. if (SearchStringMatches(fileiter->second.m_filename, searchName)) {
  149. AsciiString tempfilename;
  150. tempfilename = currentDirectory;
  151. if ((tempfilename.getLength() > 0) && (!tempfilename.endsWith("\\"))) {
  152. tempfilename.concat('\\');
  153. }
  154. tempfilename.concat(fileiter->second.m_filename);
  155. if (filenameList.find(tempfilename) == filenameList.end()) {
  156. // only insert into the list if its not already in there.
  157. filenameList.insert(tempfilename);
  158. }
  159. }
  160. fileiter++;
  161. }
  162. }
  163. void ArchiveFile::attachFile(File *file)
  164. {
  165. if (m_file != NULL) {
  166. m_file->close();
  167. m_file = NULL;
  168. }
  169. m_file = file;
  170. }
  171. const ArchivedFileInfo * ArchiveFile::getArchivedFileInfo(const AsciiString& filename) const
  172. {
  173. AsciiString path;
  174. path = filename;
  175. path.toLower();
  176. AsciiString token;
  177. const DetailedArchivedDirectoryInfo *dirInfo = &m_rootDirectory;
  178. path.nextToken(&token, "\\/");
  179. while ((token.find('.') == NULL) || (path.find('.') != NULL)) {
  180. DetailedArchivedDirectoryInfoMap::const_iterator it = dirInfo->m_directories.find(token);
  181. if (it != dirInfo->m_directories.end())
  182. {
  183. dirInfo = &it->second;
  184. }
  185. else
  186. {
  187. return NULL;
  188. }
  189. path.nextToken(&token, "\\/");
  190. }
  191. ArchivedFileInfoMap::const_iterator it = dirInfo->m_files.find(token);
  192. if (it != dirInfo->m_files.end())
  193. {
  194. return &it->second;
  195. }
  196. else
  197. {
  198. return NULL;
  199. }
  200. }