Subversion Repositories spk

Rev

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

Rev 196 Rev 197
Line 21... Line 21...
21
/////////////////////////////////////////
21
/////////////////////////////////////////
22
// STATIC FUNCTIONS
22
// STATIC FUNCTIONS
23
bool CDirIO::Exists(const Utils::WString &dir)
23
bool CDirIO::Exists(const Utils::WString &dir)
24
{
24
{
25
	return CDirIO(dir).exists();
25
	return CDirIO(dir).exists();
26
}
-
 
27
 
-
 
28
bool CDirIO::IsEmptyDir(const Utils::CStringList& dirList)
-
 
29
{
-
 
30
	// not found any files, most likly empty
-
 
31
	if (dirList.empty())
-
 
32
		return true;
-
 
33
	// check for any valid files
-
 
34
 
-
 
35
	for (auto itr = dirList.begin(); itr != dirList.end(); itr++)
-
 
36
	{
-
 
37
		Utils::String d = (*itr)->str;
-
 
38
		if (d == "." || d == "..")
-
 
39
			continue;
-
 
40
 
-
 
41
		// found something
-
 
42
		return false;
-
 
43
	}
-
 
44
 
-
 
45
	return true;
-
 
46
}
26
}
47
 
27
 
48
bool CDirIO::IsEmptyDir(const Utils::WStringList& dirList)
28
bool CDirIO::IsEmptyDir(const Utils::WStringList& dirList)
49
{
29
{
50
	// not found any files, most likly empty
30
	// not found any files, most likly empty
Line 184... Line 164...
184
}
164
}
185
 
165
 
186
 
166
 
187
bool CDirIO::create() const
167
bool CDirIO::create() const
188
{
168
{
189
	return create(Utils::String::Null());
169
	return create(Utils::WString::Null());
190
}
170
}
191
bool CDirIO::create(const Utils::WString &sDir) const
171
bool CDirIO::create(const Utils::WString &sDir) const
192
{
172
{
193
	Utils::WString dir = sDir;
173
	Utils::WString dir = sDir;
194
	if ( dir.empty() )
174
	if ( dir.empty() )
195
		dir = m_sCurrentDir;
175
		dir = m_sCurrentDir;
196
	dir = _parseDir(dir);
176
	dir = _parseDir(dir);
197
 
177
 
198
	// split up directorys
178
	// split up directorys
199
	std::vector<std::wstring> dirs;
179
	std::vector<Utils::WString> dirs;
200
	if(dir.findReplace(L"/", L"\\").findReplace(L"\\\\", L"\\").tokenise(L"\\", dirs))
180
	if(dir.findReplace(L"/", L"\\").findReplace(L"\\\\", L"\\").tokenise(L"\\", dirs))
201
	{
181
	{
202
		// check if full dir, or relative
182
		// check if full dir, or relative
203
		size_t start = 1;
183
		size_t start = 1;
204
		Utils::WString curDir = dirs.front();
184
		Utils::WString curDir = dirs.front();
Line 216... Line 196...
216
				curDir += L"/";
196
				curDir += L"/";
217
			curDir += dirs.at(i);
197
			curDir += dirs.at(i);
218
 
198
 
219
			// check if the directory exists
199
			// check if the directory exists
220
			if (!exists(curDir))
200
			if (!exists(curDir))
221
			{
201
			{
222
#ifdef _WIN32
202
#ifdef _WIN32
223
				if (_mkdir(TOSTRING(curDir).c_str()))
203
				if (_mkdir(TOSTRING(curDir).c_str()))
224
#else
204
#else
225
				if (mkdir(TOSTRING(curDir).c_str(), 0755))
205
				if (mkdir(TOSTRING(curDir).c_str(), 0755))
226
#endif
206
#endif
227
					return false;
207
					return false;
228
			}
208
			}
229
		}
209
		}
230
	}
210
	}
231
 
211
 
232
	return true;
212
	return true;
233
}
213
}
234
 
214
 
235
bool CDirIO::move(const Utils::WString &sTo)
215
bool CDirIO::move(const Utils::WString &sTo)
236
{
216
{
237
	Utils::WString to = _parseDir(sTo);
217
	Utils::WString to = _parseDir(sTo);
238
 
218
 
239
	if (exists(to))
219
	if (exists(to))
240
		return false;
220
		return false;
241
 
221
 
242
	if (!rename(TOSTRING(m_sCurrentDir).c_str(), TOSTRING(to).c_str()))
222
	if (!CFileIO::Rename(m_sCurrentDir, to))
243
	{
223
	{
244
		m_sCurrentDir = to;
224
		m_sCurrentDir = to;
245
		return true;
225
		return true;
246
	}
226
	}
247
 
227
 
Line 259... Line 239...
259
		if ( !create(to) )
239
		if ( !create(to) )
260
			return false;
240
			return false;
261
	}
241
	}
