Replace boost::scoped_ptr with std::unique_ptr.
[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 <stdarg.h>
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 #include "util.h"
36
37 using std::map;
38 using std::ostream;
39 using std::string;
40
41 /* Like sprintf, but returns the output as an allocated string. */
42 string string_printf(const char *fmt, ...)
43 {
44     va_list args;
45     char *output = NULL;
46     string result;
47
48     va_start(args, fmt);
49     if (vasprintf(&output, fmt, args) < 0) {
50         /* TODO: Error signalling? */
51         return result;
52     }
53     result = output;
54     va_end(args);
55
56     free(output);
57     return result;
58 }
59
60 /* Perform URI-style escaping of a string.  Bytes which cannot be represented
61  * directly are encoded in the form %xx (where "xx" is a string of two
62  * hexadecimal digits). */
63 string uri_encode(const string &in)
64 {
65     string out;
66
67     for (size_t i = 0; i < in.length(); i++) {
68         unsigned char c = in[i];
69
70         if (c >= '+' && c < 0x7f && c != '@') {
71             out += c;
72         } else {
73             char buf[4];
74             sprintf(buf, "%%%02x", c);
75             out += buf;
76         }
77     }
78
79     return out;
80 }
81
82 /* Decoding of strings produced by uri_encode. */
83 string uri_decode(const string &in)
84 {
85     char *buf = new char[in.size() + 1];
86
87     const char *input = in.c_str();
88     char *output = buf;
89
90     while (*input != '\0') {
91         if (*input == '%') {
92             char hexbuf[4];
93             if (isxdigit(input[1]) && isxdigit(input[2])) {
94                 hexbuf[0] = input[1];
95                 hexbuf[1] = input[2];
96                 hexbuf[2] = '\0';
97                 *output++ = strtol(hexbuf, NULL, 16);
98                 input += 3;
99             } else {
100                 input++;
101             }
102         } else {
103             *output++ = *input++;
104         }
105     }
106
107     *output = '\0';
108
109     string result(buf);
110     delete[] buf;
111     return result;
112 }
113
114 /* Return the string representation of an integer.  Will try to produce output
115  * in decimal, hexadecimal, or octal according to base, though this is just
116  * advisory.  For negative numbers, will always use decimal. */
117 string encode_int(long long n, int base)
118 {
119     if (n >= 0 && base == 16)
120         return string_printf("0x%llx", n);
121     else if (n > 0 && base == 8)
122         return string_printf("0%llo", n);
123     else
124         return string_printf("%lld", n);
125 }
126
127 /* Parse the string representation of an integer.  Accepts decimal, octal, and
128  * hexadecimal, just as C would (recognizes the 0 and 0x prefixes). */
129 long long parse_int(const string &s)
130 {
131     return strtoll(s.c_str(), NULL, 0);
132 }
133
134 /* Mark a file descriptor as close-on-exec. */
135 void cloexec(int fd)
136 {
137     long flags = fcntl(fd, F_GETFD);
138
139     if (flags < 0)
140         return;
141
142     fcntl(fd, F_SETFD, flags | FD_CLOEXEC);
143 }
144
145 /* Report a fatal error and exit. */
146 void fatal(string msg)
147 {
148     fprintf(stderr, "FATAL: %s\n", msg.c_str());
149     exit(1);
150 }
151
152 /* Available time formats. */
153 const char TimeFormat::FORMAT_FILENAME[] = "%Y%m%dT%H%M%S";
154 const char TimeFormat::FORMAT_ISO8601[] = "%Y-%m-%d %H:%M:%S";
155 const char TimeFormat::FORMAT_LOCALTIME[] = "%Y-%m-%d %H:%M:%S %z";
156
157 static size_t MAX_TIMESTAMP_LENGTH = 1024;
158
159 std::string TimeFormat::format(time_t timestamp, const char *format, bool utc)
160 {
161     struct tm time_buf;
162
163     if (utc)
164         gmtime_r(&timestamp, &time_buf);
165     else
166         localtime_r(&timestamp, &time_buf);
167
168     char buffer[MAX_TIMESTAMP_LENGTH];
169     strftime(buffer, MAX_TIMESTAMP_LENGTH, format, &time_buf);
170     return string(buffer);
171 }