Subversion Repositories spk

Rev

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

Rev 119 Rev 121
Line 10... Line 10...
10
#include <windows.h>
10
#include <windows.h>
11
#include <io.h>
11
#include <io.h>
12
#include <direct.h>
12
#include <direct.h>
13
#define ACCESS _access
13
#define ACCESS _access
14
#else
14
#else
-
 
15
#include <dirent.h>
15
#define ACCESS access
16
#define ACCESS access
16
#endif
17
#endif
17
 
18
 
18
CDirIO::CDirIO()
19
CDirIO::CDirIO()
19
{
20
{
Line 39... Line 40...
39
	m_sCurrentDir = dir.ToString();
40
	m_sCurrentDir = dir.ToString();
40
	m_sCurrentDir.toFilename();
41
	m_sCurrentDir.toFilename();
41
}
42
}
42
 
43
 
43
 
44
 
-
 
45
bool CDirIO::exists() const
-
 
46
{
-
 
47
	Utils::String dir = m_sCurrentDir;
-
 
48
 
-
 
49
	if (dir.empty())
-
 
50
		return false;
-
 
51
 
-
 
52
	if (ACCESS(dir.c_str(), 0) == 0)
-
 
53
		return true;
-
 
54
 
-
 
55
	return false;
-
 
56
}
-
 
57
 
-
 
58
bool CDirIO::exists(const Utils::String &dir) const
-
 
59
{
-
 
60
	Utils::String d = parseDir(dir);
-
 
61
 
-
 
62
	if (d.empty())
-
 
63
		return false;
-
 
64
 
-
 
65
	if (ACCESS(d.c_str(), 0) == 0)
-
 
66
		return true;
-
 
67
 
-
 
68
	return false;
-
 
69
}
-
 
70
 
44
bool CDirIO::Exists(CyString dir)
71
bool CDirIO::Exists(CyString dir)
45
{
72
{
46
	dir = ParseDir(dir);
73
	dir = ParseDir(dir);
47
 
74
 
48
	if ( dir.Empty() )
75
	if (dir.Empty())
49
		return false;
76
		return false;
50
 
77
 
51
	if ( ACCESS( dir.c_str(), 0 ) == 0 )
78
	if (ACCESS(dir.c_str(), 0) == 0)
52
		return true;
79
		return true;
53
 
80
 
54
	return false;
81
	return false;
55
}
82
}
56
 
83
 
Line 81... Line 108...
81
 
108
 
82
	dir = dir.FindReplace("//", "/");
109
	dir = dir.FindReplace("//", "/");
83
	return dir;
110
	return dir;
84
}
111
}
85
 
112
 
-
 
113
bool CDirIO::isDir(const Utils::String &sDir) const
-
 
114
{
-
 
115
	Utils::String dir = parseDir(sDir);
-
 
116
	if (ACCESS(dir.c_str(), 0) != -1)
-
 
117
	{
-
 
118
		struct stat status;
-
 
119
		stat(dir.c_str(), &status);
-
 
120
 
-
 
121
		if (status.st_mode & S_IFDIR)
-
 
122
			return true;
-
 
123
	}
-
 
124
	else {
-
 
125
		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
 
86
bool CDirIO::IsDir(CyString sDir)
136
bool CDirIO::IsDir(CyString sDir)
87
{
137
{
88
	Utils::String dir = parseDir(sDir.ToString());
138
	Utils::String dir = parseDir(sDir.ToString());
89
	if ( ACCESS( dir.c_str(), 0 ) != -1 )
139
	if ( ACCESS( dir.c_str(), 0 ) != -1 )
90
	{
140
	{
Line 99... Line 149...
99
	}
149
	}
100
 
150
 
101
	return false;
151
	return false;
102
}
152
}
103
 
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
	}
-
 
174
 
-
 
175
	return false;
-
 
176
}
104
bool CDirIO::IsFile(CyString sDir)
177
bool CDirIO::IsFile(CyString sDir)
105
{
178
{
106
	Utils::String dir = parseDir(sDir.ToString());
179
	Utils::String dir = parseDir(sDir.ToString());
107
	if ( ACCESS( dir.c_str(), 0 ) != -1 )
180
	if ( ACCESS( dir.c_str(), 0 ) != -1 )
108
	{
181
	{
Line 119... Line 192...
119
	}
192
	}
120
 
193
 
121
	return false;
194
	return false;
122
}
195
}
123
 
196
 
