Subversion Repositories spk

Rev

Rev 1 | 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
{
82
	if ( !filename.IsIn ( "::" ) )
83
		return;
84
 
85
	CyString catfile = filename.GetToken ( "::", 1, 1 );
86
	CyString filemask = filename.GetToken ( "::", 2, 2 );
87
 
88
	CCatFile cat;
89
	int err = cat.Open ( catfile, "", CATREAD_DAT );
90
	if ( err )
91
	{
92
		PrintError ( err );
93
		return;
94
	}
95
 
96
	// all files
97
	if ( filemask == "*" )
98
	{
99
		for ( int i = 0; i < cat.GetNumFiles(); i++ )
100
		{
101
			SInCatFile *f = cat.GetFile(i);
102
			if ( !cat.ExtractFile ( f, to ) )
103
				printf ( "Error: %s\n", cat.GetErrorString().c_str() );
104
			else
105
				printf ( "File has been written (%s)\n", cat.ErrorString().c_str() );
106
		}
107
 
108
		return;
109
	}
110
 
111
	CyStringList *fileList = FindFiles(filemask);
112
	if ( !fileList && fileList->Empty() )
113
	{
114
		printf ( "Error: unable to find any files matching: %s\n", filemask.c_str() );
115
		return;
116
	}
117
 
118
	for ( SStringList *fl = fileList->Head(); fl; fl = fl->next )
119
	{
120
		CyString file = fl->str;
121
		if ( !cat.ExtractFile ( file, to, preserve ) )
122
			printf ( "Error: %s\n", cat.GetErrorString().c_str() );
123
		else
124
			printf ( "File has been written (%s)\n", cat.ErrorString().c_str() );
125
	}
126
 
127
	delete fileList;
128
}
129
 
130
void AppendFile ( CyString C_File, CyString filepattern )
131
{
132
	CyString catfile;
133
	CyString file;
134
 
135
	C_File = C_File.FindReplace("\\", "/");
136
 
137
	bool doFile = false;
138
	if ( !C_File.IsIn ( "::" ) )
139
	{
140
		catfile = C_File;
141
		doFile = true;
142
	}
143
	else
144
	{
145
		catfile = C_File.GetToken ( "::", 1, 1 );
146
		file = C_File.GetToken ( "::", 2, 2 );
147
	}
148
 
149
	CyStringList *list = FindFiles(filepattern);
150
	if ( !list || !list->Count() )
151
	{
152
		printf("Error: no files found to add: %s\n", filepattern.c_str());
153
		return;
154
	}
155
 
156
	CCatFile cat;
157
	int err = cat.Open ( catfile, "", CATREAD_CATDECRYPT );
158
	if ( err )
159
	{
160
		delete list;
161
		PrintError ( err );
162
		return;
163
	}
164
 
165
	for ( SStringList *fname = list->Head(); fname; fname = fname->next )
166
	{
167
		CyString filename = fname->str;
168
		if ( doFile )
169
			file = filename;
170
 
171
		if ( !file.IsIn('.') )
172
		{
173
			if ( file[file.Length() - 1] != '/' )
174
				file += '/';
175
			file += CFileIO(filename).GetFilename();
176
		}
177
 
178
		if ( cat.AppendFile ( filename, file ) )
179
			printf ( "File %s has beed added to: %s::%s\n", filename.c_str(), catfile.c_str(), file.c_str() );
180
		else
181
			printf ( "Error: Unable to add file: %s\n", filename.c_str());
182
	}
183
 
184
	delete list;
185
}
186
 
187
void RemoveFile ( CyString C_File, CyString remfile )
188
{
189
	// first open the cat file
190
	CCatFile cat;
191
	int err = cat.Open ( C_File, "", CATREAD_CATDECRYPT );
192
	if ( err )
193
	{
194
		PrintError ( err );
195
		return;
196
	}
197
 
198
	SInCatFile *f = cat.FindData ( remfile );
199
	if ( !f )
200
	{
201
		printf ( "Unable to find %s in cat file\n", remfile.c_str() );
202
		return;
203
	}
204
 
205
	if ( cat.RemoveFile ( f ) )
206
		printf ( "File has been removed from archive\n" );
207
}
208
 
