forked from RogerGee/php-git2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
php-writestream.cpp
139 lines (111 loc) · 3.27 KB
/
php-writestream.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
/**
* php-writestream.cpp
*
* Copyright (C) Roger P. Gee
*/
#include "php-object.h"
using namespace php_git2;
// Class method entries
static PHP_METHOD(GitWritestream,write);
static PHP_METHOD(GitWritestream,close);
zend_function_entry php_git2::writestream_methods[] = {
PHP_ME(GitWritestream,write,NULL,ZEND_ACC_PUBLIC)
PHP_ME(GitWritestream,close,NULL,ZEND_ACC_PUBLIC)
PHP_FE_END
};
// Make function implementation
void php_git2::php_git2_make_writestream(zval* zp,git_writestream* ws)
{
php_writestream_object* obj;
// Create object zval.
object_init_ex(zp,php_git2::class_entry[php_git2_writestream_obj]);
obj = php_zend_object<php_writestream_object>::get_storage(zp);
obj->ws = ws;
}
// php_zend_object init function
template<>
zend_object_handlers php_git2::php_zend_object<php_writestream_object>::handlers;
template<>
void php_zend_object<php_writestream_object>::init(zend_class_entry* ce)
{
handlers.get_constructor = php_git2::not_allowed_get_constructor;
handlers.offset = offset();
}
// Implementation of php_writestream_object
php_writestream_object::php_writestream_object():
ws(nullptr)
{
}
php_writestream_object::~php_writestream_object()
{
if (ws != nullptr) {
ws->free(ws);
}
}
// Userspace method implementations
PHP_METHOD(GitWritestream,write)
{
php_bailer bailer;
int retval;
char* buffer;
size_t bufsz;
php_writestream_object* object;
object = php_zend_object<php_writestream_object>::get_storage(getThis());
// The writestream must exist and the function must be implemented.
if (object->ws == nullptr || object->ws->write == nullptr) {
zend_throw_error(nullptr,"GitWritestream::write(): method is not available");
return;
}
// Parse parameters.
if (zend_parse_parameters(ZEND_NUM_ARGS(),"s",&buffer,&bufsz) == FAILURE) {
return;
}
// Call the underlying function on the writestream.
try {
retval = object->ws->write(object->ws,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(GitWritestream,close)
{
php_bailer bailer;
int retval;
char* buffer;
size_t bufsz;
php_writestream_object* object;
object = php_zend_object<php_writestream_object>::get_storage(getThis());
// The writestream must exist and the function must be implemented.
if (object->ws == nullptr || object->ws->close == nullptr) {
zend_throw_error(nullptr,"GitWritestream::close(): method is not available");
return;
}
// Parse parameters.
if (zend_parse_parameters(ZEND_NUM_ARGS(),"s",&buffer,&bufsz) == FAILURE) {
return;
}
// Call the underlying function on the writestream.
try {
retval = object->ws->close(object->ws);
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:
*/