blob: 3c4e14b3a01d119a55dfb7d5b66b6ebb2e43f8cf [file] [log] [blame]
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -07001#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
9static vector<string> g_importPaths;
10
11void
12set_import_paths(const vector<string>& importPaths)
13{
14 g_importPaths = importPaths;
15}
16
17char*
18find_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