Subversion Repositories spk

Rev

Rev 226 | Rev 298 | Go to most recent revision | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 226 Rev 276
Line 38... Line 38...
38
			printf ( "Dat file size mismatch\n" );
38
			printf ( "Dat file size mismatch\n" );
39
			break;
39
			break;
40
	}
40
	}
41
}
41
}
42
 
42
 
43
void ListFiles ( CyString filename, Utils::WString &searchmask )
43
void ListFiles (const Utils::WString &filename, const Utils::WString &searchmask )
44
{
44
{
45
	printf ( "Listing files in %s...", filename.c_str() );
45
	wprintf(L"Listing files in %s...", filename.c_str() );
46
	if (!searchmask.empty())
46
	if (!searchmask.empty())
47
		wprintf(L"(%s)", searchmask.c_str() );
47
		wprintf(L"(%s)", searchmask.c_str() );
48
	printf("\n");
48
	printf("\n");
49
 
49
 
50
	CCatFile catfile;
50
	CCatFile catfile;
51
	int err = catfile.open(filename.ToString(), L"", CATREAD_CATDECRYPT);
51
	int err = catfile.open(filename, L"", CATREAD_CATDECRYPT);
52
 
52
 
53
	if ( err == CATERR_NONE )
53
	if ( err == CATERR_NONE )
54
	{
54
	{
55
		printf ( "Opened file\n" );
55
		printf ( "Opened file\n" );
56
		for (unsigned int i = 0; i < catfile.GetNumFiles(); i++)
56
		for (unsigned int i = 0; i < catfile.GetNumFiles(); i++)
Line 124... Line 124...
124
		else
124
		else
125
			wprintf(L"File has been written (%s)\n", cat.errorString().c_str() );
125
			wprintf(L"File has been written (%s)\n", cat.errorString().c_str() );
126
	}
126
	}
127
}
127
}
128
 
128
 
129
void AppendFile ( CyString C_File, const Utils::WString &filepattern )
129
void AppendFile (const Utils::WString &sFile, const Utils::WString &filepattern )
130
{
130
{
131
	Utils::WString catfile;
131
	Utils::WString catfile;
132
	Utils::WString file;
132
	Utils::WString file;
133
 
133
 
134
	C_File = C_File.FindReplace("\\", "/");
134
	Utils::WString C_File = sFile.findReplace(L"\\", L"/");
135
 
-
 
136
	bool doFile = false;
135
	bool doFile = false;
137
	if ( !C_File.IsIn ( "::" ) )
136
	if ( !C_File.contains(L"::"))
138
	{
137
	{
139
		catfile = C_File.ToString();
138
		catfile = C_File;
140
		doFile = true;
139
		doFile = true;
141
	}
140
	}
142
	else
141
	else
143
	{
142
	{
144
		catfile = C_File.GetToken ( "::", 1, 1 ).ToString();
143
		catfile = C_File.token(L"::", 1);
145
		file = C_File.GetToken ( "::", 2, 2 ).ToString();
144
		file = C_File.token(L"::", 1);
146
	}
145
	}
147
 
146
 
148
	Utils::WStringList list;
147
	Utils::WStringList list;
149
	findFiles(list, filepattern);
148
	findFiles(list, filepattern);
150
	if (!list.size())
149
	if (!list.size())
Line 201... Line 200...
201
 
200
 
202
	if (cat.removeFile(f))
201
	if (cat.removeFile(f))
203
		printf ( "File has been removed from archive\n" );
202
		printf ( "File has been removed from archive\n" );
204
}
203
}
205
 
204
 
