Subversion Repositories spk

Rev

Rev 1 | Rev 52 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
1 cycrow 1
#include <spk.h>
2
 
3
CyString g_dir;
4
 
5
#ifdef _WIN32
6
#include <windows.h>
7
#include <direct.h>
8
//#include <shlobj.h>
9
#else
10
#include <dirent.h> 
11
#include <sys/types.h> 
12
#include <sys/param.h> 
13
#include <sys/stat.h> 
14
#include <unistd.h> 
15
#endif
16
 
17
void PrintError ( int err )
18
{
19
	switch ( err )
20
	{
21
		case CATERR_NODATFILE:
22
			printf ( "No dat file found\n" );
23
			break;
24
		case CATERR_NOCATFILE:
25
			printf ( "Unable to open cat file\n" );
26
			break;
27
		case CATERR_FILEEMPTY:
28
			printf ( "Cat file is empty\n" );
29
			break;
30
		case CATERR_READCAT:
31
			printf ( "Unable to read cat file\n" );
32
			break;
33
		case CATERR_DECRYPT:
34
			printf ( "Unable to decrypt cat file\n" );
35
			break;
36
		case CATERR_MISMATCH:
37
			printf ( "Dat file size mismatch\n" );
38
			break;
39
	}
40
}
41
 
42
void ListFiles ( CyString filename, CyString searchmask )
43
{
44
	printf ( "Listing files in %s...", filename.c_str() );
45
	if ( !searchmask.Empty() )
46
		printf ( "(%s)", searchmask.c_str() );
47
	printf ( "\n");
48
 
49
	CCatFile catfile;
50
	int err = catfile.Open ( filename, "", CATREAD_CATDECRYPT );
51
 
52
	if ( err == CATERR_NONE )
53
	{
54
		printf ( "Opened file\n" );
55
		for ( int i = 0; i < catfile.GetNumFiles(); i++ )
56
		{
57
			SInCatFile *file = catfile.GetFile ( i );
58
 
59
			if ( !searchmask.Empty() )
60
			{
61
				if ( !searchmask.WildMatch(file->sFile) )
62
					continue;
63
			}
64
 
65
			printf ( "[%9s] %s\n", SPK::GetSizeString(file->lSize).c_str(), file->sFile.c_str() );
66
		}
67
	}
68
	else
69
		PrintError ( err );
70
}
71
 
72
CyStringList *FindFiles(CyString filepattern)
73
{
74
	CFileIO File((filepattern.IsIn(":")) ? filepattern : g_dir + "/" + filepattern);
75
	CyStringList *files = File.GetDirIO().DirList(NullString, File.GetFilename());
76
	return files;
77
}
78
 
79
 
80
void ExtractFile ( CyString filename, CyString to, bool preserve )
81
{
41 cycrow 82
	//TEST
83
	char file1[10];
84
	char file2[10];
85
	FILE *id = fopen("d:/hull.bob", "rb");
86
	if ( id ) {
87
		fread(file1, sizeof(char), 10, id);
88
		fclose(id);
89
	}
90
	id = fopen("c:/extracted/hull.bob", "rb");
91
	if ( id ) {
92
		fread(file2, sizeof(char), 10, id);
93
		fclose(id);
94
	}
95
 
1 cycrow 96
	if ( !filename.IsIn ( "::" ) )
97
		return;
98
 
99
	CyString catfile = filename.GetToken ( "::", 1, 1 );
100
	CyString filemask = filename.GetToken ( "::", 2, 2 );
101
 
102
	CCatFile cat;
103
	int err = cat.Open ( catfile, "", CATREAD_DAT );
104
	if ( err )
105
	{
106
		PrintError ( err );
107
		return;
108
	}
109
 
110
	// all files
111
	if ( filemask == "*" )
112
	{
113
		for ( int i = 0; i < cat.GetNumFiles(); i++ )
114
		{
115
			SInCatFile *f = cat.GetFile(i);
116
			if ( !cat.ExtractFile ( f, to ) )
117
				printf ( "Error: %s\n", cat.GetErrorString().c_str() );
118
			else
119
				printf ( "File has been written (%s)\n", cat.ErrorString().c_str() );
120
		}
121
 
122
		return;
123
	}
124
 
41 cycrow 125
	//TODO: add proper file mask for extracting
126
	//CyStringList *fileList = FindFiles(filemask);
127
	CyStringList *fileList = new CyStringList;
128
	fileList->PushBack(filemask);
129
	if ( !fileList || fileList->Empty() )
1 cycrow 130
	{
41 cycrow 131
		if ( fileList ) delete fileList;
1 cycrow 132
		printf ( "Error: unable to find any files matching: %s\n", filemask.c_str() );
133
		return;
134
	}
135
 
136
	for ( SStringList *fl = fileList->Head(); fl; fl = fl->next )
137
	{
138
		CyString file = fl->str;
139
		if ( !cat.ExtractFile ( file, to, preserve ) )
140
			printf ( "Error: %s\n", cat.GetErrorString().c_str() );
141
		else
142
			printf ( "File has been written (%s)\n", cat.ErrorString().c_str() );
143
	}
144
 
145
	delete fileList;
146
}
147
 
