mirror of
https://github.com/ModOrganizer2/libbsarch.git
synced 2026-07-27 14:06:31 -07:00
129 lines
3.3 KiB
C++
129 lines
3.3 KiB
C++
/* 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 <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();
|
||
|
|
}
|
||
|
|
|
||
|
|
#ifdef QT
|
||
|
|
convertible_string::convertible_string(const QString &qsvalue, bool to_native_path)
|
||
|
|
: str_(to_string(qsvalue))
|
||
|
|
, auto_convert_to_native_path(to_native_path)
|
||
|
|
{
|
||
|
|
if (auto_convert_to_native_path)
|
||
|
|
this->to_native_path();
|
||
|
|
}
|
||
|
|
#endif
|
||
|
|
|
||
|
|
/* 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;
|
||
|
|
}
|
||
|
|
|
||
|
|
#ifdef QT
|
||
|
|
convertible_string &convertible_string::operator=(const QString &qsvalue)
|
||
|
|
{
|
||
|
|
str_ = to_string(qsvalue);
|
||
|
|
if (auto_convert_to_native_path)
|
||
|
|
this->to_native_path();
|
||
|
|
return *this;
|
||
|
|
}
|
||
|
|
#endif
|
||
|
|
|
||
|
|
/* 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];
|
||
|
|
wcscpy_s(wchar_array, wstr.length() + 1, wstr.c_str());
|
||
|
|
return wchar_array;
|
||
|
|
}
|
||
|
|
#ifdef QT
|
||
|
|
convertible_string::operator QString() const
|
||
|
|
{
|
||
|
|
return to_qstring(str_);
|
||
|
|
}
|
||
|
|
#endif
|
||
|
|
|
||
|
|
/* 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
|