Subversion Repositories spk

Rev

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

Rev 298 Rev 305
Line 12... Line 12...
12
#include <sys/types.h> 
12
#include <sys/types.h> 
13
#include <sys/param.h> 
13
#include <sys/param.h> 
14
#include <sys/stat.h> 
14
#include <sys/stat.h> 
15
#include <unistd.h> 
15
#include <unistd.h> 
16
#endif
16
#endif
-
 
17
 
-
 
18
#include "Utils/CommandLine.h"
17
 
19
 
18
void PrintError ( int err )
20
void PrintError ( int err )
19
{
21
{
20
	switch ( err )
22
	switch ( err )
21
	{
23
	{
Line 61... Line 63...
61
			{
63
			{
62
				if (!searchmask.match(file->sFile))
64
				if (!searchmask.match(file->sFile))
63
					continue;
65
					continue;
64
			}
66
			}
65
 
67
 
66
			wprintf(L"[%9s] %s\n", SPK::GetSizeString(file->lSize).c_str(), file->sFile.c_str() );
68
			wprintf(L"[%9s] %s\n", SPK::GetSizeString(static_cast<unsigned long>(file->lSize)).c_str(), file->sFile.c_str() );
67
		}
69
		}
68
	}
70
	}
69
	else
71
	else
70
		PrintError ( err );
72
		PrintError ( err );
71
}
73
}
72
 
74
 
73
bool findFiles(Utils::WStringList &files, const Utils::WString &filepattern)
75
bool findFiles(Utils::WStringList &files, const Utils::WString &filepattern)
74
{
76
{
75
	CFileIO File((filepattern.contains(L":")) ? filepattern : g_dir + L"/" + filepattern);
77
	CFileIO File((filepattern.contains(L":")) ? filepattern : g_dir + L"/" + filepattern);
76
	return File.GetDirIO().dirList(files, Utils::WString::Null(), File.filename());
78
	return File.GetDirIO().dirList(files, Utils::WString::Null(), File.filename(), true);
77
}
79
}
78
 
80
 
79
 
81
 
80
void ExtractFile (const Utils::WString &filename, const Utils::WString &to, bool preserve )
82
void ExtractFile (const Utils::WString &filename, const Utils::WString &to, bool preserve )
81
{
83
{
Line 139... Line 141...
139
		doFile = true;
141
		doFile = true;
140
	}
142
	}
141
	else
143
	else
142
	{
144
	{
143
		catfile = C_File.token(L"::", 1);
145
		catfile = C_File.token(L"::", 1);
144
		file = C_File.token(L"::", 1);
146
		file = C_File.token(L"::", 2);
145
	}
147
	}
-
 
148
 
-
 
149
	if (!catfile.contains(":"))
-
 
150
		catfile = g_dir + L"/" + catfile;
146
 
151
 
147
	Utils::WStringList list;
152
	Utils::WStringList list;
148
	findFiles(list, filepattern);
153
	findFiles(list, filepattern);
149
	if (!list.size())
154
	if (!list.size())
150
	{
155
	{
151
		wprintf(L"Error: no files found to add: %s\n", filepattern.c_str());
156
		wprintf(L"Error: no files found to add: %s\n", filepattern.c_str());
152
		return;
157
		return;
153
	}
158
	}
154
 
159
 
155
	CCatFile cat;
160
	CCatFile cat;
156
	int err = cat.open(catfile, L"", CATREAD_CATDECRYPT);
161
	int err = cat.open(catfile, L"", CATREAD_CATDECRYPT);
157
	if ( err )
162
	if ( err && err != CATERR_CREATED)
158
	{
163
	{
159
		PrintError ( err );
164
		PrintError ( err );
160
		return;
165
		return;
161
	}
166
	}
162
 
167
 
163
	for (auto itr = list.begin(); itr != list.end(); itr++)
168
	for (auto itr = list.begin(); itr != list.end(); itr++)
164
	{
169
	{
165
		Utils::WString filename = (*itr)->str;
170
		Utils::WString filename = (*itr)->str;
-
 
171
		Utils::WString toFile = file;
166
		if (doFile)
172
		if (doFile)
167
			file = filename;
173
			toFile = filename;
168
 
174
 
169
		if (!file.contains(L"."))
175
		if (!toFile.contains(L"."))
170
		{
176
		{
171
			if (file[file.length() - 1] != L'/')
177
			if (toFile[toFile.length() - 1] != L'/')
172
				file += L"/";
178
				toFile += L"/";
173
			file += CFileIO(filename).filename();
179
			toFile += CFileIO(filename).filename();
174
		}
180
		}
175
 
181
 
176
		if (cat.appendFile(filename, file))
182
		if (cat.appendFile(filename, toFile))
177
			wprintf(L"File %s has beed added to: %s::%s\n", filename.c_str(), catfile.c_str(), file.c_str());
183
			wprintf(L"File %s has beed added to: %s::%s\n", filename.c_str(), catfile.c_str(), toFile.c_str());
178
		else
184
		else
179
			wprintf(L"Error: Unable to add file: %s\n", filename.c_str());
185
			wprintf(L"Error: Unable to add file: %s\n", filename.c_str());
180
	}
186
	}
