blob: 029e216b0abd1d5c032d9b48b0432a4e902dad00 [file] [log] [blame]
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001#include <unistd.h>
2#include "search_path.h"
3#include "options.h"
Elliott Hughes9ec96f92015-07-29 14:35:18 -07004#include "os.h"
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005#include <string.h>
6
Elliott Hughese17788c2015-08-17 12:41:46 -07007#ifdef _WIN32
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008#include <io.h>
9#endif
10
11static vector<string> g_importPaths;
12
13void
14set_import_paths(const vector<string>& importPaths)
15{
16 g_importPaths = importPaths;
17}
18
19char*
20find_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 Hughese17788c2015-08-17 12:41:46 -070045#ifdef _WIN32
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080046 /* 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