forked from RogerGee/php-git2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
php-array.cpp
168 lines (131 loc) · 2.93 KB
/
php-array.cpp
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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
/*
* php-array.cpp
*
* Copyright (C) Roger P. Gee
*/
#include "php-array.h"
using namespace php_git2;
array_wrapper::array_wrapper(zval& zv):
ht(Z_ARRVAL(zv)), zvp(nullptr), usesTmp(false)
{
// Give 'tmp' an initial value so that we have it in a known state
// initially.
ZVAL_UNDEF(&tmp);
}
array_wrapper::array_wrapper(zval* zvp):
ht(Z_ARRVAL_P(zvp)), zvp(nullptr), usesTmp(false)
{
ZVAL_UNDEF(&tmp);
}
array_wrapper::~array_wrapper()
{
zval_dtor(&tmp);
}
bool array_wrapper::query(const char* key)
{
size_t keysz = strlen(key);
usesTmp = false;
zvp = zend_hash_str_find(ht,key,keysz);
if (zvp != nullptr) {
return true;
}
return false;
}
bool array_wrapper::query(const char* key,size_t keysz)
{
usesTmp = false;
zvp = zend_hash_str_find(ht,key,keysz);
if (zvp != nullptr) {
return true;
}
return false;
}
bool array_wrapper::query(int index)
{
usesTmp = false;
zvp = zend_hash_index_find(ht,index);
if (zvp != nullptr) {
return true;
}
return false;
}
const char* array_wrapper::get_string() const
{
if (found()) {
zval* zv = copy_if_not_type(IS_STRING);
return Z_STRVAL_P(zv);
}
return nullptr;
}
const char* array_wrapper::get_string_nullable() const
{
if (found() && type() != IS_NULL) {
zval* zv = copy_if_not_type(IS_STRING);
return Z_STRVAL_P(zv);
}
return nullptr;
}
size_t array_wrapper::get_string_length() const
{
if (found() && type() != IS_NULL) {
zval* zv = copy_if_not_type(IS_STRING);
return Z_STRLEN_P(zv);
}
return 0;
}
zend_long array_wrapper::get_long() const
{
if (found()) {
zval* zv = copy_if_not_type(IS_LONG);
return Z_LVAL_P(zv);
}
return 0;
}
bool array_wrapper::get_bool() const
{
if (found()) {
zval* zv = copy_if_not_type(_IS_BOOL);
return Z_TYPE_P(zv) == IS_TRUE;
}
return false;
}
zval* array_wrapper::get_value() const
{
if (found()) {
return zvp;
}
zval_dtor(&tmp);
ZVAL_NULL(&tmp);
return &tmp;
}
void array_wrapper::get_oid(git_oid* out) const
{
if (found()) {
const char* src;
int srclen;
zval* zv = copy_if_not_type(IS_STRING);
src = Z_STRVAL_P(zv);
srclen = Z_STRLEN_P(zv);
convert_oid_fromstr(out,src,srclen);
}
}
zval* array_wrapper::copy_if_not_type(int type) const
{
if (usesTmp) {
return &tmp;
}
zval* zv = zvp;
if (Z_TYPE_P(zv) != type) {
// Use the temporary zval to store the converted, copied zval.
zv = &tmp;
zval_dtor(zv);
// Copy the zval into the temporary zval.
ZVAL_DUP(zv,zvp);
// Convert the copied zval.
convert_to_explicit_type(zv,type);
// Flag that we're using the temporary zval so we won't have to repeat
// this.
usesTmp = true;
}
return zv;
}