This repository was archived by the owner on Jan 19, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 25
Expand file tree
/
Copy pathstrarray.cpp
More file actions
56 lines (47 loc) · 1.49 KB
/
Copy pathstrarray.cpp
File metadata and controls
56 lines (47 loc) · 1.49 KB
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
#include <cppgit2/strarray.hpp>
#include <cstdlib>
#include <cstring>
namespace cppgit2 {
strarray::strarray() {
c_struct_.count = 0;
c_struct_.strings = nullptr;
}
strarray::strarray(const std::vector<std::string> &strings) {
auto size = strings.size();
c_struct_.count = size;
c_struct_.strings = (char **)malloc(size * sizeof(char *));
for (size_t i = 0; i < size; ++i) {
auto length = strings[i].size() + 1;
c_struct_.strings[i] = (char *)malloc(length * sizeof(char));
strncpy(c_struct_.strings[i], strings[i].c_str(), length);
c_struct_.strings[i][length] = '\0';
}
}
strarray::strarray(const git_strarray *c_ptr) {
c_struct_.count = c_ptr->count;
c_struct_.strings = (char **)malloc(c_ptr->count * sizeof(char *));
for (size_t i = 0; i < c_ptr->count; ++i) {
auto length = strlen(c_ptr->strings[i]) + 1;
c_struct_.strings[i] = (char *)malloc(length * sizeof(char));
strncpy(c_struct_.strings[i], c_ptr->strings[i], length);
c_struct_.strings[i][length] = '\0';
}
}
strarray::~strarray() {
if (c_struct_.count)
git_strarray_free(&c_struct_);
}
strarray strarray::copy() const {
strarray result;
if (git_strarray_copy(&result.c_struct_, &c_struct_))
throw git_exception();
return result;
}
std::vector<std::string> strarray::to_vector() const {
std::vector<std::string> result{};
for (auto tag : *this)
result.push_back(tag);
return result;
}
const git_strarray *strarray::c_ptr() const { return &c_struct_; }
} // namespace cppgit2