aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/main/cpp/util
diff options
context:
space:
mode:
Diffstat (limited to 'src/main/cpp/util')
-rw-r--r--src/main/cpp/util/file_windows.cc33
-rw-r--r--src/main/cpp/util/strings.cc19
-rw-r--r--src/main/cpp/util/strings.h2
3 files changed, 46 insertions, 8 deletions
diff --git a/src/main/cpp/util/file_windows.cc b/src/main/cpp/util/file_windows.cc
index 22ab715f23..63d6d66efb 100644
--- a/src/main/cpp/util/file_windows.cc
+++ b/src/main/cpp/util/file_windows.cc
@@ -38,6 +38,8 @@ using std::wstring;
// The result may have a UNC prefix.
static unique_ptr<WCHAR[]> GetCwdW();
+static bool IsDevNull(const string& path);
+
// Returns true if `path` refers to a directory or (non-dangling) junction.
// `path` must be a normalized Windows path, with UNC prefix (and absolute) if
// necessary.
@@ -287,6 +289,10 @@ bool AsWindowsPath(const string& path, wstring* result) {
result->clear();
return true;
}
+ if (IsDevNull(path)) {
+ result->assign(L"NUL");
+ return true;
+ }
string mutable_path = path;
if (path[0] == '/') {
@@ -318,6 +324,11 @@ bool AsWindowsPath(const string& path, wstring* result) {
}
static bool AsWindowsPathWithUncPrefix(const string& path, wstring* wpath) {
+ if (IsDevNull(path)) {
+ wpath->assign(L"NUL");
+ return true;
+ }
+
if (!AsWindowsPath(path, wpath)) {
PrintError("AsWindowsPathWithUncPrefix(%s): AsWindowsPath failed, err=%d\n",
path.c_str(), GetLastError());
@@ -331,6 +342,11 @@ static bool AsWindowsPathWithUncPrefix(const string& path, wstring* wpath) {
}
bool AsShortWindowsPath(const string& path, string* result) {
+ if (IsDevNull(path)) {
+ result->assign("NUL");
+ return true;
+ }
+
result->clear();
wstring wpath;
if (!AsWindowsPathWithUncPrefix(path, &wpath)) {
@@ -359,6 +375,10 @@ bool ReadFile(const string& filename, string* content, int max_size) {
if (filename.empty()) {
return false;
}
+ if (IsDevNull(filename)) {
+ content->clear();
+ return true;
+ }
wstring wfilename;
if (!AsWindowsPathWithUncPrefix(filename, &wfilename)) {
return false;
@@ -415,6 +435,10 @@ static bool UnlinkPathW(const wstring& path) {
}
bool UnlinkPath(const string& file_path) {
+ if (IsDevNull(file_path)) {
+ return false;
+ }
+
wstring wpath;
if (!AsWindowsPathWithUncPrefix(file_path, &wpath)) {
return false;
@@ -562,6 +586,9 @@ bool PathExists(const string& path) {
if (path.empty()) {
return false;
}
+ if (IsDevNull(path)) {
+ return true;
+ }
wstring wpath;
if (!AsWindowsPathWithUncPrefix(path, &wpath)) {
PrintError("PathExists(%s): AsWindowsPathWithUncPrefix failed, err=%d\n",
@@ -591,6 +618,10 @@ bool CanAccessDirectory(const std::string& path) {
}
#endif // COMPILER_MSVC
+static bool IsDevNull(const string& path) {
+ return path == "/dev/null" || AsLower(path) == "nul";
+}
+
static bool IsDirectoryW(const wstring& path) {
DWORD attrs = ::GetFileAttributesW(path.c_str());
return (attrs != INVALID_FILE_ATTRIBUTES) &&
@@ -599,7 +630,7 @@ static bool IsDirectoryW(const wstring& path) {
}
bool IsDirectory(const string& path) {
- if (path.empty()) {
+ if (path.empty() || IsDevNull(path)) {
return false;
}
wstring wpath;
diff --git a/src/main/cpp/util/strings.cc b/src/main/cpp/util/strings.cc
index 3fed561bf5..383ffa1191 100644
--- a/src/main/cpp/util/strings.cc
+++ b/src/main/cpp/util/strings.cc
@@ -299,15 +299,20 @@ void StringPrintf(string *str, const char *format, ...) {
void ToLower(string *str) {
assert(str);
- if (str->empty()) {
- return;
- }
+ *str = AsLower(*str);
+}
- string temp = "";
- for (auto ch : *str) {
- temp += tolower(ch);
+string AsLower(const string &str) {
+ if (str.empty()) {
+ return "";
+ }
+ unique_ptr<char[]> result(new char[str.size() + 1]);
+ char *result_ptr = result.get();
+ for (const auto &ch : str) {
+ *result_ptr++ = tolower(ch);
}
- *str = temp;
+ result.get()[str.size()] = 0;
+ return string(result.get());
}
template <typename U, typename V>
diff --git a/src/main/cpp/util/strings.h b/src/main/cpp/util/strings.h
index 66502266bf..61187b73cb 100644
--- a/src/main/cpp/util/strings.h
+++ b/src/main/cpp/util/strings.h
@@ -106,6 +106,8 @@ void StringPrintf(std::string *str, const char *format, ...);
// Convert str to lower case. No locale handling, this is just for ASCII.
void ToLower(std::string *str);
+std::string AsLower(const std::string &str);
+
// Convert a wchar_t string to a char string. Useful when consuming results of
// widechar Windows API functions.
std::unique_ptr<char[]> WstringToCstring(const wchar_t *input);