181
}
187
}
182
 
188
 
183
void RemoveFile (const Utils::WString &C_File, const Utils::WString &remfile )
189
void RemoveFile (const Utils::WString &C_File, const Utils::WString &remfile )
184
{
190
{
185
	// first open the cat file
191
	// first open the cat file
186
	CCatFile cat;
192
	CCatFile cat;
187
	int err = cat.open(C_File, L"", CATREAD_CATDECRYPT);
193
	int err = cat.open(C_File, L"", CATREAD_CATDECRYPT);
188
	if ( err )
194
	if ( err )
189
	{
195
	{
190
		PrintError ( err );
196
		PrintError ( err );
191
		return;
197
		return;
192
	}
198
	}
193
 
199
 
194
	SInCatFile *f = cat.findData(remfile);
200
	SInCatFile *f = cat.findData(remfile);
195
	if ( !f )
201
	if ( !f )
196
	{
202
	{
197
		wprintf(L"Unable to find %s in cat file\n", remfile.c_str() );
203
		wprintf(L"Unable to find %s in cat file\n", remfile.c_str() );
198
		return;
204
		return;
199
	}
205
	}
200
 
206
 
201
	if (cat.removeFile(f))
207
	if (cat.removeFile(f))
Line 215... Line 221...
215
	{
221
	{
216
		Utils::WString filename = CFileIO(file).dir() + L"/" + itr->str;
222
		Utils::WString filename = CFileIO(file).dir() + L"/" + itr->str;
217
 
223
 
218
		C_File f(filename);
224
		C_File f(filename);
219
		if (!f.CheckValidFilePointer())
225
		if (!f.CheckValidFilePointer())
220
		{
226
		{
221
			CFileIO File((file.contains(L":")) ? file : g_dir + L"/" + file);
227
			CFileIO File((file.contains(L":")) ? file : g_dir + L"/" + file);
222
			filename = File.GetDirIO().file(itr->str);
228
			filename = File.GetDirIO().file(itr->str);
223
			f.setFilename(filename);
229
			f.setFilename(filename);
224
		}
230
		}
225
 
231
 
226
		Utils::WString oldFilename = f.filename();
232
		Utils::WString oldFilename = f.filename();
227
 
233
 
228
		if ( !f.CheckValidFilePointer() )
234
		if ( !f.CheckValidFilePointer() )
229
			wprintf(L"Error: %s doesn't exists\n", filename.c_str() );
235
			wprintf(L"Error: %s doesn't exists\n", filename.c_str() );
Line 242... Line 248...
242
			if ( !f.writeFilePointer() )
248
			if ( !f.writeFilePointer() )
243
				wprintf(L"Error: unable to write file: %s\n", tofile.c_str() );
249
				wprintf(L"Error: unable to write file: %s\n", tofile.c_str() );
244
			else
250
			else
245
				wprintf(L"%s has been unpacked to %s\n", oldFilename.c_str(), f.filename().c_str() );
251
				wprintf(L"%s has been unpacked to %s\n", oldFilename.c_str(), f.filename().c_str() );
246
		}
252
		}
247
	}
253
	}
248
}
254
}
249
 
255
 
