Skip to content
Newer
Older
100644 166 lines (137 sloc) 4.15 KB
21bf947 @chobie for now, added commit, tree, blob classes
chobie authored
1 /*
2 * The MIT License
3 *
4 * Copyright (c) 2010 - 2012 Shuhei Tanuma
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to deal
8 * in the Software without restriction, including without limitation the rights
9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 * copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22 * THE SOFTWARE.
23 */
24
25 #include "php_git2.h"
26
27 PHPAPI zend_class_entry *git2_blob_class_entry;
28
29 static void php_git2_blob_free_storage(php_git2_blob *object TSRMLS_DC)
30 {
31 if (object->blob != NULL) {
32 git_blob_free(object->blob);
33 object->blob = NULL;
34 }
35 zend_object_std_dtor(&object->zo TSRMLS_CC);
36 efree(object);
37 }
38
39 zend_object_value php_git2_blob_new(zend_class_entry *ce TSRMLS_DC)
40 {
41 zend_object_value retval;
42
43 PHP_GIT2_STD_CREATE_OBJECT(php_git2_blob);
44 return retval;
45 }
46
86e285d added static method
Shuhei Tanuma authored
47 ZEND_BEGIN_ARG_INFO_EX(arginfo_git2_blob_create, 0,0,2)
48 ZEND_ARG_INFO(0, repository)
49 ZEND_ARG_INFO(0, contents)
50 ZEND_END_ARG_INFO()
51
52
6168010 @chobie added getContent and getSize methods for Git2\Blob
chobie authored
53 /*
54 {{{ proto: Git2\Blob::getContent()
55 */
56 PHP_METHOD(git2_blob, getContent)
57 {
58 php_git2_blob *m_blob;
59
60 m_blob = PHP_GIT2_GET_OBJECT(php_git2_blob, getThis());
61
62 if (m_blob != NULL) {
63 if (m_blob->blob == NULL) {
64 RETURN_FALSE;
65 }
66
67 RETURN_STRINGL(git_blob_rawcontent(m_blob->blob), git_blob_rawsize(m_blob->blob),1);
68 } else {
69 RETURN_FALSE;
70 }
71 }
72 /* }}} */
73
74
75 /*
76 {{{ proto: Git2\Blob::getSize()
77 */
78 PHP_METHOD(git2_blob, getSize)
79 {
80 php_git2_blob *m_blob;
81
82 m_blob = PHP_GIT2_GET_OBJECT(php_git2_blob, getThis());
83
84 if (m_blob != NULL) {
85 if (m_blob->blob == NULL) {
86 RETURN_FALSE;
87 }
88
89 RETURN_LONG(git_blob_rawsize(m_blob->blob));
90 } else {
91 RETURN_FALSE;
92 }
93 }
94 /* }}} */
21bf947 @chobie for now, added commit, tree, blob classes
chobie authored
95
9f37fe3 @chobie added Git2\Blob::__toString() method
chobie authored
96 /*
97 {{{ proto: Git2\Blob::__toString()
98 */
99 PHP_METHOD(git2_blob, __toString)
100 {
101 php_git2_blob *m_blob;
102
103 m_blob = PHP_GIT2_GET_OBJECT(php_git2_blob, getThis());
104
105 if (m_blob != NULL) {
106 if (m_blob->blob == NULL) {
107 RETURN_FALSE;
108 }
109
110 RETURN_STRINGL(git_blob_rawcontent(m_blob->blob), git_blob_rawsize(m_blob->blob),1);
111 } else {
112 RETURN_FALSE;
113 }
114 }
115 /* }}} */
116
86e285d added static method
Shuhei Tanuma authored
117 /*
118 {{{ proto: Git2\Blob::create(Git2\Repository $repository, string $contents)
119 */
120 PHP_METHOD(git2_blob, create)
121 {
122 char *contents;
123 int contents_len, error = 0;
124 zval *repository;
125 php_git2_repository *m_repository;
126 char oid_out[GIT_OID_HEXSZ] = {0};
127 git_oid oid;
128
129 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC,
130 "Os", &repository, git2_repository_class_entry, &contents, &contents_len) == FAILURE) {
131 return;
132 }
133
134 m_repository = PHP_GIT2_GET_OBJECT(php_git2_repository, repository);
135
136 if (m_repository != NULL) {
137 if (m_repository->repository == NULL) {
138 RETURN_FALSE;
139 }
140
141 error = git_blob_create_frombuffer(&oid, m_repository->repository, contents, contents_len);
142 git_oid_fmt(oid_out, &oid);
143 RETVAL_STRINGL(oid_out,GIT_OID_HEXSZ,1);
144 } else {
145 RETURN_FALSE;
146 }
147 }
148 /* }}} */
149
9f37fe3 @chobie added Git2\Blob::__toString() method
chobie authored
150
21bf947 @chobie for now, added commit, tree, blob classes
chobie authored
151 static zend_function_entry php_git2_blob_methods[] = {
6168010 @chobie added getContent and getSize methods for Git2\Blob
chobie authored
152 PHP_ME(git2_blob, getContent, NULL, ZEND_ACC_PUBLIC)
153 PHP_ME(git2_blob, getSize, NULL, ZEND_ACC_PUBLIC)
86e285d added static method
Shuhei Tanuma authored
154 PHP_ME(git2_blob, __toString, NULL, ZEND_ACC_PUBLIC)
155 PHP_ME(git2_blob, create, arginfo_git2_blob_create, ZEND_ACC_PUBLIC | ZEND_ACC_STATIC)
21bf947 @chobie for now, added commit, tree, blob classes
chobie authored
156 {NULL,NULL,NULL}
157 };
158
159 void php_git2_blob_init(TSRMLS_D)
160 {
161 zend_class_entry ce;
162
163 INIT_NS_CLASS_ENTRY(ce, PHP_GIT2_NS, "Blob", php_git2_blob_methods);
164 git2_blob_class_entry = zend_register_internal_class(&ce TSRMLS_CC);
165 git2_blob_class_entry->create_object = php_git2_blob_new;
166 }
Something went wrong with that request. Please try again.