Files
@ 4d2f3bd06950
Branch filter:
Location: libtransport.git/src/util.cpp - annotation
4d2f3bd06950
4.2 KiB
text/x-c++hdr
Check for boost_thread and also boost_thread-mt
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 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 | 324196c7f9fe 324196c7f9fe 324196c7f9fe 324196c7f9fe 324196c7f9fe 324196c7f9fe 324196c7f9fe 324196c7f9fe 324196c7f9fe 324196c7f9fe 324196c7f9fe 324196c7f9fe 324196c7f9fe 324196c7f9fe 324196c7f9fe 324196c7f9fe 324196c7f9fe 324196c7f9fe 324196c7f9fe 324196c7f9fe 324196c7f9fe 324196c7f9fe 324196c7f9fe 324196c7f9fe 324196c7f9fe 324196c7f9fe 6b81ff06d6c5 ad7461beec95 324196c7f9fe 324196c7f9fe 324196c7f9fe 324196c7f9fe 324196c7f9fe 324196c7f9fe 324196c7f9fe 324196c7f9fe 324196c7f9fe 324196c7f9fe 324196c7f9fe 324196c7f9fe 324196c7f9fe 324196c7f9fe 324196c7f9fe 324196c7f9fe 324196c7f9fe 324196c7f9fe 324196c7f9fe 324196c7f9fe 324196c7f9fe 324196c7f9fe 324196c7f9fe 324196c7f9fe 573b15bc2f3b 573b15bc2f3b 573b15bc2f3b 573b15bc2f3b 573b15bc2f3b 573b15bc2f3b 573b15bc2f3b 573b15bc2f3b 573b15bc2f3b 573b15bc2f3b 573b15bc2f3b 324196c7f9fe 324196c7f9fe 573b15bc2f3b 573b15bc2f3b 573b15bc2f3b 324196c7f9fe 324196c7f9fe 324196c7f9fe 324196c7f9fe 324196c7f9fe 324196c7f9fe 324196c7f9fe 324196c7f9fe 324196c7f9fe 324196c7f9fe 324196c7f9fe 6d64a1858b5d 6d64a1858b5d 6d64a1858b5d 6d64a1858b5d 6d64a1858b5d 6d64a1858b5d 6d64a1858b5d 6d64a1858b5d 6d64a1858b5d 6d64a1858b5d 6d64a1858b5d 6d64a1858b5d 6d64a1858b5d 6d64a1858b5d 6d64a1858b5d 6d64a1858b5d 6d64a1858b5d 6d64a1858b5d 6d64a1858b5d 6d64a1858b5d 6d64a1858b5d 6d64a1858b5d 6d64a1858b5d 6d64a1858b5d 6d64a1858b5d 6d64a1858b5d 6d64a1858b5d 6d64a1858b5d 6b81ff06d6c5 6b81ff06d6c5 6b81ff06d6c5 6b81ff06d6c5 6b81ff06d6c5 6b81ff06d6c5 6b81ff06d6c5 6b81ff06d6c5 6b81ff06d6c5 6b81ff06d6c5 6b81ff06d6c5 6b81ff06d6c5 6b81ff06d6c5 6b81ff06d6c5 6b81ff06d6c5 6b81ff06d6c5 6b81ff06d6c5 6b81ff06d6c5 6b81ff06d6c5 6b81ff06d6c5 6b81ff06d6c5 6b81ff06d6c5 6b81ff06d6c5 6b81ff06d6c5 4c334e9f1c0b 4c334e9f1c0b 4c334e9f1c0b 4c334e9f1c0b 4c334e9f1c0b 4c334e9f1c0b 4c334e9f1c0b 4c334e9f1c0b 4c334e9f1c0b ad7461beec95 ad7461beec95 ad7461beec95 ad7461beec95 ad7461beec95 ad7461beec95 ad7461beec95 ad7461beec95 ad7461beec95 ad7461beec95 ad7461beec95 ad7461beec95 ad7461beec95 ad7461beec95 ad7461beec95 ad7461beec95 ad7461beec95 ad7461beec95 ad7461beec95 ad7461beec95 ad7461beec95 ad7461beec95 ad7461beec95 ad7461beec95 ad7461beec95 ad7461beec95 ad7461beec95 ad7461beec95 ad7461beec95 ad7461beec95 ad7461beec95 ad7461beec95 324196c7f9fe 324196c7f9fe 324196c7f9fe | /**
* libtransport -- C++ library for easy XMPP Transports development
*
* Copyright (C) 2011, Jan Kaluza <hanzz.k@gmail.com>
*
* 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 2 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, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02111-1301 USA
*/
#include "transport/util.h"
#include <boost/foreach.hpp>
#include <iostream>
#include <iterator>
#include <algorithm>
#include <boost/filesystem.hpp>
#include <boost/algorithm/string.hpp>
#include <boost/numeric/conversion/cast.hpp>
using namespace boost::filesystem;
using namespace boost;
namespace Transport {
namespace Util {
void removeEverythingOlderThan(const std::vector<std::string> &dirs, time_t t) {
BOOST_FOREACH(const std::string &dir, dirs) {
path p(dir);
try {
if (!exists(p)) {
continue;
}
if (!is_directory(p)) {
continue;
}
directory_iterator end_itr;
for (directory_iterator itr(p); itr != end_itr; ++itr) {
if (last_write_time(itr->path()) < t) {
try {
if (is_regular(itr->path())) {
remove(itr->path());
}
else if (is_directory(itr->path())) {
std::vector<std::string> nextDirs;
nextDirs.push_back(itr->path().string());
removeEverythingOlderThan(nextDirs, t);
if (is_empty(itr->path())) {
remove_all(itr->path());
}
}
}
catch (const filesystem_error& ex) {
}
}
}
}
catch (const filesystem_error& ex) {
}
}
}
std::string encryptPassword(const std::string &password, const std::string &key) {
std::string encrypted;
encrypted.resize(password.size());
for (int i = 0; i < password.size(); i++) {
char c = password[i];
char keychar = key[i % key.size()];
c += keychar;
encrypted[i] = c;
}
encrypted = Swift::Base64::encode(Swift::createByteArray(encrypted));
return encrypted;
}
std::string decryptPassword(std::string &encrypted, const std::string &key) {
encrypted = Swift::byteArrayToString(Swift::Base64::decode(encrypted));
std::string password;
password.resize(encrypted.size());
for (int i = 0; i < encrypted.size(); i++) {
char c = encrypted[i];
char keychar = key[i % key.size()];
c -= keychar;
password[i] = c;
}
return password;
}
std::string serializeGroups(const std::vector<std::string> &groups) {
std::string ret;
BOOST_FOREACH(const std::string &group, groups) {
ret += group + "\n";
}
if (!ret.empty()) {
ret.erase(ret.end() - 1);
}
return ret;
}
std::vector<std::string> deserializeGroups(std::string &groups) {
std::vector<std::string> ret;
if (groups.empty()) {
return ret;
}
boost::split(ret, groups, boost::is_any_of("\n"));
if (ret.back().empty()) {
ret.erase(ret.end() - 1);
}
return ret;
}
int getRandomPort(const std::string &s) {
unsigned long r = 0;
BOOST_FOREACH(char c, s) {
r += (int) c;
}
srand(time(NULL) + r);
return 30000 + rand() % 10000;
}
#ifdef _WIN32
std::wstring utf8ToUtf16(const std::string& str)
{
try
{
if (str.empty())
return L"";
// First request the size of the required UTF-16 buffer
int numRequiredBytes = MultiByteToWideChar(CP_UTF8, 0, str.c_str(), boost::numeric_cast<int>(str.size()), NULL, 0);
if (!numRequiredBytes)
return L"";
// Allocate memory for the UTF-16 string
std::vector<wchar_t> utf16Str(numRequiredBytes);
int numConverted = MultiByteToWideChar(CP_UTF8, 0, str.c_str(), boost::numeric_cast<int>(str.size()), &utf16Str[0], numRequiredBytes);
if (!numConverted)
return L"";
std::wstring wstr(&utf16Str[0], numConverted);
return wstr;
}
catch (...)
{
// I don't believe libtransport is exception-safe so we'll just return an empty string instead
return L"";
}
}
#endif // _WIN32
}
}
|