Subversion Repositories spk

Rev

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

Rev 129 Rev 160
Line 35... Line 35...
35
{
35
{
36
}
36
}
37
 
37
 
38
CDirIO::CDirIO(CyString dir)
38
CDirIO::CDirIO(CyString dir)
39
{
39
{
40
	SetDir(dir);
40
	setDir(dir.ToString());
41
}
41
}
42
 
42
 
43
CDirIO::CDirIO ( CFileIO *file )
43
CDirIO::CDirIO ( CFileIO *file )
44
{
44
{
45
	SetDir(file->dir());
45
	setDir(file->dir());
46
}
46
}
47
 
47
 
48
/////////////////////////////////////////////////////////
48
/////////////////////////////////////////////////////////
49
//
49
//
50
 
50
 
51
void CDirIO::SetDir(CyString dir)
51
void CDirIO::setDir(const Utils::String& dir)
52
{
52
{
53
	m_sCurrentDir = dir.ToString();
53
	m_sCurrentDir = dir;
54
	m_sCurrentDir.toFilename();
54
	m_sCurrentDir.toFilename();
55
}
55
}
56
 
56
 
57
 
-
 
58
bool CDirIO::exists() const
57
bool CDirIO::exists() const
59
{
58
{
60
	Utils::String dir = m_sCurrentDir;
59
	Utils::String dir = m_sCurrentDir;
61
 
60
 
62
	if (dir.empty())
61
	if (dir.empty())
Line 140... Line 139...
140
	}
139
	}
141
 
140
 
142
	return false;
141
	return false;
143
}
142
}
144
 
143
 
145
bool CDirIO::Create(CyString sDir)
-
 
146
{
-
 
147
	return create(sDir.ToString());
-
 
148
}
144
 
149
bool CDirIO::create() const
145
bool CDirIO::create() const
150
{
146
{
151
	return create(Utils::String::Null());
147
	return create(Utils::String::Null());
152
}
148
}
153
bool CDirIO::create(const Utils::String &sDir) const
149
bool CDirIO::create(const Utils::String &sDir) const
Line 216... Line 212...
216
 
212
 
217
	return false;
213
	return false;
218
}
214
}
219
 
215
 
220
 
216
 
221
bool CDirIO::Move(CyString sFrom, CyString sTo)
217
bool CDirIO::move(const Utils::String &sFrom, const Utils::String &sTo)
222
{
218
{
223
	Utils::String from = _parseDir(sFrom.ToString());
219
	Utils::String from = _parseDir(sFrom);
224
	Utils::String to = _parseDir(sTo.ToString());
220
	Utils::String to = _parseDir(sTo);
225
 
221
 
226
	if ( !exists(to) )
222
	if ( !exists(to) )
227
	{
223
	{
228
		if ( !Create(to) )
224
		if ( !create(to) )
229
			return false;
225
			return false;
230
	}
226
	}
231
 
227
 
232
	if ( !rename(from.c_str(), to.c_str()) )
228
	if ( !rename(from.c_str(), to.c_str()) )
233
		return true;
229
		return true;
234
 
230
 
235
	return false;
231
	return false;
236
}
232
}
237
 
233
 
238
bool CDirIO::CheckEmptyDir(CyStringList *dirList)
234
bool CDirIO::checkEmptyDir(const Utils::CStringList &dirList) const
239
{
235
{
240
	// no pointer, most likly empty
-
 
241
	if ( !dirList )
-
 
242
		return true;
-
 
243
	// not found any files, most likly empty
236
	// not found any files, most likly empty
244
	if ( !dirList->Count() )
237
	if (dirList.empty())
245
		return true;
238
		return true;
246
	// check for any valid files
239
	// check for any valid files
247
 
240
 
248
	for ( SStringList *str = dirList->Head(); str; str = str->next )
241
	for(auto itr = dirList.begin(); itr != dirList.end(); itr++)
249
	{
242
	{
250
		CyString d = str->str;
243
		Utils::String d = (*itr)->str;
251
		if ( d == "." || d == ".." )
244
		if (d == "." || d == "..")
252
			continue;
245
			continue;
253
 
246
 
254
		// found something
247
		// found something
255
		return false;
248
		return false;
256
	}
249
	}
257
 
250
 
258
	return true;
251
	return true;
259
}
252
}
260
 
