/
str_array_converter.cc
64 lines (51 loc) · 1.57 KB
/
str_array_converter.cc
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
#include <nan.h>
#include <node.h>
#include <string>
#include <cstring>
#include "../include/str_array_converter.h"
#include "git2/strarray.h"
using namespace v8;
using namespace node;
git_strarray *StrArrayConverter::Convert(Local<v8::Value> val) {
if (!val->BooleanValue()) {
return NULL;
}
else if (val->IsArray()) {
return ConvertArray(Array::Cast(*val));
}
else if (val->IsString() || val->IsStringObject()) {
return ConvertString(val->ToString());
}
else {
return NULL;
}
}
git_strarray * StrArrayConverter::AllocStrArray(const size_t count) {
const size_t size = sizeof(git_strarray) + (sizeof(char*) * count);
uint8_t* memory = reinterpret_cast<uint8_t*>(malloc(size));
git_strarray *result = reinterpret_cast<git_strarray *>(memory);
result->count = count;
result->strings = reinterpret_cast<char**>(memory + sizeof(git_strarray));
return result;
}
git_strarray *StrArrayConverter::ConvertArray(Array *val) {
git_strarray *result = AllocStrArray(val->Length());
for(size_t i = 0; i < result->count; i++) {
Nan::Utf8String entry(val->Get(i));
result->strings[i] = strdup(*entry);
}
return result;
}
git_strarray* StrArrayConverter::ConvertString(Local<String> val) {
char *strings[1];
Nan::Utf8String utf8String(val);
strings[0] = *utf8String;
return ConstructStrArray(1, strings);
}
git_strarray *StrArrayConverter::ConstructStrArray(int argc, char** argv) {
git_strarray *result = AllocStrArray(argc);
for(size_t i = 0; i < result->count; i++) {
result->strings[i] = strdup(argv[i]);
}
return result;
}