1 #include "mpq_libmpq.h"
5 ArchiveSet gOpenArchives
;
7 MPQArchive::MPQArchive(const char* filename
)
9 int result
= libmpq_archive_open(&mpq_a
, (unsigned char*)filename
);
10 printf("Opening %s\n", filename
);
13 case LIBMPQ_EFILE
: /* error on file operation */
14 printf("Error opening archive '%s': File operation Error\n", filename
);
16 case LIBMPQ_EFILE_FORMAT
: /* bad file format */
17 printf("Error opening archive '%s': Bad file format\n", filename
);
19 case LIBMPQ_EFILE_CORRUPT
: /* file corrupt */
20 printf("Error opening archive '%s': File corrupt\n", filename
);
22 case LIBMPQ_EFILE_NOT_FOUND
: /* file in archive not found */
23 printf("Error opening archive '%s': File in archive not found\n", filename
);
25 case LIBMPQ_EFILE_READ
: /* Read error in archive */
26 printf("Error opening archive '%s': Read error in archive\n", filename
);
28 case LIBMPQ_EALLOCMEM
: /* maybe not enough memory? :) */
29 printf("Error opening archive '%s': Maybe not enough memory\n", filename
);
31 case LIBMPQ_EFREEMEM
: /* can not free memory */
32 printf("Error opening archive '%s': Cannot free memory\n", filename
);
34 case LIBMPQ_EINV_RANGE
: /* Given filenumber is out of range */
35 printf("Error opening archive '%s': Given filenumber is out of range\n", filename
);
37 case LIBMPQ_EHASHTABLE
: /* error in reading hashtable */
38 printf("Error opening archive '%s': Error in reading hashtable\n", filename
);
40 case LIBMPQ_EBLOCKTABLE
: /* error in reading blocktable */
41 printf("Error opening archive '%s': Error in reading blocktable\n", filename
);
44 printf("Error opening archive '%s': Unknown error\n", filename
);
49 gOpenArchives
.push_front(this);
52 void MPQArchive::close()
54 //gOpenArchives.erase(erase(&mpq_a);
55 libmpq_archive_close(&mpq_a
);
58 MPQFile::MPQFile(const char* filename
):
64 for(ArchiveSet::iterator i
=gOpenArchives
.begin(); i
!=gOpenArchives
.end();++i
)
66 mpq_archive
&mpq_a
= (*i
)->mpq_a
;
68 mpq_hash hash
= (*i
)->GetHashEntry(filename
);
69 uint32 blockindex
= hash
.blockindex
;
71 if ((blockindex
== 0xFFFFFFFF) || (blockindex
== 0)) {
72 continue; //file not found
75 uint32 fileno
= blockindex
;
77 //int fileno = libmpq_file_number(&mpq_a, filename);
78 //if(fileno == LIBMPQ_EFILE_NOT_FOUND)
82 size
= libmpq_file_info(&mpq_a
, LIBMPQ_FILE_UNCOMPRESSED_SIZE
, fileno
);
83 // HACK: in patch.mpq some files don't want to open and give 1 for filesize
89 buffer
= new char[size
];
92 libmpq_file_getdata(&mpq_a
, hash
, fileno
, (unsigned char*)buffer
);
100 size_t MPQFile::read(void* dest
, size_t bytes
)
104 size_t rpos
= pointer
+ bytes
;
106 bytes
= size
- pointer
;
110 memcpy(dest
, &(buffer
[pointer
]), bytes
);
117 void MPQFile::seek(int offset
)
120 eof
= (pointer
>= size
);
123 void MPQFile::seekRelative(int offset
)
126 eof
= (pointer
>= size
);
129 void MPQFile::close()
131 if (buffer
) delete[] buffer
;