File: splitshp.cc

package info (click to toggle)
exult 1.2-4
  • links: PTS
  • area: contrib
  • in suites: sarge
  • size: 9,040 kB
  • ctags: 10,543
  • sloc: cpp: 99,373; sh: 8,333; ansic: 4,659; makefile: 988; yacc: 769; lex: 313; xml: 19
file content (304 lines) | stat: -rw-r--r-- 6,510 bytes parent folder | download | duplicates (8)
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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
/*
Copyright (C) 2001  The Exult Team

This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
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 General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
*/

#ifdef HAVE_CONFIG_H
#  include <config.h>
#endif

#include <iostream>

#ifndef ALPHA_LINUX_CXX
#  include <cstdio>
#  include <cstring>
#  include <string>
#endif

#include "exult_types.h"


using std::cout;
using std::endl;
using std::cerr;
using std::string;
using std::strlen;
using std::strrchr;
using std::strcpy;
using std::memcpy;
using std::exit;

struct u7frame {
	bool tile;
	uint32 datalen;
	uint8 *data;
};

struct u7shape {
	int num_frames;
	struct u7frame *frames;
};

unsigned int read1(FILE *f)
{
	unsigned char b0;
	b0 = fgetc(f);
	return b0;
}

unsigned int read2(FILE *f)
{
	unsigned char b0, b1;
	b0 = fgetc(f);
	b1 = fgetc(f);
	return (b0 + (b1<<8));
}

signed int read2signed(FILE *f)
{
	unsigned char b0, b1;
	signed int i0;
	b0 = fgetc(f);
	b1 = fgetc(f);
	i0 = b0 + (b1<<8);
	if (i0 >= 32768) { i0 -= 65536; }
	return (i0);
}

unsigned int read4(FILE *f)
{
	unsigned char b0, b1, b2, b3;
	b0 = fgetc(f);
	b1 = fgetc(f);
	b2 = fgetc(f);
	b3 = fgetc(f);
	return (b0 + (b1<<8) + (b2<<16) + (b3<<24));
}

void write4(FILE *f, unsigned int b)
{
	fputc(b & 0xFF, f);
	fputc((b >> 8) & 0xFF, f);
	fputc((b >> 16) & 0xFF, f);
	fputc((b >> 24) & 0xFF, f);
}

char* framefilename(char * shapefilename, int frame)
{
	char *fn = new char[strlen(shapefilename)+5];  //_xxx\0
	char *dot = strrchr(shapefilename, '.');
#ifdef WIN32
	char *slash = strrchr(shapefilename, '\\');
#else
	char *slash = strrchr(shapefilename, '/');
#endif
	int dotpos;

	if (dot == 0 || slash > dot)
		dotpos = strlen(shapefilename);
	else
		dotpos = dot - shapefilename;

	memcpy(fn, shapefilename, dotpos);
	sprintf(fn + dotpos, "_%03i", frame);
	strcpy(fn + dotpos + 4, shapefilename + dotpos);

	return fn;
}

