xdrpp
RFC4506 XDR compiler and message library
printer.cc
1 
2 #include <iomanip>
3 #include <xdrpp/printer.h>
4 
5 namespace xdr {
6 
7 std::string
8 escape_string(const std::string &s)
9 {
10  std::ostringstream os;
11  os << '\"';
12  for (char c : s) {
13  if (c >= 0x20 && c < 0x7f) {
14  if (c == '\"' || c == '\\')
15  os << '\\';
16  os << c;
17  }
18  else
19  switch (c) {
20  case '\t':
21  os << "\\t";
22  break;
23  case '\r':
24  os << "\\r";
25  break;
26  case '\n':
27  os << "\\n";
28  break;
29  case '\"':
30  os << "\\\"";
31  break;
32  default:
33  os << "\\x" << std::setw(2) << std::setfill('0')
34  << std::oct << (unsigned(c) & 0xff);
35  break;
36  }
37  }
38  os << '\"';
39  return os.str();
40 }
41 
42 std::string
43 hexdump(const void *_data, size_t len)
44 {
45  const std::uint8_t *data = static_cast<const std::uint8_t *>(_data);
46  std::ostringstream os;
47  os.fill('0');
48  os.setf(std::ios::hex, std::ios::basefield);
49  for (; len > 0; ++data, --len)
50  os << std::setw(2) << unsigned(*data);
51  return os.str();
52 }
53 
54 namespace detail {
55 
56 std::ostream &
57 Printer::bol(const char *name)
58 {
59  if (skipnl_)
60  skipnl_ = false;
61  else {
62  if (comma_)
63  buf_ << ",";
64  buf_ << std::endl << std::string(indent_, ' ');
65  }
66  comma_ = true;
67  if (name)
68  buf_ << name << " = ";
69  return buf_;
70 }
71 
72 }
73 
74 }
std::string escape_string(const std::string &s)
Use hex escapes for non-printable characters, and prefix backslashes and quotes with backslash...
Definition: printer.cc:8
Most of the xdrpp library is encapsulated in the xdr namespace.
Definition: arpc.cc:4
std::string hexdump(const void *_data, size_t len)
Turn a string into a double-length sequence of hex nibbles.
Definition: printer.cc:43
Support for pretty-printing XDR data types.