utils.cpp 5.93 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>
22
#include <sstream>
Erwan Croze's avatar
Erwan Croze committed
23

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

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

29 30
// =============================================================================

Ronan's avatar
Ronan committed
31 32
using namespace std;

33
LINPHONE_BEGIN_NAMESPACE
Ronan's avatar
Ronan committed
34

35 36
// -----------------------------------------------------------------------------

Ronan's avatar
Ronan committed
37 38 39 40 41 42 43 44 45 46 47 48 49
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;
}

50 51
// -----------------------------------------------------------------------------

Ronan's avatar
Ronan committed
52 53 54 55 56 57 58 59 60 61
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
62

63 64
// -----------------------------------------------------------------------------

65 66
#ifndef __ANDROID__
#define TO_STRING_IMPL(TYPE) \
67
	string Utils::toString (TYPE val) { \
68 69 70 71
		return to_string(val); \
	}
#else
#define TO_STRING_IMPL(TYPE) \
72
	string Utils::toString (TYPE val) { \
73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88
		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)

89 90
#undef TO_STRING_IMPL

91 92 93 94 95 96
string Utils::toString (const void *val) {
	ostringstream ss;
	ss << val;
	return ss.str();
}

97 98 99
// -----------------------------------------------------------------------------

#define STRING_TO_NUMBER_IMPL(TYPE, SUFFIX) \
100
	TYPE Utils::sto ## SUFFIX (const string &str, size_t *idx, int base) { \
101 102
		return sto ## SUFFIX(str.c_str(), idx, base); \
	} \
103
	TYPE Utils::sto ## SUFFIX (const char *str, size_t *idx, int base) { \
104 105 106 107 108 109 110 111
		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) \
112
	TYPE Utils::sto ## SUFFIX(const string &str, size_t * idx) { \
113 114
		return sto ## SUFFIX(str.c_str(), idx); \
	} \
115
	TYPE Utils::sto ## SUFFIX(const char *str, size_t * idx) { \
116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134
		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
135

136
bool Utils::stob (const string &str) {
137 138 139 140
	const string lowerStr = stringToLower(str);
	return !lowerStr.empty() && (lowerStr == "true" || lowerStr == "1");
}

141
// -----------------------------------------------------------------------------
Benjamin REIS's avatar
Benjamin REIS committed
142

143 144 145 146 147 148
string Utils::stringToLower (const string &str) {
	string result(str.size(), ' ');
	transform(str.cbegin(), str.cend(), result.begin(), ::tolower);
	return result;
}

149 150
// -----------------------------------------------------------------------------

Ghislain MARY's avatar
Ghislain MARY committed
151 152 153 154
char *Utils::utf8ToChar (uint32_t ic) {
	char *result = new char[5];
	int size = 0;
	if (ic < 0x80) {
155
		result[0] = static_cast<char>(ic);
Ghislain MARY's avatar
Ghislain MARY committed
156 157
		size = 1;
	} else if (ic < 0x800) {
158 159
		result[1] = static_cast<char>(0x80 + ((ic & 0x3F)));
		result[0] = static_cast<char>(0xC0 + ((ic >> 6) & 0x1F));
Ghislain MARY's avatar
Ghislain MARY committed
160 161
		size = 2;
	} else if (ic < 0x100000) {
162 163 164
		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
165 166
		size = 3;
	} else if (ic < 0x110000) {
167 168 169 170
		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
171 172 173 174 175 176
		size = 4;
	}
	result[size] = '\0';
	return result;
}

177 178
// -----------------------------------------------------------------------------

179
tm Utils::getTimeTAsTm (time_t time) {
180
	tm result;
181
	return *gmtime_r(&time, &result);
182 183
}

184
long Utils::getTmAsTimeT (const tm &time) {
185 186 187
	return timegm(&const_cast<tm &>(time));
}

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

190
// TODO: Improve perf!!! Avoid c <--> cpp string conversions.
191
string Utils::localeToUtf8 (const string &str) {
192 193 194 195
	char *cStr = bctbx_locale_to_utf8(str.c_str());
	string utf8Str = cStringToCppString(cStr);
	bctbx_free(cStr);
	return utf8Str;
196 197 198
}

string Utils::utf8ToLocale (const string &str) {
199 200 201 202
	char *cStr = bctbx_utf8_to_locale(str.c_str());
	string localeStr = cStringToCppString(cStr);
	bctbx_free(cStr);
	return localeStr;
203 204 205
}

string Utils::convertString (const string &str, const string &from, const string &to) {
206 207 208 209
	char *cStr = bctbx_convert_from_to(str.c_str(), from.c_str(), to.c_str());
	string convertedStr = cStringToCppString(cStr);
	bctbx_free(cStr);
	return convertedStr;
210 211
}

212
LINPHONE_END_NAMESPACE