1 /* LBS: An LFS-inspired filesystem backup system
2 * Copyright (C) 2007 Michael Vrable
4 * Utility functions for converting various datatypes to text format (and
5 * later, for parsing them back, perhaps).
20 /* Perform URI-style escaping of a string. Bytes which cannot be represented
21 * directly are encoded in the form %xx (where "xx" is a string of two
22 * hexadecimal digits). */
23 string uri_encode(const string &in)
27 for (size_t i = 0; i < in.length(); i++) {
28 unsigned char c = in[i];
30 if (c >= '+' && c < 0x7f) {
34 sprintf(buf, "%%%02x", c);
42 /* Decoding of strings produced by uri_encode. */
43 string uri_decode(const string &in)
45 char *buf = new char[in.size() + 1];
47 const char *input = in.c_str();
50 while (*input != '\0') {
53 if (isxdigit(input[1]) && isxdigit(input[2])) {
57 *output++ = strtol(hexbuf, NULL, 16);
74 /* Return the string representation of an integer. */
75 string encode_int(long long n)
78 sprintf(buf, "%lld", n);
82 /* Return the string representation of an integer. */
83 long long parse_int(const string &s)
85 return strtoll(s.c_str(), NULL, 10);
88 /* Output a dictionary of string key/value pairs to the given output stream.
89 * The format is a sequence of lines of the form "key: value". */
90 void dict_output(ostream &o, map<string, string> dict)
92 for (map<string, string>::const_iterator i = dict.begin();
93 i != dict.end(); ++i) {
94 o << i->first << ": " << i->second << "\n";