209
void UnpackFile ( CyString file, CyString tofile )
210
{
211
	CyStringList *list = FindFiles(file);
212
	if ( !list || !list->Count() )
213
	{
214
		printf("Error: no files found to unpack: %s\n", file.c_str());
215
		return;
216
	}
217
 
218
	for ( SStringList *fname = list->Head(); fname; fname = fname->next )
219
	{
220
		CyString filename = CFileIO(file).GetDir() + "/" + fname->str;
221
 
222
		C_File f(filename);
223
		if ( !f.CheckValidFilePointer() )
224
			printf("Error: %s doesn't exists\n", filename.c_str() );
225
		else if ( !f.ReadFromFile() )
226
			printf("Error: unable to open file: %s\n", filename.c_str() );
227
		else
228
		{
229
			f.UnPCKFile();
230
			if ( tofile.Empty() )
231
				f.ChangeFileExt("xml");
232
			else if ( tofile.Left(2) == "*." )
233
				f.ChangeFileExt(tofile.Right(3));
234
			else
235
				f.SetFilename(tofile);
236
 
237
			if ( !f.WriteFilePointer() )
238
				printf("Error: unable to write file: %s\n", tofile.c_str() );
239
			else
240
				printf("%s has been unpacked to %s\n", file.c_str(), f.GetFilename().c_str() );
241
		}
242
	}
243
 
244
	delete list;
245
}
246
 
247
void PackFile ( CyString file, CyString tofile )
248
{
249
	CyStringList *list = FindFiles(file);
250
	if ( !list || !list->Count() )
251
	{
252
		printf("Error: no files found to pack: %s\n", file.c_str());
253
		return;
254
	}
255
 
256
	for ( SStringList *fname = list->Head(); fname; fname = fname->next )
257
	{
258
		CyString filename = CFileIO(file).GetDir() + "/" + fname->str;
259
 
260
		C_File f(filename);
261
		if ( !f.CheckValidFilePointer() )
262
			printf("Error: %s doesn't exists\n", filename.c_str() );
263
		else if ( !f.ReadFromFile() )
264
			printf("Error: unable to open file: %s\n", filename.c_str() );
265
		else if ( !f.PCKFile() )
266
			printf("Error: unable to pack file: %s\n", filename.c_str() );
267
		else
268
		{
269
			if ( tofile.Empty() )
270
			{
271
				if ( f.CheckFileExt("bob") )
272
					f.ChangeFileExt("pbb");
273
				else if ( f.CheckFileExt("bod") )
274
					f.ChangeFileExt("pbd");
275
				else
276
					f.ChangeFileExt("pck");
277
			}
278
			else if ( tofile.Left(2) == "*." )
279
				f.ChangeFileExt(tofile.Right(3));
280
			else
281
				f.SetFilename(tofile);
282
 
283
			if ( !f.WriteFilePointer() )
284
				printf("Error: unable to write file: %s\n", tofile.c_str() );
285
			else
286
				printf("%s has been packed to %s\n", file.c_str(), f.GetFilename().c_str() );
287
		}
288
	}
289
 
290
	delete list;
291
}
292
 
293
void PrintSyntax(CyString cmd)
294
{
295
	printf ( "Syntax: %s <flags> [arguments]\n", cmd.c_str() );
296
	printf ( "Flags:\n");
297
	printf ( "\t-l <filename> [filemask]\n");
298
	printf ( "\t\tLists the contents of a cat file, with optional search mask\n");
299
	printf ( "\t-x <catfile::filename> [dir]\n");
300
	printf ( "\t\textracts a file from a cat file to the optional directory\n");
301
	printf ( "\t-xp <catfile::filename> [dir]\n");
302
	printf ( "\t\textracts a file from a cat file to the optional directory, preserves directory structure\n");
303
	printf ( "\t-xa <catfile> [dir]\n");
304
	printf ( "\t\textracts all files to optional directory\n");
305
	printf ( "\t-a <filename> <catfile::tofile>\n");
306
	printf ( "\t\tAdds the file into a cat archive, saves it as <tofile>\n");
307
	printf ( "\t-r <catfile> <file>\n");
308
	printf ( "\t\tRemoves a file from an archive\n");
309
	printf ( "\t-u <filename> <tofile>\n");
310
	printf ( "\t\tUnpacks a file, ie from pck to xml/txt\n");
311
	printf ( "\t-p <filename> <tofile>\n");
312
	printf ( "\t\tPacks a file, ie from xml/txt to pck\n");
313
}
314
 