148
void AppendFile ( CyString C_File, CyString filepattern )
149
{
150
	CyString catfile;
151
	CyString file;
152
 
153
	C_File = C_File.FindReplace("\\", "/");
154
 
155
	bool doFile = false;
156
	if ( !C_File.IsIn ( "::" ) )
157
	{
158
		catfile = C_File;
159
		doFile = true;
160
	}
161
	else
162
	{
163
		catfile = C_File.GetToken ( "::", 1, 1 );
164
		file = C_File.GetToken ( "::", 2, 2 );
165
	}
166
 
167
	CyStringList *list = FindFiles(filepattern);
168
	if ( !list || !list->Count() )
169
	{
170
		printf("Error: no files found to add: %s\n", filepattern.c_str());
171
		return;
172
	}
173
 
174
	CCatFile cat;
175
	int err = cat.Open ( catfile, "", CATREAD_CATDECRYPT );
176
	if ( err )
177
	{
178
		delete list;
179
		PrintError ( err );
180
		return;
181
	}
182
 
183
	for ( SStringList *fname = list->Head(); fname; fname = fname->next )
184
	{
185
		CyString filename = fname->str;
186
		if ( doFile )
187
			file = filename;
188
 
189
		if ( !file.IsIn('.') )
190
		{
191
			if ( file[file.Length() - 1] != '/' )
192
				file += '/';
193
			file += CFileIO(filename).GetFilename();
194
		}
195
 
196
		if ( cat.AppendFile ( filename, file ) )
197
			printf ( "File %s has beed added to: %s::%s\n", filename.c_str(), catfile.c_str(), file.c_str() );
198
		else
199
			printf ( "Error: Unable to add file: %s\n", filename.c_str());
200
	}
201
 
202
	delete list;
203
}
204
 
205
void RemoveFile ( CyString C_File, CyString remfile )
206
{
207
	// first open the cat file
208
	CCatFile cat;
209
	int err = cat.Open ( C_File, "", CATREAD_CATDECRYPT );
210
	if ( err )
211
	{
212
		PrintError ( err );
213
		return;
214
	}
215
 
216
	SInCatFile *f = cat.FindData ( remfile );
217
	if ( !f )
218
	{
219
		printf ( "Unable to find %s in cat file\n", remfile.c_str() );
220
		return;
221
	}
222
 
223
	if ( cat.RemoveFile ( f ) )
224
		printf ( "File has been removed from archive\n" );
225
}
226
 
227
void UnpackFile ( CyString file, CyString tofile )
228
{
229
	CyStringList *list = FindFiles(file);
230
	if ( !list || !list->Count() )
231
	{
232
		printf("Error: no files found to unpack: %s\n", file.c_str());
233
		return;
234
	}
235
 
236
	for ( SStringList *fname = list->Head(); fname; fname = fname->next )
237
	{
238
		CyString filename = CFileIO(file).GetDir() + "/" + fname->str;
239
 
240
		C_File f(filename);
241
		if ( !f.CheckValidFilePointer() )
242
			printf("Error: %s doesn't exists\n", filename.c_str() );
243
		else if ( !f.ReadFromFile() )
244
			printf("Error: unable to open file: %s\n", filename.c_str() );
245
		else
246
		{
247
			f.UnPCKFile();
248
			if ( tofile.Empty() )
249
				f.ChangeFileExt("xml");
250
			else if ( tofile.Left(2) == "*." )
251
				f.ChangeFileExt(tofile.Right(3));
252
			else
253
				f.SetFilename(tofile);
254
 
255
			if ( !f.WriteFilePointer() )
256
				printf("Error: unable to write file: %s\n", tofile.c_str() );
257
			else
258
				printf("%s has been unpacked to %s\n", file.c_str(), f.GetFilename().c_str() );
259
		}
260
	}
261
 
262
	delete list;
263
}
264
 
