root/trunk/midgard/apis/php5/php_midgard_blob.c

Revision 18169, 5.0 kB (checked in by piotras, 2 months ago)

Ported r18166

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
Line 
1 /* Copyright (C) 2007, 2008 Piotr Pokora <piotrek.pokora@gmail.com>
2  * This program is free software; you can redistribute it and/or modify it
3  * under the terms of the GNU Lesser General Public License as published
4  * by the Free Software Foundation; either version 2 of the License, or
5  * (at your option) any later version.
6  *
7  * This program is distributed in the hope that it will be useful,
8  * but WITHOUT ANY WARRANTY; without even the implied warranty of
9  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
10  * GNU General Public License for more details.
11  *
12  * You should have received a copy of the GNU General Public License
13  * along with this program; if not, write to the Free Software
14  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
15  */
16
17 #include "php_midgard.h"
18 #include "php_midgard_gobject.h"
19
20 static zend_class_entry *php_midgard_blob_class;
21
22 #define _GET_BLOB_OBJECT \
23         zval *zval_object = getThis(); \
24         php_midgard_gobject *php_gobject = \
25                 (php_midgard_gobject *)zend_object_store_get_object(zval_object TSRMLS_CC); \
26         MidgardBlob *blob = MIDGARD_BLOB(php_gobject->gobject); \
27         if(!blob) php_error(E_ERROR, "Can not find underlying blob instance");
28
29 static PHP_METHOD(midgard_blob, __construct)
30 {
31         RETVAL_FALSE;
32         CHECK_MGD;
33         MidgardBlob *blob = NULL;
34         zval *param_object = NULL;
35         zval *zval_object = getThis();
36
37         if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "o",
38                                 &param_object) == FAILURE)
39                 return;
40        
41         if(param_object){
42                
43                 php_midgard_gobject *ppo =
44                         (php_midgard_gobject *)zend_object_store_get_object(
45                                         param_object TSRMLS_CC);
46                 if(ppo) {
47                         MgdObject *att = MIDGARD_OBJECT(ppo->gobject);
48                         blob = midgard_blob_new(att);
49                 }
50                
51         } else {
52                
53                 blob = midgard_blob_new(NULL);
54         }
55        
56         if(!blob) {
57                 if(php_midgard_error_throw_exception(mgd_handle()))
58                         return ;
59         }
60        
61         php_midgard_gobject *php_gobject =
62                 (php_midgard_gobject *)zend_object_store_get_object(zval_object TSRMLS_CC);
63         php_gobject->gobject = G_OBJECT(blob);
64 }
65
66 static PHP_METHOD(midgard_blob, read_content)
67 {
68         RETVAL_FALSE;
69         CHECK_MGD;
70         gchar *content;
71         gsize bytes_read = 0;
72        
73         if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "") == FAILURE)
74                 return;
75
76         _GET_BLOB_OBJECT;
77
78         content = midgard_blob_read_content(blob, &bytes_read);
79
80         if(content == NULL)
81                 RETURN_NULL();
82
83         RETURN_STRINGL(content, bytes_read, 1);
84 }
85
86 static PHP_METHOD(midgard_blob, write_content)
87 {
88         RETVAL_FALSE;
89         CHECK_MGD;
90         gchar *content;
91         guint content_length;
92
93         if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s",
94                                 &content, &content_length) == FAILURE)                           
95                 return;
96
97         _GET_BLOB_OBJECT;
98
99         gboolean rv = midgard_blob_write_content(blob, (const gchar *)content);
100
101         RETURN_BOOL(rv);
102 }
103
104 static PHP_METHOD(midgard_blob, remove_file)
105 {
106         RETVAL_FALSE;
107         CHECK_MGD;
108
109         if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "") == FAILURE)                           
110                 return;
111
112         _GET_BLOB_OBJECT;
113
114         gboolean rv = midgard_blob_remove_file(blob);
115
116         RETURN_BOOL(rv);
117 }
118
119 static PHP_METHOD(midgard_blob, get_handler)
120 {
121         RETVAL_FALSE;
122         CHECK_MGD;
123         gchar *mode = NULL;
124         guint mode_length;
125
126         if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|s", &mode, &mode_length) == FAILURE)                           
127                 return;
128
129         _GET_BLOB_OBJECT;
130
131         const gchar *path = midgard_blob_get_path(blob);
132         /* php_streams require paths so no filehandler or channel here */
133         /* GIOChannel *channel = midgard_blob_get_handler(blob); */
134
135         php_stream *stream =
136                 php_stream_open_wrapper_ex((gchar *)path, mode ? mode : "w",
137                                 IGNORE_PATH | IGNORE_URL | STREAM_DISABLE_OPEN_BASEDIR, NULL, NULL);
138
139         if(stream == NULL)
140                 RETURN_NULL();
141
142         php_stream_to_zval(stream, return_value);
143 }
144
145 static PHP_METHOD(midgard_blob, get_path)
146 {
147         RETVAL_FALSE;
148         CHECK_MGD;
149
150         if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "") == FAILURE)                           
151                 return;
152
153         _GET_BLOB_OBJECT;
154
155         const gchar *path = midgard_blob_get_path(blob);
156
157         if(!path)
158                 RETURN_NULL();
159
160         RETURN_STRING((gchar *)path, 1);
161 }
162
163 static PHP_METHOD(midgard_blob, exists)
164 {
165         RETVAL_FALSE;
166         CHECK_MGD;
167
168         if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "") == FAILURE)                           
169                 return;
170
171         _GET_BLOB_OBJECT;
172
173         RETURN_BOOL(midgard_blob_exists(blob));
174 }
175
176 void php_midgard_blob_init(int module_number)
177 {
178         static function_entry blob_methods[] = {
179                 PHP_ME(midgard_blob,    __construct,    NULL,   ZEND_ACC_PUBLIC)
180                 PHP_ME(midgard_blob,    read_content,   NULL,   ZEND_ACC_PUBLIC)
181                 PHP_ME(midgard_blob,    write_content,  NULL,   ZEND_ACC_PUBLIC)
182                 PHP_ME(midgard_blob,    remove_file,    NULL,   ZEND_ACC_PUBLIC)
183                 PHP_ME(midgard_blob,    get_handler,    NULL,   ZEND_ACC_PUBLIC)
184                 PHP_ME(midgard_blob,    get_path,       NULL,   ZEND_ACC_PUBLIC)
185                 PHP_ME(midgard_blob,    exists,         NULL,   ZEND_ACC_PUBLIC)
186                 {NULL, NULL, NULL}
187         };
188        
189         static zend_class_entry php_midgard_blob_class_entry;
190        
191         INIT_CLASS_ENTRY(
192                         php_midgard_blob_class_entry,
193                         "midgard_blob", blob_methods);
194        
195         php_midgard_blob_class =
196                 zend_register_internal_class(&php_midgard_blob_class_entry TSRMLS_CC);
197 }
Note: See TracBrowser for help on using the browser.