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