]> sourceware.org Git - systemtap.git/blame - util.h
Fix typo in mq_timedreceive probe point.
[systemtap.git] / util.h
CommitLineData
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 8const char *get_home_directory(void);
1b78aef5 9int copy_file(const char *src, const char *dest);
1b78aef5 10int create_dir(const char *dir);
2035bcd4 11int remove_file_or_dir(const char *dir);
1b78aef5
DS
12void tokenize(const std::string& str, std::vector<std::string>& tokens,
13 const std::string& delimiters);
d9736de1 14std::string find_executable(const std::string& name);
8c711d30 15const std::string cmdstr_quoted(const std::string& cmd);
a5e8d632 16std::string git_revision(const std::string& path);
4cc40e82
JS
17int stap_system(const char *command);
18int kill_stap_spawn(int sig);
72dbc915
FCE
19
20
21// stringification generics
22
23
24template <typename T>
25inline std::string
26stringify(T t)
27{
28 std::ostringstream s;
29 s << t;
30 return s.str ();
31}
32
33
dff50e09 34template <typename OUT, typename IN>
72dbc915
FCE
35inline 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 46template <typename OUT, typename IN>
72dbc915
FCE
47inline OUT
48lex_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 61template <typename IN>
72dbc915
FCE
62inline std::string
63lex_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 : */
This page took 0.063053 seconds and 5 git commands to generate.