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 38... Line 39...
38
{
39
{
39
	m_sCurrentDir = dir.ToString();
40
	m_sCurrentDir = dir.ToString();
40
	m_sCurrentDir.toFilename();
41
	m_sCurrentDir.toFilename();
41
}
42
}
42
 
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
}
43
 
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
 
57
Utils::String CDirIO::parseDir(const Utils::String &dir) const
84
Utils::String CDirIO::parseDir(const Utils::String &dir) const
58
{
85
{
59
	Utils::String sDir = dir.asFilename();
86
	Utils::String sDir = dir.asFilename();
60
	if ( !m_sCurrentDir.empty() && !sDir.isin(":") )
87
	if ( !m_sCurrentDir.empty() && !sDir.isin(":") )
Line 65... Line 92...
65
			sDir = m_sCurrentDir + "/" + sDir;
92
			sDir = m_sCurrentDir + "/" + sDir;
66
	}
93
	}
67
 
94
 
68
	return sDir.asFilename();
95
	return sDir.asFilename();
69
}
96
}
70
 
97
 
71
CyString CDirIO::ParseDir(CyString dir)
98
CyString CDirIO::ParseDir(CyString dir)
72
{
99
{
73
	dir = dir.FindReplace("\\", "/");
100
	dir = dir.FindReplace("\\", "/");
74
	if ( !m_sCurrentDir.empty() && !dir.IsIn(":") )
101
	if ( !m_sCurrentDir.empty() && !dir.IsIn(":") )
75
	{
102
	{
Line 79... Line 106...
79
			dir = CyString(m_sCurrentDir) + "/" + dir;
106
			dir = CyString(m_sCurrentDir) + "/" + dir;
80
	}
107
	}
81
 
108
 
82
	dir = dir.FindReplace("//", "/");
109
	dir = dir.FindReplace("//", "/");
83
	return dir;
110
	return dir;
84
}
111
}
85
 
112
 
86
bool CDirIO::IsDir(CyString sDir)
113
bool CDirIO::isDir(const Utils::String &sDir) const
87
{
114
{
88
	Utils::String dir = parseDir(sDir.ToString());
115
	Utils::String dir = parseDir(sDir);
89
	if ( ACCESS( dir.c_str(), 0 ) != -1 )
116
	if (ACCESS(dir.c_str(), 0) != -1)
90
	{
117
	{
91
		struct stat status;
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
 
-
 
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;
92
		stat( dir.c_str(), &status );
142
		stat( dir.c_str(), &status );
93
 
143
 
94
		if ( status.st_mode & S_IFDIR )
144
		if ( status.st_mode & S_IFDIR )
95
			return true;
145
			return true;
96
	}
146
	}
97
	else {
147
	else {
98
		return !dir.token("/", -1).isin(".");
148
		return !dir.token("/", -1).isin(".");
99
	}
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
	}
100
 
174
 
101
	return false;
175
	return false;
102
}
176
}
103
 
-
 
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
	{
109
		struct stat status;
182
		struct stat status;
110
		stat( dir.c_str(), &status );
183
		stat( dir.c_str(), &status );
111
 
184
 
112
		if ( status.st_mode & S_IFDIR )
185
		if ( status.st_mode & S_IFDIR )
113
			return false;
186
			return false;
114
		else
187
		else
115
			return true;
188
			return true;
116
	}
189
	}
117
	else {
190
	else {
118
		return dir.token("/", -1).isin(".");
191
		return dir.token("/", -1).isin(".");
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 ( &quot;/", "\\" ).FindReplace ( ";\\\\", "\\" ).SplitToken ( &apos;\\&apos;, &max );
206
	Utils::String *dirs = dir.findReplace( &quot;/", ";\\" ).findReplace( "\\\\", ";\\&quot; ).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) )
161
#endif
235
#endif
162
			{
236
			{
163
				CLEANSPLIT(dirs, max);
237
				CLEANSPLIT(dirs, max);
164
				return false;
238
				return false;
165
			}
239
			}
166
		}
240
		}
167
	}
241
	}
168
 
242
 
169
	CLEANSPLIT(dirs, max);
243
	CLEANSPLIT(dirs, max);
170
 
244
 
171
	return true;
245
	return true;
172
}
246
}
173
 
247
 
174
bool CDirIO::Move(CyString from, CyString to)
248
bool CDirIO::Move(CyString from, CyString to)
175
{
249
{
176
	from = ParseDir(from);
250
	from = ParseDir(from);
177
	to = ParseDir(to);
251
	to = ParseDir(to);
178
 
252
 
179
	if ( !Exists(to) )
253
	if ( !Exists(to) )
180
	{
254
	{
181
		if ( !Create(to) )
255
		if ( !Create(to) )
182
			return false;
256
			return false;
183
	}
257
	}
184
 
258
 
185
	if ( !rename(from.c_str(), to.c_str()) )
259
	if ( !rename(from.c_str(), to.c_str()) )
186
		return true;
260
		return true;
187
 
261
 
188
	return false;
262
	return false;
Line 207... Line 281...
207
		// found something
281
		// found something
208
		return false;
282
		return false;
209
	}
283
	}
210
 
284
 
211
	return true;
285
	return true;
212
}
286
}
213
 
287
 
214
bool CDirIO::RemoveDir(CyString dir, bool doFiles, bool recursive, CyStringList *errors)
288
bool CDirIO::RemoveDir(CyString dir, bool doFiles, bool recursive, CyStringList *errors)
215
{
289
{
216
	// check if the dir is empty
290
	// check if the dir is empty
217
	CyStringList *dirList = DirList(dir);
291
	CyStringList *dirList = DirList(dir);
218
	if ( CheckEmptyDir(dirList) )
292
	if ( CheckEmptyDir(dirList) )
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
 
360
 
285
	Utils::CStringList *files = new Utils::CStringList();
-
 
286
 
-
 
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
 
302
	HANDLE h = FindFirstFile(buf, &data);
378
	HANDLE h = FindFirstFile(buf, &data);
303
	if ( h != INVALID_HANDLE_VALUE)
379
	if ( h != INVALID_HANDLE_VALUE)
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())
-
 
464
		return filename;
-
 
465
 
-
 
466
	return CyString(m_sCurrentDir) + "/" + filename;
-
 
467
}
-
 
468
 
-
 
469
Utils::String CDirIO::file(const Utils::String &filename) const
-
 
470
{
-
 
471
	if (m_sCurrentDir.empty())
379
		return filename;
472
		return filename;
380
 
473
 
381
	return CyString(m_sCurrentDir) + "/" + filename;
474
	return m_sCurrentDir + "/" + filename;
382
}
475
}
383
 
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);
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()