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 13... Line 13...
13
#define ACCESS _access
13
#define ACCESS _access
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
 
-
 
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
18
 
29
 
19
CDirIO::CDirIO()
30
CDirIO::CDirIO()
20
{
31
{
21
}
32
}
22
 
33
 
23
CDirIO::~CDirIO()
34
CDirIO::~CDirIO()
24
{
35
{
25
}
36
}
26
 
37
 
27
CDirIO::CDirIO(CyString dir)
38
CDirIO::CDirIO(CyString dir)
28
{
39
{
29
	SetDir(dir);
40
	SetDir(dir);
30
}
41
}
31
 
42
 
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 48... Line 61...
48
 
61
 
49
	if (dir.empty())
62
	if (dir.empty())
50
		return false;
63
		return false;
51
 
64
 
52
	if (ACCESS(dir.c_str(), 0) == 0)
65
	if (ACCESS(dir.c_str(), 0) == 0)
53
		return true;
66
		return true;
54
 
67
 
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
 
-
 
68
	return false;
-
 
69
}
-
 
70
 
-
 
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
 
80
 
81
	return false;
81
	return false;
82
}
82
}
83
 
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() )
90
			sDir = m_sCurrentDir;
90
			sDir = m_sCurrentDir;
91
		else
91
		else
92
			sDir = m_sCurrentDir + "/" + sDir;
92
			sDir = m_sCurrentDir + "/" + sDir;
93
	}
93
	}
94
 
94
 
95
	return sDir.asFilename();
95
	return sDir.asFilename();
96
}
-
 
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
}
96
}
112
 
97
 
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
 
121
		if (status.st_mode & S_IFDIR)
106
		if (status.st_mode & S_IFDIR)
122
			return true;
107
			return true;
123
	}
108
	}
124
	else {
109
	else {
125
		return !dir.token("/", -1).isin(".");
110
		return !dir.token("/", -1).isin(".");
126
	}
-
 
127
 
-
 
128
	return false;
-
 
129
}
-
 
130
 
-
 
131
bool CDirIO::isDir() const
-
 
132
{
-
 
133
	return isDir(m_sCurrentDir);
-
 
134
}
-
 
135
 
-
 
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
-
 
155
{
-
 
156
	return isFile(m_sCurrentDir);
-
 
157
}
-
 
158
bool CDirIO::isFile(const Utils::String &sDir) const
-
 
159
{
-
 
160
	Utils::String dir = parseDir(sDir);
-
 
161
	if (ACCESS(dir.c_str(), 0) != -1)
-
 
162
	{
-
 
163
		struct stat status;
-
 
164
		stat(dir.c_str(), &status);
-
 
165
 
-
 
166
		if (status.st_mode & S_IFDIR)
-
 
167
			return false;
-
 
168
		else
-
 
169
			return true;
-
 
170
	}
-
 
171
	else {
-
 
172
		return dir.token("/", -1).isin(".");
-
 
173
	}
111
	}
174
 
112
 
175
	return false;
113
	return false;
176
}
114
}
-
 
115
 
-
 
116
bool CDirIO::isDir() const
-
 
117
{
-
 
118
	return isDir(m_sCurrentDir);
-
 
119
}
-
 
120
 
177
bool CDirIO::IsFile(CyString sDir)
121
bool CDirIO::isFile() const
-
 
122
{
-
 
123
	return isFile(m_sCurrentDir);
-
 
124
}
-
 
125
bool CDirIO::isFile(const Utils::String &sDir) const
178
{
126
{
179
	Utils::String dir = parseDir(sDir.ToString());
127
	Utils::String dir = _parseDir(sDir);
180
	if ( ACCESS( dir.c_str(), 0 ) != -1 )
128
	if (ACCESS(dir.c_str(), 0) != -1)
181
	{
129
	{
182
		struct stat status;
130
		struct stat status;
183
		stat( dir.c_str(), &status );
131
		stat(dir.c_str(), &status);
184
 
132
 
185
		if ( status.st_mode & S_IFDIR )
133
		if (status.st_mode & S_IFDIR)
186
			return false;
134
			return false;
187
		else
135
		else
188
			return true;
136
			return true;
189
	}
137
	}
190
	else {
138
	else {
191
		return dir.token("/", -1).isin(".");
139
		return dir.token("/", -1).isin(".");
192
	}
140
	}
193
 
141
 
194
	return false;
142
	return false;
195
}
143
}
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 222... Line 170...
222
	for ( int i = start; i < max; i++ )
170
	for ( int i = start; i < max; i++ )
