blob: 97fa7062d9970c778633cd0458d75d236fb1910c (
plain)
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
|
#include <string>
#include <vector>
#include <iostream>
#include <sstream>
#include <stdexcept>
#include <cctype>
const char *get_home_directory(void);
int copy_file(const char *src, const char *dest);
int create_dir(const char *dir);
void tokenize(const std::string& str, std::vector<std::string>& tokens,
const std::string& delimiters);
bool find_executable(const char *name, std::string& retpath);
const std::string cmdstr_quoted(const std::string& cmd);
// stringification generics
template <typename T>
inline std::string
stringify(T t)
{
std::ostringstream s;
s << t;
return s.str ();
}
template <typename OUT, typename IN>
inline OUT lex_cast(IN const & in)
{
std::stringstream ss;
OUT out;
// NB: ss >> string out assumes that "in" renders to one word
if (!(ss << in && ss >> out))
throw std::runtime_error("bad lexical cast");
return out;
}
template <typename OUT, typename IN>
inline OUT
lex_cast_hex(IN const & in)
{
std::stringstream ss;
OUT out;
// NB: ss >> string out assumes that "in" renders to one word
if (!(ss << "0x" << std::hex << in && ss >> out))
throw std::runtime_error("bad lexical cast");
return out;
}
// 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;
std::string out, out2;
if (!(ss << in))
throw std::runtime_error("bad lexical cast");
out = ss.str(); // "in" is expected to render to more than one word
out2 += '"';
for (unsigned i=0; i<out.length(); i++)
{
char c = out[i];
if (! isprint(c))
{
out2 += '\\';
// quick & dirty octal converter
out2 += "01234567" [(c >> 6) & 0x07];
out2 += "01234567" [(c >> 3) & 0x07];
out2 += "01234567" [(c >> 0) & 0x07];
}
else if (c == '"' || c == '\\')
{
out2 += '\\';
out2 += c;
}
else
out2 += c;
}
out2 += '"';
return out2;
}
|