blob: 4957c76ffff0521bccc126570c39062548351030 [file] [log] [blame]
Christopher Wiley72877ac2015-10-06 14:41:42 -07001/*
2 * Copyright (C) 2015, The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#include "import_resolver.h"
18
19#include <unistd.h>
20
21#ifdef _WIN32
22#include <io.h>
23#endif
24
25#include "os.h"
26
27using std::string;
28using std::vector;
29
30namespace android {
31namespace aidl {
32
33ImportResolver::ImportResolver(const IoDelegate& io_delegate,
34 const vector<string>& import_paths)
35 : io_delegate_(io_delegate) {
36 for (string path : import_paths) {
37 if (path.empty()) {
38 path = ".";
39 }
40 if (path[path.size() - 1] != OS_PATH_SEPARATOR) {
41 path += OS_PATH_SEPARATOR;
42 }
43 import_paths_.push_back(std::move(path));
44 }
45}
46
47
48string ImportResolver::FindImportFile(const string& canonical_name) const {
49 // Convert the canonical name to a relative file path.
50 string relative_path = canonical_name;
51 for (char& c : relative_path) {
52 if (c == '.') {
53 c = OS_PATH_SEPARATOR;
54 }
55 }
56 relative_path += ".aidl";
57
58 // Look for that relative path at each of our import roots.
59 for (string path : import_paths_) {
60 path = path + relative_path;
61 if (io_delegate_.FileIsReadable(path)) {
62 return path;
63 }
64 }
65
66 return "";
67}
68
69} // namespace android
70} // namespace aidl