223
	{
171
	{
224
		if ( !curDir.empty() )
172
		if ( !curDir.empty() )
225
			curDir += "/";
173
			curDir += "/";
226
		curDir += dirs[i];
174
		curDir += dirs[i];
227
 
175
 
228
		// check if the directory exists
176
		// check if the directory exists
229
		if ( !exists(curDir) )
177
		if ( !exists(curDir) )
230
		{
178
		{
231
#ifdef _WIN32
179
#ifdef _WIN32
232
			if ( _mkdir(curDir.c_str()) )
180
			if ( _mkdir(curDir.c_str()) )
Line 234... Line 182...
234
			if ( mkdir(curDir.c_str(), 0755) )
182
			if ( mkdir(curDir.c_str(), 0755) )
235
#endif
183
#endif
236
			{
184
			{
237
				CLEANSPLIT(dirs, max);
185
				CLEANSPLIT(dirs, max);
238
				return false;
186
				return false;
239
			}
187
			}
240
		}
188
		}
241
	}
189
	}
242
 
190
 
243
	CLEANSPLIT(dirs, max);
191
	CLEANSPLIT(dirs, max);
244
 
192
 
245
	return true;
193
	return true;
-
 
194
}
-
 
195
 
-
 
196
bool CDirIO::move(const Utils::String &sTo)
-
 
197
{
-
 
198
	Utils::String to = _parseDir(sTo);
-
 
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;
246
}
210
}
247
 
211
 
-
 
212
 
248
bool CDirIO::Move(CyString from, CyString to)
213
bool CDirIO::Move(CyString sFrom, CyString sTo)
249
{
214
{
250
	from = ParseDir(from);
215
	Utils::String from = _parseDir(sFrom.ToString());
251
	to = ParseDir(to);
216
	Utils::String to = _parseDir(sTo.ToString());
252
 
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
 
259
	if ( !rename(from.c_str(), to.c_str()) )
224
	if ( !rename(from.c_str(), to.c_str()) )
260
		return true;
225
		return true;
261
 
226
 
262
	return false;
227
	return false;
263
}
228
}
264
 
229
 
265
bool CDirIO::CheckEmptyDir(CyStringList *dirList)
230
bool CDirIO::CheckEmptyDir(CyStringList *dirList)
266
{
231
{
Line 287... Line 252...
287
 
252
 
288
bool CDirIO::RemoveDir(CyString dir, bool doFiles, bool recursive, CyStringList *errors)
253
bool CDirIO::RemoveDir(CyString dir, bool doFiles, bool recursive, CyStringList *errors)
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
300
		{
265
		{
301
			if ( errors )
266
			if ( errors )
302
				errors->PushBack(remDir);
267
				errors->PushBack(remDir);
303
		}
268
		}
304
		return true;
269
		return true;
305
	}
270
	}
306
 
271
 
307
	// not empty
272
	// not empty
308
	if ( doFiles || recursive )
273
	if ( doFiles || recursive )
309
	{
274
	{
310
		for ( SStringList *str = dirList->Head(); str; str = str->next )
275
		for ( SStringList *str = dirList->Head(); str; str = str->next )
311
		{
276
		{
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 352... Line 317...
352
 
317
 
353
Utils::CStringList CDirIO::dirList(Utils::String dir, Utils::String filePattern) const
318
Utils::CStringList CDirIO::dirList(Utils::String dir, Utils::String filePattern) const
354
{
319
{
355
	Utils::CStringList files;
320
	Utils::CStringList files;
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 468... Line 433...
468
{
433
{
469
	if (m_sCurrentDir.empty())
434
	if (m_sCurrentDir.empty())
470
		return filename;
435
		return filename;
471
 
436
 
472
	return CyString(m_sCurrentDir) + "/" + filename;
437
	return CyString(m_sCurrentDir) + "/" + filename;
473
}
438
}
474
 
439
 
475
Utils::String CDirIO::file(const Utils::String &filename) const
440
Utils::String CDirIO::file(const Utils::String &filename) const
476
{
441
{
477
	if (m_sCurrentDir.empty())
442
	if (m_sCurrentDir.empty())
478
		return filename;
443
		return filename;
479
 
444
 
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();
501
 
466
 
Line 511... Line 476...
511
bool CDirIO::CreateAndChange(CyString dir)
476
bool CDirIO::CreateAndChange(CyString dir)
512
{
477
{
513
	if ( Create(dir) )
478
	if ( Create(dir) )
514
		return cd(dir);
479
		return cd(dir);
515
	return false;
480
	return false;
516
}
481
}
517
 
482
 
518
 
483
 
519
Utils::String CDirIO::topDir() const
484
Utils::String CDirIO::topDir() const
520
{
485
{
521
	if ( m_sCurrentDir.empty() )
486
	if ( m_sCurrentDir.empty() )
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;
-
 
496
}
-
 
497
Utils::String CDirIO::back() const
-
 
498
{
-
 
499
	return m_sCurrentDir.tokens("/", 1, -2);
532
}
500
}