Subversion Repositories spk

Rev

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

Rev 317 Rev 320
Line 1... Line 1...
1
#include <spk.h>
1
#include <spk.h>
2
#include <StringList.h>
-
 
3
 
-
 
4
Utils::WString g_dir;
-
 
5
 
-
 
6
#ifdef _WIN32
-
 
7
#include <windows.h>
-
 
8
#include <direct.h>
-
 
9
//#include <shlobj.h>
-
 
10
#else
-
 
11
#include <dirent.h> 
-
 
12
#include <sys/types.h> 
-
 
13
#include <sys/param.h> 
-
 
14
#include <sys/stat.h> 
-
 
15
#include <unistd.h> 
-
 
16
#endif
-
 
17
 
-
 
18
#include "Utils/CommandLine.h"
2
#include "Utils/CommandLine.h"
19
 
3
 
20
void PrintError ( int err )
4
void PrintError ( int err )
21
{
5
{
22
	switch ( err )
6
	switch ( err )
Line 48... Line 32...
48
void ListFiles(const Utils::CommandLine &cmd)
32
void ListFiles(const Utils::CommandLine &cmd)
49
{
33
{
50
	const Utils::WString& filename = cmd.arg(1);
34
	const Utils::WString& filename = cmd.arg(1);
51
	const Utils::WString& searchmask = cmd.arg(2);
35
	const Utils::WString& searchmask = cmd.arg(2);
52
 
36
 
53
	CFileIO File(!filename.contains(L":") ? CDirIO(g_dir).file(filename) : filename);
37
	CFileIO File(!filename.contains(L":") ? CDirIO(cmd.cmdDir()).file(filename) : filename);
54
	if(!File.exists())
38
	if(!File.exists())
55
		File.open(filename);
39
		File.open(filename);
56
	if (!File.exists())
40
	if (!File.exists())
57
	{
41
	{
58
		wprintf(L"Error: Cat file: %s, does not exist", filename.c_str());
42
		wprintf(L"Error: Cat file: %s, does not exist", filename.c_str());
Line 105... Line 89...
105
				list.pushBack((*itr)->str);
89
				list.pushBack((*itr)->str);
106
		}
90
		}
107
	}
91
	}
108
}
92
}
109
 
93
 
110
bool findFiles(Utils::WStringList &files, const Utils::WString &filepattern)
94
bool findFiles(Utils::WStringList &files, const Utils::WString &filepattern, const Utils::CommandLine &cmd)
111
{
95
{
112
	CFileIO File((filepattern.contains(L":")) ? filepattern : g_dir + L"/" + filepattern);
96
	CFileIO File((filepattern.contains(L":")) ? filepattern : cmd.cmdDir() + L"/" + filepattern);
113
	return File.GetDirIO().dirList(files, Utils::WString::Null(), File.filename(), true);
97
	return File.GetDirIO().dirList(files, Utils::WString::Null(), File.filename(), true);
114
}
98
}
115
 
99
 
116
 
100
 
117
void ExtractFile (const Utils::CommandLine& cmd, const Utils::WString& fileMask, bool preserve)
101
void ExtractFile (const Utils::CommandLine& cmd, const Utils::WString& fileMask, bool preserve)
Line 125... Line 109...
125
		preserve = true;
109
		preserve = true;
126
		
110
		
127
	if ( !filename.contains(L"::") ) return;
111
	if ( !filename.contains(L"::") ) return;
128
 
112
 
129
	Utils::WString catfile = filename.token(L"::", 1);
113
	Utils::WString catfile = filename.token(L"::", 1);
130
	CFileIO File((catfile.contains(L":")) ? catfile : g_dir + L"/" + catfile);
114
	CFileIO File((catfile.contains(L":")) ? catfile : cmd.cmdDir() + L"/" + catfile);
131
	Utils::WString filemask = filename.token(L"::", 2);
115
	Utils::WString filemask = filename.token(L"::", 2);
132
 
116
 
133
	CCatFile cat;
117
	CCatFile cat;
