Put updated copyright statements in all source files.
[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 <uuid/uuid.h>
26
27 #include <iostream>
28 #include <map>
29 #include <string>
30 #include <sstream>
31
32 using std::map;
33 using std::ostream;
34 using std::string;
35
36 /* Perform URI-style escaping of a string.  Bytes which cannot be represented
37  * directly are encoded in the form %xx (where "xx" is a string of two
38  * hexadecimal digits). */
39 string uri_encode(const string &in)
40 {
41     string out;
42
43     for (size_t i = 0; i < in.length(); i++) {
44         unsigned char c = in[i];
45
46         if (c >= '+' && c < 0x7f && c != '@') {
47             out += c;
48         } else {
49             char buf[4];
50             sprintf(buf, "%%%02x", c);
51             out += buf;
52         }
53     }
54
55     return out;
56 }
57
58 /* Decoding of strings produced by uri_encode. */
59 string uri_decode(const string &in)
60 {
61     char *buf = new char[in.size() + 1];
62
63     const char *input = in.c_str();
64     char *output = buf;
65
66     while (*input != '\0') {
67         if (*input == '%') {
68             char hexbuf[4];
69             if (isxdigit(input[1]) && isxdigit(input[2])) {
70                 hexbuf[0] = input[1];
71                 hexbuf[1] = input[2];
72                 hexbuf[2] = '\0';
73                 *output++ = strtol(hexbuf, NULL, 16);
74                 input += 3;
75             } else {
76                 input++;
77             }
78         } else {
79             *output++ = *input++;
80         }
81     }
82
83     *output = '\0';
84
85     string result(buf);
86     delete[] buf;
87     return result;
88 }
89
90 /* Return the string representation of an integer.  Will try to produce output
91  * in decimal, hexadecimal, or octal according to base, though this is just
92  * advisory.  For negative numbers, will always use decimal. */
93 string encode_int(long long n, int base)
94 {
95     char buf[64];
96
97     if (n >= 0 && base == 16) {
98         sprintf(buf, "0x%llx", n);
99         return buf;
100     }
101
102     if (n > 0 && base == 8) {
103         sprintf(buf, "0%llo", n);
104         return buf;
105     }
106
107     sprintf(buf, "%lld", n);
108     return buf;
109 }
110
111 /* Parse the string representation of an integer.  Accepts decimal, octal, and
112  * hexadecimal, just as C would (recognizes the 0 and 0x prefixes). */
113 long long parse_int(const string &s)
114 {
115     return strtoll(s.c_str(), NULL, 0);
116 }