blob: dd087a5c94018dbdd5db62c71a9282425af06890 [file] [log] [blame]
Josh Gaobf8a2852016-05-27 11:59:09 -07001/*
Elliott Hughesdfb74c52016-10-24 12:53:17 -07002 * Copyright (C) 2016 The Android Open Source Project
Josh Gaobf8a2852016-05-27 11:59:09 -07003 *
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 *
Elliott Hughesdfb74c52016-10-24 12:53:17 -07008 * http://www.apache.org/licenses/LICENSE-2.0
Josh Gaobf8a2852016-05-27 11:59:09 -07009 *
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 "Utils.h"
18
19#include <err.h>
20#include <fts.h>
21#include <string.h>
22#include <unistd.h>
23
Josh Gaobfb6bae2016-07-15 17:25:21 -070024#include <sstream>
Josh Gaobf8a2852016-05-27 11:59:09 -070025#include <string>
26#include <vector>
27
Josh Gaobfb6bae2016-07-15 17:25:21 -070028#include "DeclarationDatabase.h"
29
Josh Gaobf8a2852016-05-27 11:59:09 -070030std::string getWorkingDir() {
31 char buf[PATH_MAX];
32 if (!getcwd(buf, sizeof(buf))) {
33 err(1, "getcwd failed");
34 }
35 return buf;
36}
37
38std::vector<std::string> collectFiles(const std::string& directory) {
39 std::vector<std::string> files;
40
41 char* dir_argv[2] = { const_cast<char*>(directory.c_str()), nullptr };
42 FTS* fts = fts_open(dir_argv, FTS_LOGICAL | FTS_NOCHDIR, nullptr);
43
44 if (!fts) {
45 err(1, "failed to open directory '%s'", directory.c_str());
46 }
47
48 FTSENT* ent;
49 while ((ent = fts_read(fts))) {
50 if (ent->fts_info & (FTS_D | FTS_DP)) {
51 continue;
52 }
53
54 files.push_back(ent->fts_path);
55 }
56
57 fts_close(fts);
58 return files;
59}
Josh Gaobfb6bae2016-07-15 17:25:21 -070060
61llvm::StringRef StripPrefix(llvm::StringRef string, llvm::StringRef prefix) {
62 if (string.startswith(prefix)) {
63 return string.drop_front(prefix.size());
64 }
65 return string;
66}