The map::at method does not always exist, so instead use map::find.
[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 <fcntl.h>
27 #include <unistd.h>
28 #include <uuid/uuid.h>
29
30 #include <iostream>
31 #include <map>
32 #include <string>
33 #include <sstream>
34
35 using std::map;
36 using std::ostream;
37 using std::string;
38
39 /* Perform URI-style escaping of a string.  Bytes which cannot be represented
40  * directly are encoded in the form %xx (where "xx" is a string of two
41  * hexadecimal digits). */
42 string uri_encode(const string &in)
43 {
44     string out;
45
46     for (size_t i = 0; i < in.length(); i++) {
47         unsigned char c = in[i];
48
49         if (c >= '+' && c < 0x7f && c != '@') {
50             out += c;
51         } else {
52             char buf[4];
53             sprintf(buf, "%%%02x", c);
54             out += buf;
55         }
56     }
57
58     return out;
59 }
60
61 /* Decoding of strings produced by uri_encode. */
62 string uri_decode(const string &in)
63 {
64     char *buf = new char[in.size() + 1];
65
66     const char *input = in.c_str();
67     char *output = buf;
68
69     while (*input != '\0') {
70         if (*input == '%') {
71             char hexbuf[4];
72             if (isxdigit(input[1]) && isxdigit(input[2])) {
73                 hexbuf[0] = input[1];
74                 hexbuf[1] = input[2];
75                 hexbuf[2] = '\0';
76                 *output++ = strtol(hexbuf, NULL, 16);
77                 input += 3;
78             } else {
79                 input++;
80             }
81         } else {
82             *output++ = *input++;
83         }
84     }
85
86     *output = '\0';
87
88     string result(buf);
89     delete[] buf;
90     return result;
91 }
92
93 /* Return the string representation of an integer.  Will try to produce output
94  * in decimal, hexadecimal, or octal according to base, though this is just
95  * advisory.  For negative numbers, will always use decimal. */
96 string encode_int(long long n, int base)
97 {
98     char buf[64];
99
100     if (n >= 0 && base == 16) {
101         sprintf(buf, "0x%llx", n);
102         return buf;
103     }
104
105     if (n > 0 && base == 8) {
106         sprintf(buf, "0%llo", n);
107         return buf;
108     }
109
110     sprintf(buf, "%lld", n);
111     return buf;
112 }
113
114 /* Parse the string representation of an integer.  Accepts decimal, octal, and
115  * hexadecimal, just as C would (recognizes the 0 and 0x prefixes). */
116 long long parse_int(const string &s)
117 {
118     return strtoll(s.c_str(), NULL, 0);
119 }
120
121 /* Mark a file descriptor as close-on-exec. */
122 void cloexec(int fd)
123 {
124     long flags = fcntl(fd, F_GETFD);
125
126     if (flags < 0)
127         return;
128
129     fcntl(fd, F_SETFD, flags | FD_CLOEXEC);
130 }
131
132 /* Report a fatal error and exit. */
133 void fatal(string msg)
134 {
135     fprintf(stderr, "FATAL: %s\n", msg.c_str());
136     exit(1);
137 }