Guest User

Untitled

a guest
Oct 22nd, 2017
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Diff 2.22 KB | None | 0 0
  1. Index: ui_fileinterface.cpp
  2. ===================================================================
  3. --- ui_fileinterface.cpp    (revision 16715)
  4. +++ ui_fileinterface.cpp    (working copy)
  5. @@ -14,7 +14,7 @@
  6.  UI_FileInterface::UI_FileInterface() : Rocket::Core::FileInterface()
  7.  {
  8.     // TODO Auto-generated constructor stub
  9. -
  10. +   fileSizeMap.clear();
  11.  }
  12.  
  13.  UI_FileInterface::~UI_FileInterface()
  14. @@ -29,17 +29,23 @@
  15.     if( path2[0] == '/' )
  16.         path2.Erase( 0, 1 );
  17.  
  18. -   if( trap::FS_FOpenFile( path2.CString(), &filenum, FS_READ ) == -1 )
  19. +   int length = trap::FS_FOpenFile( path2.CString(), &filenum, FS_READ );
  20. +   if( length == -1 )
  21.         return 0;
  22.  
  23. +   // cache file length
  24. +   fileSizeMap[filenum] = length;
  25. +
  26.     Com_Printf("UI_FileInterface opened %s\n", path2.CString() );
  27.     return static_cast<Rocket::Core::FileHandle>( filenum );
  28.  }
  29.  
  30.  void UI_FileInterface::Close(Rocket::Core::FileHandle file)
  31.  {
  32. -   if( file != 0 )
  33. +   if( file != 0 ) {
  34. +       fileSizeMap.erase( static_cast<int>( file ) );
  35.         trap::FS_FCloseFile( static_cast<int>( file ) );
  36. +   }
  37.  }
  38.  
  39.  size_t UI_FileInterface::Read(void *buffer, size_t size, Rocket::Core::FileHandle file)
  40. @@ -66,4 +72,18 @@
  41.     return trap::FS_Tell( static_cast<int>( file ) );
  42.  }
  43.  
  44. +size_t UI_FileInterface::Length(Rocket::Core::FileHandle file)
  45. +{
  46. +   int filenum = static_cast<int>( file );
  47. +   UI_FileSizeMap::iterator it = fileSizeMap.find( filenum );
  48. +
  49. +   // assertion failure here means that Length was called without preceeding Open call
  50. +   assert( it != fileSizeMap.end() );
  51. +   if( it == fileSizeMap.end() ) {
  52. +       return 0;
  53. +   }
  54. +
  55. +   return fileSizeMap[filenum];
  56.  }
  57. +
  58. +}
  59. Index: ui_fileinterface.h
  60. ===================================================================
  61. --- ui_fileinterface.h  (revision 16715)
  62. +++ ui_fileinterface.h  (working copy)
  63. @@ -13,6 +13,8 @@
  64.  namespace WSWUI
  65.  {
  66.  
  67. +typedef std::map<int, size_t> UI_FileSizeMap;
  68. +
  69.  class UI_FileInterface : public Rocket::Core::FileInterface
  70.  {
  71.  public:
  72. @@ -35,6 +37,12 @@
  73.  
  74.     /// Returns the current position of the file pointer.
  75.     virtual size_t Tell(Rocket::Core::FileHandle file);
  76. +
  77. +   /// Returns the length of the file.
  78. +   virtual size_t Length(Rocket::Core::FileHandle file);
  79. +
  80. +private:
  81. +   UI_FileSizeMap fileSizeMap;
  82.  };
  83.  
  84.  }
Add Comment
Please, Sign In to add comment