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.

151 lines
2.8 KiB

  1. //========= Copyright � 1996-2005, Valve Corporation, All rights reserved. ============//
  2. //
  3. // Purpose:
  4. //
  5. // $NoKeywords: $
  6. //=============================================================================//
  7. #if !defined( SFUIMEMORYFILE_H_ )
  8. #define SFUIMEMORYFILE_H_
  9. class SFUIMemoryFile : public SF::File
  10. {
  11. public:
  12. const char* GetFilePath( void ) { return m_filePath.ToCStr(); }
  13. bool IsValid( void ) { return m_valid; }
  14. bool IsWritable( void ) { return false; }
  15. bool Flush( void ) { return true; }
  16. int GetErrorCode( void ) { return 0; }
  17. int Tell( void ) { return m_fileIndex; }
  18. SF::SInt64 LTell( void ) { return (SF::SInt64) m_fileIndex; }
  19. int GetLength( void ) { return m_fileSize; }
  20. SF::SInt64 LGetLength( void ) { return (SF::SInt64) m_fileSize; }
  21. bool Close( void )
  22. {
  23. m_valid = false;
  24. return false;
  25. }
  26. int CopyFromStream( SF::File *pstream, int byteSize )
  27. {
  28. return 0;
  29. }
  30. int Write( const SF::UByte *pbuffer, int numBytes )
  31. {
  32. return 0;
  33. }
  34. int Read( SF::UByte *pbufer, int numBytes )
  35. {
  36. if ( m_fileIndex + numBytes > m_fileSize )
  37. {
  38. numBytes = m_fileSize - m_fileIndex;
  39. }
  40. if ( numBytes > 0 )
  41. {
  42. V_memcpy( pbufer, &m_fileData[m_fileIndex], numBytes );
  43. m_fileIndex += numBytes;
  44. }
  45. return numBytes;
  46. }
  47. int SkipBytes( int numBytes )
  48. {
  49. if ( m_fileIndex + numBytes > m_fileSize )
  50. {
  51. numBytes = m_fileSize - m_fileIndex;
  52. }
  53. m_fileIndex += numBytes;
  54. return numBytes;
  55. }
  56. int BytesAvailable( void )
  57. {
  58. return ( m_fileSize - m_fileIndex );
  59. }
  60. int Seek( int offset, int origin = Seek_Set )
  61. {
  62. switch ( origin )
  63. {
  64. case Seek_Set:
  65. m_fileIndex = offset;
  66. break;
  67. case Seek_Cur:
  68. m_fileIndex += offset;
  69. break;
  70. case Seek_End:
  71. m_fileIndex = m_fileSize - offset;
  72. break;
  73. default:
  74. break;
  75. }
  76. return m_fileIndex;
  77. }
  78. SF::SInt64 LSeek( SF::SInt64 offset, int origin = Seek_Set )
  79. {
  80. return ( SF::SInt64 ) Seek( ( int ) offset, origin );
  81. }
  82. bool ChangeSize( int newSize )
  83. {
  84. if ( newSize <= m_buffer.Size() )
  85. {
  86. m_fileSize = newSize;
  87. return true;
  88. }
  89. else
  90. {
  91. return false;
  92. }
  93. }
  94. CUtlBuffer& GetBuffer( void )
  95. {
  96. return m_buffer;
  97. }
  98. public:
  99. // pfileName should be encoded as UTF-8 to support international file names.
  100. SFUIMemoryFile( const char* pfileName ) :
  101. m_filePath( pfileName )
  102. {
  103. }
  104. void Init( void )
  105. {
  106. m_fileData = ( const SF::UByte * ) m_buffer.Base();
  107. m_fileSize = m_buffer.Size();
  108. m_fileIndex = 0;
  109. m_valid = ( !m_filePath.IsEmpty() && m_fileData != NULL && m_fileSize > 0 ) ? true : false;
  110. }
  111. private:
  112. SF::String m_filePath;
  113. CUtlBuffer m_buffer;
  114. const SF::UByte *m_fileData;
  115. SF::SInt32 m_fileSize;
  116. SF::SInt32 m_fileIndex;
  117. bool m_valid;
  118. };
  119. #endif /* SFUIMEMORYFILE_H_ */