cumulus-util: In list-snapshot-sizes output, display backup intent values.
[cumulus.git] / util.cc
1 /* Cumulus: Smart Filesystem Backup to Dumb Servers
2  *
3  * Copyright (C) 2007  The Regents of the University of California
4  * Written by Michael Vrable <mvrable@cs.ucsd.edu>
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License along
17  * with this program; if not, write to the Free Software Foundation, Inc.,
18  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
19  */
20
21 /* Utility functions for converting various datatypes to text format (and
22  * later, for parsing them back, perhaps). */
23
24 #include <stdio.h>
25 #include <stdlib.h>
26 #include <uuid/uuid.h>
27
28 #include <iostream>
29 #include <map>
30 #include <string>
31 #include <sstream>
32
33 using std::map;
34 using std::ostream;
35 using std::string;
36
37 /* Perform URI-style escaping of a string.  Bytes which cannot be represented
38  * directly are encoded in the form %xx (where "xx" is a string of two
39  * hexadecimal digits). */
40 string uri_encode(const string &in)
41 {
42     string out;
43
44     for (size_t i = 0; i < in.length(); i++) {
45         unsigned char c = in[i];
46
47         if (c >= '+' && c < 0x7f && c != '@') {
48             out += c;
49         } else {
50             char buf[4];
51             sprintf(buf, "%%%02x", c);
52             out += buf;
53         }
54     }
55
56     return out;
57 }
58
59 /* Decoding of strings produced by uri_encode. */
60 string uri_decode(const string &in)
61 {
62     char *buf = new char[in.size() + 1];
63
64     const char *input = in.c_str();
65     char *output = buf;
66
67     while (*input != '\0') {
68         if (*input == '%') {
69             char hexbuf[4];
70             if (isxdigit(input[1]) && isxdigit(input[2])) {
71                 hexbuf[0] = input[1];
72                 hexbuf[1] = input[2];
73                 hexbuf[2] = '\0';
74                 *output++ = strtol(hexbuf, NULL, 16);
75                 input += 3;
76             } else {
77                 input++;
78             }
79         } else {
80             *output++ = *input++;
81         }
82     }
83
84     *output = '\0';
85
86     string result(buf);
87     delete[] buf;
88     return result;
89 }
90
91 /* Return the string representation of an integer.  Will try to produce output
92  * in decimal, hexadecimal, or octal according to base, though this is just
93  * advisory.  For negative numbers, will always use decimal. */
94 string encode_int(long long n, int base)
95 {
96     char buf[64];
97
98     if (n >= 0 && base == 16) {
99         sprintf(buf, "0x%llx", n);
100         return buf;
101     }
102
103     if (n > 0 && base == 8) {
104         sprintf(buf, "0%llo", n);
105         return buf;
106     }
107
108     sprintf(buf, "%lld", n);
109     return buf;
110 }
111
112 /* Parse the string representation of an integer.  Accepts decimal, octal, and
113  * hexadecimal, just as C would (recognizes the 0 and 0x prefixes). */
114 long long parse_int(const string &s)
115 {
116     return strtoll(s.c_str(), NULL, 0);
117 }