206
void UnpackFile(const Utils::WString &file, const Utils::WString &tofile )
205
void UnpackFile(const Utils::WString &file, const Utils::WString &tofile, const Utils::WString &ext)
207
{
206
{
208
	Utils::WStringList list;
207
	Utils::WStringList list;
209
	if(!findFiles(list, file) || !list.size())
208
	if(!findFiles(list, file) || !list.size())
210
	{
209
	{
211
		wprintf(L"Error: no files found to unpack: %s\n", file.c_str());
210
		wprintf(L"Error: no files found to unpack: %s\n", file.c_str());
212
		return;
211
		return;
213
	}
212
	}
214
 
213
 
215
	for(auto itr = list.first(); itr; list.next())
214
	for(auto itr = list.first(); itr; itr = list.next())
216
	{
215
	{
217
		Utils::WString filename = CFileIO(file).dir() + L"/" + itr->str;
216
		Utils::WString filename = CFileIO(file).dir() + L"/" + itr->str;
218
 
217
 
219
		C_File f(filename);
218
		C_File f(filename);
-
 
219
		if (!f.CheckValidFilePointer())
-
 
220
		{
-
 
221
			CFileIO File((file.contains(L":")) ? file : g_dir + L"/" + file);
-
 
222
			filename = File.GetDirIO().file(itr->str);
-
 
223
			f.setFilename(filename);
-
 
224
		}
-
 
225
 
-
 
226
		Utils::WString oldFilename = f.filename();
-
 
227
 
220
		if ( !f.CheckValidFilePointer() )
228
		if ( !f.CheckValidFilePointer() )
221
			wprintf(L"Error: %s doesn't exists\n", filename.c_str() );
229
			wprintf(L"Error: %s doesn't exists\n", filename.c_str() );
222
		else if ( !f.ReadFromFile() )
230
		else if ( !f.ReadFromFile() )
223
			wprintf(L"Error: unable to open file: %s\n", filename.c_str() );
231
			wprintf(L"Error: unable to open file: %s\n", filename.c_str() );
224
		else
232
		else
225
		{
233
		{
226
			f.UnPCKFile();
234
			f.UnPCKFile();
227
			if ( tofile.empty() )
235
			if (tofile.empty())
228
				f.changeFileExt(L"xml");
236
				f.changeFileExt(ext.empty() ? L"xml" : ext);
229
			else if ( tofile.left(2) == L"*." )
237
			else if ( tofile.left(2) == L"*." )
230
				f.changeFileExt(CFileIO(tofile).extension().toString());
238
				f.changeFileExt(CFileIO(tofile).extension().toString());
231
			else
239
			else
232
				f.setFilename(tofile);
240
				f.setFilename(tofile);
233
 
241
 
234
			if ( !f.writeFilePointer() )
242
			if ( !f.writeFilePointer() )
235
				wprintf(L"Error: unable to write file: %s\n", tofile.c_str() );
243
				wprintf(L"Error: unable to write file: %s\n", tofile.c_str() );
236
			else
244
			else
237
				wprintf(L"%s has been unpacked to %s\n", file.c_str(), f.filename().c_str() );
245
				wprintf(L"%s has been unpacked to %s\n", oldFilename.c_str(), f.filename().c_str() );
238
		}
246
		}
239
	}
247
	}
240
}
248
}
241
 
249
 
242
void PackFile(const Utils::String &file, const Utils::String &tofile)
250
void PackFile(const Utils::WString &file, const Utils::WString &tofile)
243
{
251
{
244
	Utils::WStringList list;
252
	Utils::WStringList list;
245
	if(!findFiles(list, file) || !list.size())
253
	if(!findFiles(list, file) || !list.size())
246
	{
254
	{
247
		printf("Error: no files found to pack: %s\n", file.c_str());
255
		wprintf(L"Error: no files found to pack: %s\n", file.c_str());
248
		return;
256
		return;
249
	}
257
	}
250
 
258
 
251
	for(auto itr = list.first(); itr; itr = list.next())
259
	for(auto itr = list.first(); itr; itr = list.next())
252
	{
260
	{
Line 267... Line 275...
267
					f.changeFileExt(L"pbb");
275
					f.changeFileExt(L"pbb");
268
				else if ( f.checkFileExt(L"bod") )
276
				else if ( f.checkFileExt(L"bod") )
269
					f.changeFileExt(L"pbd");
277
					f.changeFileExt(L"pbd");
270
				else
278
				else
271
					f.changeFileExt(L"pck");
279
					f.changeFileExt(L"pck");
272
			}
280
			}
273
			else if ( tofile.left(2) == L"*." )
281
			else if ( tofile.left(2) == L"*." )
274
				f.changeFileExt(tofile.right(3));
282
				f.changeFileExt(tofile.right(3));
275
			else
283
			else
276
				f.setFilename(tofile);
284
				f.setFilename(tofile);
277
 
285
 
278
			if ( !f.writeFilePointer() )
286
			if ( !f.writeFilePointer() )
279
				printf("Error: unable to write file: %s\n", tofile.c_str() );
287
				wprintf(L"Error: unable to write file: %s\n", tofile.c_str() );
280
			else
288
			else
281
				wprintf(L"%hs has been packed to %s\n", file.c_str(), f.filename().c_str() );
289
				wprintf(L"%s has been packed to %s\n", file.c_str(), f.filename().c_str() );
282
		}
290
		}