265
void PackFile ( CyString file, CyString tofile )
266
{
267
	CyStringList *list = FindFiles(file);
268
	if ( !list || !list->Count() )
269
	{
270
		printf("Error: no files found to pack: %s\n", file.c_str());
271
		return;
272
	}
273
 
274
	for ( SStringList *fname = list->Head(); fname; fname = fname->next )
275
	{
276
		CyString filename = CFileIO(file).GetDir() + "/" + fname->str;
277
 
278
		C_File f(filename);
279
		if ( !f.CheckValidFilePointer() )
280
			printf("Error: %s doesn't exists\n", filename.c_str() );
281
		else if ( !f.ReadFromFile() )
282
			printf("Error: unable to open file: %s\n", filename.c_str() );
283
		else if ( !f.PCKFile() )
284
			printf("Error: unable to pack file: %s\n", filename.c_str() );
285
		else
286
		{
287
			if ( tofile.Empty() )
288
			{
289
				if ( f.CheckFileExt("bob") )
290
					f.ChangeFileExt("pbb");
291
				else if ( f.CheckFileExt("bod") )
292
					f.ChangeFileExt("pbd");
293
				else
294
					f.ChangeFileExt("pck");
295
			}
296
			else if ( tofile.Left(2) == "*." )
297
				f.ChangeFileExt(tofile.Right(3));
298
			else
299
				f.SetFilename(tofile);
300
 
301
			if ( !f.WriteFilePointer() )
302
				printf("Error: unable to write file: %s\n", tofile.c_str() );
303
			else
304
				printf("%s has been packed to %s\n", file.c_str(), f.GetFilename().c_str() );
305
		}
306
	}
307
 
308
	delete list;
309
}
310
 
311
void PrintSyntax(CyString cmd)
312
{
313
	printf ( "Syntax: %s <flags> [arguments]\n", cmd.c_str() );
314
	printf ( "Flags:\n");
315
	printf ( "\t-l <filename> [filemask]\n");
316
	printf ( "\t\tLists the contents of a cat file, with optional search mask\n");
317
	printf ( "\t-x <catfile::filename> [dir]\n");
318
	printf ( "\t\textracts a file from a cat file to the optional directory\n");
319
	printf ( "\t-xp <catfile::filename> [dir]\n");
320
	printf ( "\t\textracts a file from a cat file to the optional directory, preserves directory structure\n");
321
	printf ( "\t-xa <catfile> [dir]\n");
322
	printf ( "\t\textracts all files to optional directory\n");
323
	printf ( "\t-a <filename> <catfile::tofile>\n");
324
	printf ( "\t\tAdds the file into a cat archive, saves it as <tofile>\n");
325
	printf ( "\t-r <catfile> <file>\n");
326
	printf ( "\t\tRemoves a file from an archive\n");
327
	printf ( "\t-u <filename> <tofile>\n");
328
	printf ( "\t\tUnpacks a file, ie from pck to xml/txt\n");
329
	printf ( "\t-p <filename> <tofile>\n");
330
	printf ( "\t\tPacks a file, ie from xml/txt to pck\n");
331
}
332
 
