]> sourceware.org Git - systemtap.git/blame - util.h
Fix the edge-case of MAXTRYLOCK=0
[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);
0da3e7a0 15bool in_group_id (gid_t target_gid);
1b78aef5
DS
16void tokenize(const std::string& str, std::vector<std::string>& tokens,
17 const std::string& delimiters);
63b4fd14
SC
18std::string find_executable(const std::string& name,
19 const std::string& env_path = "PATH");
8c711d30 20const std::string cmdstr_quoted(const std::string& cmd);
a5e8d632 21std::string git_revision(const std::string& path);
36ef6d6a 22int stap_system(int verbose, const std::string& command);
4cc40e82 23int kill_stap_spawn(int sig);
c0d1b5a0 24void assert_regexp_match (const std::string& name, const std::string& value, const std::string& re);
72dbc915
FCE
25
26// stringification generics
27
28
aca66a36
JS
29template <typename IN>
30inline std::string lex_cast(IN const & in)
72dbc915 31{
aca66a36
JS
32 std::ostringstream ss;
33 if (!(ss << in))
34 throw std::runtime_error("bad lexical cast");
35 return ss.str();
72dbc915
FCE
36}
37
38
aca66a36
JS
39template <typename OUT>
40inline OUT lex_cast(std::string const & in)
72dbc915 41{
aca66a36 42 std::istringstream ss(in);
72dbc915 43 OUT out;
aca66a36 44 if (!(ss >> out && ss.eof()))
72dbc915
FCE
45 throw std::runtime_error("bad lexical cast");
46 return out;
47}
48
49
aca66a36
JS
50template <typename IN>
51inline std::string
72dbc915
FCE
52lex_cast_hex(IN const & in)
53{
aca66a36
JS
54 std::ostringstream ss;
55 if (!(ss << std::showbase << std::hex << in))
72dbc915 56 throw std::runtime_error("bad lexical cast");
aca66a36 57 return ss.str();
72dbc915
FCE
58}
59
60
61// Return as quoted string, so that when compiled as a C literal, it
62// would print to the user out nicely.
dff50e09 63template <typename IN>
72dbc915
FCE
64inline std::string
65lex_cast_qstring(IN const & in)
66{
67 std::stringstream ss;
72dbc915
FCE
68 if (!(ss << in))
69 throw std::runtime_error("bad lexical cast");
aca66a36
JS
70 return lex_cast_qstring(ss.str());
71}
72
73
74template <>
75inline std::string
76lex_cast_qstring(std::string const & in)
77{
78 std::string out;
79 out += '"';
80 for (const char *p = in.c_str(); *p; ++p)
72dbc915 81 {
aca66a36 82 unsigned char c = *p;
3f99432c
FCE
83 if (! isprint(c))
84 {
aca66a36 85 out += '\\';
3f99432c 86 // quick & dirty octal converter
aca66a36
JS
87 out += "01234567" [(c >> 6) & 0x07];
88 out += "01234567" [(c >> 3) & 0x07];
89 out += "01234567" [(c >> 0) & 0x07];
dff50e09 90 }
3f99432c
FCE
91 else if (c == '"' || c == '\\')
92 {
aca66a36
JS
93 out += '\\';
94 out += c;
3f99432c
FCE
95 }
96 else
aca66a36 97 out += c;
72dbc915 98 }
aca66a36
JS
99 out += '"';
100 return out;
72dbc915 101}
73267b89 102
c9efa5c9
JS
103
104// Delete all values from a map-like container and clear it
105// (The template is permissive -- be good!)
106template <typename T>
107void delete_map(T& t)
108{
109 for (typename T::iterator i = t.begin(); i != t.end(); ++i)
110 delete i->second;
111 t.clear();
112}
113
114
73267b89 115/* vim: set sw=2 ts=8 cino=>4,n-2,{2,^-2,t0,(0,u0,w1,M1 : */
This page took 0.07833 seconds and 5 git commands to generate.