diff --git a/stepmania/src/RageUtil.cpp b/stepmania/src/RageUtil.cpp index 982890cfd8..a287c9ee6e 100644 --- a/stepmania/src/RageUtil.cpp +++ b/stepmania/src/RageUtil.cpp @@ -19,6 +19,7 @@ #include #include #include +#include "regex.h" unsigned long randseed = time(NULL); @@ -688,6 +689,7 @@ unsigned GetFileSizeInBytes( const CString &sFilePath ) bool DoesFileExist( const CString &sPath ) { + if(sPath.empty()) return false; struct stat st; return DoStat(sPath, &st); } @@ -699,6 +701,7 @@ bool IsAFile( const CString &sPath ) bool IsADirectory( const CString &sPath ) { + if(sPath.empty()) return false; struct stat st; if (!DoStat(sPath, &st)) return false; @@ -782,3 +785,60 @@ CString DerefRedir(const CString &path) return sDir+sNewFileName; } + +/* Call this if you need flags other than the default. They'll be + * reset after the next regex() call. */ +static const int re_flags_default = REG_EXTENDED|REG_ICASE; +static int re_flags = re_flags_default; +void regex_flags(int flags) +{ + re_flags = flags; +} + +void regex_reset() +{ + re_flags = re_flags_default; +} + +bool regex(CString pattern, CString str, vector &matches) +{ + matches.clear(); + + regex_t reg; + + /* Count the number of backreferences. */ + int backref_count = 0; + int i; + for(i = 0; i < int(pattern.size()); ++i) + if(pattern[i] == '(') backref_count++; + ASSERT(backref_count+1 < 128); + + int ret = regcomp(®, pattern.c_str(), re_flags); + regex_reset(); + if(ret != 0) + RageException::Throw("Invalid regex: '%s'", pattern.c_str()); + + regmatch_t mat[128]; + ret = regexec(®, str.c_str(), 128, mat, 0); + + regfree(®); + + if(ret == REG_NOMATCH) + return false; + + for(i = 1; i < backref_count+1; ++i) + { + if(mat[i].rm_so == -1) + matches.push_back(""); /* no match */ + else + matches.push_back(str.substr(mat[i].rm_so, mat[i].rm_eo - mat[i].rm_so)); + } + + return true; +} + +bool regex(CString pattern, CString str) +{ + vector matches; + return regex(pattern, str, matches); +} diff --git a/stepmania/src/RageUtil.h b/stepmania/src/RageUtil.h index 16e2abf9aa..3e98eac29c 100644 --- a/stepmania/src/RageUtil.h +++ b/stepmania/src/RageUtil.h @@ -192,6 +192,10 @@ void TrimRight(CString &str, const char *s = "\r\n\t "); CString DerefRedir(const CString &path); +bool regex(CString str, CString pattern, vector &matches); +bool regex(CString str, CString pattern); +void regex_flags(int flags); + #ifndef WIN32 #include /* correct place with correct definitions */ #endif