253
 
261
bool CDirIO::RemoveDir(CyString dir, bool doFiles, bool recursive, CyStringList *errors)
254
bool CDirIO::removeDir(const Utils::String& dir, bool doFiles, bool recursive, Utils::CStringList* errors)
262
{
255
{
263
	// check if the dir is empty
256
	// check if the dir is empty
264
	CyStringList *dirList = DirList(dir);
257
	Utils::CStringList list;
265
	if ( CheckEmptyDir(dirList) )
258
	if (dirList(list, dir))
266
	{
259
	{
-
 
260
		if (checkEmptyDir(list))
-
 
261
		{
267
		CyString remDir = _parseDir(dir.ToString());
262
			Utils::String remDir = _parseDir(dir);
268
		#ifdef _WIN32
263
#ifdef _WIN32
269
		if ( _rmdir(remDir.c_str()) == 0 )
264
			if (_rmdir(remDir.c_str()) == 0)
270
		#else
265
#else
271
		if ( rmdir(remDir.c_str()) == 0 )
266
			if (rmdir(remDir.c_str()) == 0)
272
		#endif
267
#endif
-
 
268
			{
-
 
269
				if (errors)
-
 
270
					errors->pushBack(remDir);
-
 
271
			}
-
 
272
			return true;
-
 
273
		}
-
 
274
 
-
 
275
		// not empty
-
 
276
		if (doFiles || recursive)
273
		{
277
		{
-
 
278
			for (auto itr = list.begin(); itr != list.end(); itr++)
-
 
279
			{
-
 
280
				Utils::String d = (*itr)->str;
-
 
281
				if (d == "." || d == "..")
-
 
282
					continue;
-
 
283
 
-
 
284
				// if its a file
-
 
285
				Utils::String fullFile = dir + "\\" + d;
-
 
286
				if (doFiles && isFile(fullFile))
-
 
287
				{
-
 
288
					Utils::String remFile = _parseDir(fullFile);
-
 
289
					if (remove(remFile.c_str()) == 0)
-
 
290
					{
-
 
291
						if (errors)
-
 
292
							errors->pushBack(remFile);
-
 
293
					}
-
 
294
				}
-
 
295
				else if (recursive && isDir(fullFile))
-
 
296
					removeDir(fullFile, doFiles, recursive, errors);
-
 
297
			}
-
 
298
		}
-
 
299
	}
-
 
300
	list.clear();
-
 
301
	// now check if its empty
-
 
302
	if(dirList(list, dir))
-
 
303
	{
-
 
304
		if (checkEmptyDir(list))
-
 
305
		{
-
 
306
			Utils::String remDir = _parseDir(dir);
-
 
307
#ifdef _WIN32
-
 
308
			if (_rmdir(remDir.c_str()) == 0)
-
 
309
#else
-
 
310
			if (rmdir(remDir.c_str()) == 0)
-
 
311
#endif
-
 
312
			{
274
			if ( errors )
313
				if (errors)
275
				errors->PushBack(remDir);
314
					errors->pushBack(remDir);
-
 
315
			}
-
 
316
			return true;
276
		}
317
		}
277
		return true;
-
 
278
	}
318
	}
-
 
319
	return false;
279
 
320
 
-
 
321
}
-
 
322
bool CDirIO::RemoveDir(CyString dir, bool doFiles, bool recursive, CyStringList* errors)
-
 
323
{
280
	// not empty
324
	// check if the dir is empty
-
 
325
	Utils::CStringList list;
281
	if ( doFiles || recursive )
326
	if (dirList(list, dir.ToString()))
282
	{
327
	{
-
 
328
		if (checkEmptyDir(list))
-
 
329
		{
283
		for ( SStringList *str = dirList->Head(); str; str = str->next )
330
			Utils::String remDir = _parseDir(dir.ToString());
-
 
331
#ifdef _WIN32
-
 
332
			if (_rmdir(remDir.c_str()) == 0)
-
 
333
#else
-
 
334
			if (rmdir(remDir.c_str()) == 0)
-
 
335
#endif
-
 
336
			{
-
 
337
				if (errors)
-
 
338
					errors->PushBack(CyString(remDir));
-
 
339
			}
-
 
340
			return true;
-
 
341
		}
-
 
342
 
-
 
343
		// not empty
-
 
344
		if (doFiles || recursive)
284
		{
345
		{
285
			CyString d = str->str;
-
 
286
			if ( d == "." || d == ".." )
-
 
287
				continue;
-
 
288
 
-
 
289
			// if its a file
-
 
290
			Utils::String fullFile = (dir + "\\" + d).ToString();
346
			for (auto itr = list.begin(); itr != list.end(); itr++)
291
			if ( doFiles && isFile(fullFile) )
-
 
292
			{
347
			{
-
 
348
				Utils::String d = (*itr)->str;
-
 
349
				if (d == "." || d == "..")
-
 
350
					continue;
-
 
351
 
-
 
352
				// if its a file
293
				CyString remFile = _parseDir(fullFile);
353
				Utils::String fullFile = dir.ToString() + "\\" + d;
294
				if ( remove(remFile.c_str()) == 0 )
354
				if (doFiles && isFile(fullFile))
295
				{
355
				{
-
 
356
					Utils::String remFile = _parseDir(fullFile);
-
 
357
					if (remove(remFile.c_str()) == 0)
-
 
358
					{
296
					if ( errors )
359
						if (errors)
297
						errors->PushBack(remFile);
360
							errors->PushBack(CyString(remFile));
-
 
361
					}
298
				}
362
				}
-
 
363
				else if (recursive && isDir(fullFile))
-
 
364
					RemoveDir(fullFile, doFiles, recursive, errors);
299
			}
365
			}
300
			else if ( recursive && isDir(fullFile) )
-
 
301
				RemoveDir(fullFile, doFiles, recursive, errors);
-
 
302
		}
366
		}
303
	}
367
	}
304
 
368
 
305
	// now check if its empty
-
 
306
	delete dirList;
-
 
307
	dirList = DirList(dir);
369
	list.clear();
308
	if ( CheckEmptyDir(dirList) )
370
	if (dirList(list, dir.ToString()))
309
	{
371
	{
310
		CyString remDir = _parseDir(dir.ToString());
-
 
311
		#ifdef _WIN32
-
 
312
		if ( _rmdir(remDir.c_str()) == 0 )
-
 
313
		#else
-
 
314
		if ( rmdir(remDir.c_str()) == 0 )
372
		if (checkEmptyDir(list))
315
		#endif
-
 
316
		{
373
		{
-
 
374
			Utils::String remDir = _parseDir(dir.ToString());
-
 
375
#ifdef _WIN32
-
 
376
			if (_rmdir(remDir.c_str()) == 0)
-
 
377
#else
-
 
378
			if (rmdir(remDir.c_str()) == 0)
-
 
379
#endif
-
 
380
			{
317
			if ( errors )
381
				if (errors)
318
				errors->PushBack(remDir);
382
					errors->PushBack(CyString(remDir));
-
 
383
			}
-
 
384
			return true;
319
		}
385
		}
320
		return true;
-
 
321
	}
386
	}
322
 
-
 
323
	return false;
387
	return false;
324
}
388
}
325
 