262
 
242
 
263
	if ( !rename(TOSTRING(from).c_str(), TOSTRING(to).c_str()) )
243
	if ( !rename(TOSTRING(from).c_str(), TOSTRING(to).c_str()) )
264
		return true;
244
		return true;
265
 
245
 
266
	return false;
246
	return false;
267
}
247
}
268
 
248
 
269
bool CDirIO::checkEmptyDir(const Utils::CStringList &dirList) const
249
bool CDirIO::checkEmptyDir(const Utils::WStringList &dirList) const
270
{
250
{
271
	// not found any files, most likly empty
251
	// not found any files, most likly empty
272
	if (dirList.empty())
252
	if (dirList.empty())
273
		return true;
253
		return true;
274
	// check for any valid files
254
	// check for any valid files
275
 
255
 
276
	for(auto itr = dirList.begin(); itr != dirList.end(); itr++)
256
	for(auto itr = dirList.begin(); itr != dirList.end(); itr++)
277
	{
257
	{
278
		Utils::String d = (*itr)->str;
258
		Utils::WString d = (*itr)->str;
279
		if (d == "." || d == "..")
259
		if (d == L"." || d == L"..")
280
			continue;
260
			continue;
281
 
261
 
282
		// found something
262
		// found something
283
		return false;
263
		return false;
284
	}
264
	}
285
 
265
 
286
	return true;
266
	return true;
287
}
267
}
288
 
268
 
289
bool CDirIO::removeDir(const Utils::WString& dir, bool doFiles, bool recursive, Utils::CStringList* errors)
269
bool CDirIO::removeDir(const Utils::WString& dir, bool doFiles, bool recursive, Utils::WStringList* errors)
290
{
270
{
291
	// check if the dir is empty
271
	// check if the dir is empty
292
	Utils::WStringList list;
272
	Utils::WStringList list;
293
	if (dirList(list, dir))
273
	if (dirList(list, dir))
294
	{
274
	{
Line 300... Line 280...
300
#else
280
#else
301
			if (rmdir(TOSTRING(remDir).c_str()) == 0)
281
			if (rmdir(TOSTRING(remDir).c_str()) == 0)
302
#endif
282
#endif
303
			{
283
			{
304
				if (errors)
284
				if (errors)
305
					errors->pushBack(remDir.toString());
285
					errors->pushBack(remDir);
306
			}
286
			}
307
			return true;
287
			return true;
308
		}
288
		}
309
 
289
 
310
		// not empty
290
		// not empty
Line 319... Line 299...
319
				// if its a file
299
				// if its a file
320
				Utils::WString fullFile = dir + "\\" + d;
300
				Utils::WString fullFile = dir + "\\" + d;
321
				if (doFiles && isFile(fullFile))
301
				if (doFiles && isFile(fullFile))
322
				{
302
				{
323
					Utils::WString remFile = _parseDir(fullFile);
303
					Utils::WString remFile = _parseDir(fullFile);
324
					if (remove(TOSTRING(remFile).c_str()) == 0)
304
					if (!CFileIO::Remove(remFile))
325
					{
305
					{
326
						if (errors)
306
						if (errors)
327
							errors->pushBack(remFile.toString());
307
							errors->pushBack(remFile);
328
					}
308
					}
329
				}
309
				}
330
				else if (recursive && isDir(fullFile))
310
				else if (recursive && isDir(fullFile))
331
					removeDir(fullFile, doFiles, recursive, errors);
311
					removeDir(fullFile, doFiles, recursive, errors);
332
			}
312
			}
Line 344... Line 324...
344
#else
324
#else
345
			if (rmdir(TOSTRING(remDir).c_str()) == 0)
325
			if (rmdir(TOSTRING(remDir).c_str()) == 0)
346
#endif
326
#endif
347
			{
327
			{
348
				if (errors)
328
				if (errors)
349
					errors->pushBack(remDir.toString());
329
					errors->pushBack(remDir);
350
			}
330
			}
351
			return true;
331
			return true;
352
		}
332
		}
353
	}
333
	}
354
	return false;
334
	return false;
355
}
-
 
356
 
-
 
357
bool CDirIO::dirList(Utils::CStringList& files, Utils::WString dir, Utils::WString filePattern) const
-
 
