The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1 | #include <unistd.h> |
| 2 | #include "search_path.h" |
| 3 | #include "options.h" |
Elliott Hughes | 9ec96f9 | 2015-07-29 14:35:18 -0700 | [diff] [blame] | 4 | #include "os.h" |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5 | #include <string.h> |
| 6 | |
Elliott Hughes | e17788c | 2015-08-17 12:41:46 -0700 | [diff] [blame^] | 7 | #ifdef _WIN32 |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8 | #include <io.h> |
| 9 | #endif |
| 10 | |
| 11 | static vector<string> g_importPaths; |
| 12 | |
| 13 | void |
| 14 | set_import_paths(const vector<string>& importPaths) |
| 15 | { |
| 16 | g_importPaths = importPaths; |
| 17 | } |
| 18 | |
| 19 | char* |
| 20 | find_import_file(const char* given) |
| 21 | { |
| 22 | string expected = given; |
| 23 | |
| 24 | int N = expected.length(); |
| 25 | for (int i=0; i<N; i++) { |
| 26 | char c = expected[i]; |
| 27 | if (c == '.') { |
| 28 | expected[i] = OS_PATH_SEPARATOR; |
| 29 | } |
| 30 | } |
| 31 | expected += ".aidl"; |
| 32 | |
| 33 | vector<string>& paths = g_importPaths; |
| 34 | for (vector<string>::iterator it=paths.begin(); it!=paths.end(); it++) { |
| 35 | string f = *it; |
| 36 | if (f.size() == 0) { |
| 37 | f = "."; |
| 38 | f += OS_PATH_SEPARATOR; |
| 39 | } |
| 40 | else if (f[f.size()-1] != OS_PATH_SEPARATOR) { |
| 41 | f += OS_PATH_SEPARATOR; |
| 42 | } |
| 43 | f.append(expected); |
| 44 | |
Elliott Hughes | e17788c | 2015-08-17 12:41:46 -0700 | [diff] [blame^] | 45 | #ifdef _WIN32 |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 46 | /* check that the file exists and is not write-only */ |
| 47 | if (0 == _access(f.c_str(), 0) && /* mode 0=exist */ |
| 48 | 0 == _access(f.c_str(), 4) ) { /* mode 4=readable */ |
| 49 | #else |
| 50 | if (0 == access(f.c_str(), R_OK)) { |
| 51 | #endif |
| 52 | return strdup(f.c_str()); |
| 53 | } |
| 54 | } |
| 55 | |
| 56 | return NULL; |
| 57 | } |
| 58 | |