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
 
-
 
57
 
56
 
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())
63
		return false;
62
		return false;
64
 
63
 
65
	if (ACCESS(dir.c_str(), 0) == 0)
64
	if (ACCESS(dir.c_str(), 0) == 0)
66
		return true;
65
		return true;
67
 
66
 
68
	return false;
67
	return false;
69
}
68
}
70
 
69
 
71
bool CDirIO::exists(const Utils::String &dir) const
70
bool CDirIO::exists(const Utils::String &dir) const
72
{
71
{
73
	Utils::String d = _parseDir(dir);
72
	Utils::String d = _parseDir(dir);
74
 
73
 
75
	if (d.empty())
74
	if (d.empty())
76
		return false;
75
		return false;
77
 
76
 
78
	if (ACCESS(d.c_str(), 0) == 0)
77
	if (ACCESS(d.c_str(), 0) == 0)
79
		return true;
78
		return true;
80
 
79
 
81
	return false;
80
	return false;
82
}
81
}
83
 
82
 
84
Utils::String CDirIO::_parseDir(const Utils::String &dir) const
83
Utils::String CDirIO::_parseDir(const Utils::String &dir) const
85
{
84
{
86
	Utils::String sDir = dir.asFilename();
85
	Utils::String sDir = dir.asFilename();
87
	if ( !m_sCurrentDir.empty() && !sDir.isin(":") )
86
	if ( !m_sCurrentDir.empty() && !sDir.isin(":") )
88
	{
87
	{
Line 94... Line 93...
94
 
93
 
95
	return sDir.asFilename();
94
	return sDir.asFilename();
96
}
95
}
97
 
96
 
98
bool CDirIO::isDir(const Utils::String &sDir) const
97
bool CDirIO::isDir(const Utils::String &sDir) const
99
{
98
{
100
	Utils::String dir = _parseDir(sDir);
99
	Utils::String dir = _parseDir(sDir);
101
	if (ACCESS(dir.c_str(), 0) != -1)
100
	if (ACCESS(dir.c_str(), 0) != -1)
102
	{
101
	{
103
		struct stat status;
102
		struct stat status;
104
		stat(dir.c_str(), &status);
103
		stat(dir.c_str(), &status);
105
 
-
 
106
		if (status.st_mode & S_IFDIR)
-
 
107
			return true;
-
 
108
	}
-
 
109
	else {
-
 
110
		return !dir.token("/", -1).isin(".");
-
 
111
	}
-
 
112
 
104
 
-
 
105
		if (status.st_mode & S_IFDIR)
-
 
106
			return true;
-
 
107
	}
-
 
108
	else {
-
 
109
		return !dir.token("/", -1).isin(".");
-
 
110
	}
-
 
111
 
113
	return false;
112
	return false;
114
}
113
}
115
 
114
 
116
bool CDirIO::isDir() const
115
bool CDirIO::isDir() const
117
{
116
{
118
	return isDir(m_sCurrentDir);
117
	return isDir(m_sCurrentDir);
119
}
118
}
120
 
119
 
121
bool CDirIO::isFile() const
120
bool CDirIO::isFile() const
122
{
121
{
123
	return isFile(m_sCurrentDir);
122
	return isFile(m_sCurrentDir);
124
}
123
}
Line 135... Line 134...
135
		else
134
		else
136
			return true;
135
			return true;
137
	}
136
	}
138
	else {
137
	else {
139
		return dir.token("/", -1).isin(".");
138
		return dir.token("/", -1).isin(".");
140
	}
139
	}
-
 
140
 
-
 
141
	return false;
-
 
142
}
-
 
143
 
141
 
144
 
142
	return false;
-
 
143
}
-
 
144
 
-
 
145
bool CDirIO::Create(CyString sDir)
-
 
146
{
-
 
147
	return create(sDir.ToString());
-
 
148
}
-
 
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 167... Line 163...
167
	
163
	
168
	if ( dirs && max )
164
	if ( dirs && max )
169
		curDir = dirs[0];
165
		curDir = dirs[0];
170
 
166
 
171
	if ( !curDir.isin(":") )