124
bool CDirIO::Create(CyString dir)
197
bool CDirIO::Create(CyString sDir)
125
{
198
{
-
 
199
	Utils::String dir = sDir.ToString();
126
	if ( dir.Empty() )
200
	if ( dir.empty() )
127
		dir = m_sCurrentDir;
201
		dir = m_sCurrentDir;
128
	dir = ParseDir(dir);
202
	dir = parseDir(dir);
129
 
203
 
130
	// split up directorys
204
	// split up directorys
131
	int max = 0;
205
	int max = 0;
132
	CyString *dirs = dir.FindReplace ( "/", "\\" ).FindReplace ( "\\\\", "\\" ).SplitToken ( '\\', &max );
206
	Utils::String *dirs = dir.findReplace( "/", "\\" ).findReplace( "\\\\", "\\" ).tokenise("\\", &max );
133
 
207
 
134
	// check if full dir, or relative
208
	// check if full dir, or relative
135
	int start = 1;
209
	int start = 1;
136
	CyString curDir;
210
	Utils::String curDir;
137
	
211
	
138
	if ( dirs && max )
212
	if ( dirs && max )
139
		curDir = dirs[0];
213
		curDir = dirs[0];
140
 
214
 
141
	if ( !curDir.IsIn(":") )
215
	if ( !curDir.isin(":") )
142
	{
216
	{
143
		curDir = m_sCurrentDir;
217
		curDir = m_sCurrentDir;
144
		start = 0;
218
		start = 0;
145
	}
219
	}
146
 
220
 
147
	// process each dir
221
	// process each dir
148
	for ( int i = start; i < max; i++ )
222
	for ( int i = start; i < max; i++ )
149
	{
223
	{
150
		if ( !curDir.Empty() )
224
		if ( !curDir.empty() )
151
			curDir += "/";
225
			curDir += "/";
152
		curDir += dirs[i];
226
		curDir += dirs[i];
153
 
227
 
154
		// check if the directory exists
228
		// check if the directory exists
155
		if ( !Exists(curDir) )
229
		if ( !exists(curDir) )
156
		{
230
		{
157
#ifdef _WIN32
231
#ifdef _WIN32
158
			if ( _mkdir(curDir.c_str()) )
232
			if ( _mkdir(curDir.c_str()) )
159
#else
233
#else
160
			if ( mkdir(curDir.c_str(), 0755) )
234
			if ( mkdir(curDir.c_str(), 0755) )
Line 274... Line 348...
274
	}
348
	}
275
 
349
 
276
	return false;
350
	return false;
277
}
351
}
278
 
352
 
279
Utils::CStringList *CDirIO::dirList(Utils::String dir, Utils::String filePattern)
353
Utils::CStringList CDirIO::dirList(Utils::String dir, Utils::String filePattern) const
280
{
354
{
-
 
355
	Utils::CStringList files;
-
 
356
 
281
	dir = parseDir(dir);
357
	dir = parseDir(dir);
282
	if ( dir.empty() )
358
	if ( dir.empty() )
283
		return 0;
359
		return files;
284
 
-
 
285
	Utils::CStringList *files = new Utils::CStringList();
-
 
286
 
360
 
287
#ifdef _WIN32
-
 
288
	dir = dir.findReplace("/", "\\");
361
	dir = dir.findReplace("\\", "/");
289
	if ( filePattern.empty() )
362
	if (filePattern.empty())
290
		dir += "\\*";
363
		dir += "/*";
291
	else
364
	else
292
	{
365
	{
293
		dir += "\\";
366
		dir += "/";
294
		dir += filePattern;
367
		dir += filePattern;
295
	}
368
	}
-
 
369
	dir = dir.findReplace("//", "/");
-
 
370
 
-
 
371
#ifdef _WIN32
296
	dir = dir.findReplace("\\\\", "\\");
372
	dir = dir.findReplace("/", "\\");
297
 
373
 
298
	WIN32_FIND_DATA data;
374
	WIN32_FIND_DATA data;
299
	TCHAR buf[5000];
375
	TCHAR buf[5000];
300
	wsprintf(buf, L"%hs", dir.c_str());
376
	wsprintf(buf, L"%hs", dir.c_str());
301
 
377
 
Line 305... Line 381...
305
		std::wstring ws(data.cFileName);
381
		std::wstring ws(data.cFileName);
306
		std::string s(ws.begin(), ws.end());
382
		std::string s(ws.begin(), ws.end());
307
 
383
 
308
		Utils::String checkFile(s);
384
		Utils::String checkFile(s);
309
		if ( !checkFile.Compare(".") && !checkFile.Compare("..") )
385
		if ( !checkFile.Compare(".") && !checkFile.Compare("..") )
310
			files->pushBack(checkFile);
386
			files.pushBack(checkFile);
311
 
387
 
312
		while ( FindNextFile(h, &data) )
388
		while ( FindNextFile(h, &data) )
313
		{
389
		{
314
			std::wstring ws(data.cFileName);
390
			std::wstring ws(data.cFileName);
315
			std::string s(ws.begin(), ws.end());
391
			std::string s(ws.begin(), ws.end());
316
 
392
 
317
			Utils::String checkFile(s);
393
			Utils::String checkFile(s);
318
			if ( checkFile != "." && checkFile != ".." )
394
			if ( checkFile != "." && checkFile != ".." )
319
				files->pushBack(checkFile);
395
				files.pushBack(checkFile);
320
		}
396
		}
321
 
397
 
322
		FindClose(h);
398
		FindClose(h);
323
	}
399
	}
324
#else
400
#else
-
 
401
	DIR *dir;
-
 
402
	struct dirent *ent;
-
 
403
	if ((dir = opendir(dir.c_str())) != NULL) {
-
 
404
		while ((ent = readdir(dir)) != NULL) {
-
 
405
			Utils::String checkFile(ent->d_name);
-
 
406
			if (checkFile != "." && checkFile != "..")
-
 
407
				files.pushBack(checkFile);
-
 
408
		}
-
 
409
		closedir(dir);
325
 
410
	}
326
#endif//_WIN32
411
#endif//_WIN32
327
 
412
 
328
	return files;
413
	return files;
329
}
414
}
330
 
