Team Fortress 2 Source Code as on 22/4/2020
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.

317 lines
6.8 KiB

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