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.

243 lines
6.1 KiB

  1. //========= Copyright 1996-2008, Valve Corporation, All rights reserved. ============//
  2. //
  3. // Purpose: A collection of utility classes to simplify file I/O, and
  4. // as much as possible contain portability problems. Here avoiding
  5. // including windows.h.
  6. //
  7. //=============================================================================
  8. #ifndef FILEIO_H
  9. #define FILEIO_H
  10. #if defined (_WIN32)
  11. typedef __time64_t time64_t;
  12. #else
  13. #include <sys/types.h>
  14. #include <sys/stat.h>
  15. typedef int64_t time64_t;
  16. #if !defined( _PS3 )
  17. #include <signal.h>
  18. #endif // _PS3
  19. #endif
  20. #include "tier0/platform.h"
  21. #include "tier0/t0constants.h"
  22. #include "tier1/utlstring.h"
  23. #include "tier1/utllinkedlist.h"
  24. class CPathString
  25. {
  26. public:
  27. // Constructors: Automatically fixes slashes and removes double slashes when the object is
  28. // constructed, and then knows how to append magic \\?\ on Windows for unicode paths
  29. CPathString( const char *pchUTF8Path );
  30. ~CPathString();
  31. // Gets the path in UTF8
  32. const char *GetUTF8Path();
  33. // Gets wchar_t based path, with \\?\ pre-pended (allowing long paths on Win32, should only be used with unicode aware filesystem calls)
  34. const wchar_t *GetWCharPathPrePended();
  35. private:
  36. void PopulateWCharPath();
  37. char *m_pchUTF8Path;
  38. wchar_t *m_pwchWideCharPathPrepended;
  39. };
  40. // iterator class, initialize with the path & pattern you want to want files/dirs for.
  41. //
  42. // all string setters and accessors use UTF-8 encoding.
  43. class CDirIterator
  44. {
  45. public:
  46. #if !defined( _PS3 )
  47. CDirIterator( const char *pchSearchPath );
  48. #endif
  49. CDirIterator( const char *pchPath, const char *pchPattern );
  50. ~CDirIterator();
  51. bool IsValid() const;
  52. // fetch the next file
  53. bool BNextFile();
  54. // name of the current file - file portion only, not full path
  55. const char *CurrentFileName();
  56. // size of the current file
  57. int64 CurrentFileLength() const;
  58. // creation time of the current file
  59. time64_t CurrentFileCreateTime() const;
  60. // mod time of the current file
  61. time64_t CurrentFileWriteTime() const;
  62. // mode/type checks:
  63. // is the current file actually a directory?
  64. bool BCurrentIsDir() const;
  65. // is the current file hidden?
  66. bool BCurrentIsHidden() const;
  67. // is the current file read-only?
  68. bool BCurrentIsReadOnly() const;
  69. // is the current file a system file?
  70. bool BCurrentIsSystem() const;
  71. // is the current file's archive bit set?
  72. bool BCurrentIsMarkedForArchive() const;
  73. #ifdef DBGFLAG_VALIDATE
  74. void Validate( CValidator &validator, const char *pchName )
  75. {
  76. #if defined( _PS3 )
  77. ValidateObj( m_strPattern );
  78. #else
  79. validator.ClaimMemory( m_pFindData );
  80. #endif
  81. }
  82. #endif
  83. private:
  84. void Init( const char *pchSearchPath );
  85. bool BValidFilename();
  86. bool m_bNoFiles, m_bUsedFirstFile;
  87. #if defined(_PS3)
  88. bool BFindNextPS3();
  89. bool m_bOpenHandle; // don't see an invalid value for the FD returned from OpenDir
  90. int m_hFind;
  91. CellFsDirectoryEntry *m_pDirEntry;
  92. CUtlString m_strPattern;
  93. #elif defined(_WIN32)
  94. HANDLE m_hFind;
  95. struct _WIN32_FIND_DATAW *m_pFindData;
  96. char m_rgchFileName[MAX_PATH * 4];
  97. #else
  98. int64 m_hFind;
  99. struct _finddata_t *m_pFindData;
  100. #endif
  101. };
  102. //-----------------------------------------------------------------------------
  103. // Purpose: Encapsulates buffered async writing to a large file (one that will require multiple write calls)
  104. // calling Close() or destructing this object will block until the file is completely written
  105. //-----------------------------------------------------------------------------
  106. class CFileWriter
  107. {
  108. public:
  109. // Possible seek types
  110. enum ESeekOrigin
  111. {
  112. k_ESeekSet,
  113. k_ESeekCur,
  114. k_ESeekEnd
  115. };
  116. CFileWriter( bool bAsync = false );
  117. virtual ~CFileWriter();
  118. bool BFileOpen();
  119. bool BSetFile( const char *pchFile, bool bAllowOpenExisting = false );
  120. bool Write( const void *pvData, uint32 cubData );
  121. int Printf( char *pDest, int bufferLen, PRINTF_FORMAT_STRING char const *pFormat, ... );
  122. bool Seek( uint64 offset, ESeekOrigin eOrigin );
  123. void Flush();
  124. void Close();
  125. uint64 GetBytesWritten();
  126. #ifdef _WIN32
  127. static void __stdcall ThreadedWriteFileCompletionFunc( unsigned long dwErrorCode, unsigned long dwBytesTransfered, struct _OVERLAPPED *pOverlapped );
  128. #elif defined( _PS3 )
  129. // not implemented on PS3
  130. #elif defined(POSIX)
  131. static void __stdcall ThreadedWriteFileCompletionFunc( sigval sigval );
  132. #else
  133. #error
  134. #endif
  135. void Sleep( uint nMSec ); // system specific sleep call
  136. private:
  137. HANDLE m_hFileDest;
  138. uint64 m_cubWritten;
  139. volatile int m_cubOutstanding;
  140. bool m_bAsync;
  141. bool m_bDefaultAsync;
  142. uint32 m_unThreadID; // main thread for this FileWriter. On this thread we support correct async IO
  143. // if the CFileWriter is called from any other thread, we block until the write is complete
  144. // this is not great but a good enough for log files and we didn't need a full blow IOCP manager for this.
  145. volatile int m_cPendingCallbacksFromOtherThreads;
  146. };
  147. // data accessor
  148. inline uint64 CFileWriter::GetBytesWritten()
  149. {
  150. return m_cubWritten;
  151. }
  152. #if !defined(_PS3)
  153. //-----------------------------------------------------------------------------
  154. // Purpose: Encapsulates watching a directory for file changes
  155. //-----------------------------------------------------------------------------
  156. class CDirWatcher
  157. {
  158. public:
  159. CDirWatcher();
  160. ~CDirWatcher();
  161. // only one directory can be watched at a time
  162. void SetDirToWatch( const char *pchDir );
  163. // retrieve any changes
  164. bool GetChangedFile( CUtlString *psFile );
  165. #ifdef DBGFLAG_VALIDATE
  166. void Validate( CValidator &validator, const char *pchName );
  167. #endif
  168. private:
  169. CUtlLinkedList<CUtlString> m_listChangedFiles;
  170. void *m_hFile;
  171. void *m_pOverlapped;
  172. void *m_pFileInfo;
  173. #ifdef OSX
  174. public:
  175. struct timespec m_modTime;
  176. void AddFileToChangeList( const char *pchFile );
  177. CUtlString m_BaseDir;
  178. private:
  179. void *m_WatcherStream;
  180. #endif
  181. friend class CDirWatcherFriend;
  182. #ifdef LINUX
  183. void AddFileToChangeList( const char *pchFile );
  184. #endif
  185. #ifdef WIN32
  186. // used by callback functions to push a file onto the list
  187. void AddFileToChangeList( const char *pchFile );
  188. void PostDirWatch();
  189. #endif
  190. };
  191. #endif // _PS3
  192. bool CreateDirRecursive( const char *pchPathIn );
  193. bool BFileExists( const char *pchFileNameIn );
  194. bool BCreateDirectory( const char *path );
  195. bool BRemoveDirectoryRecursive( const char *pchPath );
  196. #endif // FILEIO_H