Subversion Repositories spk

Rev

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

Rev 312 Rev 314
Line 70... Line 70...
70
	}
70
	}
71
	else
71
	else
72
		PrintError ( err );
72
		PrintError ( err );
73
}
73
}
74
 
74
 
-
 
75
void findAllFiles(const CDirIO &dirIO, const Utils::WString& dir, const Utils::WString& filepattern, Utils::WStringList& list)
-
 
76
{
-
 
77
	Utils::WStringList dirList;
-
 
78
	if (dirIO.dirList(dirList, dir, filepattern, true))
-
 
79
	{
-
 
80
		for (auto itr = dirList.begin(); itr != dirList.end(); itr++)
-
 
81
		{
-
 
82
			if (dirIO.isDir((*itr)->str))
-
 
83
				findAllFiles(dirIO, (*itr)->str, filepattern, list);
-
 
84
			else
-
 
85
				list.pushBack((*itr)->str);
-
 
86
		}
-
 
87
	}
-
 
88
}
-
 
89
 
75
bool findFiles(Utils::WStringList &files, const Utils::WString &filepattern)
90
bool findFiles(Utils::WStringList &files, const Utils::WString &filepattern)
76
{
91
{
77
	CFileIO File((filepattern.contains(L":")) ? filepattern : g_dir + L"/" + filepattern);
92
	CFileIO File((filepattern.contains(L":")) ? filepattern : g_dir + L"/" + filepattern);
78
	return File.GetDirIO().dirList(files, Utils::WString::Null(), File.filename(), true);
93
	return File.GetDirIO().dirList(files, Utils::WString::Null(), File.filename(), true);
79
}
94
}
80
 
95
 
81
 
96
 
