backslash-escaping predicate

This commit is contained in:
Jack Andersen 2015-11-10 18:01:36 -10:00
parent 1f3ac3805e
commit faccdbc55a
2 changed files with 19 additions and 1 deletions

View File

@ -3,7 +3,7 @@ find_library(PNG_LIB png)
endif() endif()
if(WIN32 OR APPLE OR PNG_LIB STREQUAL PNG_LIB-NOTFOUND) if(WIN32 OR APPLE OR PNG_LIB STREQUAL PNG_LIB-NOTFOUND)
message("-- Using HECL's built-in libpng") message(STATUS "Using HECL's built-in libpng")
include_directories(${ZLIB_INCLUDE_DIR}) include_directories(${ZLIB_INCLUDE_DIR})
add_library(png add_library(png
png.h png.h

View File

@ -12,13 +12,22 @@ void SanitizePath(std::string& path)
path.erase(std::remove(path.begin(), path.end(), '\n'), path.end()); path.erase(std::remove(path.begin(), path.end(), '\n'), path.end());
path.erase(std::remove(path.begin(), path.end(), '\r'), path.end()); path.erase(std::remove(path.begin(), path.end(), '\r'), path.end());
std::string::iterator p1 = path.begin(); std::string::iterator p1 = path.begin();
bool ic = false;
std::transform(path.begin(), path.end(), path.begin(), [&](const char a) -> char { std::transform(path.begin(), path.end(), path.begin(), [&](const char a) -> char {
++p1; ++p1;
if (ic)
{
ic = false;
return a;
}
static const std::string illegals {"<>?*\"|"}; static const std::string illegals {"<>?*\"|"};
if (illegals.find_first_of(a) != std::string::npos) if (illegals.find_first_of(a) != std::string::npos)
return '_'; return '_';
if (a == '\\' && (p1 == path.end() || *p1 != '\\')) if (a == '\\' && (p1 == path.end() || *p1 != '\\'))
{
ic = true;
return '/'; return '/';
}
return a; return a;
}); });
} }
@ -30,13 +39,22 @@ void SanitizePath(std::wstring& path)
path.erase(std::remove(path.begin(), path.end(), L'\n'), path.end()); path.erase(std::remove(path.begin(), path.end(), L'\n'), path.end());
path.erase(std::remove(path.begin(), path.end(), L'\r'), path.end()); path.erase(std::remove(path.begin(), path.end(), L'\r'), path.end());
std::wstring::iterator p1 = path.begin(); std::wstring::iterator p1 = path.begin();
bool ic = false;
std::transform(path.begin(), path.end(), path.begin(), [&](const wchar_t a) -> wchar_t { std::transform(path.begin(), path.end(), path.begin(), [&](const wchar_t a) -> wchar_t {
++p1; ++p1;
if (ic)
{
ic = false;
return a;
}
static const std::wstring illegals {L"<>?*\"|"}; static const std::wstring illegals {L"<>?*\"|"};
if (illegals.find_first_of(a) != std::wstring::npos) if (illegals.find_first_of(a) != std::wstring::npos)
return L'_'; return L'_';
if (a == L'\\' && (p1 == path.end() || *p1 != L'\\')) if (a == L'\\' && (p1 == path.end() || *p1 != L'\\'))
{
ic = true;
return L'/'; return L'/';
}
return a; return a;
}); });
} }