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