Subversion Repositories spk

Rev

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

Rev 196 Rev 197
Line 1... Line 1...
1
#include <spk.h>
1
#include <spk.h>
2
 
2
 
3
CyString g_dir;
3
Utils::WString g_dir;
4
 
4
 
5
#ifdef _WIN32
5
#ifdef _WIN32
6
#include <windows.h>
6
#include <windows.h>
7
#include <direct.h>
7
#include <direct.h>
8
//#include <shlobj.h>
8
//#include <shlobj.h>
Line 37... Line 37...
37
			printf ( "Dat file size mismatch\n" );
37
			printf ( "Dat file size mismatch\n" );
38
			break;
38
			break;
39
	}
39
	}
40
}
40
}
41
 
41
 
42
void ListFiles ( CyString filename, CyString searchmask )
42
void ListFiles ( CyString filename, Utils::WString &searchmask )
43
{
43
{
44
	printf ( "Listing files in %s...", filename.c_str() );
44
	printf ( "Listing files in %s...", filename.c_str() );
45
	if ( !searchmask.Empty() )
45
	if (!searchmask.empty())
46
		printf ( "(%s)", searchmask.c_str() );
46
		wprintf(L"(%s)", searchmask.c_str() );
47
	printf ( "\n");
47
	printf("\n");
48
 
48
 
49
	CCatFile catfile;
49
	CCatFile catfile;
50
	int err = catfile.open(filename.ToString(), "", CATREAD_CATDECRYPT);
50
	int err = catfile.open(filename.ToString(), L"", CATREAD_CATDECRYPT);
51
 
51
 
52
	if ( err == CATERR_NONE )
52
	if ( err == CATERR_NONE )
53
	{
53
	{
54
		printf ( "Opened file\n" );
54
		printf ( "Opened file\n" );
55
		for (unsigned int i = 0; i < catfile.GetNumFiles(); i++)
55
		for (unsigned int i = 0; i < catfile.GetNumFiles(); i++)
56
		{
56
		{
57
			SInCatFile *file = catfile.GetFile ( i );
57
			SInCatFile *file = catfile.GetFile ( i );
58
 
58
 
59
			if ( !searchmask.Empty() )
59
			if (!searchmask.empty())
60
			{
60
			{
61
				if ( !searchmask.WildMatch(file->sFile) )
61
				if (!searchmask.match(file->sFile))
62
					continue;
62
					continue;
63
			}
63
			}
64
 
64
 
65
			printf ( "[%9s] %s\n", SPK::GetSizeString(file->lSize).c_str(), file->sFile.c_str() );
65
			wprintf(L"[%9s] %s\n", SPK::GetSizeString(file->lSize).c_str(), file->sFile.c_str() );
66
		}
66
		}
67
	}
67
	}
68
	else
68
	else
69
		PrintError ( err );
69
		PrintError ( err );
70
}
70
}
71
 
71
 
72
bool findFiles(Utils::CStringList &files, const Utils::String &filepattern)
72
bool findFiles(Utils::WStringList &files, const Utils::WString &filepattern)
73
{
73
{
74
	CFileIO File((filepattern.contains(":")) ? filepattern : g_dir.ToString() + "/" + filepattern);
74
	CFileIO File((filepattern.contains(L":")) ? filepattern : g_dir + L"/" + filepattern);
75
	return File.GetDirIO().dirList(files, Utils::String::Null(), File.filename());
75
	return File.GetDirIO().dirList(files, Utils::WString::Null(), File.filename());
76
}
76
}
77
 
77
 
78
 
78
 
