Subversion Repositories spk

Rev

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

Rev 124 Rev 125
Line 14... Line 14...
14
#else
14
#else
15
#include <dirent.h>
15
#include <dirent.h>
16
#define ACCESS access
16
#define ACCESS access
17
#endif
17
#endif
18
 
18
 
-
 
19
/////////////////////////////////////////
-
 
20
// STATIC FUNCTIONS
-
 
21
bool CDirIO::Exists(const Utils::String &dir)
-
 
22
{
-
 
23
	return CDirIO(dir).exists();
-
 
24
}
-
 
25
 
-
 
26
 
-
 
27
////////////////////////////////////////
-
 
28
// Ctor/Dtor
-
 
29
 
19
CDirIO::CDirIO()
30
CDirIO::CDirIO()
20
{
31
{
21
}
32
}
22
 
33
 
23
CDirIO::~CDirIO()
34
CDirIO::~CDirIO()
Line 32... Line 43...
32
CDirIO::CDirIO ( CFileIO *file )
43
CDirIO::CDirIO ( CFileIO *file )
33
{
44
{
34
	SetDir(file->dir());
45
	SetDir(file->dir());
35
}
46
}
36
 
47
 
-
 
48
/////////////////////////////////////////////////////////
-
 
49
//
37
 
50
 
38
void CDirIO::SetDir(CyString dir)
51
void CDirIO::SetDir(CyString dir)
39
{
52
{
40
	m_sCurrentDir = dir.ToString();
53
	m_sCurrentDir = dir.ToString();
41
	m_sCurrentDir.toFilename();
54
	m_sCurrentDir.toFilename();
Line 55... Line 68...
55
	return false;
68
	return false;
56
}
69
}
57
 
70
 
58
bool CDirIO::exists(const Utils::String &dir) const
71
bool CDirIO::exists(const Utils::String &dir) const
59
{
72
{
60
	Utils::String d = parseDir(dir);
73
	Utils::String d = _parseDir(dir);
61
 
74
 
62
	if (d.empty())
75
	if (d.empty())
63
		return false;
76
		return false;
64
 
77
 
65
	if (ACCESS(d.c_str(), 0) == 0)
78
	if (ACCESS(d.c_str(), 0) == 0)
66
		return true;
79
		return true;
67
 
80
 
68
	return false;
81
	return false;
69
}
82
}
70
 
83
 
71
bool CDirIO::Exists(CyString dir)
-
 
72
{
-
 
73
	dir = ParseDir(dir);
-
 
74
 
-
 
75
	if (dir.Empty())
-
 
76
		return false;
-
 
77
 
-
 
78
	if (ACCESS(dir.c_str(), 0) == 0)
-
 
79
		return true;
-
 
80
 
-
 
81
	return false;
-
 
82
}
-
 
83
 
-
 
84
Utils::String CDirIO::parseDir(const Utils::String &dir) const
84
Utils::String CDirIO::_parseDir(const Utils::String &dir) const
85
{
85
{
86
	Utils::String sDir = dir.asFilename();
86
	Utils::String sDir = dir.asFilename();
87
	if ( !m_sCurrentDir.empty() && !sDir.isin(":") )
87
	if ( !m_sCurrentDir.empty() && !sDir.isin(":") )
88
	{
88
	{
89
		if ( sDir.empty() )
89
		if ( sDir.empty() )
Line 93... Line 93...
93
	}
93
	}
94
 
94
 
95
	return sDir.asFilename();
95
	return sDir.asFilename();
96
}
96
}
97
 
97
 
98
CyString CDirIO::ParseDir(CyString dir)
-
 
99
{
-
 
100
	dir = dir.FindReplace("\\", "/");
-
 
101
	if ( !m_sCurrentDir.empty() && !dir.IsIn(":") )
-
 
102
	{
-
 
103
		if ( dir.Empty() )
-
 
104
			dir = m_sCurrentDir;
-
 
105
		else
-
 
106
			dir = CyString(m_sCurrentDir) + "/" + dir;
-
 
107
	}
-
 
108
 
-
 
109
	dir = dir.FindReplace("//", "/");
-
 
110
	return dir;
-
 
111
}
-
 