333
int main ( int argc, char **argv )
334
{
335
	printf ( "\nCATPCK Tool V1.21 27/03/2011 (SPK: %.2f) Created by Cycrow\n\n", GetLibraryVersion() );
336
 
337
	// parse the cmd name
338
	CyString cmd (argv[0]);
339
	cmd = cmd.FindReplace ( "\\", "/" );
340
	g_dir = cmd.GetToken ( 0, cmd.NumToken('/') - 1, '/' );
341
	cmd = cmd.GetToken ( cmd.NumToken('/'), '/' );
342
 
343
	if ( g_dir.Empty() )
344
	{
345
	    #ifdef _WIN32
346
		g_dir = CyString(_getcwd(NULL, 0));
347
		#else
348
		g_dir = CyString(getcwd(NULL, 0));
349
		#endif
350
		if ( g_dir.Empty() )
351
			g_dir = "./";
352
	}
353
 
354
	if ( argc < 2 )
355
		PrintSyntax(cmd);
356
	else
357
	{
358
		CyString command ( argv[1] );
359
		command.ToLower();
360
 
361
		if ( (command == "-l") || (command == "-list") )
362
		{
363
			if ( argc < 3 )
364
				printf ( "Syntax: %s -l <filename> [filemask]\n\tLists the contents of the cat file\n", cmd.c_str() );
365
			else if ( argc < 4 )
366
				ListFiles ( CyString(argv[2]), "" );
367
			else
368
				ListFiles ( CyString(argv[2]), CyString(argv[3]) );
369
		}
370
		else if ( (command == "-x") || (command == "-extract") || (command == "-xp") || (command == "-extractpreserve") )
371
		{
372
			if ( argc < 3 )
373
				printf ( "Syntax: %s %s <catfile::filename> [dir]\n\tExtracts a file from the cat archive\n", cmd.c_str(), command.c_str() );
374
			else if ( argc < 4 )
375
				ExtractFile ( CyString(argv[2]), CyString(""), (command == "-xp" || command == "-extractpreserve") ? true : false );
376
			else
377
				ExtractFile ( CyString(argv[2]), CyString(argv[3]), (command == "-xp" || command == "-extractpreserve") ? true : false );
378
		}
379
		else if ( (command == "-xa") || (command == "-extractall") )
380
		{
381
			if ( argc < 3 )
382
				printf ( "Syntax: %s %s <catfile> [dir]\n\tExtracts all files from the cat archive\n", cmd.c_str(), command.c_str() );
383
			else if ( argc < 4 )
384
				ExtractFile ( CyString(argv[2]) + "::*", CyString(""), true );
385
			else
386
				ExtractFile ( CyString(argv[2]) + "::*", CyString(argv[3]), true );
387
		}
388
		else if ( (command == "-a") || (command == "-append") )
389
		{
390
			if ( argc < 4 )
391
				printf ( "Syntax: %s -a <filename> <catfile::tofile>\n\tAppends a file into the archive\n", cmd.c_str() );
392
			else
393
				AppendFile ( CyString(argv[3]), CyString(argv[2]) );
394
		}
395
		else if ( (command == "-r") || (command == "-remove") )
396
		{
397
			if ( argc < 4 )
398
				printf ( "Syntax: %s -r <catfile> <filename>\n\tRemoves a file from the archive\n", cmd.c_str() );
399
			else
400
				RemoveFile ( CyString(argv[2]), CyString(argv[3]) );
401
		}
402
		else if ( (command == "-u") || (command == "-unpack") )
403
		{
404
			if ( argc < 3 )
405
				printf ( "Syntax: %s -u <filename> [to]\n\tUnpacks a file", cmd.c_str() );
406
			else if ( argc < 4 )
407
				UnpackFile ( CyString(argv[2]), CyString("") );
408
			else
409
				UnpackFile ( CyString(argv[2]), CyString(argv[3]) );
410
		}
411
		else if ( (command == "-p") || (command == "-pack") )
412
		{
413
			if ( argc < 3 )
414
				printf ( "Syntax: %s -p <filename> [to]\n\tPacks a file to .pck", cmd.c_str() );
415
			else if ( argc < 4 )
416
				PackFile ( CyString(argv[2]), CyString("") );
417
			else
418
				PackFile ( CyString(argv[2]), CyString(argv[3]) );
419
		}
420
		else
421
		{
422
			printf("Invalaid flag: %s\n\n", command.c_str());
423
			PrintSyntax(cmd);
424
		}
425
	}
426
 
427
#ifdef _DEBUG
428
	char pause;
429
	printf ( "\n\nPress a key to end\n" );
430
	scanf ( "%c", &pause );
431
#endif
432
 
433
	return 0;
434
}
435