ffactory.cpp 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310
  1. /*
  2. ** Command & Conquer Renegade(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. *** C O N F I D E N T I A L --- W E S T W O O D S T U D I O S ***
  20. ***********************************************************************************************
  21. * *
  22. * Project Name : Command & Conquer *
  23. * *
  24. * $Archive:: /Commando/Code/wwlib/ffactory.cpp $*
  25. * *
  26. * $Author:: Jani_p $*
  27. * *
  28. * $Modtime:: 8/24/01 11:50a $*
  29. * *
  30. * $Revision:: 17 $*
  31. * *
  32. *---------------------------------------------------------------------------------------------*
  33. * - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
  34. #include "ffactory.h"
  35. #include "rawfile.h"
  36. #include "bufffile.h"
  37. #include "realcrc.h"
  38. #include <stdio.h>
  39. #include <stdlib.h>
  40. #include <assert.h>
  41. #include <string.h>
  42. /*
  43. ** Statics
  44. ** NOTE: If _TheFileFactory is ever changed to point to an object of a different class which does
  45. ** not derive from SimpleFileFactoryClass, _TheSimpleFileFactory should be set to NULL.
  46. */
  47. SimpleFileFactoryClass _DefaultFileFactory;
  48. FileFactoryClass * _TheFileFactory = &_DefaultFileFactory;
  49. SimpleFileFactoryClass * _TheSimpleFileFactory = &_DefaultFileFactory;
  50. SimpleFileFactoryClass _DefaultWritingFileFactory;
  51. FileFactoryClass * _TheWritingFileFactory = &_DefaultWritingFileFactory;
  52. /*
  53. **
  54. */
  55. file_auto_ptr::file_auto_ptr(FileFactoryClass *fac, const char *filename) :
  56. _Ptr(NULL), _Fac(fac)
  57. {
  58. assert(_Fac);
  59. _Ptr=_Fac->Get_File(filename);
  60. if ( _Ptr == NULL ) {
  61. _Ptr = new BufferedFileClass();
  62. }
  63. }
  64. file_auto_ptr::~file_auto_ptr()
  65. {
  66. _Fac->Return_File(_Ptr);
  67. }
  68. /*
  69. ** RawFileFactoryClass implementation
  70. */
  71. FileClass * RawFileFactoryClass::Get_File( char const *filename )
  72. {
  73. return new RawFileClass( filename );
  74. }
  75. void RawFileFactoryClass::Return_File( FileClass *file )
  76. {
  77. delete file;
  78. }
  79. /*
  80. ** SimpleFileFactoryClass implementation
  81. */
  82. SimpleFileFactoryClass::SimpleFileFactoryClass( void ) :
  83. IsStripPath( false ),
  84. Mutex( )
  85. {
  86. }
  87. void SimpleFileFactoryClass::Get_Sub_Directory( StringClass& new_dir ) const
  88. {
  89. // BEGIN SERIALIZATION
  90. // We cannot return a const char * here because the StringClass
  91. // may reallocate its buffer during a call to Set_Sub_Directory.
  92. // I opted to return a StringClass instead of a reference to
  93. // StringClass because it seems like that would behave more
  94. // reasonably. (no sudden changes from or to empty string in
  95. // the middle of a calling function.) (DRM, 04/19/01)
  96. // Jani: Returning a StringClass object causes a memory allocation
  97. // and release so it is better to take a reference to the
  98. // destination StringClass object and modify that.
  99. CriticalSectionClass::LockClass lock(Mutex);
  100. new_dir=SubDirectory;
  101. // END SERIALIZATION
  102. }
  103. void SimpleFileFactoryClass::Set_Sub_Directory( const char * sub_directory )
  104. {
  105. // BEGIN SERIALIZATION
  106. // StringClass makes no guarantees on the atomicity of assignment.
  107. // Just to be safe, we lock before executing the assignment code.
  108. // (DRM, 04/19/01)
  109. CriticalSectionClass::LockClass lock(Mutex);
  110. SubDirectory = sub_directory;
  111. // END SERIALIZATION
  112. }
  113. void SimpleFileFactoryClass::Prepend_Sub_Directory( const char * sub_directory )
  114. {
  115. int sub_len = strlen(sub_directory);
  116. // Overflow prevention
  117. if (sub_len > 1021) {
  118. WWASSERT(0);
  119. return;
  120. } else if (sub_len < 1) {
  121. return;
  122. }
  123. // Ensure sub_directory ends with a slash, and append a semicolon
  124. char temp_sub_dir[1024];
  125. strcpy(temp_sub_dir, sub_directory);
  126. if (temp_sub_dir[sub_len - 1] != '\\') {
  127. temp_sub_dir[sub_len] = '\\';
  128. temp_sub_dir[sub_len + 1] = 0;
  129. sub_len++;
  130. }
  131. temp_sub_dir[sub_len] = ';';
  132. temp_sub_dir[sub_len + 1] = 0;
  133. // BEGIN SERIALIZATION
  134. // StringClass makes no guarantees on the atomicity of concatenation.
  135. // Just to be safe, we lock before executing the concatenation code.
  136. // (NH, 04/23/01)
  137. CriticalSectionClass::LockClass lock(Mutex);
  138. SubDirectory = temp_sub_dir + SubDirectory;
  139. // END SERIALIZATION
  140. }
  141. void SimpleFileFactoryClass::Append_Sub_Directory( const char * sub_directory )
  142. {
  143. int sub_len = strlen(sub_directory);
  144. // Overflow prevention
  145. if (sub_len > 1022) {
  146. WWASSERT(0);
  147. return;
  148. } else if (sub_len < 1) {
  149. return;
  150. }
  151. // Ensure sub_directory ends with a slash
  152. char temp_sub_dir[1024];
  153. strcpy(temp_sub_dir, sub_directory);
  154. if (temp_sub_dir[sub_len - 1] != '\\') {
  155. temp_sub_dir[sub_len] = '\\';
  156. temp_sub_dir[sub_len + 1] = 0;
  157. sub_len++;
  158. }
  159. // BEGIN SERIALIZATION
  160. // We are doing various dependent operations on SubDirectory.
  161. // Just to be safe, we lock before this section.
  162. // (NH, 04/23/01)
  163. CriticalSectionClass::LockClass lock(Mutex);
  164. // Ensure a trailing semicolon is present, unless the directory list is empty
  165. int len = SubDirectory.Get_Length();
  166. if (len && SubDirectory[len - 1] != ';') {
  167. SubDirectory += ';';
  168. }
  169. SubDirectory += temp_sub_dir;
  170. // END SERIALIZATION
  171. }
  172. /*
  173. ** Is_Full_Path
  174. */
  175. static bool
  176. Is_Full_Path (const char *path)
  177. {
  178. bool retval = false;
  179. if (path != NULL && path[0] != 0) {
  180. // Check for drive designation
  181. retval = bool(path[1] == ':');
  182. // Check for network path
  183. retval |= bool((path[0] == '\\') && (path[1] == '\\'));
  184. }
  185. return retval;
  186. }
  187. /*
  188. **
  189. */
  190. FileClass * SimpleFileFactoryClass::Get_File( char const *filename )
  191. {
  192. // strip off the path (if needed). Note that if path stripping is off, and the requested file
  193. // has a path in its name, and the current subdirectory is not empty, the paths will just be
  194. // concatenated which may not produce reasonable results.
  195. StringClass stripped_name(true);
  196. if (IsStripPath) {
  197. const char * ptr = ::strrchr( filename, '\\' );
  198. if (ptr != 0) {
  199. ptr++;
  200. stripped_name = ptr;
  201. } else {
  202. stripped_name = filename;
  203. }
  204. } else {
  205. stripped_name = filename;
  206. }
  207. RawFileClass *file = new BufferedFileClass();// new RawWritingFileClass();
  208. assert( file );
  209. //
  210. // Do we need to find the path for this file request?
  211. //
  212. StringClass new_name(stripped_name,true);
  213. if (Is_Full_Path ( new_name ) == false) {
  214. // BEGIN SERIALIZATION
  215. // We need to lock here because we are using the contents of SubDirectory
  216. // in two places. I'd rather be overly cautious about the implementation
  217. // of StringClass and wrap all calls to it. We can optimize later if this
  218. // proves too slow. (DRM, 04/19/01)
  219. CriticalSectionClass::LockClass lock(Mutex);
  220. if (!SubDirectory.Is_Empty()) {
  221. //
  222. // SubDirectory may contain a semicolon seperated search path...
  223. // If the file doesn't exist, we'll set the path to the last dir in
  224. // the search path. Therefore newly created files will always go in the
  225. // last dir in the search path.
  226. //
  227. StringClass subdir(SubDirectory,true);
  228. if (strchr(subdir,';'))
  229. {
  230. char *tokstart=subdir.Peek_Buffer();
  231. const char *tok;
  232. while((tok=strtok(tokstart, ";")) != NULL) {
  233. tokstart=NULL;
  234. new_name.Format("%s%s",tok,stripped_name.Peek_Buffer());
  235. file->Set_Name( new_name ); // Call Set_Name to force an allocated name
  236. if (file->Open()) {
  237. file->Close();
  238. break;
  239. }
  240. }
  241. } else {
  242. new_name.Format("%s%s",SubDirectory,stripped_name);
  243. }
  244. }
  245. // END SERIALIZATION
  246. }
  247. file->Set_Name( new_name ); // Call Set_Name to force an allocated name
  248. return file;
  249. }
  250. void SimpleFileFactoryClass::Return_File( FileClass *file )
  251. {
  252. delete file;
  253. }