forked from quadra-game/quadra
/
resfile.cpp
181 lines (149 loc) · 3.82 KB
/
resfile.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
/* -*- Mode: C++; c-basic-offset: 2; tab-width: 2; indent-tabs-mode: nil -*-
*
* Quadra, an action puzzle game
* Copyright (C) 1998-2000 Ludus Design
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
#include <stdint.h>
#include <stdio.h>
#include <string.h>
#include "error.h"
#include "res.h"
#include "resfile.h"
#include "byteorder.h"
Resdata::Resdata(char *resname, int ressize, uint8_t *resdata, Resdata *list) {
name = resname;
size = ressize;
data = resdata;
next = list;
}
Resdata::~Resdata() {
delete[] name;
delete[] data;
if(next)
delete next;
}
Resfile::Resfile()
: list(NULL) {
}
Resfile::Resfile(const char *fname)
: list(NULL) {
Res_dos res(fname, RES_TRY);
if(res.exist) {
skelton_msgbox("Resfile %s is fine\n", fname);
thaw(res);
} else
skelton_msgbox("Resfile %s does not exist\n", fname);
}
Resfile::~Resfile() {
clear();
}
void Resfile::freeze(Res_dos& res) const {
int resnamelen;
Resdata *ptr;
uint32_t d;
res.write(&signature, sizeof(signature));
ptr = list;
while(ptr != NULL) {
resnamelen = strlen(ptr->name)+1;
d = INTELDWORD(resnamelen);
res.write(&d, sizeof(d));
res.write(ptr->name, resnamelen);
d = INTELDWORD(ptr->size);
res.write(&d, sizeof(d));
res.write(ptr->data, ptr->size);
ptr = ptr->next;
}
resnamelen = 0;
res.write(&resnamelen, sizeof(resnamelen));
}
void Resfile::thaw(Res& res) {
char sig[sizeof(signature)];
int resnamelen;
char *resname;
uint8_t *resdata;
int ressize;
res.position(0);
res.read(sig, sizeof(signature));
if(strncmp(sig, signature, sizeof(signature)) != 0) {
msgbox("Resfile::thaw(): invalid signature\n");
return;
}
do {
res.read(&resnamelen, sizeof(resnamelen));
resnamelen = INTELDWORD(resnamelen);
if(resnamelen == 0)
break;
resname = new char[resnamelen];
res.read(resname, resnamelen);
res.read(&ressize, sizeof(ressize));
ressize = INTELDWORD(ressize);
resdata = new uint8_t[ressize];
res.read(resdata, ressize);
list = new Resdata(resname, ressize, resdata, list);
} while(resnamelen > 0);
}
void Resfile::clear() {
if(list)
delete list;
list = NULL;
}
void Resfile::add(const char *resname, const int size, const char *resdata) {
char *myname;
uint8_t *mydata;
myname = new char[strlen(resname)+1];
memcpy(myname, resname, strlen(resname)+1);
mydata = new uint8_t[size];
memcpy(mydata, resdata, size);
list = new Resdata(myname, size, mydata, list);
}
int Resfile::get(const ResName& resname, uint8_t **resdata) const {
Resdata *ptr;
ptr = list;
while(ptr != NULL) {
if(strcasecmp(ptr->name, resname.name_.c_str()) == 0)
break;
ptr = ptr->next;
}
if(ptr) {
*resdata = ptr->data;
return ptr->size;
} else {
*resdata = NULL;
return 0;
}
}
void Resfile::remove(const char* resname) {
Resdata *ptr, *prev;
prev = NULL;
ptr = list;
while(ptr != NULL) {
if(strcasecmp(ptr->name, resname) == 0)
break;
prev = ptr;
ptr = ptr->next;
}
if(ptr) {
if(prev)
prev->next = ptr->next;
else
list = ptr->next;
ptr->next = NULL;
delete ptr;
}
// Can somebody tell me why Resfile::list isn't an std::vector<Resdata>?
// We all know linked lists suck, don't we? Whatever...
}