79
void ExtractFile ( CyString filename, CyString to, bool preserve )
79
void ExtractFile ( CyString filename, CyString to, bool preserve )
80
{
80
{
Line 96... Line 96...
96
	{
96
	{
97
		for (unsigned int i = 0; i < cat.GetNumFiles(); i++)
97
		for (unsigned int i = 0; i < cat.GetNumFiles(); i++)
98
		{
98
		{
99
			SInCatFile *f = cat.GetFile(i);
99
			SInCatFile *f = cat.GetFile(i);
100
			if (!cat.extractFile(f, to.ToString()))
100
			if (!cat.extractFile(f, to.ToString()))
101
				printf ( "Error: %s\n", cat.getErrorString().c_str() );
101
				wprintf(L"Error: %s\n", cat.getErrorString().c_str() );
102
			else
102
			else
103
				printf ( "File has been written (%s)\n", cat.errorString().c_str() );
103
				wprintf(L"File has been written (%s)\n", cat.errorString().c_str() );
104
		}
104
		}
105
 
105
 
106
		return;
106
		return;
107
	}
107
	}
108
 
108
 
Line 119... Line 119...
119
 
119
 
120
	for ( SStringList *fl = fileList->Head(); fl; fl = fl->next )
120
	for ( SStringList *fl = fileList->Head(); fl; fl = fl->next )
121
	{
121
	{
122
		Utils::String file = fl->str.ToString();
122
		Utils::String file = fl->str.ToString();
123
		if (!cat.extractFile(file, to.ToString(), preserve))
123
		if (!cat.extractFile(file, to.ToString(), preserve))
124
			printf ( "Error: %s\n", cat.getErrorString().c_str() );
124
			wprintf(L"Error: %s\n", cat.getErrorString().c_str() );
125
		else
125
		else
126
			printf ( "File has been written (%s)\n", cat.errorString().c_str() );
126
			wprintf(L"File has been written (%s)\n", cat.errorString().c_str() );
127
	}
127
	}
128
 
128
 
129
	delete fileList;
129
	delete fileList;
130
}
130
}
131
 
131
 
132
void AppendFile ( CyString C_File, CyString filepattern )
132
void AppendFile ( CyString C_File, const Utils::WString &filepattern )
133
{
133
{
134
	Utils::String catfile;
134
	Utils::WString catfile;
135
	Utils::String file;
135
	Utils::WString file;
136
 
136
 
137
	C_File = C_File.FindReplace("\\", "/");
137
	C_File = C_File.FindReplace("\\", "/");
138
 
138
 
139
	bool doFile = false;
139
	bool doFile = false;
140
	if ( !C_File.IsIn ( "::" ) )
140
	if ( !C_File.IsIn ( "::" ) )
Line 146... Line 146...
146
	{
146
	{
147
		catfile = C_File.GetToken ( "::", 1, 1 ).ToString();
147
		catfile = C_File.GetToken ( "::", 1, 1 ).ToString();
148
		file = C_File.GetToken ( "::", 2, 2 ).ToString();
148
		file = C_File.GetToken ( "::", 2, 2 ).ToString();
149
	}
149
	}
150
 
150
 
151
	Utils::CStringList list;
151
	Utils::WStringList list;
152
	findFiles(list, filepattern.ToString());
152
	findFiles(list, filepattern);
153
	if (!list.size())
153
	if (!list.size())
154
	{
154
	{
155
		printf("Error: no files found to add: %s\n", filepattern.c_str());
155
		wprintf(L"Error: no files found to add: %s\n", filepattern.c_str());
156
		return;
156
		return;
157
	}
157
	}
158
 
158
 
159
	CCatFile cat;
159
	CCatFile cat;
160
	int err = cat.open(catfile, "", CATREAD_CATDECRYPT);
160
	int err = cat.open(catfile, L"", CATREAD_CATDECRYPT);
161
	if ( err )
161
	if ( err )
162
	{
162
	{
163
		PrintError ( err );
163
		PrintError ( err );
164
		return;
164
		return;
165
	}
165
	}
166
 
166
 
167
	for (auto itr = list.begin(); itr != list.end(); itr++)
167
	for (auto itr = list.begin(); itr != list.end(); itr++)
168
	{
168
	{
169
		Utils::String filename = (*itr)->str;
169
		Utils::WString filename = (*itr)->str;
170
		if (doFile)
170
		if (doFile)
171
			file = filename;
171
			file = filename;
172
 
172
 
173
		if (!file.isin(&apos;.&apos;))
173
		if (!file.contains(L&quot;.&quot;))
174
		{
174
		{
175
			if (file[file.length() - 1] != '/')
175
			if (file[file.length() - 1] != L'/')
176
				file += &apos;/&apos;;
176
				file += L&quot;/&quot;;
177
			file += CFileIO(filename).filenameStr();
177
			file += CFileIO(filename).filename();
178
		}
178
		}
179
 
179
 
180
		if (cat.appendFile(filename, file))
180
		if (cat.appendFile(filename, file))
181
			printf("File %s has beed added to: %s::%s\n", filename.c_str(), catfile.c_str(), file.c_str());
181
			wprintf(L"File %s has beed added to: %s::%s\n", filename.c_str(), catfile.c_str(), file.c_str());
182
		else
182
		else
183
			printf("Error: Unable to add file: %s\n", filename.c_str());
183
			wprintf(L"Error: Unable to add file: %s\n", filename.c_str());
184
	}
184
	}
185
}
185
}
186
 