134
	int err = cat.open(File.fullFilename(), L"", CATREAD_DAT);
118
	int err = cat.open(File.fullFilename(), L"", CATREAD_DAT);
135
	if ( err )
119
	if ( err )
Line 188... Line 172...
188
		catfile = C_File.token(L"::", 1);
172
		catfile = C_File.token(L"::", 1);
189
		file = C_File.token(L"::", 2);
173
		file = C_File.token(L"::", 2);
190
	}
174
	}
191
 
175
 
192
	if (!catfile.contains(":"))
176
	if (!catfile.contains(":"))
193
		catfile = g_dir + L"/" + catfile;
177
		catfile = cmd.cmdDir() + L"/" + catfile;
194
 
178
 
195
	Utils::WStringList list;
179
	Utils::WStringList list;
196
 
180
 
197
	CFileIO File((filepattern.contains(L":")) ? filepattern : g_dir + L"/" + filepattern);
181
	CFileIO File((filepattern.contains(L":")) ? filepattern : cmd.cmdDir() + L"/" + filepattern);
198
	if (recursive)
182
	if (recursive)
199
		findAllFiles(File.dirIO(), L"", File.filename(), list);
183
		findAllFiles(File.dirIO(), L"", File.filename(), list);
200
	else
184
	else
201
		findFiles(list, filepattern);
185
		findFiles(list, filepattern, cmd);
202
	if (!list.size())
186
	if (!list.size())
203
	{
187
	{
204
		wprintf(L"Error: no files found to add: %s\n", filepattern.c_str());
188
		wprintf(L"Error: no files found to add: %s\n", filepattern.c_str());
205
		return;
189
		return;
206
	}
190
	}
Line 241... Line 225...
241
 
225
 
242
	if(!added)
226
	if(!added)
243
		wprintf(L"Warning: Nothing to add: %s\n", filepattern.c_str());
227
		wprintf(L"Warning: Nothing to add: %s\n", filepattern.c_str());
244
}
228
}
245
 
229
 
246
void RemoveFile (const Utils::WString &C_File, const Utils::WString &remfile )
230
void RemoveFile (const Utils::CommandLine &cmd)
247
{
231
{
-
 
232
	Utils::WString C_File = cmd.arg(1);
-
 
233
	Utils::WString remfile = cmd.arg(2);
-
 
234
 
-
 
235
	CFileIO File((C_File.contains(L":")) ? C_File : cmd.cmdDir() + L"/" + C_File);
248
	// first open the cat file
236
	// first open the cat file
249
	CCatFile cat;
237
	CCatFile cat;
250
	int err = cat.open(C_File, L"", CATREAD_CATDECRYPT);
238
	int err = cat.open(File.fullFilename(), L"", CATREAD_CATDECRYPT);
251
	if ( err )
239
	if ( err )
252
	{
240
	{
253
		PrintError ( err );
241
		PrintError ( err );
254
		return;
242
		return;
255
	}
243
	}
256
 
244
 
257
	SInCatFile *f = cat.findData(remfile);
245
	std::vector<Utils::WString> files;
258
	if ( !f )
246
	if (remfile.containsAny(L"*?"))
259
	{
247
	{
260
		wprintf(L"Unable to find %s in cat file\n", remfile.c_str() );
248
		for (unsigned int i = 0; i < cat.GetNumFiles(); i++)
261
		return;
249
		{
-
 
250
			SInCatFile* f = cat.GetFile(i);
-
 
251
			if (f->sFile.match(remfile))
-
 
252
				files.push_back(f->sFile);
-
 
253
		}
262
	}
254
	}
-
 
255
	else
-
 
256
		files.push_back(remfile);
263
 
257
 
-
 
258
	for (auto itr = files.begin(); itr != files.end(); itr++)
-
 
259
	{
264
	if (cat.removeFile(f))
260
		if (!cat.removeFile(*itr))
-
 
261
			wprintf(L"Unable to find %s in cat file\n", itr->c_str());
-
 
262
		else
265
		printf ( "File has been removed from archive\n" );
263
			wprintf(L"%s: has been removed from archive\n", itr->c_str());
-
 
264
	}
266
}
265
}
267
 
