TrinityCore
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
MPQFile Class Reference

#include <mpqfile.h>

Public Member Functions

 MPQFile (HANDLE mpq, const char *filename, bool warnNoExist=true)
 
 ~MPQFile ()
 
size_t read (void *dest, size_t bytes)
 
size_t getSize ()
 
size_t getPos ()
 
char * getBuffer ()
 
char * getPointer ()
 
bool isEof ()
 
void seek (int offset)
 
void seekRelative (int offset)
 
void close ()
 

Private Member Functions

 MPQFile (const MPQFile &f)
 
void operator= (const MPQFile &f)
 

Private Attributes

bool eof
 
char * buffer
 
size_t pointer
 
size_t size
 

Constructor & Destructor Documentation

MPQFile::MPQFile ( const MPQFile f)
private
MPQFile::MPQFile ( HANDLE  mpq,
const char *  filename,
bool  warnNoExist = true 
)
5  :
6  eof(false),
7  buffer(0),
8  pointer(0),
9  size(0)
10 {
11  HANDLE file;
12  if (!CascOpenFile(mpq, filename, CASC_LOCALE_ALL, 0, &file))
13  {
14  if (warnNoExist || GetLastError() != ERROR_FILE_NOT_FOUND)
15  fprintf(stderr, "Can't open %s, err=%u!\n", filename, GetLastError());
16  eof = true;
17  return;
18  }
19 
20  DWORD hi = 0;
21  size = CascGetFileSize(file, &hi);
22 
23  if (hi)
24  {
25  fprintf(stderr, "Can't open %s, size[hi] = %u!\n", filename, uint32(hi));
26  CascCloseFile(file);
27  eof = true;
28  return;
29  }
30 
31  if (size <= 1)
32  {
33  fprintf(stderr, "Can't open %s, size = %u!\n", filename, uint32(size));
34  CascCloseFile(file);
35  eof = true;
36  return;
37  }
38 
39  DWORD read = 0;
40  buffer = new char[size];
41  if (!CascReadFile(file, buffer, size, &read) || size != read)
42  {
43  fprintf(stderr, "Can't read %s, size=%u read=%u!\n", filename, uint32(size), uint32(read));
44  CascCloseFile(file);
45  eof = true;
46  return;
47  }
48 
49  CascCloseFile(file);
50 }
#define CASC_LOCALE_ALL
Definition: CascLib.h:64
void * HANDLE
Definition: CascPort.h:146
FMT_API int fprintf(std::FILE *f, CStringRef format, ArgList args)
bool eof
Definition: mpqfile.h:35
#define ERROR_FILE_NOT_FOUND
Definition: CascPort.h:205
char * buffer
Definition: mpqfile.h:36
DWORD WINAPI CascGetFileSize(HANDLE hFile, PDWORD pdwFileSizeHigh)
Definition: CascReadFile.cpp:367
size_t pointer
Definition: mpqfile.h:37
unsigned int DWORD
Definition: CascPort.h:139
int GetLastError()
Definition: Common.cpp:70
bool WINAPI CascCloseFile(HANDLE hFile)
Definition: CascOpenFile.cpp:273
bool WINAPI CascReadFile(HANDLE hFile, void *lpBuffer, DWORD dwToRead, PDWORD pdwRead)
Definition: CascReadFile.cpp:459
size_t read(void *dest, size_t bytes)
Definition: mpqfile.cpp:52
size_t size
Definition: mpqfile.h:37
uint32_t uint32
Definition: g3dmath.h:168
bool WINAPI CascOpenFile(HANDLE hStorage, const char *szFileName, DWORD dwLocale, DWORD dwFlags, HANDLE *phFile)
Definition: CascOpenFile.cpp:196

+ Here is the call graph for this function:

MPQFile::~MPQFile ( )
inline
45 { close(); }
void close()
Definition: mpqfile.cpp:81

+ Here is the call graph for this function:

Member Function Documentation

void MPQFile::close ( )
82 {
83  if (buffer) delete[] buffer;
84  buffer = 0;
85  eof = true;
86 }
bool eof
Definition: mpqfile.h:35
char * buffer
Definition: mpqfile.h:36

+ Here is the caller graph for this function:

char* MPQFile::getBuffer ( )
inline
49 { return buffer; }
char * buffer
Definition: mpqfile.h:36
char* MPQFile::getPointer ( )
inline
50 { return buffer + pointer; }
char * buffer
Definition: mpqfile.h:36
size_t pointer
Definition: mpqfile.h:37
size_t MPQFile::getPos ( )
inline
48 { return pointer; }
size_t pointer
Definition: mpqfile.h:37

+ Here is the caller graph for this function:

size_t MPQFile::getSize ( )
inline
47 { return size; }
size_t size
Definition: mpqfile.h:37
bool MPQFile::isEof ( )
inline
51 { return eof; }
bool eof
Definition: mpqfile.h:35

+ Here is the caller graph for this function:

void MPQFile::operator= ( const MPQFile f)
private
size_t MPQFile::read ( void *  dest,
size_t  bytes 
)
53 {
54  if (eof) return 0;
55 
56  size_t rpos = pointer + bytes;
57  if (rpos > size) {
58  bytes = size - pointer;
59  eof = true;
60  }
61 
62  memcpy(dest, &(buffer[pointer]), bytes);
63 
64  pointer = rpos;
65 
66  return bytes;
67 }
bool eof
Definition: mpqfile.h:35
char * buffer
Definition: mpqfile.h:36
size_t pointer
Definition: mpqfile.h:37
size_t size
Definition: mpqfile.h:37

+ Here is the caller graph for this function:

void MPQFile::seek ( int  offset)
70 {
71  pointer = offset;
72  eof = (pointer >= size);
73 }
bool eof
Definition: mpqfile.h:35
size_t pointer
Definition: mpqfile.h:37
size_t size
Definition: mpqfile.h:37

+ Here is the caller graph for this function:

void MPQFile::seekRelative ( int  offset)
76 {
77  pointer += offset;
78  eof = (pointer >= size);
79 }
bool eof
Definition: mpqfile.h:35
size_t pointer
Definition: mpqfile.h:37
size_t size
Definition: mpqfile.h:37

Member Data Documentation

char* MPQFile::buffer
private
bool MPQFile::eof
private
size_t MPQFile::pointer
private
size_t MPQFile::size
private

The documentation for this class was generated from the following files: