diff --git a/src/utils/common.cpp b/src/utils/common.cpp index 908d9b1..e5d97f4 100644 --- a/src/utils/common.cpp +++ b/src/utils/common.cpp @@ -23,3 +23,51 @@ std::string get_current_git_path() // sub->add_option("directory", directory, "info about directory arg") // ->check(CLI::ExistingDirectory | CLI::NonexistentPath) // ->default_val(std::filesystem::current_path()); + +git_strarray_wrapper::git_strarray_wrapper(std::vector patterns) + : m_patterns(std::move(patterns)) +{ + init_str_array(); +} + +git_strarray_wrapper::git_strarray_wrapper(git_strarray_wrapper&& rhs) + : m_patterns(std::move(rhs.m_patterns)) +{ + init_str_array(); + rhs.reset_str_array(); +} + +git_strarray_wrapper& git_strarray_wrapper::operator=(git_strarray_wrapper&& rhs) +{ + using std::swap; + swap(m_patterns, rhs.m_patterns); + swap(m_array.strings, rhs.m_array.strings); + swap(m_array.count, rhs.m_array.count); + return *this; +} + +git_strarray_wrapper::~git_strarray_wrapper() +{ + reset_str_array(); +} + +git_strarray_wrapper::operator git_strarray*() +{ + return &m_array; +} + +void git_strarray_wrapper::reset_str_array() +{ + delete[] m_array.strings; + m_array={nullptr, 0}; +} + +void git_strarray_wrapper::init_str_array() +{ + m_array.strings = new char*[m_patterns.size()]; + m_array.count = m_patterns.size(); + for (size_t i=0; i(m_patterns[i].c_str()); + } +} diff --git a/src/utils/common.hpp b/src/utils/common.hpp index ade2158..c19b1b3 100644 --- a/src/utils/common.hpp +++ b/src/utils/common.hpp @@ -2,6 +2,9 @@ #include #include +#include + +#include class noncopyable_nonmovable { @@ -57,3 +60,30 @@ class libgit2_object : private noncopyable_nonmovable }; std::string get_current_git_path(); + +class git_strarray_wrapper +{ +public: + git_strarray_wrapper() + : m_patterns{} + , m_array{nullptr, 0} + {} + git_strarray_wrapper(std::vector patterns); + + git_strarray_wrapper(const git_strarray_wrapper&) = delete; + git_strarray_wrapper& operator=(const git_strarray_wrapper&) = delete; + + git_strarray_wrapper(git_strarray_wrapper&& rhs); + git_strarray_wrapper& operator=(git_strarray_wrapper&&); + + ~git_strarray_wrapper(); + + operator git_strarray*(); + +private: + std::vector m_patterns; + git_strarray m_array; + + void reset_str_array(); + void init_str_array(); +};