forked from RogerGee/php-git2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
php-odb-stream-internal.cpp
219 lines (175 loc) · 6.02 KB
/
php-odb-stream-internal.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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
/*
* php-odb-stream-internal.cpp
*
* Copyright (C) Roger P. Gee
*/
#include "php-object.h"
using namespace php_git2;
// Class entry
static PHP_METHOD(GitODBStream_Internal,read);
static PHP_METHOD(GitODBStream_Internal,write);
static PHP_METHOD(GitODBStream_Internal,finalize_write);
zend_function_entry php_git2::odb_stream_internal_methods[] = {
PHP_ME(GitODBStream_Internal,read,NULL,ZEND_ACC_PUBLIC)
PHP_ME(GitODBStream_Internal,write,NULL,ZEND_ACC_PUBLIC)
PHP_ME(GitODBStream_Internal,finalize_write,NULL,ZEND_ACC_PUBLIC)
PHP_FE_END
};
// Make function implementation
void php_git2::php_git2_make_odb_stream(zval* zp,git_odb_stream* stream,php_git_odb* owner)
{
php_odb_stream_internal_object* obj;
// Create object zval.
object_init_ex(zp,php_git2::class_entry[php_git2_odb_stream_internal_obj]);
obj = php_zend_object<php_odb_stream_internal_object>::get_storage(zp);
// Assign the stream handle.
obj->stream = stream;
// Assign owner. (This increments the owner refcount if set to prevent the
// ODB from freeing while the stream is in use.)
obj->assign_owner(owner);
// Assign the kind based on whether the owner was set.
if (obj->owner != nullptr) {
// stream freed via git_odb_stream_free()
obj->kind = php_odb_stream_object::conventional;
}
else {
obj->kind = php_odb_stream_object::user; // stream freed via $stream->free()
}
}
// php_zend_object init function
template<>
zend_object_handlers php_git2::php_zend_object<php_odb_stream_object>::handlers;
template<>
zend_object_handlers php_git2::php_zend_object<php_odb_stream_internal_object>::handlers;
template<>
void php_zend_object<php_odb_stream_internal_object>::init(zend_class_entry* ce)
{
auto& parentHandlers = php_zend_object<php_odb_stream_object>::handlers;
handlers.read_property = parentHandlers.read_property;
handlers.write_property = parentHandlers.write_property;
handlers.has_property = parentHandlers.has_property;
handlers.get_constructor = php_git2::not_allowed_get_constructor;
handlers.offset = offset();
UNUSED(ce);
}
// Implementation of php_odb_stream_internal_object
php_odb_stream_internal_object::php_odb_stream_internal_object()
{
}
// Implementation of class methods
PHP_METHOD(GitODBStream_Internal,read)
{
/* NOTE: it isn't actually clear how useful this method (and its wrapper,
* git_odb_stream_read) really are. The documentation seems to indicate that
* they are not to be used. Plus it is not obvious how to obtain the number
* of bytes read. For now these functions will return a string zval with the
* length of the allocated buffer.
*/
php_bailer bailer;
int retval;
char* buffer;
zend_long bufsz;
php_odb_stream_internal_object* object;
object = php_zend_object<php_odb_stream_internal_object>::get_storage(getThis());
// Stream must be created and the function must be implemented.
if (object->stream == nullptr || object->stream->read == nullptr) {
zend_throw_error(nullptr,"GitODBStream_Internal::read(): method is not available");
return;
}
// Parse parameters.
if (zend_parse_parameters(ZEND_NUM_ARGS(),"l",&bufsz) == FAILURE) {
return;
}
if (bufsz <= 0) {
bufsz = 4096;
}
// Call underlying function.
try {
buffer = (char*)emalloc(bufsz);
retval = object->stream->read(object->stream,buffer,bufsz);
if (retval < 0) {
efree(buffer);
php_git2::git_error(retval);
}
else {
RETVAL_STRINGL(buffer,bufsz);
efree(buffer);
}
} catch (php_git2::php_git2_exception_base& ex) {
php_bailout_context ctx(bailer);
if (BAILOUT_ENTER_REGION(ctx)) {
ex.handle();
}
}
}
PHP_METHOD(GitODBStream_Internal,write)
{
php_bailer bailer;
int retval;
char* buffer;
size_t bufsz;
php_odb_stream_internal_object* object;
object = php_zend_object<php_odb_stream_internal_object>::get_storage(getThis());
// Stream must be created and the function must be implemented.
if (object->stream == nullptr || object->stream->write == nullptr) {
zend_throw_error(nullptr,"GitODBStream_Internal::write(): method is not available");
return;
}
// Parse parameters.
if (zend_parse_parameters(ZEND_NUM_ARGS(),"s",&buffer,&bufsz) == FAILURE) {
return;
}
// Call underlying function.
try {
retval = object->stream->write(object->stream,buffer,bufsz);
if (retval < 0) {
php_git2::git_error(retval);
}
} catch (php_git2::php_git2_exception_base& ex) {
php_bailout_context ctx(bailer);
if (BAILOUT_ENTER_REGION(ctx)) {
ex.handle();
}
}
}
PHP_METHOD(GitODBStream_Internal,finalize_write)
{
php_bailer bailer;
int retval;
char* input;
size_t input_len;
git_oid oid;
php_odb_stream_internal_object* object;
object = php_zend_object<php_odb_stream_internal_object>::get_storage(getThis());
// Stream must be created and the function must be implemented.
if (object->stream == nullptr || object->stream->finalize_write == nullptr) {
zend_throw_error(
nullptr,
"GitODBStream_Internal::finalize_write(): method is not available");
return;
}
// Read OID parameter from function arguments.
if (zend_parse_parameters(ZEND_NUM_ARGS(),"s",&input,&input_len) == FAILURE) {
return;
}
// Convert OID string to binary structure.
convert_oid_fromstr(&oid,input,input_len);
// Call underlying function.
try {
retval = object->stream->finalize_write(object->stream,&oid);
if (retval < 0) {
php_git2::git_error(retval);
}
} catch (php_git2::php_git2_exception_base& ex) {
php_bailout_context ctx(bailer);
if (BAILOUT_ENTER_REGION(ctx)) {
ex.handle();
}
}
}
/*
* Local Variables:
* indent-tabs-mode:nil
* tab-width:4
* End:
*/