Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(1712)

Unified Diff: testing/utils/path_service.cpp

Issue 1411403012: Merge to XFA: Add path service to retrieve test data directory at run time so tests can be run from… (Closed) Base URL: https://pdfium.googlesource.com/pdfium.git@xfa
Patch Set: remove a blank line Created 5 years, 1 month ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « testing/utils/path_service.h ('k') | no next file » | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: testing/utils/path_service.cpp
diff --git a/testing/utils/path_service.cpp b/testing/utils/path_service.cpp
new file mode 100644
index 0000000000000000000000000000000000000000..1501a2da68a394b582cb6bec40f94efa5d44a50f
--- /dev/null
+++ b/testing/utils/path_service.cpp
@@ -0,0 +1,101 @@
+// Copyright 2015 PDFium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#include "testing/utils/path_service.h"
+
+#ifdef _WIN32
+#include <Windows.h>
+#elif defined(__APPLE__)
+#include <mach-o/dyld.h>
+#else // Linux
+#include <linux/limits.h>
+#include <unistd.h>
+#endif // _WIN32
+
+#include "core/include/fxcrt/fx_system.h"
+
+// static
+bool PathService::EndsWithSeparator(const std::string& path) {
+ return path.size() > 1 && path[path.size() - 1] == PATH_SEPARATOR;
+}
+
+// static
+bool PathService::GetExecutableDir(std::string* path) {
+// Get the current executable file path.
+#ifdef _WIN32
+ char path_buffer[MAX_PATH];
+ path_buffer[0] = 0;
+
+ if (GetModuleFileNameA(NULL, path_buffer, MAX_PATH) == 0)
+ return false;
+ *path = std::string(path_buffer);
+#elif defined(__APPLE__)
+ FXSYS_assert(path);
+ unsigned int path_length = 0;
+ _NSGetExecutablePath(NULL, &path_length);
+ if (path_length == 0)
+ return false;
+
+ path->reserve(path_length);
+ path->resize(path_length - 1);
+ if (_NSGetExecutablePath(&((*path)[0]), &path_length))
+ return false;
+#else // Linux
+ static const char kProcSelfExe[] = "/proc/self/exe";
+ char buf[PATH_MAX];
+ ssize_t count = ::readlink(kProcSelfExe, buf, PATH_MAX);
+ if (count <= 0)
+ return false;
+
+ *path = std::string(buf, count);
+#endif // _WIN32
+
+ // Get the directory path.
+ std::size_t pos = path->size() - 1;
+ if (EndsWithSeparator(*path))
+ pos--;
+ std::size_t found = path->find_last_of(PATH_SEPARATOR, pos);
+ if (found == std::string::npos)
+ return false;
+ path->resize(found);
+ return true;
+}
+
+// static
+bool PathService::GetSourceDir(std::string* path) {
+ if (!GetExecutableDir(path))
+ return false;
+
+ if (!EndsWithSeparator(*path))
+ path->push_back(PATH_SEPARATOR);
+ path->append("..");
+ path->push_back(PATH_SEPARATOR);
+ path->append("..");
+ return true;
+}
+
+// static
+bool PathService::GetTestDataDir(std::string* path) {
+ if (!GetSourceDir(path))
+ return false;
+
+ if (!EndsWithSeparator(*path))
+ path->push_back(PATH_SEPARATOR);
+ path->append("testing");
+ path->push_back(PATH_SEPARATOR);
+ path->append("resources");
+ return true;
+}
+
+// static
+bool PathService::GetTestFilePath(const std::string& file_name,
+ std::string* path) {
+ if (!GetTestDataDir(path))
+ return false;
+
+ if (!EndsWithSeparator(*path))
+ path->push_back(PATH_SEPARATOR);
+ path->append(file_name);
+ return true;
+}
« no previous file with comments | « testing/utils/path_service.h ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698