OLD | NEW |
---|---|
(Empty) | |
1 // Copyright 2015 PDFium Authors. All rights reserved. | |
2 // Use of this source code is governed by a BSD-style license that can be | |
3 // found in the LICENSE file. | |
4 | |
5 #include "testing/utils/path_service.h" | |
6 | |
7 #ifdef _WIN32 | |
8 #include <Windows.h> | |
9 #elif defined(__APPLE__) | |
10 #include <mach-o/dyld.h> | |
11 #else // Linux | |
12 #include <linux/limits.h> | |
13 #include <unistd.h> | |
14 #endif // _WIN32 | |
15 | |
16 #include "core/include/fxcrt/fx_system.h" | |
17 | |
18 // static | |
19 bool PathService::EndsWithSeparator(const std::string& path) { | |
20 if (path.size() > 1 && path[path.size() - 1] == PATH_SEPARATOR) | |
Lei Zhang
2015/11/07 01:48:51
if (foo)
return true;
return false;
--> return
Wei Li
2015/11/07 02:36:12
Done.
| |
21 return true; | |
22 return false; | |
23 } | |
24 | |
25 // static | |
26 bool PathService::GetExecutableDir(std::string* path) { | |
27 // Get the current executable file path. | |
28 #ifdef _WIN32 | |
29 char path_buffer[MAX_PATH]; | |
30 path_buffer[0] = 0; | |
31 | |
32 if (GetModuleFileNameA(NULL, path_buffer, MAX_PATH) == 0) | |
33 return false; | |
34 *path = std::string(path_buffer); | |
35 #elif defined(__APPLE__) | |
36 FXSYS_assert(path); | |
37 unsigned int path_length = 0; | |
38 _NSGetExecutablePath(NULL, &path_length); | |
39 if (path_length == 0) | |
40 return false; | |
41 | |
42 path->reserve(path_length); | |
43 path->resize(path_length - 1); | |
44 if (_NSGetExecutablePath(&((*path)[0]), &path_length)) | |
45 return false; | |
46 #else // Linux | |
47 static const char kProcSelfExe[] = "/proc/self/exe"; | |
48 char buf[PATH_MAX]; | |
49 ssize_t count = ::readlink(kProcSelfExe, buf, PATH_MAX); | |
50 if (count <= 0) | |
51 return false; | |
52 | |
53 *path = std::string(buf, count); | |
54 #endif // _WIN32 | |
55 | |
56 // Get the directory path. | |
57 std::size_t pos = path->size() - 1; | |
58 if (EndsWithSeparator(*path)) | |
59 pos--; | |
60 std::size_t found = path->find_last_of(PATH_SEPARATOR, pos); | |
61 if (found == std::string::npos) | |
62 return false; | |
63 path->resize(found); | |
64 return true; | |
65 } | |
66 | |
67 // static | |
68 bool PathService::GetSourceDir(std::string* path) { | |
69 if (!GetExecutableDir(path)) | |
70 return false; | |
71 | |
72 if (!EndsWithSeparator(*path)) | |
73 path->push_back(PATH_SEPARATOR); | |
74 // Assume executables always run from out/<Debug|Release>/, the source | |
Lei Zhang
2015/11/07 01:48:51
BTW, did some of the code in this file come from C
Wei Li
2015/11/07 02:36:12
Chromium's path service is much more complex. We m
Lei Zhang
2015/11/07 02:43:30
There could be other assumptions, like looking for
| |
75 // directory is two levels above the executable directory. | |
76 path->append(".."); | |
77 path->push_back(PATH_SEPARATOR); | |
78 path->append(".."); | |
79 return true; | |
80 } | |
81 | |
82 // static | |
83 bool PathService::GetTestDataDir(std::string* path) { | |
84 if (!GetSourceDir(path)) | |
85 return false; | |
86 | |
87 if (!EndsWithSeparator(*path)) | |
88 path->push_back(PATH_SEPARATOR); | |
89 path->append("testing"); | |
90 path->push_back(PATH_SEPARATOR); | |
91 path->append("resources"); | |
92 return true; | |
93 } | |
94 | |
95 // static | |
96 bool PathService::GetTestFilePath(const std::string& file_name, | |
97 std::string* path) { | |
98 if (!GetTestDataDir(path)) | |
99 return false; | |
100 | |
101 if (!EndsWithSeparator(*path)) | |
102 path->push_back(PATH_SEPARATOR); | |
103 path->append(file_name); | |
104 return true; | |
105 } | |
OLD | NEW |