167
	if ( !curDir.isin(":") )
172
	{
168
	{
173
		curDir = m_sCurrentDir;
169
		curDir = m_sCurrentDir;
174
		start = 0;
170
		start = 0;
175
	}
171
	}
176
 
172
 
177
	// process each dir
173
	// process each dir
Line 181... Line 177...
181
			curDir += "/";
177
			curDir += "/";
182
		curDir += dirs[i];
178
		curDir += dirs[i];
183
 
179
 
184
		// check if the directory exists
180
		// check if the directory exists
185
		if ( !exists(curDir) )
181
		if ( !exists(curDir) )
186
		{
182
		{
187
#ifdef _WIN32
183
#ifdef _WIN32
188
			if ( _mkdir(curDir.c_str()) )
184
			if ( _mkdir(curDir.c_str()) )
189
#else
185
#else
190
			if ( mkdir(curDir.c_str(), 0755) )
186
			if ( mkdir(curDir.c_str(), 0755) )
191
#endif
187
#endif
192
			{
188
			{
Line 200... Line 196...
200
 
196
 
201
	return true;
197
	return true;
202
}
198
}
203
 
199
 
204
bool CDirIO::move(const Utils::String &sTo)
200
bool CDirIO::move(const Utils::String &sTo)
205
{
201
{
206
	Utils::String to = _parseDir(sTo);
202
	Utils::String to = _parseDir(sTo);
207
 
203
 
208
	if (exists(to))
204
	if (exists(to))
209
		return false;
205
		return false;
210
 
206
 
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) )
-
 
266
	{
-
 
267
		CyString remDir = _parseDir(dir.ToString());
-
 
268
		#ifdef _WIN32
-
 
269
		if ( _rmdir(remDir.c_str()) == 0 )
-
 
270
		#else
-
 
271
		if ( rmdir(remDir.c_str()) == 0 )
-
 
272
		#endif
-
 
273
		{
-
 
274
			if ( errors )
-
 
275
				errors->PushBack(remDir);
-
 
276
		}
-
 
277
		return true;
-
 
278
	}
-
 
279
 
-
 
280
	// not empty
-
 
281
	if ( doFiles || recursive )
258
	if (dirList(list, dir))
282
	{
259
	{
283
		for ( SStringList *str = dirList->Head(); str; str = str->next )
260
		if (checkEmptyDir(list))
284
		{
261
		{
285
			CyString d = str->str;
262
			Utils::String remDir = _parseDir(dir);
-
 
263
#ifdef _WIN32
-
 
264
			if (_rmdir(remDir.c_str()) == 0)
-
 
265
#else
286
			if ( d == "." || d == ".." )
266
			if (rmdir(remDir.c_str()) == 0)
-
 
267
#endif
-
 
268
			{
-
 
269
				if (errors)
-
 
270
					errors->pushBack(remDir);
-
 
271
			}
287
				continue;
272
			return true;
-
 
273
		}
288
 
274
 
289
			// if its a file
275
		// not empty
290
			Utils::String fullFile = (dir + "\\" + d).ToString();
276
		if (doFiles || recursive)
-
 
277
		{
291
			if ( doFiles &&; isFile(fullFile) )
278
			for (auto itr = list.begin(); itr != list.end(); itr++)
292
			{
279
			{
-
 
280
				Utils::String d = (*itr)->str;
-
 
281
				if (d == "." || d == "..")
-
 
282
					continue;
-
 
283
 
-
 
284
				// if its a file
293
				CyString remFile = _parseDir(fullFile);
285
				Utils::String fullFile = dir + "\\" + d;
294
				if ( remove(remFile.c_str()) == 0 )
286
				if (doFiles && isFile(fullFile))
295
				{
287
				{
-
 
288
					Utils::String remFile = _parseDir(fullFile);
-
 
289
					if (remove(remFile.c_str()) == 0)
-
 
290
					{
296
					if ( errors )
291
						if (errors)
297
						errors->PushBack(remFile);
292
							errors->pushBack(remFile);
-
 
293
					}
298
				}
294
				}
-
 
295
				else if (recursive && isDir(fullFile))
-
 
296
					removeDir(fullFile, doFiles, recursive, errors);
299
			}
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
			{
-
 
313
				if (errors)
-
 
314
					errors->pushBack(remDir);
-
 
315
			}
-
 
316
			return true;
-
 
317
		}
-
 
318
	}
-
 
319
	return false;
-
 
320
 
-
 
321
}
-
 
322
bool CDirIO::RemoveDir(CyString dir, bool doFiles, bool recursive, CyStringList* errors)
-
 
323
{
-
 
324
	// check if the dir is empty
-
 
325
	Utils::CStringList list;
-
 
326
	if (dirList(list, dir.ToString()))
-
 
327
	{
-
 
328
		if (checkEmptyDir(list))
-
 
329
		{
-
 
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)
-
 
345
		{
-
 
346
			for (auto itr = list.begin(); itr != list.end(); itr++)
-
 
347
			{
-
 
348
				Utils::String d = (*itr)->str;
-
 
349
				if (d == "." || d == "..")
-
 
350
					continue;
-
 
351
 
-
 
352
				// if its a file
-
 
353
				Utils::String fullFile = dir.ToString() + "\\" + d;
-
 
354
				if (doFiles && isFile(fullFile))
-
 
355
				{
-
 
356
					Utils::String remFile = _parseDir(fullFile);
-
 
357
					if (remove(remFile.c_str()) == 0)
-
 
358
					{
-
 
359
						if (errors)
-
 
360
							errors->PushBack(CyString(remFile));
-
 
361
					}
-
 
362
				}
300
			else if ( recursive && isDir(fullFile) )
363
				else if (recursive && isDir(fullFile))
301
				RemoveDir(fullFile, doFiles, recursive, errors);
364
					RemoveDir(fullFile, doFiles, recursive, errors);
-
 
365
			}
302
		}
366
		}
303
	}
367
	}
304
 
368
 
305
	// now check if its empty
-
 
306
	delete dirList;
369
	list.clear();
307
	dirList = DirList(dir);
370
	if (dirList(list, dir.ToString()))
-
 
371
	{
308
	if ( CheckEmptyDir(dirList) )
372
		if (checkEmptyDir(list))
309
	{
373
		{
310
		CyString remDir = _parseDir(dir.ToString());
374
			Utils::String remDir = _parseDir(dir.ToString());
311
		#ifdef _WIN32
375
#ifdef _WIN32
312
		if ( _rmdir(remDir.c_str()) == 0 )
376
			if (_rmdir(remDir.c_str()) == 0)
313
		#else
377
#else
314
		if ( rmdir(remDir.c_str()) == 0 )
378
			if (rmdir(remDir.c_str()) == 0)
315
		#endif
379
#endif
316
		{
380
			{
317
			if ( errors )
381
				if (errors)
318
				errors->PushBack(remDir);
382
					errors->PushBack(CyString(remDir));
319
		}
383
			}
320
		return true;
384
			return true;
-
 
385
		}
321
	}
386
	}
322
 
-
 
323
	return false;
387
	return false;
324
}
388
}
-
 
389
 
325
 
390
 
326
Utils::CStringList CDirIO::dirList(Utils::String dir, Utils::String filePattern) const
-
 
327
{
-
 
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
 
453
	return m_sCurrentDir + "/" + filename;
459
	return m_sCurrentDir + "/" + filename;
454
}
460
}
455
 
461
 
456
Utils::String CDirIO::dir(const Utils::String &sDir) const
462
Utils::String CDirIO::dir(const Utils::String &sDir) const
457
{
463
{
458
	return _parseDir(sDir);
464
	return _parseDir(sDir);
459
}
465
}
460
 
466
 
461
const Utils::String &CDirIO::dir() const
467
const Utils::String &CDirIO::dir() const
462
{
-
 
463
	return m_sCurrentDir;
-
 
464
}
-
 
465
 
-
 
466
CyString CDirIO::Dir(CyString dir)
-
 
467
{
468
{
468
	return _parseDir(dir.ToString());
469
	return m_sCurrentDir;
469
}
470
}
470
 
471
 
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() )
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