TYPE3
[iec.git] / src / type3_AndroidCloud / anbox-master / src / anbox / utils.h
1 /*
2  * Copyright (C) 2016 Simon Fels <morphis@gravedo.de>
3  *
4  * This program is free software: you can redistribute it and/or modify it
5  * under the terms of the GNU General Public License version 3, as published
6  * by the Free Software Foundation.
7  *
8  * This program is distributed in the hope that it will be useful, but
9  * WITHOUT ANY WARRANTY; without even the implied warranties of
10  * MERCHANTABILITY, SATISFACTORY QUALITY, or FITNESS FOR A PARTICULAR
11  * PURPOSE.  See the GNU General Public License for more details.
12  *
13  * You should have received a copy of the GNU General Public License along
14  * with this program.  If not, see <http://www.gnu.org/licenses/>.
15  *
16  */
17
18 #ifndef ANBOX_UTILS_H_
19 #define ANBOX_UTILS_H_
20
21 #include <boost/format.hpp>
22
23 #include <string>
24 #include <vector>
25
26 namespace anbox {
27 namespace utils {
28
29 std::vector<std::string> collect_arguments(int argc, char **argv);
30
31 std::string read_file_if_exists_or_throw(const std::string &file_path);
32
33 bool write_to_file(const std::string &file_path,
34                    const std::string &content = "");
35 int write_to_fd(int fd, const char *content, ssize_t len);
36 int write_file_at(int dirfd, const char *path, const char *content);
37
38 bool string_starts_with(const std::string &text, const std::string &prefix);
39
40 std::vector<std::string> string_split(const std::string &text, char sep);
41
42 std::string strip_surrounding_quotes(const std::string &text);
43
44 std::string hex_dump(const uint8_t *data, uint32_t size);
45
46 std::string get_env_value(const std::string &name,
47                           const std::string &default_value = "");
48 bool is_env_set(const std::string &name);
49
50 void ensure_paths(const std::vector<std::string> &paths);
51
52 std::string prefix_dir_from_env(const std::string &path,
53                                 const std::string &env_var);
54
55 std::string process_get_exe_path(const pid_t &pid);
56
57 bool is_mounted(const std::string &path);
58
59 std::string find_program_on_path(const std::string &name);
60
61 template <typename... Types>
62 static std::string string_format(const std::string &fmt_str, Types &&... args);
63 }  // namespace utils
64 }  // namespace anbox
65
66 namespace impl {
67 // Base case, just return the passed in boost::format instance.
68 inline boost::format &string_format(boost::format &f) { return f; }
69 // Sprintf recursively walks the parameter pack at compile time.
70 template <typename Head, typename... Tail>
71 inline boost::format &string_format(boost::format &f, Head const &head,
72                                     Tail &&... tail) {
73   return string_format(f % head, std::forward<Tail>(tail)...);
74 }
75 }  // namespace impl
76
77 template <typename... Types>
78 inline std::string anbox::utils::string_format(const std::string &format,
79                                                Types &&... args) {
80   boost::format f(format);
81   return impl::string_format(f, std::forward<Types>(args)...).str();
82 }
83
84 #endif