utils.cpp 6.92 KB
Newer Older
Ronan's avatar
Ronan committed
1 2
/*
 * utils.cpp
3
 * Copyright (C) 2010-2018 Belledonne Communications SARL
Ronan's avatar
Ronan committed
4
 *
Ghislain MARY's avatar
Ghislain MARY committed
5 6 7 8
 * 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.
Ronan's avatar
Ronan committed
9 10 11 12 13 14 15
 *
 * 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
Ghislain MARY's avatar
Ghislain MARY committed
16 17
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
Ronan's avatar
Ronan committed
18 19
 */

20
#include <algorithm>
Erwan Croze's avatar
Erwan Croze committed
21
#include <cstdlib>
Erwan Croze's avatar
Erwan Croze committed
22
#include <list>
23
#include <sstream>
Erwan Croze's avatar
Erwan Croze committed
24

Ghislain MARY's avatar
Ghislain MARY committed
25
#include <bctoolbox/port.h>
26
#include <bctoolbox/charconv.h>
Ghislain MARY's avatar
Ghislain MARY committed
27

28
#include "linphone/utils/utils.h"
Ronan's avatar
Ronan committed
29

30 31 32 33
#include "logger/logger.h"

#include "private.h"

34 35
// =============================================================================

Ronan's avatar
Ronan committed
36 37
using namespace std;

38
LINPHONE_BEGIN_NAMESPACE
Ronan's avatar
Ronan committed
39

40 41
// -----------------------------------------------------------------------------

Ronan's avatar
Ronan committed
42 43 44 45 46 47 48 49 50 51 52 53 54
bool Utils::iequals (const string &a, const string &b) {
	size_t size = a.size();
	if (b.size() != size)
		return false;

	for (size_t i = 0; i < size; ++i) {
		if (tolower(a[i]) != tolower(b[i]))
			return false;
	}

	return true;
}

55 56
// -----------------------------------------------------------------------------

Ronan's avatar
Ronan committed
57 58 59 60 61 62 63 64 65 66
vector<string> Utils::split (const string &str, const string &delimiter) {
	vector<string> out;

	size_t pos = 0, oldPos = 0;
	for (; (pos = str.find(delimiter, pos)) != string::npos; oldPos = pos + 1, pos = oldPos)
		out.push_back(str.substr(oldPos, pos - oldPos));
	out.push_back(str.substr(oldPos));

	return out;
}
Ronan's avatar
Ronan committed
67

68 69
// -----------------------------------------------------------------------------

70 71
#ifndef __ANDROID__
#define TO_STRING_IMPL(TYPE) \
72
	string Utils::toString (TYPE val) { \
73 74 75 76
		return to_string(val); \
	}
#else
#define TO_STRING_IMPL(TYPE) \
77
	string Utils::toString (TYPE val) { \
78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93
		ostringstream os; \
		os << val; \
		return os.str(); \
	}
#endif // ifndef __ANDROID__

TO_STRING_IMPL(int)
TO_STRING_IMPL(long)
TO_STRING_IMPL(long long)
TO_STRING_IMPL(unsigned)
TO_STRING_IMPL(unsigned long)
TO_STRING_IMPL(unsigned long long)
TO_STRING_IMPL(float)
TO_STRING_IMPL(double)
TO_STRING_IMPL(long double)

94 95
#undef TO_STRING_IMPL

96 97 98 99 100 101
string Utils::toString (const void *val) {
	ostringstream ss;
	ss << val;
	return ss.str();
}

102 103 104
// -----------------------------------------------------------------------------

#define STRING_TO_NUMBER_IMPL(TYPE, SUFFIX) \
105
	TYPE Utils::sto ## SUFFIX (const string &str, size_t *idx, int base) { \
106 107
		return sto ## SUFFIX(str.c_str(), idx, base); \
	} \
108
	TYPE Utils::sto ## SUFFIX (const char *str, size_t *idx, int base) { \
109 110 111 112 113 114 115 116
		char *p; \
		TYPE v = strto ## SUFFIX(str, &p, base); \
		if (idx) \
			*idx = static_cast<size_t>(p - str); \
		return v; \
	} \

#define STRING_TO_NUMBER_IMPL_BASE_LESS(TYPE, SUFFIX) \
117
	TYPE Utils::sto ## SUFFIX(const string &str, size_t * idx) { \
118 119
		return sto ## SUFFIX(str.c_str(), idx); \
	} \
120
	TYPE Utils::sto ## SUFFIX(const char *str, size_t * idx) { \
121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139
		char *p; \
		TYPE v = strto ## SUFFIX(str, &p); \
		if (idx) \
			*idx = static_cast<size_t>(p - str); \
		return v; \
	} \

#define strtoi(STR, IDX, BASE) static_cast<int>(strtol(STR, IDX, BASE))
STRING_TO_NUMBER_IMPL(int, i)
#undef strtoi

STRING_TO_NUMBER_IMPL(long long, ll)
STRING_TO_NUMBER_IMPL(unsigned long long, ull)

