]> sourceware.org Git - systemtap.git/blame - util.h
Merge branch 'master' of ssh://sources.redhat.com/git/systemtap
[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);
b12c8986
DB
9size_t get_file_size(const std::string &path);
10bool file_exists (const std::string &path);
e16dc041
JS
11bool copy_file(const std::string& src, const std::string& dest,
12 bool verbose=false);
1b78aef5 13int create_dir(const char *dir);
98f552c2 14int remove_file_or_dir(const char *dir);
1b78aef5
DS
15void tokenize(const std::string& str, std::vector<std::string>& tokens,
16 const std::string& delimiters);
d9736de1 17std::string find_executable(const std::string& name);
8c711d30 18const std::string cmdstr_quoted(const std::string& cmd);
a5e8d632 19std::string git_revision(const std::string& path);
36ef6d6a 20int stap_system(int verbose, const std::string& command);
4cc40e82 21int kill_stap_spawn(int sig);
72dbc915
FCE
22
23
24// stringification generics
25
26
aca66a36
JS
27template <typename IN>
28inline std::string lex_cast(IN const & in)
72dbc915 29{
aca66a36
JS
30 std::ostringstream ss;
31 if (!(ss << in))
32 throw std::runtime_error("bad lexical cast");
33 return ss.str();
72dbc915
FCE
34}
35
36
aca66a36
JS
37template <typename OUT>
38inline OUT lex_cast(std::string const & in)
72dbc915 39{
aca66a36 40 std::istringstream ss(in);
72dbc915 41 OUT out;
aca66a36 42 if (!(ss >> out && ss.eof()))
72dbc915
FCE
43 throw std::runtime_error("bad lexical cast");
44 return out;
45}
46
47
aca66a36
JS
48template <typename IN>
49inline std::string
72dbc915
FCE
50lex_cast_hex(IN const & in)
51{
aca66a36
JS
52 std::ostringstream ss;
53 if (!(ss << std::showbase << std::hex << in))
72dbc915 54 throw std::runtime_error("bad lexical cast");
aca66a36 55 return ss.str();
72dbc915
FCE
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;
72dbc915
FCE
66 if (!(ss << in))
67 throw std::runtime_error("bad lexical cast");
aca66a36
JS
68 return lex_cast_qstring(ss.str());
69}
70
71
72template <>
73inline std::string
74lex_cast_qstring(std::string const & in)
75{
76 std::string out;
77 out += '"';
78 for (const char *p = in.c_str(); *p; ++p)
72dbc915 79 {
aca66a36 80 unsigned char c = *p;
3f99432c
FCE
81 if (! isprint(c))
82 {
aca66a36 83 out += '\\';
3f99432c 84 // quick & dirty octal converter
aca66a36
JS
85 out += "01234567" [(c >> 6) & 0x07];
86 out += "01234567" [(c >> 3) & 0x07];
87 out += "01234567" [(c >> 0) & 0x07];
dff50e09 88 }
3f99432c
FCE
89 else if (c == '"' || c == '\\')
90 {
aca66a36
JS
91 out += '\\';
92 out += c;
3f99432c
FCE
93 }
94 else
aca66a36 95 out += c;
72dbc915 96 }
aca66a36
JS
97 out += '"';
98 return out;
72dbc915 99}
73267b89 100
c9efa5c9
JS
101
102// Delete all values from a map-like container and clear it
103// (The template is permissive -- be good!)
104template <typename T>
105void delete_map(T& t)
106{
107 for (typename T::iterator i = t.begin(); i != t.end(); ++i)
108 delete i->second;
109 t.clear();
110}
111
112
73267b89 113/* vim: set sw=2 ts=8 cino=>4,n-2,{2,^-2,t0,(0,u0,w1,M1 : */
This page took 0.071282 seconds and 5 git commands to generate.