186
 
187
void RemoveFile ( CyString C_File, CyString remfile )
187
void RemoveFile ( CyString C_File, CyString remfile )
188
{
188
{
Line 206... Line 206...
206
		printf ( "File has been removed from archive\n" );
206
		printf ( "File has been removed from archive\n" );
207
}
207
}
208
 
208
 
209
void UnpackFile(const Utils::String &file, const Utils::String &tofile )
209
void UnpackFile(const Utils::String &file, const Utils::String &tofile )
210
{
210
{
211
	Utils::CStringList list;
211
	Utils::WStringList list;
212
	if(!findFiles(list, file) || !list.size())
212
	if(!findFiles(list, file) || !list.size())
213
	{
213
	{
214
		printf("Error: no files found to unpack: %s\n", file.c_str());
214
		printf("Error: no files found to unpack: %s\n", file.c_str());
215
		return;
215
		return;
216
	}
216
	}
217
 
217
 
218
	for(auto itr = list.first(); itr; list.next())
218
	for(auto itr = list.first(); itr; list.next())
219
	{
219
	{
220
		Utils::String filename = CFileIO(file).dir().toString() + "/" + itr->str;
220
		Utils::WString filename = CFileIO(file).dir() + L"/" + itr->str;
221
 
221
 
222
		C_File f(filename);
222
		C_File f(filename);
223
		if ( !f.CheckValidFilePointer() )
223
		if ( !f.CheckValidFilePointer() )
224
			printf("Error: %s doesn't exists\n", filename.c_str() );
224
			wprintf(L"Error: %s doesn't exists\n", filename.c_str() );
225
		else if ( !f.ReadFromFile() )
225
		else if ( !f.ReadFromFile() )
226
			printf("Error: unable to open file: %s\n", filename.c_str() );
226
			wprintf(L"Error: unable to open file: %s\n", filename.c_str() );
227
		else
227
		else
228
		{
228
		{
229
			f.UnPCKFile();
229
			f.UnPCKFile();
230
			if ( tofile.empty() )
230
			if ( tofile.empty() )
231
				f.changeFileExt("xml");
231
				f.changeFileExt("xml");
Line 235... Line 235...
235
				f.setFilename(tofile);
235
				f.setFilename(tofile);
236
 
236
 
237
			if ( !f.writeFilePointer() )
237
			if ( !f.writeFilePointer() )
238
				printf("Error: unable to write file: %s\n", tofile.c_str() );
238
				printf("Error: unable to write file: %s\n", tofile.c_str() );
239
			else
239
			else
240
				printf("%s has been unpacked to %s\n", file.c_str(), f.filename().c_str() );
240
				wprintf(L"%hs has been unpacked to %s\n", file.c_str(), f.filename().c_str() );
241
		}
241
		}
242
	}
242
	}
243
}
243
}
244
 
244
 
