home *** CD-ROM | disk | FTP | other *** search
/ Game.EXE 2002 April / Game.EXE_04_2002.iso / Alawar / FileResource.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  2002-03-02  |  962 b   |  44 lines

  1. #include "FileResource.h"
  2. #include "String.h"
  3.  
  4. FileResource::FileResource(const String & name)
  5. {
  6.     hfile = CreateFile( LPCTSTR(name), GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL | FILE_FLAG_RANDOM_ACCESS, NULL );
  7. }
  8.  
  9. FileResource::~FileResource()
  10. {
  11.     if( hfile == INVALID_HANDLE_VALUE )
  12.         return;
  13.     CloseHandle( hfile ); hfile = INVALID_HANDLE_VALUE;
  14. }
  15.  
  16. bool FileResource::operator!()const
  17. {
  18.     return hfile == INVALID_HANDLE_VALUE;
  19. }
  20.  
  21. void FileResource::reset()
  22. {
  23.     if( hfile == INVALID_HANDLE_VALUE )
  24.         return;
  25.     SetFilePointer( hfile, 0, NULL, FILE_BEGIN );
  26. }
  27.  
  28. unsigned FileResource::read(void * data, unsigned count)
  29. {
  30.     if( hfile == INVALID_HANDLE_VALUE )
  31.         return 0;
  32.     DWORD read = 0;
  33.     if( ReadFile( hfile, data, count, &read, NULL ) == 0 )
  34.         return 0;
  35.     return read;
  36. }
  37.  
  38. unsigned FileResource::get_size()
  39. {
  40.     if( hfile == INVALID_HANDLE_VALUE )
  41.         return 0;
  42.     return GetFileSize( hfile, NULL );
  43. }
  44.