-
Notifications
You must be signed in to change notification settings - Fork 7
/
utils.hpp
127 lines (104 loc) · 4.15 KB
/
utils.hpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
/*
* Copyright (c) 2018, Psiphon Inc.
* All rights reserved.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/
#ifndef PSICASHLIB_UTILS_H
#define PSICASHLIB_UTILS_H
#include <string>
#include <map>
#include <vector>
#include <sstream>
#include <iterator>
#include "error.hpp"
namespace utils {
// From https://stackoverflow.com/a/25386444/729729
/// Can be used like `s = Stringer("lucky ", 42, '!');
template<typename T>
std::string Stringer(const T& value) {
std::ostringstream oss;
oss << value;
return oss.str();
}
template<typename T, typename ... Args>
std::string Stringer(const T& value, const Args& ... args) {
return Stringer(value) + Stringer(args...);
}
// From https://stackoverflow.com/a/43894724/729729
/// Synchronizes access to a block, using the given mutex. Used like:
/// SYNCHRONIZE_BLOCK(mutex_) {
/// ...do stuff
/// }
#define SYNCHRONIZE_BLOCK(m) for(std::unique_lock<std::recursive_mutex> lk(m); lk; lk.unlock())
/// Synchronize the current scope using the given mutex.
#define SYNCHRONIZE(m) std::lock_guard<std::recursive_mutex> synchronize_lock(m)
/// Tests if the given filepath+name exists.
bool FileExists(const std::string& filename);
/// Gets the size of the file at the given path.
psicash::error::Error FileSize(const std::string& path, uint64_t& o_size);
/// Generates a large random ID.
std::string RandomID();
/// Finds the value of the header with the given key in `headers` (case-insensitive).
/// Returns the value if found, or empty string if not found.
/// If there are multiple header values for the key, the first one is returned.
std::string FindHeaderValue(const std::map<std::string, std::vector<std::string>>& headers, const std::string& key);
/// Returns all cookie name=values in the Set-Cookie headers in a single
/// semicolon-separated string (suitable for sending in a request Cookie header).
std::string GetCookies(const std::map<std::string, std::vector<std::string>>& headers);
// From https://stackoverflow.com/a/5289170/729729
/// note: delimiter cannot contain NUL characters
template <typename Range, typename Value = typename Range::value_type>
std::string Join(Range const& elements, const char *const delimiter) {
std::ostringstream os;
auto b = begin(elements), e = end(elements);
if (b != e) {
std::copy(b, prev(e), std::ostream_iterator<Value>(os, delimiter));
b = prev(e);
}
if (b != e) {
os << *b;
}
return os.str();
}
// From https://stackoverflow.com/a/5289170/729729
/// note: imput is assumed to not contain NUL characters
template <typename Input, typename Output, typename Value = typename Output::value_type>
void Split(char delimiter, Output &output, Input const& input) {
using namespace std;
for (auto cur = begin(input), beg = cur; ; ++cur) {
if (cur == end(input) || *cur == delimiter || !*cur) {
output.insert(output.end(), Value(beg, cur));
if (cur == end(input) || !*cur)
break;
else
beg = next(cur);
}
}
}
/// Trim whitespace from start of string (in place)
void TrimLeft(std::string &s);
/// Trim whitespace from start of string (in place)
void TrimRight(std::string &s);
/// Trim whitespace from both ends of string (in place)
void Trim(std::string &s);
/// Trim whitespace from start of string (copying)
std::string TrimLeftCopy(std::string s);
/// Trim whitespace from end of string (copying)
std::string TrimRightCopy(std::string s);
/// Trim whitespace from both ends of string (copying)
std::string TrimCopy(std::string s);
}
#endif //PSICASHLIB_UTILS_H