]>
Commit | Line | Data |
---|---|---|
60d98537 | 1 | #include <cstring> |
1b78aef5 DS |
2 | #include <string> |
3 | #include <vector> | |
72dbc915 FCE |
4 | #include <iostream> |
5 | #include <sstream> | |
6 | #include <stdexcept> | |
3f99432c | 7 | #include <cctype> |
1b78aef5 | 8 | |
72dbc915 | 9 | const char *get_home_directory(void); |
b12c8986 DB |
10 | size_t get_file_size(const std::string &path); |
11 | bool file_exists (const std::string &path); | |
e16dc041 JS |
12 | bool copy_file(const std::string& src, const std::string& dest, |
13 | bool verbose=false); | |
1b78aef5 | 14 | int create_dir(const char *dir); |
98f552c2 | 15 | int remove_file_or_dir(const char *dir); |
0da3e7a0 | 16 | bool in_group_id (gid_t target_gid); |
1b78aef5 DS |
17 | void tokenize(const std::string& str, std::vector<std::string>& tokens, |
18 | const std::string& delimiters); | |
63b4fd14 SC |
19 | std::string find_executable(const std::string& name, |
20 | const std::string& env_path = "PATH"); | |
8c711d30 | 21 | const std::string cmdstr_quoted(const std::string& cmd); |
a5e8d632 | 22 | std::string git_revision(const std::string& path); |
36ef6d6a | 23 | int stap_system(int verbose, const std::string& command); |
4cc40e82 | 24 | int kill_stap_spawn(int sig); |
c0d1b5a0 | 25 | void assert_regexp_match (const std::string& name, const std::string& value, const std::string& re); |
72dbc915 FCE |
26 | |
27 | // stringification generics | |
28 | ||
29 | ||
aca66a36 JS |
30 | template <typename IN> |
31 | inline std::string lex_cast(IN const & in) | |
72dbc915 | 32 | { |
aca66a36 JS |
33 | std::ostringstream ss; |
34 | if (!(ss << in)) | |
35 | throw std::runtime_error("bad lexical cast"); | |
36 | return ss.str(); | |
72dbc915 FCE |
37 | } |
38 | ||
39 | ||
aca66a36 JS |
40 | template <typename OUT> |
41 | inline OUT lex_cast(std::string const & in) | |
72dbc915 | 42 | { |
aca66a36 | 43 | std::istringstream ss(in); |
72dbc915 | 44 | OUT out; |
aca66a36 | 45 | if (!(ss >> out && ss.eof())) |
72dbc915 FCE |
46 | throw std::runtime_error("bad lexical cast"); |
47 | return out; | |
48 | } | |
49 | ||
50 | ||
aca66a36 JS |
51 | template <typename IN> |
52 | inline std::string | |
72dbc915 FCE |
53 | lex_cast_hex(IN const & in) |
54 | { | |
aca66a36 JS |
55 | std::ostringstream ss; |
56 | if (!(ss << std::showbase << std::hex << in)) | |
72dbc915 | 57 | throw std::runtime_error("bad lexical cast"); |
aca66a36 | 58 | return ss.str(); |
72dbc915 FCE |
59 | } |
60 | ||
61 | ||
62 | // Return as quoted string, so that when compiled as a C literal, it | |
63 | // would print to the user out nicely. | |
dff50e09 | 64 | template <typename IN> |
72dbc915 FCE |
65 | inline std::string |
66 | lex_cast_qstring(IN const & in) | |
67 | { | |
68 | std::stringstream ss; | |
72dbc915 FCE |
69 | if (!(ss << in)) |
70 | throw std::runtime_error("bad lexical cast"); | |
aca66a36 JS |
71 | return lex_cast_qstring(ss.str()); |
72 | } | |
73 | ||
74 | ||
75 | template <> | |
76 | inline std::string | |
77 | lex_cast_qstring(std::string const & in) | |
78 | { | |
79 | std::string out; | |
80 | out += '"'; | |
81 | for (const char *p = in.c_str(); *p; ++p) | |
72dbc915 | 82 | { |
aca66a36 | 83 | unsigned char c = *p; |
3f99432c FCE |
84 | if (! isprint(c)) |
85 | { | |
aca66a36 | 86 | out += '\\'; |
3f99432c | 87 | // quick & dirty octal converter |
aca66a36 JS |
88 | out += "01234567" [(c >> 6) & 0x07]; |
89 | out += "01234567" [(c >> 3) & 0x07]; | |
90 | out += "01234567" [(c >> 0) & 0x07]; | |
dff50e09 | 91 | } |
3f99432c FCE |
92 | else if (c == '"' || c == '\\') |
93 | { | |
aca66a36 JS |
94 | out += '\\'; |
95 | out += c; | |
3f99432c FCE |
96 | } |
97 | else | |
aca66a36 | 98 | out += c; |
72dbc915 | 99 | } |
aca66a36 JS |
100 | out += '"'; |
101 | return out; | |
72dbc915 | 102 | } |
73267b89 | 103 | |
c9efa5c9 JS |
104 | |
105 | // Delete all values from a map-like container and clear it | |
106 | // (The template is permissive -- be good!) | |
107 | template <typename T> | |
108 | void delete_map(T& t) | |
109 | { | |
110 | for (typename T::iterator i = t.begin(); i != t.end(); ++i) | |
111 | delete i->second; | |
112 | t.clear(); | |
113 | } | |
114 | ||
115 | ||
60d98537 JS |
116 | // Returns whether a string starts with the given prefix |
117 | inline bool | |
118 | startswith(const std::string & s, const char * prefix) | |
119 | { | |
120 | return (s.compare(0, std::strlen(prefix), prefix) == 0); | |
121 | } | |
122 | ||
123 | ||
124 | // Returns whether a string ends with the given suffix | |
125 | inline bool | |
126 | endswith(const std::string & s, const char * suffix) | |
127 | { | |
128 | size_t s_len = s.size(), suffix_len = std::strlen(suffix); | |
129 | if (suffix_len > s_len) | |
130 | return false; | |
131 | return (s.compare(s_len - suffix_len, suffix_len, suffix) == 0); | |
132 | } | |
133 | ||
134 | ||
73267b89 | 135 | /* vim: set sw=2 ts=8 cino=>4,n-2,{2,^-2,t0,(0,u0,w1,M1 : */ |