16 #include <sys/types.h>
22 typedef struct _stat
STAT;
24 # define S_IFREG _S_IFREG
25 # define S_IFDIR _S_IFDIR
26 # define access _waccess
27 # define rename _wrename
28 # define mkdir _wmkdir
29 # define rmdir _wrmdir
59 return ((st.st_mode & S_IFREG) == S_IFREG);
61 return S_ISREG(st.st_mode);
72 return ((st.st_mode & S_IFDIR) == S_IFDIR);
74 return S_ISDIR(st.st_mode);
85 return ((st.st_mode & S_IFREG) == S_IFREG ||
86 (st.st_mode & S_IFDIR) == S_IFDIR);
88 return (S_ISREG(st.st_mode) || S_ISDIR(st.st_mode));
100 std::string::size_type start = path.find_last_of(
Separator);
101 #ifdef WIN32 // WIN32 also understands '/' as the separator.
103 if (start == std::string::npos)
104 start = path.find_last_of(
"/");
108 if (start == std::string::npos) start = 0;
111 std::string::size_type end = path.find_last_of(
".");
113 if (end == std::string::npos || end < start)
116 return path.substr(start, end - start);
121 std::string::size_type start = path.find_last_of(
Separator);
122 #ifdef WIN32 // WIN32 also understands '/' as the separator.
124 if (start == std::string::npos)
125 start = path.find_last_of(
"/");
129 if (start == std::string::npos) start = 0;
132 return path.substr(start);
137 if (path ==
"")
return path;
139 #ifdef WIN32 // WIN32 also understands '/' as the separator.
140 std::string::size_type end = path.find_last_of(
Separator +
"/");
142 std::string::size_type end = path.find_last_of(
Separator);
145 if (end == path.length() - 1)
147 #ifdef WIN32 // WIN32 also understands '/' as the separator.
148 end = path.find_last_of(
Separator +
"/", end);
154 if (end == std::string::npos)
return "";
156 return path.substr(0, end);
161 std::string::size_type start = path.find_last_of(
Separator);
162 #ifdef WIN32 // WIN32 also understands '/' as the separator.
164 if (start == std::string::npos)
165 start = path.find_last_of(
"/");
169 if (start == std::string::npos) start = 0;
172 std::string::size_type end = path.find_last_of(
".");
174 if (end == std::string::npos || end < start)
177 return path.substr(end);
181 const std::string & parent)
185 if (parent !=
"") Dir = parent +
Separator;
203 const std::string & suffix)
207 std::string RandomName;
214 for (
size_t i = 0; i < 8; i++)
219 RandomName +=
'0' + Char;
221 RandomName +=
'a' - 10 + Char;
226 while (
exist(RandomName));
234 const std::string & to)
236 if (!
isFile(from))
return false;
245 if (
isDir(To))
return false;
250 if (
exist(To) && !
remove(To))
266 success = out.good();
291 const std::string & path)
294 std::vector< std::string > PatternList;
302 std::string FilePattern = path +
"\\*";
305 struct _wfinddata_t Entry;
308 if (hList == -1)
return success;
314 if (
match(Utf8, PatternList))
316 if (Entry.attrib | _A_NORMAL)
334 while (_wfindnext(hList, &Entry) == 0);
342 if (!pDir)
return false;
344 struct dirent * pEntry;
346 while ((pEntry = readdir(pDir)) != NULL)
350 if (
match(Utf8, PatternList))
374 std::string::size_type pos = 0;
375 std::string::size_type start = 0;
376 std::string::size_type end = 0;
377 std::vector< std::string > PatternList;
379 while (pos != std::string::npos)
382 pos = pattern.find_first_of(
"*?", pos);
384 end =
std::min(pos, pattern.length());
387 PatternList.push_back(pattern.substr(start, end - start));
390 PatternList.push_back(pattern.substr(start, 1));
399 const std::vector< std::string > & patternList)
401 std::vector< std::string >::const_iterator it = patternList.begin();
402 std::vector< std::string >::const_iterator end = patternList.end();
403 std::string::size_type at = 0;
404 std::string::size_type after = 0;
408 while (it != end && Match)
419 if (Path.length() < 2)
425 if (Path[0] ==
'/' && Path[1] ==
'/')
430 return (path.length() < 1 || path[0] !=
'/');
435 const std::string & relativeTo)
440 std:: string RelativeTo =
normalize(relativeTo);
444 if (!
isDir(RelativeTo))
return false;
448 size_t i, imax =
std::min(absolutePath.length(), RelativeTo.length());
450 for (i = 0; i < imax; i++)
451 if (absolutePath[i] != RelativeTo[i])
break;
455 i = absolutePath.find_last_of(
'/', i) + 1;
459 if (i == 0)
return false;
463 RelativeTo = RelativeTo.substr(i);
465 std::string relativePath;
467 while (RelativeTo !=
"")
469 relativePath +=
"../";
470 RelativeTo =
dirName(RelativeTo);
473 if (relativePath !=
"")
474 absolutePath = relativePath + absolutePath.substr(i);
476 absolutePath = absolutePath.substr(i + 1);
482 const std::string & absoluteTo)
487 std:: string AbsoluteTo =
normalize(absoluteTo);
491 if (!
isDir(AbsoluteTo))
return false;
495 while (!relativePath.compare(0, 3,
"../"))
497 AbsoluteTo =
dirName(AbsoluteTo);
498 relativePath = relativePath.substr(3);
501 relativePath = AbsoluteTo +
"/" + relativePath;
507 const std::string pattern,
508 std::string::size_type & at,
509 std::string::size_type & after)
517 if (at != std::string::npos)
520 at = std::string::npos;
527 if (at != std::string::npos)
530 Match = (name.length() >= at);
535 Match = (name.length() >= after);
542 if (at != std::string::npos)
544 Match = (name.compare(at, pattern.length(), pattern) == 0);
545 at += pattern.length();
549 at = name.find(pattern, after);
550 Match = (at != std::string::npos);
551 at += pattern.length();
562 std::string Normalized = path;
568 for (i = 0, imax = Normalized.length(); i < imax; i++)
569 if (Normalized[i] ==
'\\') Normalized[i] =
'/';
572 if (Normalized.length() > 0 && Normalized[0] ==
'/')
574 if ((Normalized.length() > 1 && Normalized[1] !=
'/') ||
575 Normalized.length() == 1)
580 Normalized = PWD.substr(0, 2) + Normalized;
587 while (!Normalized.compare(0, 2,
"./"))
588 Normalized = Normalized.substr(2);
591 std::string::size_type pos = 1;
595 pos = Normalized.find(
"//", pos);
597 if (pos == std::string::npos)
break;
599 Normalized.erase(pos, 1);
607 pos = Normalized.find(
"/./", pos);
609 if (pos == std::string::npos)
break;
611 Normalized.erase(pos, 2);
615 std::string::size_type start = Normalized.length();
619 pos = Normalized.rfind(
"/../", start);
621 if (pos == std::string::npos)
break;
623 start = Normalized.rfind(
'/', pos - 1);
625 if (start == std::string::npos)
break;
627 if (!Normalized.compare(start, 4,
"/../"))
continue;
629 Normalized.erase(start, pos - start + 3);
630 start = Normalized.length();
static bool isFile(const std::string &path)
static bool remove(const std::string &path)
static bool isDir(const std::string &path)
static bool move(const std::string &from, const std::string &to)
static bool isRelativePath(const std::string &path)
static bool match(const std::string &name, const std::vector< std::string > &patternList)
static std::string fileName(const std::string &path)
static bool removeFiles(const std::string &pattern, const std::string &dir)
static std::string baseName(const std::string &path)
static bool matchInternal(const std::string &name, const std::string pattern, std::string::size_type &at, std::string::size_type &after)
static std::string dirName(const std::string &path)
static CRandom * createGenerator(CRandom::Type type=CRandom::mt19937, unsigned C_INT32 seed=0)
virtual unsigned C_INT32 getRandomU()
static bool createDir(const std::string &dir, const std::string &parent="")
static bool exist(const std::string &path)
static const std::string Separator
static bool isWritable(const std::string &path)
static std::string createTmpName(const std::string &dir, const std::string &suffix)
static void getValue(const std::string &name, CType &value)
static std::vector< std::string > compilePattern(const std::string &pattern)
std::string toUtf8() const
static std::string normalize(const std::string &path)
static bool isReadable(const std::string &path)
static CLocaleString fromUtf8(const std::string &utf8)
static bool makePathAbsolute(std::string &relativePath, const std::string &absoluteTo)
static bool makePathRelative(std::string &absolutePath, const std::string &relativeTo)
static std::string suffix(const std::string &path)