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