112
 
-
 
113
bool CDirIO::isDir(const Utils::String &sDir) const
98
bool CDirIO::isDir(const Utils::String &sDir) const
114
{
99
{
115
	Utils::String dir = parseDir(sDir);
100
	Utils::String dir = _parseDir(sDir);
116
	if (ACCESS(dir.c_str(), 0) != -1)
101
	if (ACCESS(dir.c_str(), 0) != -1)
117
	{
102
	{
118
		struct stat status;
103
		struct stat status;
119
		stat(dir.c_str(), &status);
104
		stat(dir.c_str(), &status);
120
 
105
 
Line 131... Line 116...
131
bool CDirIO::isDir() const
116
bool CDirIO::isDir() const
132
{
117
{
133
	return isDir(m_sCurrentDir);
118
	return isDir(m_sCurrentDir);
134
}
119
}
135
 
120
 
136
bool CDirIO::IsDir(CyString sDir)
-
 
137
{
-
 
138
	Utils::String dir = parseDir(sDir.ToString());
-
 
139
	if ( ACCESS( dir.c_str(), 0 ) != -1 )
-
 
140
	{
-
 
141
		struct stat status;
-
 
142
		stat( dir.c_str(), &status );
-
 
143
 
-
 
144
		if ( status.st_mode & S_IFDIR )
-
 
145
			return true;
-
 
146
	}
-
 
147
	else {
-
 
148
		return !dir.token("/", -1).isin(".");
-
 
149
	}
-
 
150
 
-
 
151
	return false;
-
 
152
}
-
 
153
 
-
 
154
bool CDirIO::isFile() const
121
bool CDirIO::isFile() const
155
{
122
{
156
	return isFile(m_sCurrentDir);
123
	return isFile(m_sCurrentDir);
157
}
124
}
158
bool CDirIO::isFile(const Utils::String &sDir) const
125
bool CDirIO::isFile(const Utils::String &sDir) const
159
{
126
{
160
	Utils::String dir = parseDir(sDir);
127
	Utils::String dir = _parseDir(sDir);
161
	if (ACCESS(dir.c_str(), 0) != -1)
128
	if (ACCESS(dir.c_str(), 0) != -1)
162
	{
129
	{
163
		struct stat status;
130
		struct stat status;
164
		stat(dir.c_str(), &status);
131
		stat(dir.c_str(), &status);
165
 
132
 
Line 172... Line 139...
172
		return dir.token("/", -1).isin(".");
139
		return dir.token("/", -1).isin(".");
173
	}
140
	}
174
 
141
 
175
	return false;
142
	return false;
176
}
143
}
177
bool CDirIO::IsFile(CyString sDir)
-
 
178
{
-
 
179
	Utils::String dir = parseDir(sDir.ToString());
-
 
180
	if ( ACCESS( dir.c_str(), 0 ) != -1 )
-
 
181
	{
-
 
182
		struct stat status;
-
 
183
		stat( dir.c_str(), &status );
-
 
184
 
-
 
185
		if ( status.st_mode & S_IFDIR )
-
 
186
			return false;
-
 
187
		else
-
 
188
			return true;
-
 
189
	}
-
 
190
	else {
-
 
191
		return dir.token("/", -1).isin(".");
-
 
192
	}
-
 
193
 
-
 
194
	return false;
-
 
195
}
-
 
196
 
144
 
197
bool CDirIO::Create(CyString sDir)
145
bool CDirIO::Create(CyString sDir)
198
{
146
{
199
	Utils::String dir = sDir.ToString();
147
	Utils::String dir = sDir.ToString();
200
	if ( dir.empty() )
148
	if ( dir.empty() )
201
		dir = m_sCurrentDir;
149
		dir = m_sCurrentDir;
202
	dir = parseDir(dir);
150
	dir = _parseDir(dir);
203
 
151
 
204
	// split up directorys
152
	// split up directorys
205
	int max = 0;
153
	int max = 0;
206
	Utils::String *dirs = dir.findReplace( "/", "\\" ).findReplace( "\\\\", "\\" ).tokenise("\\", &max );
154
	Utils::String *dirs = dir.findReplace( "/", "\\" ).findReplace( "\\\\", "\\" ).tokenise("\\", &max );
207
 
155
 
Line 243... Line 191...
243
	CLEANSPLIT(dirs, max);
191
	CLEANSPLIT(dirs, max);
244
 
192
 
245
	return true;
193
	return true;
246
}
194
}
247
 
195
 
248
bool CDirIO::Move(CyString from, CyString to)
196
bool CDirIO::move(const Utils::String &sTo)
249
{
197
{
250
	from = ParseDir(from);
-
 
251
	to = ParseDir(to);
198
	Utils::String to = _parseDir(sTo);
252
 
199
 
-
 
200
	if (exists(to))
-
 
201
		return false;
-
 
202
 
-
 
203
	if (!rename(m_sCurrentDir.c_str(), to.c_str()))
-
 
204
	{
-
 
205
		m_sCurrentDir = to;
-
 
206
		return true;
-
 
207
	}
-
 
208
 
-
 
209
	return false;
-
 
210
}
-
 
211
 
-
 
212
 
-
 
213
bool CDirIO::Move(CyString sFrom, CyString sTo)
-
 
214
{
-
 
215
	Utils::String from = _parseDir(sFrom.ToString());
-
 
216
	Utils::String to = _parseDir(sTo.ToString());
-
 
217
 
253
	if ( !Exists(to) )
218
	if ( !exists(to) )
254
	{
219
	{
255
		if ( !Create(to) )
220
		if ( !Create(to) )
256
			return false;
221
			return false;
257
	}
222
	}
258
 
223
 
Line 289... Line 254...
289
{
254
{
290
	// check if the dir is empty
255
	// check if the dir is empty
291
	CyStringList *dirList = DirList(dir);
256
	CyStringList *dirList = DirList(dir);
292
	if ( CheckEmptyDir(dirList) )
257
	if ( CheckEmptyDir(dirList) )
293
	{
258
	{
294
		CyString remDir = ParseDir(dir);
259
		CyString remDir = _parseDir(dir.ToString());
295
		#ifdef _WIN32
260
		#ifdef _WIN32
296
		if ( _rmdir(remDir.c_str()) == 0 )
261
		if ( _rmdir(remDir.c_str()) == 0 )
297
		#else
262
		#else
298
		if ( rmdir(remDir.c_str()) == 0 )
263
		if ( rmdir(remDir.c_str()) == 0 )
299
		#endif
264
		#endif
Line 312... Line 277...
312
			CyString d = str->str;
277
			CyString d = str->str;
313
			if ( d == "." || d == ".." )
278
			if ( d == "." || d == ".." )
314
				continue;
279
				continue;
315
 
280
 
316
			// if its a file
281
			// if its a file
317
			CyString fullFile = dir + "\\" + d;
282
			Utils::String fullFile = (dir + "\\" + d).ToString();
318
			if ( doFiles && IsFile(fullFile) )
283
			if ( doFiles && isFile(fullFile) )
319
			{
284
			{
320
				CyString remFile = ParseDir(fullFile);
285
				CyString remFile = _parseDir(fullFile);
321
				if ( remove(remFile.c_str()) == 0 )
286
				if ( remove(remFile.c_str()) == 0 )
322
				{
287
				{
323
					if ( errors )
288
					if ( errors )
324
						errors->PushBack(remFile);
289
						errors->PushBack(remFile);
325
				}
290
				}
326
			}
291
			}
327
			else if ( recursive && IsDir(fullFile) )
292
			else if ( recursive && isDir(fullFile) )
328
				RemoveDir(fullFile, doFiles, recursive, errors);
293
				RemoveDir(fullFile, doFiles, recursive, errors);
329
		}
294
		}
330
	}
295
	}
331
 
296
 
332
	// now check if its empty
297
	// now check if its empty
333
	delete dirList;
298
	delete dirList;
334
	dirList = DirList(dir);
299
	dirList = DirList(dir);
335
	if ( CheckEmptyDir(dirList) )
300
	if ( CheckEmptyDir(dirList) )
336
	{
301
	{
337
		CyString remDir = ParseDir(dir);
302
		CyString remDir = _parseDir(dir.ToString());
338
		#ifdef _WIN32
303
		#ifdef _WIN32
339
		if ( _rmdir(remDir.c_str()) == 0 )
304
		if ( _rmdir(remDir.c_str()) == 0 )
340
		#else
305
		#else
341
		if ( rmdir(remDir.c_str()) == 0 )
306
		if ( rmdir(remDir.c_str()) == 0 )
342
		#endif
307
		#endif
Line 356... Line 321...
356
	dirList(files, dir, filePattern);
321
	dirList(files, dir, filePattern);
357
	return files;
322
	return files;
358
}
323
}
359
bool CDirIO::dirList(Utils::CStringList &files, Utils::String dir, Utils::String filePattern) const
324
bool CDirIO::dirList(Utils::CStringList &files, Utils::String dir, Utils::String filePattern) const
360
{
325
{
361
	dir = parseDir(dir);
326
	dir = _parseDir(dir);
362
	if ( dir.empty() )
327
	if ( dir.empty() )
363
		return false;
328
		return false;
364
 
329
 
365
	dir = dir.findReplace("\\", "/");
330
	dir = dir.findReplace("\\", "/");
366
	if (filePattern.empty())
331
	if (filePattern.empty())
Line 419... Line 384...
419
 
384
 
420
}
385
}
421
 
386
 
422
CyStringList *CDirIO::DirList(CyString dir, CyString filepattern)
387
CyStringList *CDirIO::DirList(CyString dir, CyString filepattern)
423
{
388
{
424
	dir = ParseDir(dir);
389
	dir = _parseDir(dir.ToString());
425
	if ( dir.Empty() )
390
	if ( dir.Empty() )
426
		return 0;
391
		return 0;
427
 
392
 
428
	CyStringList *files = new CyStringList;
393
	CyStringList *files = new CyStringList;
429
 
394
 
Line 480... Line 445...
480
	return m_sCurrentDir + "/" + filename;
445
	return m_sCurrentDir + "/" + filename;
481
}
446
}
482
 
447
 
483
Utils::String CDirIO::dir(const Utils::String &sDir) const
448
Utils::String CDirIO::dir(const Utils::String &sDir) const
484
{
449
{
485
	return parseDir(sDir);
450
	return _parseDir(sDir);
486
}
451
}
487
 
452
 
488
const Utils::String &CDirIO::dir() const
453
const Utils::String &CDirIO::dir() const
489
{
454
{
490
	return m_sCurrentDir;
455
	return m_sCurrentDir;
491
}
456
}
492
 
457
 
493
CyString CDirIO::Dir(CyString dir)
458
CyString CDirIO::Dir(CyString dir)
494
{
459
{
495
	return parseDir(dir.ToString());
460
	return _parseDir(dir.ToString());
496
}
461
}
497
 
462
 
498
bool CDirIO::cd(CyString dir)
463
bool CDirIO::cd(CyString dir)
499
{
464
{
500
	Utils::String sDir = dir.ToString();
465
	Utils::String sDir = dir.ToString();
Line 522... Line 487...
522
		return Utils::String("");
487
		return Utils::String("");
523
 
488
 
524
	return m_sCurrentDir.token("/", -1);
489
	return m_sCurrentDir.token("/", -1);
525
}
490
}
526
 
491
 
527
CyString CDirIO::Back()
492
const Utils::String &CDirIO::moveBack()
528
{
493
{
529
	m_sCurrentDir = m_sCurrentDir.tokens("/", 1, -2);
494
	m_sCurrentDir = m_sCurrentDir.tokens("/", 1, -2);
530
	
-
 
531
	return m_sCurrentDir;
495
	return m_sCurrentDir;
532
}
-
 
533
496
}
-
 
497
Utils::String CDirIO::back() const
-
 
498
{
-
 
499
	return m_sCurrentDir.tokens("/", 1, -2);
-
 
500
}
-
 
501