[9158] Fixed build errors for contrib/extractor at some x64 plaforms
[getmangos.git] / contrib / extractor / mpq_libmpq.cpp
blobbdf1f7d3c731b97a02f361484f0ee7d8e7116678
1 #include "mpq_libmpq.h"
2 #include <deque>
3 #include <stdio.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);
11 if(result) {
12 switch(result) {
13 case LIBMPQ_EFILE : /* error on file operation */
14 printf("Error opening archive '%s': File operation Error\n", filename);
15 break;
16 case LIBMPQ_EFILE_FORMAT : /* bad file format */
17 printf("Error opening archive '%s': Bad file format\n", filename);
18 break;
19 case LIBMPQ_EFILE_CORRUPT : /* file corrupt */
20 printf("Error opening archive '%s': File corrupt\n", filename);
21 break;
22 case LIBMPQ_EFILE_NOT_FOUND : /* file in archive not found */
23 printf("Error opening archive '%s': File in archive not found\n", filename);
24 break;
25 case LIBMPQ_EFILE_READ : /* Read error in archive */
26 printf("Error opening archive '%s': Read error in archive\n", filename);
27 break;
28 case LIBMPQ_EALLOCMEM : /* maybe not enough memory? :) */
29 printf("Error opening archive '%s': Maybe not enough memory\n", filename);
30 break;
31 case LIBMPQ_EFREEMEM : /* can not free memory */
32 printf("Error opening archive '%s': Cannot free memory\n", filename);
33 break;
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);
36 break;
37 case LIBMPQ_EHASHTABLE : /* error in reading hashtable */
38 printf("Error opening archive '%s': Error in reading hashtable\n", filename);
39 break;
40 case LIBMPQ_EBLOCKTABLE : /* error in reading blocktable */
41 printf("Error opening archive '%s': Error in reading blocktable\n", filename);
42 break;
43 default:
44 printf("Error opening archive '%s': Unknown error\n", filename);
45 break;
47 return;
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):
59 eof(false),
60 buffer(0),
61 pointer(0),
62 size(0)
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)
79 // continue;
81 // 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
84 if (size<=1) {
85 eof = true;
86 buffer = 0;
87 return;
89 buffer = new char[size];
91 //libmpq_file_getdata
92 libmpq_file_getdata(&mpq_a, hash, fileno, (unsigned char*)buffer);
93 return;
96 eof = true;
97 buffer = 0;
100 size_t MPQFile::read(void* dest, size_t bytes)
102 if (eof) return 0;
104 size_t rpos = pointer + bytes;
105 if (rpos > size) {
106 bytes = size - pointer;
107 eof = true;
110 memcpy(dest, &(buffer[pointer]), bytes);
112 pointer = rpos;
114 return bytes;
117 void MPQFile::seek(int offset)
119 pointer = offset;
120 eof = (pointer >= size);
123 void MPQFile::seekRelative(int offset)
125 pointer += offset;
126 eof = (pointer >= size);
129 void MPQFile::close()
131 if (buffer) delete[] buffer;
132 buffer = 0;
133 eof = true;