266
 
268
void UnpackFile(const Utils::WString &file, const Utils::WString &tofile, const Utils::WString &ext)
267
void UnpackFile(const Utils::CommandLine &cmd)
269
{
268
{
-
 
269
	const Utils::WString& file = cmd.arg(1);
-
 
270
	const Utils::WString & tofile = cmd.arg(2);
-
 
271
	const Utils::WString& ext = cmd.switchData(L"ext");
-
 
272
 
270
	Utils::WStringList list;
273
	Utils::WStringList list;
271
	if(!findFiles(list, file) || !list.size())
274
	if(!findFiles(list, file, cmd) || !list.size())
272
	{
275
	{
273
		wprintf(L"Error: no files found to unpack: %s\n", file.c_str());
276
		wprintf(L"Error: no files found to unpack: %s\n", file.c_str());
274
		return;
277
		return;
275
	}
278
	}
276
 
279
 
Line 279... Line 282...
279
		Utils::WString filename = itr->str;
282
		Utils::WString filename = itr->str;
280
 
283
 
281
		C_File f(filename);
284
		C_File f(filename);
282
		if (!f.CheckValidFilePointer())
285
		if (!f.CheckValidFilePointer())
283
		{
286
		{
284
			CFileIO File((file.contains(L":")) ? file : g_dir + L"/" + file);
287
			CFileIO File((file.contains(L":")) ? file : cmd.cmdDir() + L"/" + file);
285
			filename = File.GetDirIO().file(itr->str);
288
			filename = File.GetDirIO().file(itr->str);
286
			f.setFilename(filename);
289
			f.setFilename(filename);
287
		}
290
		}
288
 
291
 
289
		Utils::WString oldFilename = f.filename();
292
		Utils::WString oldFilename = f.filename();
Line 312... Line 315...
312
			}
315
			}
313
		}
316
		}
314
	}
317
	}
315
}
318
}
316
 
319
 
317
void PackFile(const Utils::WString &file, const Utils::WString &tofile)
320
void PackFile(const Utils::CommandLine &cmd)
318
{
321
{
-
 
322
	const Utils::WString& file = cmd.arg(1);
-
 
323
	const Utils::WString& tofile = cmd.arg(2);
-
 
324
 
319
	Utils::WStringList list;
325
	Utils::WStringList list;
320
	if(!findFiles(list, file) || !list.size())
326
	if(!findFiles(list, file, cmd) || !list.size())
321
	{
327
	{
322
		wprintf(L"Error: no files found to pack: %s\n", file.c_str());
328
		wprintf(L"Error: no files found to pack: %s\n", file.c_str());
323
		return;
329
		return;
324
	}
330
	}
325
 
331
 
Line 351... Line 357...
351
				f.setFilename(tofile);
357
				f.setFilename(tofile);
352
 
358
 
353
			if ( !f.writeFilePointer() )
359
			if ( !f.writeFilePointer() )
354
				wprintf(L"Error: unable to write file: %s\n", tofile.c_str() );
360
				wprintf(L"Error: unable to write file: %s\n", tofile.c_str() );
355
			else
361
			else
356
				wprintf(L"%s has been packed to %s\n", file.c_str(), f.filename().c_str() );
362
				wprintf(L"%s has been packed to %s\n", CFileIO(filename).filename().c_str(), f.filename().c_str());
357
		}
363
		}
358
	}
364
	}
359
}
365
}
360
 
366
 