void split_shape(char* filename)
{
	FILE *shpfile, *framefile;
	int file_size, shape_size, hdr_size;
	int frame_offset, next_frame_offset;
	int num_frames;
	int datalen;
	uint8 *data;
	char *framename;
	int i;
	
	shpfile = fopen (filename, "rb");
	if (!shpfile) {
		cerr << "Can't open " << filename << endl;
		return;
	}
	fseek(shpfile, 0, SEEK_END);
	file_size = ftell(shpfile);
	fseek(shpfile, 0, SEEK_SET);
	
	shape_size = read4(shpfile);
	
	if(file_size!=shape_size) {	/* 8x8 tile */
		num_frames = file_size/64;
		fseek(shpfile, 0, SEEK_SET);		/* Return to start of file */
		cout << "num_frames = " << num_frames << endl;
		data = new uint8[64];
		for(i=0; i<num_frames; i++) {
			framename = framefilename(filename, i);
			cout << "writing " << framename << "..." << endl;
			framefile = fopen(framename, "wb");
			fread(data, 1, 64, shpfile);
			fwrite(data, 1, 64, framefile);
			fclose(framefile);
			delete[] framename;
		}
		delete[] data;
	} else {
		hdr_size = read4(shpfile);
		num_frames = (hdr_size-4)/4;
		
		cout << "num_frames = "<< num_frames << endl;
		
		for(i=0; i<num_frames; i++) {
			framename = framefilename(filename, i);
			cout << "writing " << framename << "..." << endl;
			framefile = fopen(framename, "wb");

			// Go to where frame offset is stored
			fseek(shpfile, (i+1)*4, SEEK_SET);
			frame_offset = read4(shpfile);

			if (i+1 < num_frames)
				next_frame_offset = read4(shpfile);
			else
				next_frame_offset = shape_size;

			fseek(shpfile, frame_offset, SEEK_SET);
			datalen = next_frame_offset - frame_offset;

			write4(framefile, datalen+8);
			write4(framefile, 8);

			data = new uint8[datalen];
			fread(data, 1, datalen, shpfile);
			fwrite(data, 1, datalen, framefile);
			fclose(framefile);
			delete[] framename;
			delete[] data;
		}
	}
	cout << "done" << endl;

	fclose(shpfile);
}

void merge_frames(char *shapefile, char** framefiles, int numframefiles)
{
	FILE *shpfile, *framefile;
	int i;
	bool tiles = false;
	int file_size, shape_size, hdr_size, frame_size;
	int total_size;
	uint8 *data;

	shpfile = fopen(shapefile, "wb");

	total_size = 4 + 4*numframefiles;

	for (i=0; i<numframefiles; i++) {
		framefile = fopen(framefiles[i], "rb");

		cout << "reading " << framefiles[i] << "..." << endl;
		
		fseek(framefile, 0, SEEK_END);
		file_size = ftell(framefile);
		fseek(framefile, 0, SEEK_SET);
		shape_size = read4(framefile);

		if (file_size != shape_size) { // 8x8 tile
			if (i > 0 && !tiles) {
				cout << "Error: can't mix 8x8 tiles and non-tile shapes!" << endl;
				exit(1);
			}

			tiles = true;

			fseek(framefile, 0, SEEK_SET);
			fseek(shpfile, 64*i, SEEK_SET);

			data = new uint8[64];

			fread(data, 1, 64, framefile);
			fwrite(data, 1, 64, shpfile);
			fclose(framefile);

			delete [] data;
		} else {
			if (tiles) {
				cout << "Error: can't mix 8x8 tiles and non-tile shapes!" << endl;
				exit(1);
			}

			hdr_size = read4(framefile);
			
			if (hdr_size > 8) {
				frame_size = read4(framefile);
				frame_size -= hdr_size;
			} else {
				frame_size = shape_size - hdr_size;
			}

			data = new uint8[frame_size];
			fseek(framefile, hdr_size, SEEK_SET);
			fread(data, 1, frame_size, framefile);
			fclose(framefile);

			fseek(shpfile, 4 + (i*4), SEEK_SET);
			write4(shpfile, total_size);
			fseek(shpfile, total_size, SEEK_SET);
			fwrite(data, 1, frame_size, shpfile);

			total_size += frame_size;
			delete [] data;
		}
	}

	if (!tiles) {
		fseek(shpfile, 0, SEEK_SET);
		write4(shpfile, total_size);
	}

	cout << "done" << endl;

	fclose(shpfile);
}

int main(int argc, char *argv[])
{
	char* shapefile;
	int numframefiles;
	char* framefiles[255];
	u7shape* sh;
	
	if (argc < 2) {
	  cout << "Usage: To split: splitshp [shape file]" << endl
           << "     or to pack: splitshp [shape file] [frame files]" << endl;
	  return 0;
	}
	
	shapefile = argv[1];

	if (argc > 2) {
		numframefiles = argc - 2;
		for (int i=0; i<numframefiles; i++)
			framefiles[i] = argv[i+2];
		merge_frames(shapefile, framefiles, numframefiles);
	} else {
		split_shape(shapefile);
	}

	return 0;
}