blob: 7876f231e0135dfaeb17144be0003ad95666496e [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#pragma once
18
Josh Gao16016df2016-11-07 18:27:16 -080019#include <errno.h>
20#include <libgen.h>
21#include <sys/stat.h>
22#include <unistd.h>
23
Josh Gaobf8a2852016-05-27 11:59:09 -070024#include <string>
25#include <vector>
26
Josh Gaobfb6bae2016-07-15 17:25:21 -070027#include <llvm/ADT/StringRef.h>
28
Josh Gaobf8a2852016-05-27 11:59:09 -070029std::string getWorkingDir();
30std::vector<std::string> collectFiles(const std::string& directory);
31
Josh Gao16016df2016-11-07 18:27:16 -080032static inline std::string dirname(const std::string& path) {
33 std::unique_ptr<char, decltype(&free)> path_copy(strdup(path.c_str()), free);
34 return dirname(path_copy.get());
35}
36
37static inline bool is_directory(const std::string& path) {
38 struct stat st;
39 if (stat(path.c_str(), &st) == 0 && S_ISDIR(st.st_mode)) {
40 return true;
41 }
42 return false;
43}
44
45static inline bool mkdirs(const std::string& path) {
46 if (is_directory(path)) {
47 return true;
48 }
49
50 std::string parent = dirname(path);
51 if (parent == path) {
52 return false;
53 }
54
55 if (!mkdirs(parent)) {
56 return false;
57 }
58
59 if (mkdir(path.c_str(), 0700) != 0) {
60 if (errno != EEXIST) {
61 return false;
62 }
63 return is_directory(path);
64 }
65
66 return true;
67}
68
69static inline std::string to_string(const char* c) {
Josh Gaobf8a2852016-05-27 11:59:09 -070070 return c;
71}
72
Josh Gao16016df2016-11-07 18:27:16 -080073static inline const std::string& to_string(const std::string& str) {
Josh Gaobf8a2852016-05-27 11:59:09 -070074 return str;
75}
Josh Gaobf8a2852016-05-27 11:59:09 -070076
77template <typename Collection>
Josh Gao16016df2016-11-07 18:27:16 -080078static inline std::string Join(Collection c, const std::string& delimiter = ", ") {
Josh Gaobf8a2852016-05-27 11:59:09 -070079 std::string result;
80 for (const auto& item : c) {
Josh Gaof8592a32016-07-26 18:58:27 -070081 using namespace std;
Josh Gaobfb6bae2016-07-15 17:25:21 -070082 result.append(to_string(item));
Josh Gaobf8a2852016-05-27 11:59:09 -070083 result.append(delimiter);
84 }
85 if (!result.empty()) {
86 result.resize(result.length() - delimiter.length());
87 }
88 return result;
89}
Josh Gaobfb6bae2016-07-15 17:25:21 -070090
91llvm::StringRef StripPrefix(llvm::StringRef string, llvm::StringRef prefix);