389
 
326
Utils::CStringList CDirIO::dirList(Utils::String dir, Utils::String filePattern) const
-
 
327
{
390
 
328
	Utils::CStringList files;
-
 
329
	dirList(files, dir, filePattern);
-
 
330
	return files;
-
 
331
}
-
 
332
bool CDirIO::dirList(Utils::CStringList &files, Utils::String dir, Utils::String filePattern) const
391
bool CDirIO::dirList(Utils::CStringList &files, Utils::String dir, Utils::String filePattern) const
333
{
392
{
334
	dir = _parseDir(dir);
393
	dir = _parseDir(dir);
335
	if ( dir.empty() )
394
	if ( dir.empty() )
336
		return false;
395
		return false;
Line 390... Line 449...
390
	return true;
449
	return true;
391
#endif//_WIN32
450
#endif//_WIN32
392
 
451
 
393
}
452
}
394
 
453
 
395
CyStringList *CDirIO::DirList(CyString dir, CyString filepattern)
-
 
396
{
-
 
397
	dir = _parseDir(dir.ToString());
-
 
398
	if ( dir.Empty() )
-
 
399
		return 0;
-
 
400
 
-
 
401
	CyStringList *files = new CyStringList;
-
 
402
 
-
 
403
#ifdef _WIN32
-
 
404
	dir = dir.FindReplace("/", "\\");
-
 
405
	if ( filepattern.Empty() )
-
 
406
		dir += "\\*";
-
 
407
	else
-
 
408
	{
-
 
409
		dir += "\\";
-
 
410
		dir += filepattern;
-
 
411
	}
-
 
412
	dir = dir.FindReplace("\\\\", "\\");
-
 
413
 
-
 
414
	WIN32_FIND_DATA data;
-
 
415
	TCHAR buf[5000];
-
 
416
	wsprintf(buf, L"%hs", dir.c_str());
-
 
417
 
-
 
418
	HANDLE h = FindFirstFile(buf, &data);
-
 
419
	if ( h != INVALID_HANDLE_VALUE)
-
 
420
	{
-
 
421
		CyString checkFile(data.cFileName);
-
 
422
		if ( !checkFile.Compare(".") && !checkFile.Compare("..") )
-
 
423
			files->PushBack(checkFile);
-
 
424
		while ( FindNextFile(h, &data) )
-
 
425
		{
-
 
426
			CyString checkFile(data.cFileName);
-
 
427
			if ( checkFile != "." && checkFile != ".." )
-
 
428
				files->PushBack(checkFile);
-
 
429
		}
-
 
430
 
-
 
431
		FindClose(h);
-
 
432
	}
-
 
433
#else
-
 
434
 
-
 
435
#endif//_WIN32
-
 
436
 
-
 
437
	return files;
-
 
438
}
-
 