283
	}
291
	}
284
}
292
}
285
 
293
 
286
void PrintSyntax(CyString cmd)
294
void PrintSyntax(CyString cmd)
Line 297... Line 305...
297
	printf ( "\t\textracts all files to optional directory\n");
305
	printf ( "\t\textracts all files to optional directory\n");
298
	printf ( "\t-a <filename> <catfile::tofile>\n");
306
	printf ( "\t-a <filename> <catfile::tofile>\n");
299
	printf ( "\t\tAdds the file into a cat archive, saves it as <tofile>\n");
307
	printf ( "\t\tAdds the file into a cat archive, saves it as <tofile>\n");
300
	printf ( "\t-r <catfile> <file>\n");
308
	printf ( "\t-r <catfile> <file>\n");
301
	printf ( "\t\tRemoves a file from an archive\n");
309
	printf ( "\t\tRemoves a file from an archive\n");
302
	printf ( "\t-u <;filename> <tofile>\n");
310
	printf ( "\t[--ext:EXTENSION] -u <;filename> [tofile]\n");
303
	printf ( "\t\tUnpacks a file, ie from pck to xml/txt\n");
311
	printf ( "\t\tUnpacks a file, ie from pck to xml/txt\n");
304
	printf ( "\t-p <filename> <tofile>\n");
312
	printf ( "\t-p <filename> <tofile>\n");
305
	printf ( "\t\tPacks a file, ie from xml/txt to pck\n");
313
	printf ( "\t\tPacks a file, ie from xml/txt to pck\n");
306
}
314
}
307
 
315
 
