mirror of
https://github.com/ModOrganizer2/libbsarch.git
synced 2026-07-27 14:06:31 -07:00
Reorganized directories
Readded .editorconfig C wrapper can be built again Delphi library can be built again Initial cleanup of cmake files
This commit is contained in:
@@ -0,0 +1,66 @@
|
||||
/* 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/. */
|
||||
#pragma once
|
||||
|
||||
#include "libbsarch.h"
|
||||
#include "utils/convertible_string.hpp"
|
||||
#include <variant>
|
||||
|
||||
namespace libbsarch {
|
||||
struct memory_blob : public bsa_result_buffer_t
|
||||
{
|
||||
memory_blob(uint32_t buffer_size, bsa_buffer_t buffer_data, bsa_archive_t parent)
|
||||
: bsa_result_buffer_t{buffer_size, buffer_data}
|
||||
, parent_(parent)
|
||||
{}
|
||||
|
||||
memory_blob(bsa_result_buffer_t buffer, bsa_archive_t parent)
|
||||
: bsa_result_buffer_t(buffer)
|
||||
, parent_(parent)
|
||||
{}
|
||||
|
||||
bsa_archive_t parent_;
|
||||
|
||||
memory_blob(memory_blob const &) = default;
|
||||
~memory_blob() { bsa_file_data_free(parent_, *this); }
|
||||
};
|
||||
|
||||
struct disk_blob
|
||||
{
|
||||
disk_blob(const convertible_string &root_dir, const convertible_string &absolute_path)
|
||||
: path_in_archive(absolute_path)
|
||||
, path_on_disk(absolute_path)
|
||||
{
|
||||
path_in_archive.remove_substring(root_dir);
|
||||
}
|
||||
|
||||
disk_blob(const convertible_string &path_in_archive,
|
||||
const convertible_string &absolute_path,
|
||||
[[maybe_unused]] bool decoy_parameter)
|
||||
: path_in_archive(path_in_archive)
|
||||
, path_on_disk(absolute_path)
|
||||
{}
|
||||
|
||||
convertible_string path_in_archive;
|
||||
convertible_string path_on_disk;
|
||||
};
|
||||
|
||||
struct bs_packed_file
|
||||
{
|
||||
bs_packed_file(const disk_blob &blob)
|
||||
: path_in_archive(blob.path_in_archive)
|
||||
, data(blob.path_on_disk)
|
||||
{}
|
||||
|
||||
bs_packed_file(convertible_string path_in_archive, memory_blob blob)
|
||||
: path_in_archive(std::move(path_in_archive))
|
||||
, data(blob)
|
||||
{}
|
||||
|
||||
convertible_string path_in_archive;
|
||||
std::variant<memory_blob, convertible_string> data;
|
||||
};
|
||||
|
||||
} // namespace libbsarch
|
||||
@@ -0,0 +1,185 @@
|
||||
#include "bs_archive.h"
|
||||
|
||||
namespace libbsarch {
|
||||
|
||||
bs_archive::bs_archive()
|
||||
: archive_(bsa_create())
|
||||
{}
|
||||
|
||||
bs_archive::bs_archive(const bsa_archive_type_t &type)
|
||||
: archive_(bsa_create())
|
||||
, type_(type)
|
||||
{}
|
||||
|
||||
bs_archive::~bs_archive()
|
||||
{
|
||||
bsa_close(archive_);
|
||||
bsa_free(archive_);
|
||||
}
|
||||
|
||||
/* Properties */
|
||||
convertible_string bs_archive::get_filename()
|
||||
{
|
||||
wchar_t name[max_string_buffer_size];
|
||||
bsa_filename_get(archive_, max_string_buffer_size, name);
|
||||
return name;
|
||||
}
|
||||
|
||||
bsa_archive_type_t bs_archive::get_type()
|
||||
{
|
||||
return bsa_archive_type_get(archive_);
|
||||
}
|
||||
|
||||
uint32_t bs_archive::get_version()
|
||||
{
|
||||
return bsa_version_get(archive_);
|
||||
}
|
||||
|
||||
convertible_string bs_archive::get_format_name()
|
||||
{
|
||||
wchar_t format_name[max_string_buffer_size];
|
||||
bsa_format_name_get(archive_, max_string_buffer_size, format_name);
|
||||
return format_name;
|
||||
}
|
||||
|
||||
uint32_t bs_archive::get_file_count()
|
||||
{
|
||||
return bsa_file_count_get(archive_);
|
||||
}
|
||||
|
||||
uint32_t bs_archive::get_archive_flags()
|
||||
{
|
||||
return bsa_archive_flags_get(archive_);
|
||||
}
|
||||
|
||||
void bs_archive::set_archive_flags(uint32_t flags)
|
||||
{
|
||||
bsa_archive_flags_set(archive_, flags);
|
||||
}
|
||||
|
||||
uint32_t bs_archive::get_file_flags()
|
||||
{
|
||||
return bsa_file_flags_get(archive_);
|
||||
}
|
||||
|
||||
void bs_archive::set_file_flags(uint32_t flags)
|
||||
{
|
||||
bsa_file_flags_set(archive_, flags);
|
||||
}
|
||||
|
||||
void bs_archive::set_compressed(bool value)
|
||||
{
|
||||
bsa_compress_set(archive_, value);
|
||||
}
|
||||
|
||||
void bs_archive::set_share_data(bool value)
|
||||
{
|
||||
bsa_share_data_set(archive_, value);
|
||||
}
|
||||
|
||||
bool bs_archive::get_compressed()
|
||||
{
|
||||
return bsa_compress_get(archive_);
|
||||
}
|
||||
|
||||
bool bs_archive::get_share_data()
|
||||
{
|
||||
return bsa_share_data_get(archive_);
|
||||
}
|
||||
|
||||
void bs_archive::load_from_disk(const convertible_string &archive_path)
|
||||
{
|
||||
debug_log() << "Opening archive: " << archive_path;
|
||||
const std::wstring str = archive_path;
|
||||
const auto &result = bsa_load_from_file(archive_, str.c_str());
|
||||
libbsarch::checkResult(result);
|
||||
}
|
||||
|
||||
void bs_archive::create(const convertible_string &archiveName, const bs_archive_entries &entries)
|
||||
{
|
||||
create(archiveName, entries, type_);
|
||||
}
|
||||
|
||||
void bs_archive::create(const convertible_string &archiveName,
|
||||
const bs_archive_entries &entries,
|
||||
const bsa_archive_type_t type)
|
||||
{
|
||||
debug_log() << "Creating archive. Archive name: " << archiveName << '\n'
|
||||
<< "type: " << type_ << '\n'
|
||||
<< "entries: " << entries.getEntries();
|
||||
bsa_create_archive(archive_, archiveName, type, entries.getEntries());
|
||||
}
|
||||
|
||||
void bs_archive::save() const
|
||||
{
|
||||
debug_log() << "Saving archive: " << archive_;
|
||||
|
||||
const auto &result = bsa_save(archive_);
|
||||
|
||||
libbsarch::checkResult(result);
|
||||
}
|
||||
|
||||
void bs_archive::add_file_from_disk(const disk_blob &blob)
|
||||
{
|
||||
debug_log() << "Adding file from disk root. Path in archive: " << blob.path_in_archive << '\n'
|
||||
<< "Filepath: " << blob.path_on_disk << '\n'
|
||||
<< "Archive: " << archive_;
|
||||
|
||||
const auto &result = bsa_add_file_from_disk(archive_, blob.path_in_archive, blob.path_on_disk);
|
||||
|
||||
libbsarch::checkResult(result);
|
||||
}
|
||||
|
||||
void bs_archive::add_file_from_memory(const convertible_string &path_in_archive,
|
||||
const memory_blob &memory_data) //NOTE UNTESTED
|
||||
{
|
||||
debug_log() << "Adding file from memory. Filename: " << path_in_archive << '\n'
|
||||
<< "Data size: " << memory_data.size;
|
||||
const auto &result = bsa_add_file_from_memory(archive_, path_in_archive, memory_data.size, memory_data.data);
|
||||
libbsarch::checkResult(result);
|
||||
}
|
||||
|
||||
bsa_file_record_t bs_archive::find_file_record(const convertible_string &filename)
|
||||
{
|
||||
debug_log() << "Finding file record of: " << filename;
|
||||
const auto &result = bsa_find_file_record(archive_, filename);
|
||||
return result;
|
||||
}
|
||||
|
||||
memory_blob bs_archive::extract_to_memory(bsa_file_record_t record)
|
||||
{
|
||||
const auto &result = bsa_extract_file_data_by_record(archive_, record);
|
||||
libbsarch::checkResult(result);
|
||||
return memory_blob(result.buffer, archive_);
|
||||
}
|
||||
|
||||
memory_blob bs_archive::extract_to_memory(const convertible_string &filename)
|
||||
{
|
||||
const auto &result = bsa_extract_file_data_by_filename(archive_, filename);
|
||||
libbsarch::checkResult(result);
|
||||
return memory_blob(result.buffer, archive_);
|
||||
}
|
||||
|
||||
void bs_archive::extract_to_disk(const convertible_string &filename, const convertible_string &save_as) const
|
||||
{
|
||||
debug_log() << "Extracting: " << filename << " saved as " << save_as;
|
||||
const auto &result = bsa_extract_file(archive_, filename, save_as);
|
||||
|
||||
libbsarch::checkResult(result);
|
||||
}
|
||||
|
||||
std::vector<convertible_string> bs_archive::list_files() const
|
||||
{
|
||||
bsa_entry_list_t list = bsa_entry_list_create();
|
||||
const auto &result = bsa_get_resource_list(archive_, list, L"");
|
||||
|
||||
libbsarch::checkResult(result);
|
||||
|
||||
return bs_archive_entries(list).list();
|
||||
}
|
||||
|
||||
bsa_archive_t bs_archive::get_archive() const
|
||||
{
|
||||
return archive_;
|
||||
}
|
||||
} // namespace libbsarch
|
||||
@@ -0,0 +1,100 @@
|
||||
#pragma once
|
||||
|
||||
#include "bs_archive_entries.h"
|
||||
#include "libbsarch.hpp"
|
||||
|
||||
namespace libbsarch {
|
||||
class bs_archive
|
||||
{
|
||||
public:
|
||||
bs_archive();
|
||||
bs_archive(const bsa_archive_type_t &type);
|
||||
virtual ~bs_archive();
|
||||
|
||||
/* Properties */
|
||||
convertible_string get_filename();
|
||||
bsa_archive_type_t get_type();
|
||||
uint32_t get_version();
|
||||
convertible_string get_format_name();
|
||||
uint32_t get_file_count();
|
||||
|
||||
uint32_t get_archive_flags();
|
||||
void set_archive_flags(uint32_t flags);
|
||||
|
||||
uint32_t get_file_flags();
|
||||
void set_file_flags(uint32_t flags);
|
||||
|
||||
void set_compressed(bool value);
|
||||
bool get_compressed();
|
||||
|
||||
void set_share_data(bool value);
|
||||
bool get_share_data();
|
||||
|
||||
/* Input-Output */
|
||||
virtual void load_from_disk(const convertible_string &archive_path);
|
||||
virtual void create(const convertible_string &archiveName, const bs_archive_entries &entries);
|
||||
virtual void create(const convertible_string &archiveName,
|
||||
const bs_archive_entries &entries,
|
||||
const bsa_archive_type_t type);
|
||||
void save() const;
|
||||
|
||||
/* Add to archive */
|
||||
virtual void add_file_from_disk(const disk_blob &blob);
|
||||
virtual void add_file_from_memory(const convertible_string &path_in_archive, const memory_blob &memory_data);
|
||||
|
||||
/* Extract */
|
||||
memory_blob extract_to_memory(bsa_file_record_t record);
|
||||
memory_blob extract_to_memory(const convertible_string &filename);
|
||||
void extract_to_disk(const convertible_string &filename, const convertible_string &save_as) const;
|
||||
|
||||
/* Selectors */
|
||||
bsa_file_record_t find_file_record(const convertible_string &filename);
|
||||
std::vector<convertible_string> list_files() const;
|
||||
|
||||
/* Other */
|
||||
|
||||
/*! \brief Sets the DDS callback. It is required to use Fallout 4 DDS archives. You do not have
|
||||
* to manage the lifetime of the provided context
|
||||
* \example
|
||||
void dds_callback([[maybe_unused]] bsa_archive_t archive,
|
||||
const wchar_t *file_path,
|
||||
bsa_dds_info_t *dds_info,
|
||||
void *context)
|
||||
{
|
||||
const auto &path = *static_cast<convertible_string *>(context) + '/' + convertible_string(file_path);
|
||||
|
||||
auto image = std::make_unique<DirectX::ScratchImage>();
|
||||
DirectX::TexMetadata info;
|
||||
|
||||
const auto hr = LoadFromDDSFile(PREPARE_PATH_LIBBSARCH(path), DirectX::DDS_FLAGS_BAD_DXTN_TAILS, &info, *image);
|
||||
if (FAILED(hr))
|
||||
{
|
||||
//Do not throw here. Exceptions will be ignored
|
||||
return;
|
||||
}
|
||||
|
||||
dds_info->width = info.width;
|
||||
dds_info->height = info.height;
|
||||
dds_info->mipmaps = info.mipLevels;
|
||||
}
|
||||
*/
|
||||
template<typename context_type>
|
||||
void set_dds_callback(bsa_file_dds_info_proc_t dds_function, const context_type &context)
|
||||
{
|
||||
debug_log() << "Setting DDS callback for archive: " << archive_ << "\nCallback adress: " << &dds_function;
|
||||
|
||||
auto heap_context = new context_type(context);
|
||||
callback_contexts_.emplace_back(heap_context);
|
||||
bsa_file_dds_info_callback_set(archive_, dds_function, callback_contexts_.back());
|
||||
}
|
||||
|
||||
bsa_archive_t get_archive() const;
|
||||
|
||||
protected:
|
||||
bsa_archive_t archive_;
|
||||
bsa_archive_type_t type_;
|
||||
|
||||
private:
|
||||
std::vector<void *> callback_contexts_;
|
||||
};
|
||||
} // namespace libbsarch
|
||||
@@ -0,0 +1,89 @@
|
||||
/* 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 "bs_archive_auto.hpp"
|
||||
#include <filesystem>
|
||||
namespace libbsarch {
|
||||
bs_archive_auto::bs_archive_auto(const bsa_archive_type_t &type)
|
||||
: bs_archive(type)
|
||||
{}
|
||||
|
||||
void bs_archive_auto::add_file_from_disk(const disk_blob &blob)
|
||||
{
|
||||
files_.emplace_back(blob);
|
||||
}
|
||||
|
||||
void bs_archive_auto::add_file_from_disk(const std::vector<disk_blob> &blobs)
|
||||
{
|
||||
files_.reserve(blobs.size());
|
||||
files_.insert(files_.end(), blobs.cbegin(), blobs.cend());
|
||||
}
|
||||
|
||||
void bs_archive_auto::add_file_from_memory(const convertible_string &path_in_archive, const memory_blob &memory_data)
|
||||
{
|
||||
files_.emplace_back(bs_packed_file(path_in_archive, memory_data));
|
||||
}
|
||||
|
||||
void bs_archive_auto::add_file(const bs_packed_file &file)
|
||||
{
|
||||
files_.emplace_back(file);
|
||||
}
|
||||
|
||||
void bs_archive_auto::extract_all(const convertible_string &directory, bool overwrite_current_files)
|
||||
{
|
||||
namespace fs = std::filesystem;
|
||||
for (const auto &file : list_files())
|
||||
{
|
||||
convertible_string path_string = std::string(directory) + "/" + std::string(file);
|
||||
fs::path current_file(path_string);
|
||||
fs::path current_path = fs::path(current_file).remove_filename();
|
||||
|
||||
fs::create_directories(current_path);
|
||||
if (fs::exists(current_file) && overwrite_current_files)
|
||||
{
|
||||
fs::remove(current_file);
|
||||
extract_to_disk(file, current_file.string());
|
||||
}
|
||||
else if (!fs::exists(current_file))
|
||||
extract_to_disk(file, current_file.string());
|
||||
}
|
||||
}
|
||||
|
||||
void bs_archive_auto::load_from_disk(const convertible_string &archive_path)
|
||||
{
|
||||
bs_archive::load_from_disk(archive_path);
|
||||
for (const auto &file : list_files())
|
||||
entries_.add(file);
|
||||
}
|
||||
|
||||
void bs_archive_auto::save_to_disk(const convertible_string &archive_path)
|
||||
{
|
||||
for (const auto &file : files_)
|
||||
entries_.add(file.path_in_archive);
|
||||
|
||||
bs_archive::create(archive_path, entries_);
|
||||
|
||||
for (const auto &file : files_)
|
||||
{
|
||||
switch (file.data.index())
|
||||
{
|
||||
case 0:
|
||||
{
|
||||
const auto &blob = std::get<memory_blob>(file.data);
|
||||
bs_archive::add_file_from_memory(file.path_in_archive, blob);
|
||||
break;
|
||||
}
|
||||
case 1:
|
||||
{
|
||||
const auto &file_path = std::get<convertible_string>(file.data);
|
||||
const auto &blob = disk_blob(file.path_in_archive, file_path, bool());
|
||||
bs_archive::add_file_from_disk(blob);
|
||||
}
|
||||
}
|
||||
}
|
||||
bs_archive::save();
|
||||
}
|
||||
|
||||
} // namespace libbsarch
|
||||
@@ -0,0 +1,39 @@
|
||||
/* 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/. */
|
||||
#pragma once
|
||||
|
||||
#include "bs_archive.h"
|
||||
|
||||
namespace libbsarch {
|
||||
/*!
|
||||
* \brief The bs_archive_auto class is a convenience class. It manages both bs_archive and bs_archive_entries.
|
||||
* It is recommended when packing from disk/extracting to disk
|
||||
* Otherwise, if using memory, the use of these two separate classes is recommended
|
||||
*/
|
||||
class bs_archive_auto : public bs_archive
|
||||
{
|
||||
protected:
|
||||
bs_archive_entries entries_;
|
||||
|
||||
public:
|
||||
bs_archive_auto() = default;
|
||||
bs_archive_auto(const bsa_archive_type_t &type);
|
||||
|
||||
void add_file_from_disk(const disk_blob &blob) override;
|
||||
void add_file_from_disk(const std::vector<disk_blob> &blobs);
|
||||
void add_file_from_memory(const convertible_string &path_in_archive, const memory_blob &memory_data) override;
|
||||
void add_file(const bs_packed_file &file);
|
||||
|
||||
void extract_all(const convertible_string &directory, bool overwrite_current_files);
|
||||
|
||||
void load_from_disk(const convertible_string &archive_path) override;
|
||||
void save_to_disk(const convertible_string &archive_path);
|
||||
|
||||
private:
|
||||
using bs_archive::create; //Use save_to_disk instead
|
||||
using bs_archive::save; //Use save_to_disk instead
|
||||
std::vector<bs_packed_file> files_;
|
||||
};
|
||||
} // namespace libbsarch
|
||||
@@ -0,0 +1,54 @@
|
||||
#include "bs_archive_entries.h"
|
||||
|
||||
namespace libbsarch {
|
||||
bs_archive_entries::bs_archive_entries()
|
||||
: _entries(bsa_entry_list_create())
|
||||
{
|
||||
}
|
||||
|
||||
bs_archive_entries::bs_archive_entries(const std::vector<convertible_string> &entries)
|
||||
: _entries(bsa_entry_list_create())
|
||||
{
|
||||
for (const auto &entry : entries)
|
||||
add(entry);
|
||||
}
|
||||
|
||||
bs_archive_entries::bs_archive_entries(const bsa_entry_list_t &entries)
|
||||
: _entries(entries)
|
||||
{
|
||||
}
|
||||
|
||||
bs_archive_entries::~bs_archive_entries()
|
||||
{
|
||||
bsa_entry_list_free(_entries);
|
||||
}
|
||||
|
||||
void bs_archive_entries::add(const convertible_string &filepath)
|
||||
{
|
||||
debug_log() << "Adding to entries: " << filepath;
|
||||
const auto &result = bsa_entry_list_add(_entries, filepath);
|
||||
libbsarch::checkResult(result);
|
||||
}
|
||||
|
||||
uint32_t bs_archive_entries::count()
|
||||
{
|
||||
return bsa_entry_list_count(_entries);
|
||||
}
|
||||
|
||||
std::vector<convertible_string> bs_archive_entries::list()
|
||||
{
|
||||
std::vector<convertible_string> list;
|
||||
for (uint32_t i = 0; i < count(); ++i)
|
||||
{
|
||||
wchar_t buffer[max_string_buffer_size];
|
||||
bsa_entry_list_get(_entries, i, max_string_buffer_size, buffer);
|
||||
list.push_back(buffer);
|
||||
}
|
||||
return list;
|
||||
}
|
||||
|
||||
bsa_entry_list_t bs_archive_entries::getEntries() const
|
||||
{
|
||||
return _entries;
|
||||
}
|
||||
} // namespace libbsarch
|
||||
@@ -0,0 +1,24 @@
|
||||
#pragma once
|
||||
|
||||
#include "libbsarch.hpp"
|
||||
#include <vector>
|
||||
|
||||
namespace libbsarch {
|
||||
class bs_archive_entries
|
||||
{
|
||||
public:
|
||||
bs_archive_entries();
|
||||
explicit bs_archive_entries(const std::vector<convertible_string> &entries);
|
||||
explicit bs_archive_entries(const bsa_entry_list_t &entries);
|
||||
~bs_archive_entries();
|
||||
|
||||
void add(const convertible_string &filepath);
|
||||
uint32_t count();
|
||||
std::vector<convertible_string> list();
|
||||
|
||||
bsa_entry_list_t getEntries() const;
|
||||
|
||||
protected:
|
||||
bsa_entry_list_t _entries;
|
||||
};
|
||||
} // namespace libbsarch
|
||||
@@ -0,0 +1,32 @@
|
||||
#pragma once
|
||||
|
||||
#include "base_types.hpp"
|
||||
#include "utils/convertible_ostream.hpp"
|
||||
#include "utils/convertible_string.hpp"
|
||||
|
||||
namespace libbsarch {
|
||||
constexpr bool enable_debug_log = true;
|
||||
[[maybe_unused]] constexpr int max_string_buffer_size = 1024;
|
||||
|
||||
inline convertible_ostream &debug_log()
|
||||
{
|
||||
static convertible_ostream ostr;
|
||||
if constexpr (enable_debug_log)
|
||||
return ostr << "[libbsarch] " << __FUNCTION__ << ' ';
|
||||
}
|
||||
|
||||
inline void checkResult(const bsa_result_message_s &result)
|
||||
{
|
||||
if (result.code == BSA_RESULT_EXCEPTION)
|
||||
{
|
||||
const std::string &error = to_string(result.text);
|
||||
debug_log() << error;
|
||||
throw std::runtime_error(error);
|
||||
}
|
||||
}
|
||||
|
||||
inline void checkResult(const bsa_result_message_buffer_s &result)
|
||||
{
|
||||
checkResult(result.message);
|
||||
}
|
||||
} // namespace libbsarch
|
||||
@@ -0,0 +1,58 @@
|
||||
/* 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_ostream.hpp"
|
||||
|
||||
namespace libbsarch {
|
||||
convertible_ostream &convertible_ostream::operator<<(const std::string &rh)
|
||||
{
|
||||
std::cout << rh;
|
||||
return *this;
|
||||
}
|
||||
convertible_ostream &convertible_ostream::operator<<(const char *rh)
|
||||
{
|
||||
std::cout << rh;
|
||||
return *this;
|
||||
}
|
||||
convertible_ostream &convertible_ostream::operator<<(const char rh)
|
||||
{
|
||||
std::cout << rh;
|
||||
return *this;
|
||||
}
|
||||
convertible_ostream &convertible_ostream::operator<<(const std::wstring &rh)
|
||||
{
|
||||
std::wcout << rh;
|
||||
return *this;
|
||||
}
|
||||
convertible_ostream &convertible_ostream::operator<<(const void *rh)
|
||||
{
|
||||
std::cout << rh;
|
||||
return *this;
|
||||
}
|
||||
convertible_ostream &convertible_ostream::operator<<(const convertible_string &rh)
|
||||
{
|
||||
return *this << std::string(rh);
|
||||
}
|
||||
|
||||
convertible_ostream::operator std::ostream &()
|
||||
{
|
||||
return std::cout;
|
||||
}
|
||||
convertible_ostream::operator std::wostream &()
|
||||
{
|
||||
return std::wcout;
|
||||
}
|
||||
|
||||
#ifdef QT
|
||||
convertible_ostream::operator QDebug()
|
||||
{
|
||||
return qDebug();
|
||||
}
|
||||
convertible_ostream &convertible_ostream::operator<<(const QString &rh)
|
||||
{
|
||||
qDebug() << rh;
|
||||
return *this;
|
||||
}
|
||||
#endif
|
||||
} // namespace libbsarch
|
||||
@@ -0,0 +1,37 @@
|
||||
/* 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/. */
|
||||
#pragma once
|
||||
|
||||
#include "convertible_string.hpp"
|
||||
#include <iostream>
|
||||
|
||||
namespace libbsarch {
|
||||
class convertible_ostream
|
||||
{
|
||||
public:
|
||||
convertible_ostream &operator<<(const std::string &rh);
|
||||
convertible_ostream &operator<<(const char *rh);
|
||||
convertible_ostream &operator<<(const char rh);
|
||||
convertible_ostream &operator<<(const std::wstring &rh);
|
||||
convertible_ostream &operator<<(const void *rh);
|
||||
convertible_ostream &operator<<(const convertible_string &rh);
|
||||
|
||||
template<typename number, //real type
|
||||
typename = typename std::enable_if<std::is_arithmetic<number>::value, number>::type>
|
||||
convertible_ostream &operator<<(const number &num)
|
||||
{
|
||||
std::cout << num;
|
||||
return *this;
|
||||
}
|
||||
|
||||
operator std::ostream &();
|
||||
operator std::wostream &();
|
||||
|
||||
#ifdef QT
|
||||
operator QDebug();
|
||||
convertible_ostream &operator<<(const QString &rh);
|
||||
#endif
|
||||
};
|
||||
} // namespace libbsarch
|
||||
@@ -0,0 +1,128 @@
|
||||
/* 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
|
||||
@@ -0,0 +1,54 @@
|
||||
/* 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/. */
|
||||
#pragma once
|
||||
|
||||
//See https://stackoverflow.com/questions/4804298/how-to-convert-wstring-into-string
|
||||
|
||||
#include "string_convert.hpp"
|
||||
#ifdef QT
|
||||
#include <QString>
|
||||
#endif
|
||||
|
||||
namespace libbsarch {
|
||||
class convertible_string
|
||||
{
|
||||
public:
|
||||
// default ctor
|
||||
convertible_string() = default;
|
||||
|
||||
/* conversion ctors */
|
||||
convertible_string(std::string value, bool to_native_path = true);
|
||||
convertible_string(const char *const val_array, bool to_native_path = true);
|
||||
convertible_string(const std::wstring &wvalue, bool to_native_path = true);
|
||||
convertible_string(const wchar_t *const wval_array, bool to_native_path = true);
|
||||
#ifdef QT
|
||||
convertible_string(const QString &qsvalue, bool to_native_path = true);
|
||||
#endif
|
||||
|
||||
/* assignment operators */
|
||||
convertible_string &operator=(const std::string &value);
|
||||
convertible_string &operator=(const std::wstring &wvalue);
|
||||
convertible_string &operator=(const wchar_t *wvalue);
|
||||
#ifdef QT
|
||||
convertible_string &operator=(const QString &qsvalue);
|
||||
#endif
|
||||
|
||||
/* implicit conversion operators */
|
||||
operator std::string() const;
|
||||
operator std::wstring() const;
|
||||
operator const wchar_t *() const;
|
||||
#ifdef QT
|
||||
operator QString() const;
|
||||
#endif
|
||||
|
||||
/* Util */
|
||||
bool remove_substring(const convertible_string &sub_str);
|
||||
convertible_string &to_native_path();
|
||||
|
||||
private:
|
||||
std::string str_;
|
||||
bool auto_convert_to_native_path = true;
|
||||
};
|
||||
} // namespace libbsarch
|
||||
@@ -0,0 +1,49 @@
|
||||
/* 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 "string_convert.hpp"
|
||||
|
||||
namespace libbsarch {
|
||||
#ifdef QT
|
||||
std::wstring to_wstring(const QString &str)
|
||||
{
|
||||
#ifdef _MSC_VER
|
||||
return std::wstring(reinterpret_cast<const wchar_t *>(str.utf16()));
|
||||
#else
|
||||
return str.toStdWString();
|
||||
#endif
|
||||
}
|
||||
|
||||
std::string to_string(const QString &str)
|
||||
{
|
||||
return str.toStdString();
|
||||
}
|
||||
|
||||
QString to_qstring(const std::string &str)
|
||||
{
|
||||
return QString::fromStdString(str);
|
||||
}
|
||||
|
||||
QString to_qstring(const std::wstring &str)
|
||||
{
|
||||
#ifdef _MSC_VER
|
||||
return QString::fromUtf16(reinterpret_cast<const ushort *>(str.c_str()));
|
||||
#else
|
||||
return QString::fromStdWString(str);
|
||||
#endif
|
||||
}
|
||||
#endif
|
||||
|
||||
static std::wstring_convert<std::codecvt_utf8_utf16<wchar_t>> s_converter;
|
||||
std::string to_string(const std::wstring &str)
|
||||
{
|
||||
return s_converter.to_bytes(str);
|
||||
}
|
||||
|
||||
std::wstring to_wstring(const std::string &str)
|
||||
{
|
||||
auto ws = s_converter.from_bytes(str);
|
||||
return ws;
|
||||
}
|
||||
} // namespace libbsarch
|
||||
@@ -0,0 +1,24 @@
|
||||
/* 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/. */
|
||||
#pragma once
|
||||
|
||||
#ifdef QT
|
||||
#include <QDebug>
|
||||
#endif
|
||||
#include <codecvt>
|
||||
#include <locale>
|
||||
#include <string>
|
||||
|
||||
namespace libbsarch {
|
||||
#ifdef QT
|
||||
std::wstring to_wstring(const QString &str);
|
||||
std::string to_string(const QString &str);
|
||||
QString to_qstring(const std::string &str);
|
||||
QString to_qstring(const std::wstring &str);
|
||||
#endif
|
||||
|
||||
std::string to_string(const std::wstring &str);
|
||||
std::wstring to_wstring(const std::string &str);
|
||||
} // namespace libbsarch
|
||||
Reference in New Issue
Block a user