82
void ExtractFile (const Utils::WString &filename, const Utils::WString &to, bool preserve )
97
void ExtractFile (const Utils::WString &filename, const Utils::WString &to, bool preserve )
83
{
98
{
84
	if ( !filename.contains(L"::") ) return;
99
	if ( !filename.contains(L"::") ) return;
85
 
100
 
86
	Utils::WString catfile = filename.token(L"::", 1);
101
	Utils::WString catfile = filename.token(L"::", 1);
87
	Utils::WString filemask = filename.token(L"::", 2);
102
	Utils::WString filemask = filename.token(L"::", 2);
88
 
103
 
89
	CCatFile cat;
104
	CCatFile cat;
90
	int err = cat.open(catfile, L"", CATREAD_DAT);
105
	int err = cat.open(catfile, L"", CATREAD_DAT);
91
	if ( err )
106
	if ( err )
92
	{
107
	{
93
		PrintError ( err );
108
		PrintError ( err );
94
		return;
109
		return;
95
	}
110
	}
96
 
111
 
97
	// all files
112
	// all files
98
	if ( filemask == L"*" )
113
	if ( filemask == L"*" )
99
	{
114
	{
100
		for (unsigned int i = 0; i < cat.GetNumFiles(); i++)
115
		for (unsigned int i = 0; i < cat.GetNumFiles(); i++)
101
		{
116
		{
102
			SInCatFile *f = cat.GetFile(i);
117
			SInCatFile *f = cat.GetFile(i);
103
			if (!cat.extractFile(f, to))
118
			if (!cat.extractFile(f, to))
104
				wprintf(L"Error: %s\n", cat.getErrorString().c_str() );
119
				wprintf(L"Error: %s\n", cat.getErrorString().c_str() );
105
			else
120
			else
106
				wprintf(L"File has been written (%s)\n", cat.errorString().c_str() );
121
				wprintf(L"File has been written (%s)\n", cat.errorString().c_str() );
Line 123... Line 138...
123
		Utils::WString file = (*itr)->str;
138
		Utils::WString file = (*itr)->str;
124
		if (!cat.extractFile(file, to, preserve))
139
		if (!cat.extractFile(file, to, preserve))
125
			wprintf(L"Error: %s\n", cat.getErrorString().c_str() );
140
			wprintf(L"Error: %s\n", cat.getErrorString().c_str() );
126
		else
141
		else
127
			wprintf(L"File has been written (%s)\n", cat.errorString().c_str() );
142
			wprintf(L"File has been written (%s)\n", cat.errorString().c_str() );
128
	}
143
	}
129
}
144
}
130
 
145
 
131
void AppendFile (const Utils::WString &sFile, const Utils::WString &filepattern )
146
void AppendFile (const Utils::CommandLine& cmd)
132
{
147
{
-
 
148
	Utils::WString sFile = cmd.arg(2);
-
 
149
	Utils::WString filepattern = cmd.arg(1);
133
	Utils::WString catfile;
150
	Utils::WString catfile;
134
	Utils::WString file;
151
	Utils::WString file;
-
 
152
 
-
 
153
	bool recursive = cmd.hasSwitch(L"recursive");
135
 
154
 
136
	Utils::WString C_File = sFile.findReplace(L"\\", L"/");
155
	Utils::WString C_File = sFile.findReplace(L"\\", L"/");
137
	bool doFile = false;
156
	bool doFile = false;
138
	if ( !C_File.contains(L"::"))
157
	if ( !C_File.contains(L"::"))
139
	{
158
	{
140
		catfile = C_File;
159
		catfile = C_File;
141
		doFile = true;
160
		doFile = true;
142
	}
161
	}
143
	else
162
	else
144
	{
163
	{
145
		catfile = C_File.token(L"::", 1);
164
		catfile = C_File.token(L"::", 1);
146
		file = C_File.token(L"::", 2);
165
		file = C_File.token(L"::", 2);
147
	}
166
	}
148
 
167
 
149
	if (!catfile.contains(":"))
168
	if (!catfile.contains(":"))
150
		catfile = g_dir + L"/" + catfile;
169
		catfile = g_dir + L"/" + catfile;
151
 
170
 
152
	Utils::WStringList list;
171
	Utils::WStringList list;
-
 
172
 
-
 
173
	CFileIO File((filepattern.contains(L":")) ? filepattern : g_dir + L"/" + filepattern);
-
 
174
	if (recursive)
-
 
175
		findAllFiles(File.dirIO(), L"", File.filename(), list);
-
 
176
	else
153
	findFiles(list, filepattern);
177
		findFiles(list, filepattern);
154
	if (!list.size())
178
	if (!list.size())
155
	{
179
	{
156
		wprintf(L"Error: no files found to add: %s\n", filepattern.c_str());
180
		wprintf(L"Error: no files found to add: %s\n", filepattern.c_str());
157
		return;
181
		return;
158
	}
182
	}
159
 
183
 
160
	CCatFile cat;
184
	CCatFile cat;
161
	int err = cat.open(catfile, L"", CATREAD_CATDECRYPT);
185
	int err = cat.open(catfile, L"", CATREAD_CATDECRYPT);
162
	if ( err && err != CATERR_CREATED)
186
	if ( err && err != CATERR_CREATED)
163
	{
187
	{
164
		PrintError ( err );
188
		PrintError ( err );
165
		return;
189
		return;
166
	}
190
	}
167
 
191
 
-
 
192
 
-
 
193
	int added = 0;
168
	for (auto itr = list.begin(); itr != list.end(); itr++)
194
	for (auto itr = list.begin(); itr != list.end(); itr++)
169
	{
195
	{
-
 
196
		if (File.dirIO().isDir((*itr)->str))
-
 
197
			continue;
-
 
198
 
170
		Utils::WString filename = (*itr)->str;
199
		Utils::WString filename = (*itr)->str;
171
		Utils::WString toFile = file;
200
		Utils::WString toFile = file;
172
		if (doFile)
201
		if (doFile)			
173
			toFile = filename;
202
			toFile = File.dirIO().relativePath(filename);;
174
 
203
 
175
		if (!toFile.contains(L"."))
204
		if (!toFile.contains(L"."))
176
		{
205
		{
177
			if (toFile[toFile.length() - 1] != L'/')
206
			if (toFile[toFile.length() - 1] != L'/')
178
				toFile += L"/";
207
				toFile += L"/";
179
			toFile += CFileIO(filename).filename();
208
			toFile += CFileIO(filename).filename();
180
		}
209
		}
181
 
210
 
-
 
211
		++added;
182
		if (cat.appendFile(filename, toFile))
212
		if (cat.appendFile(filename, toFile))
183
			wprintf(L"File %s has beed added to: %s::%s\n", filename.c_str(), catfile.c_str(), toFile.c_str());
213
			wprintf(L"File %s has beed added to: %s::%s\n", filename.c_str(), catfile.c_str(), toFile.c_str());
184
		else
214
		else
185
			wprintf(L"Error: Unable to add file: %s\n", filename.c_str());
215
			wprintf(L"Error: Unable to add file: %s\n", filename.c_str());
186
	}
216
	}
-
 
217
 
-
 
218
	if(!added)
-
 
219
		wprintf(L"Warning: Nothing to add: %s\n", filepattern.c_str());
187
}
220
}
188
 
221
 
189
void RemoveFile (const Utils::WString &C_File, const Utils::WString &remfile )
222
void RemoveFile (const Utils::WString &C_File, const Utils::WString &remfile )
190
{
223
{
191
	// first open the cat file
224
	// first open the cat file
Line 369... Line 402...
369
		else if ( (command == L"-a") || (command == L"-append") )
402
		else if ( (command == L"-a") || (command == L"-append") )
370
		{
403
		{
371
			if (args.size() < 3 )
404
			if (args.size() < 3 )
372
				wprintf(L"Syntax: %s -a <filename> <catfile::tofile>\n\tAppends a file into the archive\n", cmd.cmdName().c_str() );
405
				wprintf(L"Syntax: %s -a <filename> <catfile::tofile>\n\tAppends a file into the archive\n", cmd.cmdName().c_str() );
373
			else
406
			else
374
				AppendFile(cmd.arg(2), cmd.arg(1));
407
				AppendFile(cmd);
375
		}
408
		}
376
		else if ( (command == L"-r") || (command == L"-remove") )
409
		else if ( (command == L"-r") || (command == L"-remove") )
377
		{
410
		{
378
			if ( args.size() < 3 )
411
			if ( args.size() < 3 )
379
				wprintf(L"Syntax: %s -r <catfile> <filename>\n\tRemoves a file from the archive\n", cmd.cmdName().c_str() );
412
				wprintf(L"Syntax: %s -r <catfile> <filename>\n\tRemoves a file from the archive\n", cmd.cmdName().c_str() );