245
void PackFile(const Utils::String &file, const Utils::String &tofile)
245
void PackFile(const Utils::String &file, const Utils::String &tofile)
246
{
246
{
247
	Utils::CStringList list;
247
	Utils::WStringList list;
248
	if(!findFiles(list, file) || !list.size())
248
	if(!findFiles(list, file) || !list.size())
249
	{
249
	{
250
		printf("Error: no files found to pack: %s\n", file.c_str());
250
		printf("Error: no files found to pack: %s\n", file.c_str());
251
		return;
251
		return;
252
	}
252
	}
253
 
253
 
254
	for(auto itr = list.first(); itr; itr = list.next())
254
	for(auto itr = list.first(); itr; itr = list.next())
255
	{
255
	{
256
		Utils::String filename = CFileIO(file).dir().toString() + "/" + itr->str;
256
		Utils::WString filename = CFileIO(file).dir() + L"/" + itr->str;
257
 
257
 
258
		C_File f(filename);
258
		C_File f(filename);
259
		if ( !f.CheckValidFilePointer() )
259
		if ( !f.CheckValidFilePointer() )
260
			printf("Error: %s doesn't exists\n", filename.c_str() );
260
			wprintf(L"Error: %s doesn't exists\n", filename.c_str() );
261
		else if ( !f.ReadFromFile() )
261
		else if ( !f.ReadFromFile() )
262
			printf("Error: unable to open file: %s\n", filename.c_str() );
262
			wprintf(L"Error: unable to open file: %s\n", filename.c_str() );
263
		else if ( !f.PCKFile() )
263
		else if ( !f.PCKFile() )
264
			printf("Error: unable to pack file: %s\n", filename.c_str() );
264
			wprintf(L"Error: unable to pack file: %s\n", filename.c_str() );
265
		else
265
		else
266
		{
266
		{
267
			if ( tofile.empty() )
267
			if ( tofile.empty() )
268
			{
268
			{
269
				if ( f.checkFileExt("bob") )
269
				if ( f.checkFileExt(L"bob") )
270
					f.changeFileExt("pbb");
270
					f.changeFileExt(L"pbb");
271
				else if ( f.checkFileExt("bod") )
271
				else if ( f.checkFileExt(L"bod") )
272
					f.changeFileExt("pbd");
272
					f.changeFileExt(L"pbd");
273
				else
273
				else
274
					f.changeFileExt("pck");
274
					f.changeFileExt(L"pck");
275
			}
275
			}
276
			else if ( tofile.left(2) == "*." )
276
			else if ( tofile.left(2) == L"*." )
277
				f.changeFileExt(tofile.right(3));
277
				f.changeFileExt(tofile.right(3));
278
			else
278
			else
279
				f.setFilename(tofile);
279
				f.setFilename(tofile);
280
 
280
 
281
			if ( !f.writeFilePointer() )
281
			if ( !f.writeFilePointer() )
282
				printf("Error: unable to write file: %s\n", tofile.c_str() );
282
				printf("Error: unable to write file: %s\n", tofile.c_str() );
283
			else
283
			else
284
				printf("%s has been packed to %s\n", file.c_str(), f.filename().c_str() );
284
				wprintf(L"%hs has been packed to %s\n", file.c_str(), f.filename().c_str() );
285
		}
285
		}
286
	}
286
	}
287
}
287
}
288
 
288
 
