aboutsummaryrefslogtreecommitdiff
path: root/libs/libbsarch/src/utils/convertible_string.cpp
blob: c965202cfff632f5e0a402a47911dbeb4d0e7ec2 (plain)
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
/* Copyright (C) 2019 G'k
 * This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at https://mozilla.org/MPL/2.0/. */

#include "convertible_string.hpp"
#include <cwchar>
#include <filesystem>

namespace libbsarch {
/* conversion ctors */
convertible_string::convertible_string(std::string value, bool to_native_path)
    : str_(std::move(value))
    , auto_convert_to_native_path(to_native_path)
{
    if (auto_convert_to_native_path)
        this->to_native_path();
}

convertible_string::convertible_string(const char *const val_array, bool to_native_path)
    : str_(std::string(val_array))
    , auto_convert_to_native_path(to_native_path)

{
    if (auto_convert_to_native_path)
        this->to_native_path();
}
convertible_string::convertible_string(const std::wstring &wvalue, bool to_native_path)
    : str_(to_string(wvalue))
    , auto_convert_to_native_path(to_native_path)

{
    if (auto_convert_to_native_path)
        this->to_native_path();
}
convertible_string::convertible_string(wchar_t const *wval_array, bool to_native_path)
    : str_(to_string(std::wstring(wval_array)))
    , auto_convert_to_native_path(to_native_path)
{
    if (auto_convert_to_native_path)
        this->to_native_path();
}

/* assignment operators */
convertible_string &convertible_string::operator=(const std::string &value)
{
    str_ = value;
    if (auto_convert_to_native_path)
        this->to_native_path();
    return *this;
}
convertible_string &convertible_string::operator=(const std::wstring &wvalue)
{
    str_ = to_string(wvalue);
    if (auto_convert_to_native_path)
        this->to_native_path();
    return *this;
}

convertible_string &convertible_string::operator=(const wchar_t *wvalue)
{
    str_ = to_string(std::wstring(wvalue));
    if (auto_convert_to_native_path)
        this->to_native_path();
    return *this;
}

/* implicit conversion operators */
convertible_string::operator std::string() const
{
    return str_;
}
convertible_string::operator std::wstring() const
{
    return to_wstring(str_);
}
convertible_string::operator const wchar_t *() const
{
    const std::wstring wstr = to_wstring(str_);
    wchar_t *wchar_array = new wchar_t[wstr.length() + 1];
#ifdef _WIN32
    wcscpy_s(wchar_array, wstr.length() + 1, wstr.c_str());
#else
    std::wmemcpy(wchar_array, wstr.c_str(), wstr.length() + 1);
#endif
    return wchar_array;
}

/* Util */
bool convertible_string::remove_substring(const convertible_string &sub_str)
{
    size_t pos = str_.find(sub_str);
    if (pos != std::string::npos)
    {
        str_.erase(pos, std::string(sub_str).length());
        return true;
    }
    return false;
}

convertible_string &convertible_string::to_native_path()
{
    std::filesystem::path path(str_);
    str_ = path.make_preferred().string();
    return *this;
}

} // namespace libbsarch