250
void PackFile(const Utils::WString &file, const Utils::WString &tofile)
256
void PackFile(const Utils::WString &file, const Utils::WString &tofile)
251
{
257
{
252
	Utils::WStringList list;
258
	Utils::WStringList list;
253
	if(!findFiles(list, file) || !list.size())
259
	if(!findFiles(list, file) || !list.size())
254
	{
260
	{
255
		wprintf(L"Error: no files found to pack: %s\n", file.c_str());
261
		wprintf(L"Error: no files found to pack: %s\n", file.c_str());
256
		return;
262
		return;
257
	}
263
	}
258
 
264
 
259
	for(auto itr = list.first(); itr; itr = list.next())
265
	for(auto itr = list.first(); itr; itr = list.next())
260
	{
266
	{
261
		Utils::WString filename = CFileIO(file).dir() + L"/" + itr->str;
267
		Utils::WString filename = CFileIO(file).dir() + L"/" + itr->str;
262
 
268
 
263
		C_File f(filename);
269
		C_File f(filename);
Line 316... Line 322...
316
int main ( int argc, char **argv )
322
int main ( int argc, char **argv )
317
{
323
{
318
	printf ( "\nCATPCK Tool V1.21 27/03/2011 (SPK: %.2f) Created by Cycrow\n\n", GetLibraryVersion() );
324
	printf ( "\nCATPCK Tool V1.21 27/03/2011 (SPK: %.2f) Created by Cycrow\n\n", GetLibraryVersion() );
319
 
325
 
320
	// parse the cmd name
326
	// parse the cmd name
321
	Utils::WString cmd (argv[0]);
327
	Utils::CommandLine cmd(argc, argv);
322
 
-
 
323
	cmd = cmd.findReplace(L"\\", L"/");
-
 
324
	g_dir = cmd.tokens(L"/", 1, cmd.countToken(L"/") - 1);
-
 
325
	cmd = cmd.token (L"/", -1);
328
	g_dir = cmd.cmdDir();
326
 
-
 
327
 
-
 
328
 
329
 
329
	if (g_dir.empty() || !g_dir.contains("/"))
330
	if (g_dir.empty() || !g_dir.contains("/"))
330
	{
331
	{
331
	    #ifdef _WIN32
332
	    #ifdef _WIN32
332
		g_dir = Utils::WString(_getcwd(NULL, 0));
333
		g_dir = Utils::WString(_getcwd(NULL, 0));
333
		#else
334
		#else
334
		g_dir = Utils::WString(getcwd(NULL, 0));
335
		g_dir = Utils::WString(getcwd(NULL, 0));
335
		#endif
336
		#endif
336
		if (g_dir.empty())
337
		if (g_dir.empty())
337
			g_dir = L"./";
338
			g_dir = L"./";
338
	}
339
	}
339
 
340
 
340
	if ( argc < 2 )
341
	if ( argc < 2 )
341
		PrintSyntax(cmd);
342
		PrintSyntax(cmd.cmdName());
342
	else
343
	else
343
	{
344
	{
344
		Utils::WString sCommand;
345
		Utils::WString command(cmd.arg(0).lower());
345
		for (int i = 1; i < argc; ++i)
-
 
346
			sCommand = sCommand.addToken(L" ", Utils::WString(argv[i]));	
-
 
347
		sCommand.toLower();
-
 
348
 
-
 
349
		Utils::WStringList flags;
-
 
350
		while (sCommand.contains(L"--"))
-
 
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
 
346
 
373
		Utils::WString command(args[0]);
347
		auto& args = cmd.args();
374
 
348
 
375
		if ( (command == L"-l") || (command == L"-list") )
349
		if ( (command == L"-l") || (command == L"-list") )
376
		{
350
		{
377
			if ( args.size() < 2 )
351
			if ( args.size() < 2 )
378
				wprintf(L"Syntax: %s -l <filename> [filemask]\n\tLists the contents of the cat file\n", cmd.c_str() );
352
				wprintf(L"Syntax: %s -l <filename> [filemask]\n\tLists the contents of the cat file\n", cmd.cmdName().c_str());
379
			else if ( args.size() < 3)
353
			else if ( args.size() < 3)
380
				ListFiles(args[1], Utils::WString(L""));
354
				ListFiles(args[1], Utils::WString(L""));
381
			else
355
			else
382
				ListFiles(args[1], args[2]);
356
				ListFiles(args[1], args[2]);
383
		}
357
		}
384
		else if ( (command == L"-x") || (command == L"-extract") || (command == L"-xp") || (command == L"-extractpreserve") )
358
		else if ( (command == L"-x") || (command == L"-extract") || (command == L"-xp") || (command == L"-extractpreserve") )
385
		{
359
		{
386
			if ( args.size() < 2 )
360
			if ( args.size() < 2 )
387
				wprintf(L"Syntax: %s %s <catfile::filename> [dir]\n\tExtracts a file from the cat archive\n", cmd.c_str(), command.c_str() );
361
				wprintf(L"Syntax: %s %s <catfile::filename> [dir]\n\tExtracts a file from the cat archive\n", cmd.cmdName().c_str(), command.c_str() );
388
			else if ( args.size() < 3)
362
			else if ( args.size() < 3)
389
				ExtractFile (args[1], L"", (command == L"-xp" || command == L"-extractpreserve") ? true : false );
363
				ExtractFile (args[1], L"", (command == L"-xp" || command == L"-extractpreserve") ? true : false );
390
			else
364
			else
391
				ExtractFile (args[1], args[2], (command == L"-xp" || command == L"-extractpreserve") ? true : false );
365
				ExtractFile (args[1], args[2], (command == L"-xp" || command == L"-extractpreserve") ? true : false );
392
		}
366
		}
393
		else if ( (command == L"-xa") || (command == L"-extractall") )
367
		else if ( (command == L"-xa") || (command == L"-extractall") )
394
		{
368
		{
395
			if ( args.size() < 2)
369
			if ( args.size() < 2)
396
				wprintf(L"Syntax: %s %s <catfile> [dir]\n\tExtracts all files from the cat archive\n", cmd.c_str(), command.c_str() );
370
				wprintf(L"Syntax: %s %s <catfile> [dir]\n\tExtracts all files from the cat archive\n", cmd.cmdName().c_str(), command.c_str() );
397
			else if ( args.size() < 3)
371
			else if ( args.size() < 3)
398
				ExtractFile(args[1] + L"::*", L"", true );
372
				ExtractFile(args[1] + L"::*", L"", true );
399
			else
373
			else
400
				ExtractFile(args[1] + L"::*", args[2], true );
374
				ExtractFile(args[1] + L"::*", args[2], true );
401
		}
375
		}
402
		else if ( (command == L"-a") || (command == L"-append") )
376
		else if ( (command == L"-a") || (command == L"-append") )
403
		{
377
		{
404
			if (args.size() < 3 )
378
			if (args.size() < 3 )
405
				wprintf(L"Syntax: %s -a <filename> <catfile::tofile>\n\tAppends a file into the archive\n", cmd.c_str() );
379
				wprintf(L"Syntax: %s -a <filename> <catfile::tofile>\n\tAppends a file into the archive\n", cmd.cmdName().c_str() );
406
			else
380
			else
407
				AppendFile(args[2], args[1]);
381
				AppendFile(cmd.arg(2), cmd.arg(1));
408
		}
382
		}
409
		else if ( (command == L"-r") || (command == L"-remove") )
383
		else if ( (command == L"-r") || (command == L"-remove") )
410
		{
384
		{
411
			if ( args.size() < 3 )
385
			if ( args.size() < 3 )
412
				wprintf(L"Syntax: %s -r <catfile> <filename>\n\tRemoves a file from the archive\n", cmd.c_str() );
386
				wprintf(L"Syntax: %s -r <catfile> <filename>\n\tRemoves a file from the archive\n", cmd.cmdName().c_str() );
413
			else
387
			else
414
				RemoveFile (args[1], args[2]);
388
				RemoveFile (args[1], args[2]);
415
		}
389
		}
416
		else if ( (command == L"-u") || (command == L"-unpack") )
390
		else if ( (command == L"-u") || (command == L"-unpack") )
417
		{
391
		{
418
			Utils::WString ext = flags.findString(L"--ext");
392
			Utils::WString ext = cmd.switchData(L"--ext");
419
 
393
 
420
			if ( args.size() < 2 )
394
			if ( args.size() < 2 )
421
				wprintf(L"Syntax: %s [--ext:EXTENSION] -u <filename> [to]\n\tUnpacks a file", cmd.c_str() );
395
				wprintf(L"Syntax: %s [--ext:EXTENSION] -u <filename> [to]\n\tUnpacks a file", cmd.cmdName().c_str() );
422
			else if ( args.size() < 3)
396
			else if ( args.size() < 3)
423
				UnpackFile(args[1], L"", ext);
397
				UnpackFile(args[1], L"", ext);
424
			else
398
			else
425
				UnpackFile(args[1], args[2], ext);
399
				UnpackFile(args[1], args[2], ext);
426
		}
400
		}
427
		else if ( (command == L"-p") || (command == L"-pack") )
401
		else if ( (command == L"-p") || (command == L"-pack") )
428
		{
402
		{
429
			if ( args.size() < 2 )
403
			if ( args.size() < 2 )
430
				wprintf(L"Syntax: %s -p <filename> [to]\n\tPacks a file to .pck", cmd.c_str() );
404
				wprintf(L"Syntax: %s -p <filename> [to]\n\tPacks a file to .pck", cmd.cmdName().c_str() );
431
			else if ( args.size() < 3)
405
			else if ( args.size() < 3)
432
				PackFile(args[1], L"");
406
				PackFile(args[1], L"");
433
			else
407
			else
434
				PackFile(args[1], args[2]);
408
				PackFile(args[1], args[2]);
435
		}
409
		}
436
		else
410
		else
437
		{
411
		{
438
			wprintf(L"Invalaid flag: %s\n\n", command.c_str());
412
			wprintf(L"Invalaid flag: %s\n\n", command.c_str());
439
			PrintSyntax(cmd);
413
			PrintSyntax(cmd.cmdName());
440
		}
414
		}
441
	}
415
	}
442
 
416
 
443
#ifdef _DEBUG
417
#ifdef _DEBUG
444
	char pause;
418
	char pause;