361
void PrintSyntax(const Utils::WString &cmd)
367
void PrintSyntax(const Utils::WString &cmd)
Line 382... Line 388...
382
{
388
{
383
	printf ( "\nCATPCK Tool V1.22 17/05/2025 (SPK: %.2f) Created by Cycrow\n\n", GetLibraryVersion() );
389
	printf ( "\nCATPCK Tool V1.22 17/05/2025 (SPK: %.2f) Created by Cycrow\n\n", GetLibraryVersion() );
384
 
390
 
385
	// parse the cmd name
391
	// parse the cmd name
386
	Utils::CommandLine cmd(argc, argv);
392
	Utils::CommandLine cmd(argc, argv);
387
	g_dir = cmd.cmdDir();
-
 
388
 
393
 
389
	if ( argc < 2 )
394
	if ( argc < 2 )
390
		PrintSyntax(cmd.cmdName());
395
		PrintSyntax(cmd.cmdName());
391
	else
396
	else
392
	{
397
	{
Line 415... Line 420...
415
			else
420
			else
416
				ExtractFile(cmd, L"::*", true);
421
				ExtractFile(cmd, L"::*", true);
417
		}
422
		}
418
		else if ( (command == L"-a") || (command == L"-append") )
423
		else if ( (command == L"-a") || (command == L"-append") )
419
		{
424
		{
420
			if (args.size() < 3 )
425
			if (cmd.argCount() < 3 )
421
				wprintf(L"Syntax: %s -a <filename> <catfile::tofile>\n\tAppends a file into the archive\n", cmd.cmdName().c_str() );
426
				wprintf(L"Syntax: %s -a <filename> <catfile::tofile>\n\tAppends a file into the archive\n", cmd.cmdName().c_str() );
422
			else
427
			else
423
				AppendFile(cmd);
428
				AppendFile(cmd);
424
		}
429
		}
425
		else if ( (command == L"-r") || (command == L"-remove") )
430
		else if ( (command == L"-r") || (command == L"-remove") )
426
		{
431
		{
427
			if ( args.size() < 3 )
432
			if (cmd.argCount() < 3 )
428
				wprintf(L"Syntax: %s -r <catfile> <filename>\n\tRemoves a file from the archive\n", cmd.cmdName().c_str() );
433
				wprintf(L"Syntax: %s -r <catfile> <filename>\n\tRemoves a file from the archive\n", cmd.cmdName().c_str() );
429
			else
434
			else
430
				RemoveFile (args[1], args[2]);
435
				RemoveFile(cmd);
431
		}
436
		}
432
		else if ( (command == L"-u") || (command == L"-unpack") )
437
		else if ( (command == L"-u") || (command == L"-unpack") )
433
		{
438
		{
434
			Utils::WString ext = cmd.switchData(L"--ext");
-
 
435
 
-
 
436
			if ( args.size() < 2 )
439
			if (cmd.argCount() < 2 )
437
				wprintf(L"Syntax: %s [--ext:EXTENSION] -u <filename> [to]\n\tUnpacks a file", cmd.cmdName().c_str() );
440
				wprintf(L"Syntax: %s [--ext:EXTENSION] -u <filename> [to]\n\tUnpacks a file", cmd.cmdName().c_str() );
438
			else if ( args.size() < 3)
-
 
439
				UnpackFile(args[1], L"", ext);
-
 
440
			else
441
			else
441
				UnpackFile(args[1], args[2], ext);
442
				UnpackFile(cmd);
442
		}
443
		}
443
		else if ( (command == L"-p") || (command == L"-pack") )
444
		else if ( (command == L"-p") || (command == L"-pack") )
444
		{
445
		{
445
			if ( args.size() < 2 )
446
			if (cmd.argCount() < 2 )
446
				wprintf(L"Syntax: %s -p <filename> [to]\n\tPacks a file to .pck", cmd.cmdName().c_str() );
447
				wprintf(L"Syntax: %s -p <filename> [to]\n\tPacks a file to .pck", cmd.cmdName().c_str() );
447
			else if ( args.size() < 3)
-
 
448
				PackFile(args[1], L"");
-
 
449
			else
448
			else
450
				PackFile(args[1], args[2]);
449
				PackFile(cmd);
451
		}
450
		}
452
		else
451
		else
453
		{
452
		{
454
			wprintf(L"Invalaid flag: %s\n\n", command.c_str());
453
			wprintf(L"Invalaid flag: %s\n\n", command.c_str());
455
			PrintSyntax(cmd.cmdName());
454
			PrintSyntax(cmd.cmdName());