439
 
-
 
440
CyString CDirIO::File(CyString filename)
-
 
441
{
-
 
442
	if (m_sCurrentDir.empty())
-
 
443
		return filename;
-
 
444
 
-
 
445
	return CyString(m_sCurrentDir) + "/" + filename;
-
 
446
}
-
 
447
 
-
 
448
Utils::String CDirIO::file(const Utils::String &filename) const
454
Utils::String CDirIO::file(const Utils::String &filename) const
449
{
455
{
450
	if (m_sCurrentDir.empty())
456
	if (m_sCurrentDir.empty())
451
		return filename;
457
		return filename;
452
 
458
 
Line 461... Line 467...
461
const Utils::String &CDirIO::dir() const
467
const Utils::String &CDirIO::dir() const
462
{
468
{
463
	return m_sCurrentDir;
469
	return m_sCurrentDir;
464
}
470
}
465
 
471
 
466
CyString CDirIO::Dir(CyString dir)
-
 
467
{
-
 
468
	return _parseDir(dir.ToString());
-
 
469
}
-
 
470
 
-
 
471
bool CDirIO::cd(const Utils::String &sDir)
472
bool CDirIO::cd(const Utils::String &sDir)
472
{
473
{
473
	if ( m_sCurrentDir.empty() )
474
	if ( m_sCurrentDir.empty() )
474
		m_sCurrentDir = sDir;
475
		m_sCurrentDir = sDir;
475
	else
476
	else
Line 477... Line 478...
477
	m_sCurrentDir = m_sCurrentDir.asFilename();
478
	m_sCurrentDir = m_sCurrentDir.asFilename();
478
 
479
 
479
	return exists();
480
	return exists();
480
}
481
}
481
 
482
 
482
bool CDirIO::CreateAndChange(CyString dir)
483
bool CDirIO::createAndChange(const Utils::String &dir)
483
{
484
{
484
	if ( Create(dir) )
485
	if ( create(dir) )
485
		return cd(dir.ToString());
486
		return cd(dir);
486
	return false;
487
	return false;
487
}
488
}
488
 
489
 
489
 
490
 
490
Utils::String CDirIO::topDir() const
491
Utils::String CDirIO::topDir() const