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