Counter Strike : Global Offensive Source Code
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

320 lines
7.1 KiB

  1. //===== Copyright � 2005-2005, Valve Corporation, All rights reserved. ======//
  2. //
  3. // Purpose: A higher level link library for general use in the game and tools.
  4. //
  5. //===========================================================================//
  6. #ifndef FILEUTILS_H
  7. #define FILEUTILS_H
  8. #if defined( _WIN32 )
  9. #pragma once
  10. #endif
  11. #ifndef TIER2_H
  12. #include "tier2/tier2.h"
  13. #endif
  14. #ifndef FILESYSTEM_H
  15. #include "filesystem.h"
  16. #endif
  17. #include "tier0/platform.h"
  18. // Builds a directory which is a subdirectory of the current mod
  19. void GetModSubdirectory( const char *pSubDir, char *pBuf, int nBufLen );
  20. // Builds a directory which is a subdirectory of the current mod's *content*
  21. void GetModContentSubdirectory( const char *pSubDir, char *pBuf, int nBufLen );
  22. // Generates a filename under the 'game' subdirectory given a subdirectory of 'content'
  23. void ComputeModFilename( const char *pContentFileName, char *pBuf, size_t nBufLen );
  24. // Generates a filename under the 'content' subdirectory given a subdirectory of 'game'
  25. void ComputeModContentFilename( const char *pGameFileName, char *pBuf, size_t nBufLen );
  26. // Finds all files matching the a name within a directory and its sub directories. Output entries are paths to found files (relative to and including szStartDirectory)
  27. void RecursiveFindFilesMatchingName( CUtlVector< CUtlString > *outFileList, const char* szStartDirectory, const char* szTargetFileName, const char *pathID );
  28. // Builds a list of all files under a directory with a particular extension
  29. void AddFilesToList( CUtlVector< CUtlString > &list, const char *pDirectory, const char *pPath, const char *pExtension );
  30. // Returns the search path as a list of paths
  31. void GetSearchPath( CUtlVector< CUtlString > &path, const char *pPathID );
  32. // Given file name generate a full path using the following rules.
  33. // 1. if its full path already return
  34. // 2. if its a relative path try to find it under the path id
  35. // 3. if find fails treat relative path as relative to the current dir
  36. bool GenerateFullPath( const char *pFileName, char const *pPathID, char *pBuf, int nBufLen );
  37. // Generates a .360 file if it doesn't exist or is out of sync with the pc source file
  38. #define UOC_FAIL -1
  39. #define UOC_NOT_CREATED 0
  40. #define UOC_CREATED 1
  41. typedef bool ( *CreateCallback_t )( const char *pSourceName, const char *pTargetName, const char *pPathID, void *pExtraData );
  42. int UpdateOrCreate( const char *pSourceName, char *pTargetName, int targetLen, const char *pPathID, CreateCallback_t pfnCreate, bool bForce = false, void *pExtraData = NULL );
  43. char *CreatePlatformFilename( const char *pSourceName, char *pTargetName, int targetLen );
  44. FORCEINLINE const char *AdjustFileExtensionForPlatform( const char *pSourceName, char *pTargetName, int targetLen )
  45. {
  46. #ifdef PLATFORM_X360
  47. return CreatePlatformFilename( pSourceName, pTargetName, targetLen );
  48. #else
  49. return pSourceName;
  50. #endif
  51. }
  52. // simple file classes. File I/O mode (text/binary, read/write) is based upon the subclass chosen.
  53. // classes with the word Required on them abort with a message if the file can't be opened.
  54. // destructores close the file handle, or it can be explicitly closed with the Close() method.
  55. class CBaseFile
  56. {
  57. public:
  58. FileHandle_t m_FileHandle;
  59. CBaseFile(void)
  60. {
  61. m_FileHandle = FILESYSTEM_INVALID_HANDLE;
  62. }
  63. ~CBaseFile( void )
  64. {
  65. Close();
  66. }
  67. FileHandle_t Handle( void ) const
  68. {
  69. return m_FileHandle;
  70. }
  71. void Close( void )
  72. {
  73. if ( m_FileHandle != FILESYSTEM_INVALID_HANDLE )
  74. g_pFullFileSystem->Close( m_FileHandle );
  75. m_FileHandle = FILESYSTEM_INVALID_HANDLE;
  76. }
  77. void Open( char const *fname, char const *modes )
  78. {
  79. Close();
  80. m_FileHandle = g_pFullFileSystem->Open( fname, modes );
  81. }
  82. char *ReadLine( char *pOutput, int maxChars )
  83. {
  84. return g_pFullFileSystem->ReadLine( pOutput, maxChars, m_FileHandle );
  85. }
  86. // read every line of the file into a vector of strings
  87. void ReadLines( CUtlStringList &sList, int nMaxLineLength = 2048 );
  88. int Read( void* pOutput, int size )
  89. {
  90. return g_pFullFileSystem->Read( pOutput, size, m_FileHandle );
  91. }
  92. void MustRead( void* pOutput, int size )
  93. {
  94. int ret=Read( pOutput, size );
  95. if (ret != size )
  96. Error("failed to read %d bytes\n", size );
  97. }
  98. int Write( void const* pInput, int size)
  99. {
  100. return g_pFullFileSystem->Write( pInput, size, m_FileHandle );
  101. }
  102. // {Get|Put}{Int|Float} read and write ints and floats from a file in x86 order, swapping on
  103. // input for big-endian systems.
  104. void PutInt( int n )
  105. {
  106. int n1=LittleDWord( n );
  107. Write(&n1, sizeof( n1 ) );
  108. }
  109. int GetInt( void )
  110. {
  111. int ret;
  112. MustRead( &ret, sizeof( ret ));
  113. return LittleDWord( ret );
  114. }
  115. float GetFloat( void )
  116. {
  117. float ret;
  118. MustRead( &ret, sizeof( ret ));
  119. LittleFloat( &ret, &ret );
  120. return ret;
  121. }
  122. void PutFloat( float f )
  123. {
  124. LittleFloat( &f, &f );
  125. Write( &f, sizeof( f ) );
  126. }
  127. bool IsOk( void )
  128. {
  129. return ( m_FileHandle != FILESYSTEM_INVALID_HANDLE) &&
  130. ( g_pFullFileSystem->IsOk( m_FileHandle ) );
  131. }
  132. void Seek( int pos, FileSystemSeek_t nSeekType = FILESYSTEM_SEEK_HEAD )
  133. {
  134. g_pFullFileSystem->Seek( m_FileHandle, pos, nSeekType );
  135. }
  136. unsigned int Tell()
  137. {
  138. return g_pFullFileSystem->Tell( m_FileHandle );
  139. }
  140. unsigned int Size( void )
  141. {
  142. Assert( IsOk() );
  143. return g_pFullFileSystem->Size( m_FileHandle );
  144. }
  145. void ReadFile( CUtlBuffer &dataBuf );
  146. };
  147. class COutputFile : public CBaseFile
  148. {
  149. public:
  150. void Open( char const *pFname )
  151. {
  152. CBaseFile::Open( pFname, "wb" );
  153. }
  154. COutputFile( char const *pFname ) : CBaseFile()
  155. {
  156. Open( pFname );
  157. }
  158. COutputFile( void ) : CBaseFile()
  159. {
  160. }
  161. };
  162. class COutputTextFile : public CBaseFile
  163. {
  164. public:
  165. void Open( char const *pFname )
  166. {
  167. CBaseFile::Open( pFname, "w" );
  168. }
  169. COutputTextFile( char const *pFname ) : CBaseFile()
  170. {
  171. Open( pFname );
  172. }
  173. COutputTextFile( void ) : CBaseFile()
  174. {
  175. }
  176. };
  177. class CAppendTextFile : public CBaseFile
  178. {
  179. public:
  180. void Open( char const *pFname )
  181. {
  182. CBaseFile::Open( pFname, "a+" );
  183. }
  184. CAppendTextFile( char const *pFname ) : CBaseFile()
  185. {
  186. Open( pFname );
  187. }
  188. CAppendTextFile( void ) : CBaseFile()
  189. {
  190. }
  191. };
  192. class CInputFile : public CBaseFile
  193. {
  194. public:
  195. void Open( char const *pFname )
  196. {
  197. CBaseFile::Open( pFname, "rb" );
  198. }
  199. CInputFile( char const *pFname ) : CBaseFile()
  200. {
  201. Open( pFname );
  202. }
  203. CInputFile( void ) : CBaseFile()
  204. {
  205. }
  206. };
  207. class CInputTextFile : public CBaseFile
  208. {
  209. public:
  210. void Open( char const *pFname )
  211. {
  212. CBaseFile::Open( pFname, "r" );
  213. }
  214. CInputTextFile( char const *pFname ) : CBaseFile()
  215. {
  216. Open( pFname );
  217. }
  218. CInputTextFile( void ) : CBaseFile()
  219. {
  220. }
  221. };
  222. class CRequiredInputTextFile : public CBaseFile
  223. {
  224. public:
  225. void Open( char const *pFname )
  226. {
  227. CBaseFile::Open( pFname, "r" );
  228. if ( ! IsOk() )
  229. {
  230. Error("error opening required file %s\n", pFname );
  231. }
  232. }
  233. CRequiredInputTextFile( char const *pFname ) : CBaseFile()
  234. {
  235. Open( pFname );
  236. }
  237. CRequiredInputTextFile( void ) : CBaseFile()
  238. {
  239. }
  240. };
  241. class CRequiredInputFile : public CBaseFile
  242. {
  243. public:
  244. void Open( char const *pFname )
  245. {
  246. CBaseFile::Open( pFname, "rb" );
  247. if ( ! IsOk() )
  248. {
  249. Error("error opening required file %s\n", pFname );
  250. }
  251. }
  252. CRequiredInputFile( char const *pFname ) : CBaseFile()
  253. {
  254. Open( pFname );
  255. }
  256. CRequiredInputFile( void ) : CBaseFile()
  257. {
  258. }
  259. };
  260. #endif // FILEUTILS_H