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.

131 lines
2.4 KiB

  1. //========= Copyright Valve Corporation, All rights reserved. ============//
  2. //
  3. // Purpose:
  4. //
  5. // $NoKeywords: $
  6. //=============================================================================//
  7. #ifndef FILEBUFFER_H
  8. #define FILEBUFFER_H
  9. #ifdef _WIN32
  10. #pragma once
  11. #endif
  12. #include "tier1/smartptr.h"
  13. #include "tier2/p4helpers.h"
  14. class CFileBuffer
  15. {
  16. public:
  17. CFileBuffer( int size )
  18. {
  19. m_pData = new unsigned char[size];
  20. #ifdef _DEBUG
  21. m_pUsed = new const char *[size];
  22. memset( m_pUsed, 0, size * sizeof( const char * ) );
  23. #endif
  24. m_Size = size;
  25. m_pCurPos = m_pData;
  26. #ifdef _DEBUG
  27. memset( m_pData, 0xbaadf00d, size );
  28. #endif
  29. }
  30. ~CFileBuffer()
  31. {
  32. delete [] m_pData;
  33. #ifdef _DEBUG
  34. delete [] m_pUsed;
  35. #endif
  36. }
  37. #ifdef _DEBUG
  38. void TestWritten( int EndOfFileOffset )
  39. {
  40. if ( !g_quiet )
  41. {
  42. printf( "testing to make sure that the whole file has been written\n" );
  43. }
  44. int i;
  45. for( i = 0; i < EndOfFileOffset; i++ )
  46. {
  47. if( !m_pUsed[i] )
  48. {
  49. printf( "offset %d not written, end of file invalid!\n", i );
  50. assert( 0 );
  51. }
  52. }
  53. }
  54. #endif
  55. void WriteToFile( const char *fileName, int size )
  56. {
  57. CPlainAutoPtr< CP4File > spFile( g_p4factory->AccessFile( fileName ) );
  58. spFile->Edit();
  59. FILE *fp = fopen( fileName, "wb" );
  60. if( !fp )
  61. {
  62. MdlWarning( "Can't open \"%s\" for writing!\n", fileName );
  63. return;
  64. }
  65. fwrite( m_pData, 1, size, fp );
  66. fclose( fp );
  67. spFile->Add();
  68. }
  69. void WriteAt( int offset, void *data, int size, const char *name )
  70. {
  71. // printf( "WriteAt: \"%s\" offset: %d end: %d size: %d\n", name, offset, offset + size - 1, size );
  72. m_pCurPos = m_pData + offset;
  73. #ifdef _DEBUG
  74. int i;
  75. const char **used = m_pUsed + offset;
  76. bool bitched = false;
  77. for( i = 0; i < size; i++ )
  78. {
  79. if( used[i] )
  80. {
  81. if( !bitched )
  82. {
  83. printf( "overwrite at %d! (overwriting \"%s\" with \"%s\")\n", i + offset, used[i], name );
  84. assert( 0 );
  85. bitched = true;
  86. }
  87. }
  88. else
  89. {
  90. used[i] = name;
  91. }
  92. }
  93. #endif // _DEBUG
  94. Append( data, size );
  95. }
  96. int GetOffset( void )
  97. {
  98. return m_pCurPos - m_pData;
  99. }
  100. void *GetPointer( int offset )
  101. {
  102. return m_pData + offset;
  103. }
  104. private:
  105. void Append( void *data, int size )
  106. {
  107. assert( m_pCurPos + size - m_pData < m_Size );
  108. memcpy( m_pCurPos, data, size );
  109. m_pCurPos += size;
  110. }
  111. CFileBuffer(); // undefined
  112. int m_Size;
  113. unsigned char *m_pData;
  114. unsigned char *m_pCurPos;
  115. #ifdef _DEBUG
  116. const char **m_pUsed;
  117. #endif
  118. };
  119. #endif // FILEBUFFER_H