ffactory.cpp 8.7 KB

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