utils.cpp 5 KB
Newer Older
Ronan's avatar
Ronan committed
1 2
/*
 * utils.cpp
Ghislain MARY's avatar
Ghislain MARY committed
3
 * Copyright (C) 2010-2017 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 25
#include <bctoolbox/port.h>

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

28 29
// =============================================================================

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

32
LINPHONE_BEGIN_NAMESPACE
Ronan's avatar
Ronan committed
33 34 35 36 37 38 39 40 41 42 43 44 45 46

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;
}

47 48
// -----------------------------------------------------------------------------

Ronan's avatar
Ronan committed
49 50 51 52 53 54 55 56 57 58
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
59

60 61
// -----------------------------------------------------------------------------

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

86 87
#undef TO_STRING_IMPL

88 89 90 91 92 93
string Utils::toString (const void *val) {
	ostringstream ss;
	ss << val;
	return ss.str();
}

94 95 96
// -----------------------------------------------------------------------------

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

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

138
// -----------------------------------------------------------------------------
Benjamin REIS's avatar
Benjamin REIS committed
139

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

146 147
// -----------------------------------------------------------------------------

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

174 175 176 177 178 179 180
// -----------------------------------------------------------------------------

tm Utils::getLongAsTm (long time) {
	tm result;
	return *gmtime_r(&static_cast<time_t &>(time), &result);
}

181
LINPHONE_END_NAMESPACE