289
void PrintSyntax(CyString cmd)
289
void PrintSyntax(CyString cmd)
Line 311... Line 311...
311
int main ( int argc, char **argv )
311
int main ( int argc, char **argv )
312
{
312
{
313
	printf ( "\nCATPCK Tool V1.21 27/03/2011 (SPK: %.2f) Created by Cycrow\n\n", GetLibraryVersion() );
313
	printf ( "\nCATPCK Tool V1.21 27/03/2011 (SPK: %.2f) Created by Cycrow\n\n", GetLibraryVersion() );
314
 
314
 
315
	// parse the cmd name
315
	// parse the cmd name
316
	CyString cmd (argv[0]);
316
	Utils::WString cmd (argv[0]);
317
	cmd = cmd.FindReplace ( ";\\";, "/" );
317
	cmd = cmd.findReplace(L";\\";, L"/");
318
	g_dir = cmd.GetToken ( 0, cmd.NumToken('/') - 1, &apos;/&apos; );
318
	g_dir = cmd.tokens(L"/", 1, cmd.countToken(L";/&quot;) - 1);
319
	cmd = cmd.GetToken ( cmd.NumToken('/'), '/&apos; );
319
	cmd = cmd.token (L"/&quot;, -1);
320
 
320
 
321
	if ( g_dir.Empty() )
321
	if (g_dir.empty())
322
	{
322
	{
323
	    #ifdef _WIN32
323
	    #ifdef _WIN32
324
		g_dir = CyString(_getcwd(NULL, 0));
324
		g_dir = Utils::WString(_getcwd(NULL, 0));
325
		#else
325
		#else
326
		g_dir = CyString(getcwd(NULL, 0));
326
		g_dir = Utils::WString(getcwd(NULL, 0));
327
		#endif
327
		#endif
328
		if ( g_dir.Empty() )
328
		if (g_dir.empty())
329
			g_dir = "./";
329
			g_dir = L"./";
330
	}
330
	}
331
 
331
 
332
	if ( argc < 2 )
332
	if ( argc < 2 )
333
		PrintSyntax(cmd);
333
		PrintSyntax(CyString(cmd.toString()));
334
	else
334
	else
335
	{
335
	{
336
		CyString command ( argv[1] );
336
		CyString command ( argv[1] );
337
		command.ToLower();
337
		command.ToLower();
338
 
338
 
339
		if ( (command == "-l") || (command == "-list") )
339
		if ( (command == "-l") || (command == "-list") )
340
		{
340
		{
341
			if ( argc < 3 )
341
			if ( argc < 3 )
342
				printf ( "Syntax: %s -l <filename> [filemask]\n\tLists the contents of the cat file\n", cmd.c_str() );
342
				wprintf(L"Syntax: %s -l <filename> [filemask]\n\tLists the contents of the cat file\n", cmd.c_str() );
343
			else if ( argc < 4 )
343
			else if ( argc < 4 )
344
				ListFiles ( CyString(argv[2]), "" );
344
				ListFiles ( CyString(argv[2]), Utils::WString(L""));
345
			else
345
			else
346
				ListFiles ( CyString(argv[2]), CyString(argv[3]) );
346
				ListFiles ( CyString(argv[2]), Utils::WString(argv[3]));
347
		}
347
		}
348
		else if ( (command == "-x") || (command == "-extract") || (command == "-xp") || (command == "-extractpreserve") )
348
		else if ( (command == "-x") || (command == "-extract") || (command == "-xp") || (command == "-extractpreserve") )
349
		{
349
		{
350
			if ( argc < 3 )
350
			if ( argc < 3 )
351
				printf ( "Syntax: %s %s <catfile::filename> [dir]\n\tExtracts a file from the cat archive\n", cmd.c_str(), command.c_str() );
351
				wprintf(L"Syntax: %s %hs <catfile::filename> [dir]\n\tExtracts a file from the cat archive\n", cmd.c_str(), command.c_str() );
352
			else if ( argc < 4 )
352
			else if ( argc < 4 )
353
				ExtractFile ( CyString(argv[2]), CyString(""), (command == "-xp" || command == "-extractpreserve") ? true : false );
353
				ExtractFile ( CyString(argv[2]), CyString(""), (command == "-xp" || command == "-extractpreserve") ? true : false );
354
			else
354
			else
355
				ExtractFile ( CyString(argv[2]), CyString(argv[3]), (command == "-xp" || command == "-extractpreserve") ? true : false );
355
				ExtractFile ( CyString(argv[2]), CyString(argv[3]), (command == "-xp" || command == "-extractpreserve") ? true : false );
356
		}
356
		}
357
		else if ( (command == "-xa") || (command == "-extractall") )
357
		else if ( (command == "-xa") || (command == "-extractall") )
358
		{
358
		{
359
			if ( argc < 3 )
359
			if ( argc < 3 )
360
				printf ( "Syntax: %s %s <catfile> [dir]\n\tExtracts all files from the cat archive\n", cmd.c_str(), command.c_str() );
360
				wprintf(L"Syntax: %s %hs <catfile> [dir]\n\tExtracts all files from the cat archive\n", cmd.c_str(), command.c_str() );
361
			else if ( argc < 4 )
361
			else if ( argc < 4 )
362
				ExtractFile ( CyString(argv[2]) + "::*", CyString(""), true );
362
				ExtractFile ( CyString(argv[2]) + "::*", CyString(""), true );
363
			else
363
			else
364
				ExtractFile ( CyString(argv[2]) + "::*", CyString(argv[3]), true );
364
				ExtractFile ( CyString(argv[2]) + "::*", CyString(argv[3]), true );
365
		}
365
		}
366
		else if ( (command == "-a") || (command == "-append") )
366
		else if ( (command == "-a") || (command == "-append") )
367
		{
367
		{
368
			if ( argc < 4 )
368
			if ( argc < 4 )
369
				printf ( "Syntax: %s -a <filename> <catfile::tofile>\n\tAppends a file into the archive\n", cmd.c_str() );
369
				wprintf(L"Syntax: %s -a <filename> <catfile::tofile>\n\tAppends a file into the archive\n", cmd.c_str() );
370
			else
370
			else
371
				AppendFile ( CyString(argv[3]), CyString(argv[2]) );
371
				AppendFile ( CyString(argv[3]), Utils::WString(argv[2]));
372
		}
372
		}
373
		else if ( (command == "-r") || (command == "-remove") )
373
		else if ( (command == "-r") || (command == "-remove") )
374
		{
374
		{
375
			if ( argc < 4 )
375
			if ( argc < 4 )
376
				printf ( "Syntax: %s -r <catfile> <filename>\n\tRemoves a file from the archive\n", cmd.c_str() );
376
				wprintf(L"Syntax: %s -r <catfile> <filename>\n\tRemoves a file from the archive\n", cmd.c_str() );
377
			else
377
			else
378
				RemoveFile ( CyString(argv[2]), CyString(argv[3]) );
378
				RemoveFile ( CyString(argv[2]), CyString(argv[3]) );
379
		}
379
		}
380
		else if ( (command == "-u") || (command == "-unpack") )
380
		else if ( (command == "-u") || (command == "-unpack") )
381
		{
381
		{
382
			if ( argc < 3 )
382
			if ( argc < 3 )
383
				printf ( "Syntax: %s -u <filename> [to]\n\tUnpacks a file", cmd.c_str() );
383
				wprintf(L"Syntax: %s -u <filename> [to]\n\tUnpacks a file", cmd.c_str() );
384
			else if ( argc < 4 )
384
			else if ( argc < 4 )
385
				UnpackFile(argv[2], "" );
385
				UnpackFile(argv[2], "" );
386
			else
386
			else
387
				UnpackFile(argv[2], argv[3]);
387
				UnpackFile(argv[2], argv[3]);
388
		}
388
		}
389
		else if ( (command == "-p") || (command == "-pack") )
389
		else if ( (command == "-p") || (command == "-pack") )
390
		{
390
		{
391
			if ( argc < 3 )
391
			if ( argc < 3 )
392
				printf ( "Syntax: %s -p <filename> [to]\n\tPacks a file to .pck", cmd.c_str() );
392
				wprintf(L"Syntax: %s -p <filename> [to]\n\tPacks a file to .pck", cmd.c_str() );
393
			else if ( argc < 4 )
393
			else if ( argc < 4 )
394
				PackFile(argv[2], "");
394
				PackFile(argv[2], "");
395
			else
395
			else
396
				PackFile(argv[2], argv[3]);
396
				PackFile(argv[2], argv[3]);
397
		}
397
		}
398
		else
398
		else
399
		{
399
		{
400
			printf("Invalaid flag: %s\n\n", command.c_str());
400
			printf("Invalaid flag: %s\n\n", command.c_str());
401
			PrintSyntax(cmd);
401
			PrintSyntax(CyString(cmd.toString()));
402
		}
402
		}
403
	}
403
	}
404
 
404
 
405
#ifdef _DEBUG
405
#ifdef _DEBUG
406
	char pause;
406
	char pause;