415
 
Line 373... Line 458...
373
	return files;
458
	return files;
374
}
459
}
375
 
460
 
376
CyString CDirIO::File(CyString filename)
461
CyString CDirIO::File(CyString filename)
377
{
462
{
378
	if ( m_sCurrentDir.empty() )
463
	if (m_sCurrentDir.empty())
379
		return filename;
464
		return filename;
380
 
465
 
381
	return CyString(m_sCurrentDir) + "/" + filename;
466
	return CyString(m_sCurrentDir) + "/" + filename;
382
}
467
}
383
 
468
 
-
 
469
Utils::String CDirIO::file(const Utils::String &filename) const
-
 
470
{
-
 
471
	if (m_sCurrentDir.empty())
-
 
472
		return filename;
-
 
473
 
-
 
474
	return m_sCurrentDir + "/" + filename;
-
 
475
}
-
 
476
 
384
Utils::String CDirIO::dir(const Utils::String &sDir) const
477
Utils::String CDirIO::dir(const Utils::String &sDir) const
385
{
478
{
386
	return parseDir(sDir);
479
	return parseDir(sDir);
387
}
480
}
388
 
481
 
Line 404... Line 497...
404
		m_sCurrentDir = sDir;
497
		m_sCurrentDir = sDir;
405
	else
498
	else
406
		m_sCurrentDir = m_sCurrentDir + "/" + sDir;
499
		m_sCurrentDir = m_sCurrentDir + "/" + sDir;
407
	m_sCurrentDir = m_sCurrentDir.asFilename();
500
	m_sCurrentDir = m_sCurrentDir.asFilename();
408
 
501
 
409
	return Exists();
502
	return exists();
410
}
503
}
411
 
504
 
412
bool CDirIO::CreateAndChange(CyString dir)
505
bool CDirIO::CreateAndChange(CyString dir)
413
{
506
{
414
	if ( Create(dir) )
507
	if ( Create(dir) )
415
		return cd(dir);
508
		return cd(dir);
416
	return false;
509
	return false;
417
}
510
}
418
 
511
 
419
 
512
 
420
CyString CDirIO::TopDir()
513
Utils::String CDirIO::topDir() const
421
{
514
{
422
	if ( m_sCurrentDir.empty() )
515
	if ( m_sCurrentDir.empty() )
423
		return NullString;
516
		return Utils::String("");
424
 
517
 
425
	return m_sCurrentDir.token("/", -1);
518
	return m_sCurrentDir.token("/", -1);
426
}
519
}
427
 
520
 
428
CyString CDirIO::Back()
521
CyString CDirIO::Back()