]>
Commit | Line | Data |
---|---|---|
1b78aef5 DS |
1 | #include <string> |
2 | #include <vector> | |
72dbc915 FCE |
3 | #include <iostream> |
4 | #include <sstream> | |
5 | #include <stdexcept> | |
3f99432c | 6 | #include <cctype> |
1b78aef5 | 7 | |
72dbc915 | 8 | const char *get_home_directory(void); |
1b78aef5 | 9 | int copy_file(const char *src, const char *dest); |
1b78aef5 | 10 | int create_dir(const char *dir); |
2035bcd4 | 11 | int remove_file_or_dir(const char *dir); |
1b78aef5 DS |
12 | void tokenize(const std::string& str, std::vector<std::string>& tokens, |
13 | const std::string& delimiters); | |
d9736de1 | 14 | std::string find_executable(const std::string& name); |
8c711d30 | 15 | const std::string cmdstr_quoted(const std::string& cmd); |
a5e8d632 | 16 | std::string git_revision(const std::string& path); |
4cc40e82 JS |
17 | int stap_system(const char *command); |
18 | int kill_stap_spawn(int sig); | |
72dbc915 FCE |
19 | |
20 | ||
21 | // stringification generics | |
22 | ||
23 | ||
24 | template <typename T> | |
25 | inline std::string | |
26 | stringify(T t) | |
27 | { | |
28 | std::ostringstream s; | |
29 | s << t; | |
30 | return s.str (); | |
31 | } | |
32 | ||
33 | ||
dff50e09 | 34 | template <typename OUT, typename IN> |
72dbc915 FCE |
35 | inline OUT lex_cast(IN const & in) |
36 | { | |
37 | std::stringstream ss; | |
38 | OUT out; | |
7e41d3dc | 39 | // NB: ss >> string out assumes that "in" renders to one word |
72dbc915 FCE |
40 | if (!(ss << in && ss >> out)) |
41 | throw std::runtime_error("bad lexical cast"); | |
42 | return out; | |
43 | } | |
44 | ||
45 | ||
dff50e09 | 46 | template <typename OUT, typename IN> |
72dbc915 FCE |
47 | inline OUT |
48 | lex_cast_hex(IN const & in) | |
49 | { | |
50 | std::stringstream ss; | |
51 | OUT out; | |
7e41d3dc FCE |
52 | // NB: ss >> string out assumes that "in" renders to one word |
53 | if (!(ss << "0x" << std::hex << in && ss >> out)) | |
72dbc915 FCE |
54 | throw std::runtime_error("bad lexical cast"); |
55 | return out; | |
56 | } | |
57 | ||
58 | ||
59 | // Return as quoted string, so that when compiled as a C literal, it | |
60 | // would print to the user out nicely. | |
dff50e09 | 61 | template <typename IN> |
72dbc915 FCE |
62 | inline std::string |
63 | lex_cast_qstring(IN const & in) | |
64 | { | |
65 | std::stringstream ss; | |
66 | std::string out, out2; | |
67 | if (!(ss << in)) | |
68 | throw std::runtime_error("bad lexical cast"); | |
7e41d3dc | 69 | out = ss.str(); // "in" is expected to render to more than one word |
72dbc915 FCE |
70 | out2 += '"'; |
71 | for (unsigned i=0; i<out.length(); i++) | |
72 | { | |
3f99432c FCE |
73 | char c = out[i]; |
74 | if (! isprint(c)) | |
75 | { | |
76 | out2 += '\\'; | |
77 | // quick & dirty octal converter | |
78 | out2 += "01234567" [(c >> 6) & 0x07]; | |
79 | out2 += "01234567" [(c >> 3) & 0x07]; | |
80 | out2 += "01234567" [(c >> 0) & 0x07]; | |
dff50e09 | 81 | } |
3f99432c FCE |
82 | else if (c == '"' || c == '\\') |
83 | { | |
84 | out2 += '\\'; | |
85 | out2 += c; | |
86 | } | |
87 | else | |
88 | out2 += c; | |
72dbc915 FCE |
89 | } |
90 | out2 += '"'; | |
91 | return out2; | |
92 | } | |
73267b89 JS |
93 | |
94 | /* vim: set sw=2 ts=8 cino=>4,n-2,{2,^-2,t0,(0,u0,w1,M1 : */ |