358
{
-
 
359
	dir = _parseDir(dir);
-
 
360
	if (dir.empty())
-
 
361
		return false;
-
 
362
 
-
 
363
	dir = dir.findReplace("\\", "/");
-
 
364
	if (filePattern.empty())
-
 
365
		dir += "/*";
-
 
366
	else
-
 
367
	{
-
 
368
		dir += "/";
-
 
369
		dir += filePattern;
-
 
370
	}
-
 
371
	dir = dir.findReplace("//", "/");
-
 
372
 
-
 
373
#ifdef _WIN32
-
 
374
	dir = dir.findReplace("/", "\\");
-
 
375
 
-
 
376
	WIN32_FIND_DATA data;
-
 
377
 
-
 
378
	HANDLE h = FindFirstFile(dir.c_str(), & data);
-
 
379
	if (h != INVALID_HANDLE_VALUE)
-
 
380
	{
-
 
381
		std::wstring ws(data.cFileName);
-
 
382
		std::string s(ws.begin(), ws.end());
-
 
383
 
-
 
384
		Utils::String checkFile(s);
-
 
385
		if (!checkFile.Compare(".") && !checkFile.Compare(".."))
-
 
386
			files.pushBack(checkFile);
-
 
387
 
-
 
388
		while (FindNextFile(h, &data))
-
 
389
		{
-
 
390
			std::wstring ws(data.cFileName);
-
 
391
			std::string s(ws.begin(), ws.end());
-
 
392
 
-
 
393
			Utils::String checkFile(s);
-
 
394
			if (checkFile != "." && checkFile != "..")
-
 
395
				files.pushBack(checkFile);
-
 
396
		}
-
 
397
 
-
 
398
		FindClose(h);
-
 
399
		return true;
-
 
400
	}
-
 
401
	return false;
-
 
402
#else
-
 
403
	DIR* dir;
-
 
404
	struct dirent* ent;
-
 
405
	if ((dir = opendir(dir.c_str())) != NULL) {
-
 
406
		while ((ent = readdir(dir)) != NULL) {
-
 
407
			Utils::String checkFile(ent->d_name);
-
 
408
			if (checkFile != "." && checkFile != "..")
-
 
409
				files.pushBack(checkFile);
-
 
410
		}
-
 
411
		closedir(dir);
-
 
412
	}
-
 
413
	return true;
-
 
414
#endif//_WIN32
-
 
415
 
-
 
416
}
335
}
417
 
336
 
418
bool CDirIO::dirList(Utils::WStringList& files, const Utils::WString &sDir, const Utils::WString &filePattern) const
337
bool CDirIO::dirList(Utils::WStringList& files, const Utils::WString &sDir, const Utils::WString &filePattern) const
419
{
338
{
420
	Utils::WString dir = _parseDir(sDir);
339
	Utils::WString dir = _parseDir(sDir);
Line 442... Line 361...
442
		if (!checkFile.Compare(L".") && !checkFile.Compare(L".."))
361
		if (!checkFile.Compare(L".") && !checkFile.Compare(L".."))
443
			files.pushBack(checkFile);
362
			files.pushBack(checkFile);
444
 
363
 
445
		while (FindNextFile(h, &data))
364
		while (FindNextFile(h, &data))
446
		{
365
		{
447
			Utils::String checkFile(data.cFileName);
366
			Utils::WString checkFile(data.cFileName);
448
			if (checkFile != L"." && checkFile != L"..")
367
			if (checkFile != L"." && checkFile != L"..")
449
				files.pushBack(checkFile);
368
				files.pushBack(checkFile);
450
		}
369
		}
451
 
370
 
452
		FindClose(h);
371
		FindClose(h);
Line 456... Line 375...
456
#else
375
#else
457
	DIR* dir;
376
	DIR* dir;
458
	struct dirent* ent;
377
	struct dirent* ent;
459
	if ((dir = opendir(dir.c_str())) != NULL) {
378
	if ((dir = opendir(dir.c_str())) != NULL) {
460
		while ((ent = readdir(dir)) != NULL) {
379
		while ((ent = readdir(dir)) != NULL) {
461
			Utils::String checkFile(ent->d_name);
380
			Utils::WString checkFile(ent->d_name);
462
			if (checkFile != "." && checkFile != "..")
381
			if (checkFile != L"." && checkFile != L"..")
463
				files.pushBack(checkFile);
382
				files.pushBack(checkFile);
464
		}
383
		}
465
		closedir(dir);
384
		closedir(dir);
466
	}
385
	}
467
	return true;
386
	return true;