315
int main ( int argc, char **argv )
316
{
317
	printf ( "\nCATPCK Tool V1.21 27/03/2011 (SPK: %.2f) Created by Cycrow\n\n", GetLibraryVersion() );
318
 
319
	// parse the cmd name
320
	CyString cmd (argv[0]);
321
	cmd = cmd.FindReplace ( "\\", "/" );
322
	g_dir = cmd.GetToken ( 0, cmd.NumToken('/') - 1, '/' );
323
	cmd = cmd.GetToken ( cmd.NumToken('/'), '/' );
324
 
325
	if ( g_dir.Empty() )
326
	{
327
	    #ifdef _WIN32
328
		g_dir = CyString(_getcwd(NULL, 0));
329
		#else
330
		g_dir = CyString(getcwd(NULL, 0));
331
		#endif
332
		if ( g_dir.Empty() )
333
			g_dir = "./";
334
	}
335
 
336
	if ( argc < 2 )
337
		PrintSyntax(cmd);
338
	else
339
	{
340
		CyString command ( argv[1] );
341
		command.ToLower();
342
 
343
		if ( (command == "-l") || (command == "-list") )
344
		{
345
			if ( argc < 3 )
346
				printf ( "Syntax: %s -l <filename> [filemask]\n\tLists the contents of the cat file\n", cmd.c_str() );
347
			else if ( argc < 4 )
348
				ListFiles ( CyString(argv[2]), "" );
349
			else
350
				ListFiles ( CyString(argv[2]), CyString(argv[3]) );
351
		}
352
		else if ( (command == "-x") || (command == "-extract") || (command == "-xp") || (command == "-extractpreserve") )
353
		{
354
			if ( argc < 3 )
355
				printf ( "Syntax: %s %s <catfile::filename> [dir]\n\tExtracts a file from the cat archive\n", cmd.c_str(), command.c_str() );
356
			else if ( argc < 4 )
357
				ExtractFile ( CyString(argv[2]), CyString(""), (command == "-xp" || command == "-extractpreserve") ? true : false );
358
			else
359
				ExtractFile ( CyString(argv[2]), CyString(argv[3]), (command == "-xp" || command == "-extractpreserve") ? true : false );
360
		}
361
		else if ( (command == "-xa") || (command == "-extractall") )
362
		{
363
			if ( argc < 3 )
364
				printf ( "Syntax: %s %s <catfile> [dir]\n\tExtracts all files from the cat archive\n", cmd.c_str(), command.c_str() );
365
			else if ( argc < 4 )
366
				ExtractFile ( CyString(argv[2]) + "::*", CyString(""), true );
367
			else
368
				ExtractFile ( CyString(argv[2]) + "::*", CyString(argv[3]), true );
369
		}
370
		else if ( (command == "-a") || (command == "-append") )
371
		{
372
			if ( argc < 4 )
373
				printf ( "Syntax: %s -a <filename> <catfile::tofile>\n\tAppends a file into the archive\n", cmd.c_str() );
374
			else
375
				AppendFile ( CyString(argv[3]), CyString(argv[2]) );
376
		}
377
		else if ( (command == "-r") || (command == "-remove") )
378
		{
379
			if ( argc < 4 )
380
				printf ( "Syntax: %s -r <catfile> <filename>\n\tRemoves a file from the archive\n", cmd.c_str() );
381
			else
382
				RemoveFile ( CyString(argv[2]), CyString(argv[3]) );
383
		}
384
		else if ( (command == "-u") || (command == "-unpack") )
385
		{
386
			if ( argc < 3 )
387
				printf ( "Syntax: %s -u <filename> [to]\n\tUnpacks a file", cmd.c_str() );
388
			else if ( argc < 4 )
389
				UnpackFile ( CyString(argv[2]), CyString("") );
390
			else
391
				UnpackFile ( CyString(argv[2]), CyString(argv[3]) );
392
		}
393
		else if ( (command == "-p") || (command == "-pack") )
394
		{
395
			if ( argc < 3 )
396
				printf ( "Syntax: %s -p <filename> [to]\n\tPacks a file to .pck", cmd.c_str() );
397
			else if ( argc < 4 )
398
				PackFile ( CyString(argv[2]), CyString("") );
399
			else
400
				PackFile ( CyString(argv[2]), CyString(argv[3]) );
401
		}
402
		else
403
		{
404
			printf("Invalaid flag: %s\n\n", command.c_str());
405
			PrintSyntax(cmd);
406
		}
407
	}
408
 
409
#ifdef _DEBUG
410
	char pause;
411
	printf ( "\n\nPress a key to end\n" );
412
	scanf ( "%c", &pause );
413
#endif
414
 
415
	return 0;
416
}
417