STRING_TO_NUMBER_IMPL_BASE_LESS(double, d)
STRING_TO_NUMBER_IMPL_BASE_LESS(float, f)

#undef STRING_TO_NUMBER_IMPL
#undef STRING_TO_NUMBER_IMPL_BASE_LESS
140

141
bool Utils::stob (const string &str) {
142 143 144 145
	const string lowerStr = stringToLower(str);
	return !lowerStr.empty() && (lowerStr == "true" || lowerStr == "1");
}

146
// -----------------------------------------------------------------------------
Benjamin REIS's avatar
Benjamin REIS committed
147

148 149 150 151 152 153
string Utils::stringToLower (const string &str) {
	string result(str.size(), ' ');
	transform(str.cbegin(), str.cend(), result.begin(), ::tolower);
	return result;
}

154 155
// -----------------------------------------------------------------------------

Ghislain MARY's avatar
Ghislain MARY committed
156 157 158 159
char *Utils::utf8ToChar (uint32_t ic) {
	char *result = new char[5];
	int size = 0;
	if (ic < 0x80) {
160
		result[0] = static_cast<char>(ic);
Ghislain MARY's avatar
Ghislain MARY committed
161 162
		size = 1;
	} else if (ic < 0x800) {
163 164
		result[1] = static_cast<char>(0x80 + ((ic & 0x3F)));
		result[0] = static_cast<char>(0xC0 + ((ic >> 6) & 0x1F));
Ghislain MARY's avatar
Ghislain MARY committed
165 166
		size = 2;
	} else if (ic < 0x100000) {
167 168 169
		result[2] = static_cast<char>(0x80 + (ic & 0x3F));
		result[1] = static_cast<char>(0x80 + ((ic >> 6) & 0x3F));
		result[0] = static_cast<char>(0xE0 + ((ic >> 12) & 0xF));
Ghislain MARY's avatar
Ghislain MARY committed
170 171
		size = 3;
	} else if (ic < 0x110000) {
172 173 174 175
		result[3] = static_cast<char>(0x80 + (ic & 0x3F));
		result[2] = static_cast<char>(0x80 + ((ic >> 6) & 0x3F));
		result[1] = static_cast<char>(0x80 + ((ic >> 12) & 0x3F));
		result[0] = static_cast<char>(0xF0 + ((ic >> 18) & 0x7));
Ghislain MARY's avatar
Ghislain MARY committed
176 177 178 179 180 181
		size = 4;
	}
	result[size] = '\0';
	return result;
}

182 183 184 185 186 187
string Utils::trim (const string &str) {
	auto itFront = find_if_not(str.begin(), str.end(), [] (int c) { return isspace(c); });
	auto itBack = find_if_not(str.rbegin(), str.rend(), [] (int c) { return isspace(c); }).base();
	return (itBack <= itFront ? string() : string(itFront, itBack));
}

188 189
// -----------------------------------------------------------------------------

190
tm Utils::getTimeTAsTm (time_t time) {
191 192 193
	#ifdef _WIN32
		return *gmtime(&time);
	#else
194 195 196
		tm result;
		return *gmtime_r(&time, &result);
	#endif
197 198
}

199
time_t Utils::getTmAsTimeT (const tm &time) {
200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226
	time_t result;

	#if defined(LINPHONE_WINDOWS_UNIVERSAL) || defined(LINPHONE_MSC_VER_GREATER_19)
		long adjust_timezone;
	#else
		time_t adjust_timezone;
	#endif

	#if TARGET_IPHONE_SIMULATOR
		result = timegm(&const_cast<tm &>(time));
		adjust_timezone = 0;
	#else
		result = mktime(&const_cast<tm &>(time));

		#if defined(LINPHONE_WINDOWS_UNIVERSAL) || defined(LINPHONE_MSC_VER_GREATER_19)
			_get_timezone(&adjust_timezone);
		#else
			adjust_timezone = timezone;
		#endif
	#endif

	if (result == (time_t)-1) {
		lError() << "mktime failed: " << strerror(errno);
		return (time_t)-1;
	}

	return result - (time_t)adjust_timezone;
227 228
}

229 230
// -----------------------------------------------------------------------------

231
// TODO: Improve perf!!! Avoid c <--> cpp string conversions.
232
string Utils::localeToUtf8 (const string &str) {
233 234 235 236
	char *cStr = bctbx_locale_to_utf8(str.c_str());
	string utf8Str = cStringToCppString(cStr);
	bctbx_free(cStr);
	return utf8Str;
237 238 239
}

string Utils::utf8ToLocale (const string &str) {
240 241 242 243
	char *cStr = bctbx_utf8_to_locale(str.c_str());
	string localeStr = cStringToCppString(cStr);
	bctbx_free(cStr);
	return localeStr;
244 245 246
}

string Utils::convertString (const string &str, const string &from, const string &to) {
247 248 249 250
	char *cStr = bctbx_convert_from_to(str.c_str(), from.c_str(), to.c_str());
	string convertedStr = cStringToCppString(cStr);
	bctbx_free(cStr);
	return convertedStr;
251 252
}

253
LINPHONE_END_NAMESPACE