Line 309... Line 317...
309
{
317
{
310
	printf ( "\nCATPCK Tool V1.21 27/03/2011 (SPK: %.2f) Created by Cycrow\n\n", GetLibraryVersion() );
318
	printf ( "\nCATPCK Tool V1.21 27/03/2011 (SPK: %.2f) Created by Cycrow\n\n", GetLibraryVersion() );
311
 
319
 
312
	// parse the cmd name
320
	// parse the cmd name
313
	Utils::WString cmd (argv[0]);
321
	Utils::WString cmd (argv[0]);
-
 
322
 
314
	cmd = cmd.findReplace(L"\\", L"/");
323
	cmd = cmd.findReplace(L"\\", L"/");
315
	g_dir = cmd.tokens(L"/", 1, cmd.countToken(L"/") - 1);
324
	g_dir = cmd.tokens(L"/", 1, cmd.countToken(L"/") - 1);
316
	cmd = cmd.token (L"/", -1);
325
	cmd = cmd.token (L"/", -1);
317
 
326
 
-
 
327
 
-
 
328
 
318
	if (g_dir.empty())
329
	if (g_dir.empty() || !g_dir.contains("/"))
319
	{
330
	{
320
	    #ifdef _WIN32
331
	    #ifdef _WIN32
321
		g_dir = Utils::WString(_getcwd(NULL, 0));
332
		g_dir = Utils::WString(_getcwd(NULL, 0));
322
		#else
333
		#else
323
		g_dir = Utils::WString(getcwd(NULL, 0));
334
		g_dir = Utils::WString(getcwd(NULL, 0));
Line 328... Line 339...
328
 
339
 
329
	if ( argc < 2 )
340
	if ( argc < 2 )
330
		PrintSyntax(CyString(cmd.toString()));
341
		PrintSyntax(CyString(cmd.toString()));
331
	else
342
	else
332
	{
343
	{
-
 
344
		Utils::WString sCommand;
333
		CyString command ( argv[1] );
345
		for (int i = 1; i < argc; ++i)
-
 
346
			sCommand = sCommand.addToken(L" ", Utils::WString(argv[i]));	
334
		command.ToLower();
347
		sCommand.toLower();
335
 
348
 
-
 
349
		Utils::WStringList flags;
336
		if ( (command == "-l&quot;) || (command == "-list") )
350
		while (sCommand.contains(L&quot;--"))
337
		{
351
		{
-
 
352
			long pos = 0;
-
 
353
			pos = sCommand.findPos(L"--", pos);
-
 
354
			if (pos > -1)
-
 
355
			{
-
 
356
				long nextPos = sCommand.findPos(L" ", pos);
-
 
357
				if (nextPos > -1)
-
 
358
				{
-
 
359
					Utils::WString sFlag = sCommand.mid(pos, nextPos);
-
 
360
					std::vector<Utils::WString> split;
-
 
361
					if (sFlag.tokenise(L":", split))
-
 
362
						flags.pushBack(split[0], split[1]);
-
 
363
					sCommand = sCommand.findRemove(sFlag);
-
 
364
				}
-
 
365
			}
-
 
366
		}		
-
 
367
 
-
 
368
		std::vector<Utils::WString> args;
-
 
369
		sCommand.removeFirstSpace();
-
 
370
		sCommand.removeEndSpace();
-
 
371
		sCommand.tokenise(L" ", args);
-
 
372
 
-
 
373
		Utils::WString command(args[0]);
-
 
374
 
-
 
375
		if ( (command == L"-l") || (command == L"-list") )
-
 
376
		{
338
			if ( argc < 3 )
377
			if ( args.size() < 2 )
339
				wprintf(L"Syntax: %s -l <filename> [filemask]\n\tLists the contents of the cat file\n", cmd.c_str() );
378
				wprintf(L"Syntax: %s -l <filename> [filemask]\n\tLists the contents of the cat file\n", cmd.c_str() );
340
			else if ( argc < 4 )
379
			else if ( args.size() < 3)
341
				ListFiles ( CyString(argv[2]), Utils::WString(L""));
380
				ListFiles(args[1], Utils::WString(L""));
342
			else
381
			else
343
				ListFiles ( CyString(argv[2]), Utils::WString(argv[3]));
382
				ListFiles(args[1], args[2]);
344
		}
383
		}
345
		else if ( (command == "-x") || (command == "-extract") || (command == "-xp") || (command == "-extractpreserve") )
384
		else if ( (command == L"-x") || (command == L"-extract") || (command == L"-xp") || (command == L"-extractpreserve") )
346
		{
385
		{
347
			if ( argc < 3 )
386
			if ( args.size() < 2 )
348
				wprintf(L"Syntax: %s %hs <catfile::filename> [dir]\n\tExtracts a file from the cat archive\n", cmd.c_str(), command.c_str() );
387
				wprintf(L"Syntax: %s %s <catfile::filename> [dir]\n\tExtracts a file from the cat archive\n", cmd.c_str(), command.c_str() );
349
			else if ( argc < 4 )
388
			else if ( args.size() < 3)
350
				ExtractFile (Utils::WString(argv[2]), L"", (command == "-xp" || command == "-extractpreserve") ? true : false );
389
				ExtractFile (args[1], L"", (command == L"-xp" || command == L"-extractpreserve") ? true : false );
351
			else
390
			else
352
				ExtractFile (Utils::WString(argv[2]), Utils::WString(argv[3]), (command == "-xp" || command == "-extractpreserve") ? true : false );
391
				ExtractFile (args[1], args[2], (command == L"-xp" || command == L"-extractpreserve") ? true : false );
353
		}
392
		}
354
		else if ( (command == "-xa") || (command == "-extractall") )
393
		else if ( (command == L"-xa") || (command == L"-extractall") )
355
		{
394
		{
356
			if ( argc < 3 )
395
			if ( args.size() < 2)
357
				wprintf(L"Syntax: %s %hs <catfile> [dir]\n\tExtracts all files from the cat archive\n", cmd.c_str(), command.c_str() );
396
				wprintf(L"Syntax: %s %s <catfile> [dir]\n\tExtracts all files from the cat archive\n", cmd.c_str(), command.c_str() );
358
			else if ( argc < 4 )
397
			else if ( args.size() < 3)
359
				ExtractFile(Utils::WString::FromString(argv[2]) + L"::*", L"", true );
398
				ExtractFile(args[1] + L"::*", L"", true );
360
			else
399
			else
361
				ExtractFile(Utils::WString::FromString(argv[2]) + L"::*", Utils::WString(argv[3]), true );
400
				ExtractFile(args[1] + L"::*", args[2], true );
362
		}
401
		}
363
		else if ( (command == "-a") || (command == "-append") )
402
		else if ( (command == L"-a") || (command == L"-append") )
364
		{
403
		{
365
			if ( argc < 4 )
404
			if (args.size() < 3 )
366
				wprintf(L"Syntax: %s -a <filename> <catfile::tofile>\n\tAppends a file into the archive\n", cmd.c_str() );
405
				wprintf(L"Syntax: %s -a <filename> <catfile::tofile>\n\tAppends a file into the archive\n", cmd.c_str() );
367
			else
406
			else
368
				AppendFile ( CyString(argv[3]), Utils::WString(argv[2]));
407
				AppendFile(args[2], args[1]);
369
		}
408
		}
370
		else if ( (command == "-r") || (command == "-remove") )
409
		else if ( (command == L"-r") || (command == L"-remove") )
371
		{
410
		{
372
			if ( argc < 4 )
411
			if ( args.size() < 3 )
373
				wprintf(L"Syntax: %s -r <catfile> <filename>\n\tRemoves a file from the archive\n", cmd.c_str() );
412
				wprintf(L"Syntax: %s -r <catfile> <filename>\n\tRemoves a file from the archive\n", cmd.c_str() );
374
			else
413
			else
375
				RemoveFile (Utils::WString::FromString(argv[2]), Utils::WString::FromString(argv[3]));
414
				RemoveFile (args[1], args[2]);
376
		}
415
		}
377
		else if ( (command == "-u") || (command == "-unpack") )
416
		else if ( (command == L"-u") || (command == L"-unpack") )
378
		{
417
		{
-
 
418
			Utils::WString ext = flags.findString(L"--ext");
-
 
419
 
379
			if ( argc < 3 )
420
			if ( args.size() < 2 )
380
				wprintf(L"Syntax: %s -u <filename> [to]\n\tUnpacks a file", cmd.c_str() );
421
				wprintf(L"Syntax: %s [--ext:EXTENSION] -u <filename> [to]\n\tUnpacks a file", cmd.c_str() );
381
			else if ( argc < 4 )
422
			else if ( args.size() < 3)
382
				UnpackFile(Utils::WString::FromString(argv[2]), L"");
423
				UnpackFile(args[1], L"", ext);
383
			else
424
			else
384
				UnpackFile(Utils::WString::FromString(argv[2]), Utils::WString::FromString(argv[3]));
425
				UnpackFile(args[1], args[2], ext);
385
		}
426
		}
386
		else if ( (command == "-p") || (command == "-pack") )
427
		else if ( (command == L"-p") || (command == L"-pack") )
387
		{
428
		{
388
			if ( argc < 3 )
429
			if ( args.size() < 2 )
389
				wprintf(L"Syntax: %s -p <filename> [to]\n\tPacks a file to .pck", cmd.c_str() );
430
				wprintf(L"Syntax: %s -p <filename> [to]\n\tPacks a file to .pck", cmd.c_str() );
390
			else if ( argc < 4 )
431
			else if ( args.size() < 3)
391
				PackFile(argv[2], "");
432
				PackFile(args[1], L"");
392
			else
433
			else
393
				PackFile(argv[2], argv[3]);
434
				PackFile(args[1], args[2]);
394
		}
435
		}
395
		else
436
		else
396
		{
437
		{
397
			printf("Invalaid flag: %s\n\n", command.c_str());
438
			wprintf(L"Invalaid flag: %s\n\n", command.c_str());
398
			PrintSyntax(CyString(cmd.toString()));
439
			PrintSyntax(CyString(cmd.toString()));
399
		}
440
		}
400
	}
441
	}
401
 
442
 
402
#ifdef _DEBUG
443
#ifdef _DEBUG