string_utils.cpp 3.14 KB
Newer Older
1
/*
2
 *  Copyright (C) 2014-2019 Savoir-faire Linux Inc.
3 4
 *
 *  Author: Tristan Matthews <tristan.matthews@savoirfairelinux.com>
Adrien Béraud's avatar
Adrien Béraud committed
5
 *  Author: Adrien Béraud <adrien.beraud@savoirfairelinux.com>
6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
 *
 *  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, write to the Free Software
 *  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301 USA.
 */

#include "string_utils.h"
#include <sstream>
24 25
#include <cctype>
#include <algorithm>
26 27
#include <ostream>
#include <stdexcept>
Adrien Béraud's avatar
Adrien Béraud committed
28 29
#ifdef _WIN32
#include <windows.h>
30
#include <oleauto.h>
Adrien Béraud's avatar
Adrien Béraud committed
31
#endif
32

33 34
#include <ciso646> // fix windows compiler bug

Adrien Béraud's avatar
Adrien Béraud committed
35
namespace jami {
36

Adrien Béraud's avatar
Adrien Béraud committed
37 38
#ifdef _WIN32

39 40
std::wstring
to_wstring(const std::string& s)
Adrien Béraud's avatar
Adrien Béraud committed
41
{
42
    int slength = (int)s.length();
43
    int len = MultiByteToWideChar(CP_UTF8, 0, s.c_str(), slength, nullptr, 0);
Adrien Béraud's avatar
Adrien Béraud committed
44 45 46
    if (not len)
        throw std::runtime_error("Can't convert string to wchar");
    std::wstring r((size_t)len, 0);
47
    if (!MultiByteToWideChar(CP_UTF8, 0, s.c_str(), slength, &(*r.begin()), len))
Adrien Béraud's avatar
Adrien Béraud committed
48 49 50 51
        throw std::runtime_error("Can't convert string to wchar");
    return r;
}

52 53 54 55 56 57 58 59 60
std::string
decodeMultibyteString(const std::string& s)
{
    if (not s.length())
        return {};
    auto wstr = to_wstring(s);
    return std::string(wstr.begin(), wstr.end());
}

61 62 63 64 65 66 67 68 69 70
std::string
bstrToStdString(BSTR bstr)
{
    int wslen = ::SysStringLen(bstr);
    if (wslen != 0) {
        std::wstring wstr(bstr, wslen);
        return std::string(wstr.begin(), wstr.end());
    }
    return {};
}
Adrien Béraud's avatar
Adrien Béraud committed
71 72
#endif

73 74 75 76 77 78 79 80 81 82
std::string
to_string(double value)
{
    char buf[64];
    int len = snprintf(buf, sizeof(buf), "%-.*G", 16, value);
    if (len <= 0)
        throw std::invalid_argument{"can't parse double"};
    return {buf, (size_t)len};
}

83 84 85 86 87 88 89
std::string
trim(const std::string &s)
{
   auto wsfront = std::find_if_not(s.cbegin(),s.cend(), [](int c){return std::isspace(c);});
   return std::string(wsfront, std::find_if_not(s.rbegin(),std::string::const_reverse_iterator(wsfront), [](int c){return std::isspace(c);}).base());
}

90 91 92 93 94 95 96 97 98 99 100 101 102
std::vector<std::string>
split_string(const std::string &s, char delim)
{
    std::vector<std::string> result;
    std::string token;
    std::istringstream ss(s);

    while (std::getline(ss, token, delim))
        if (not token.empty())
            result.emplace_back(token);
    return result;
}

103 104 105 106 107 108 109 110 111
std::vector<unsigned>
split_string_to_unsigned(const std::string &s, char delim)
{
    std::vector<unsigned> result;
    std::string token;
    std::istringstream ss(s);

    while (std::getline(ss, token, delim))
        if (not token.empty())
Adrien Béraud's avatar
Adrien Béraud committed
112
            result.emplace_back(jami::stoi(token));
113 114 115
    return result;
}

Adrien Béraud's avatar
Adrien Béraud committed
116
} // namespace jami