OSDN Git Service

Initial commit of senna-1.1.2-fast.
[ludiafuncs/senna-1.1.2-fast.git] / bindings / php / context / sen_ctx.c
1 /* Copyright(C) 2007 Brazil
2
3   This library is free software; you can redistribute it and/or
4   modify it under the terms of the GNU Lesser General Public
5   License as published by the Free Software Foundation; either
6   version 2.1 of the License, or (at your option) any later version.
7
8   This library is distributed in the hope that it will be useful,
9   but WITHOUT ANY WARRANTY; without even the implied warranty of
10   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
11   Lesser General Public License for more details.
12
13   You should have received a copy of the GNU Lesser General Public
14   License along with this library; if not, write to the Free Software
15   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
16 */
17
18 /* $ Id: $ */
19
20 #include "php_sen_ctx.h"
21
22 #if HAVE_SEN_CTX
23
24 int le_sen_ctx;
25 void sen_ctx_dtor(zend_rsrc_list_entry *rsrc TSRMLS_DC)
26 {
27   sen_ctx * resource = (sen_ctx *)(rsrc->ptr);
28   sen_ctx_close(resource);
29 }
30
31 int le_sen_pctx;
32 void sen_pctx_dtor(zend_rsrc_list_entry *rsrc TSRMLS_DC)
33 {
34   sen_ctx * resource = (sen_ctx *)(rsrc->ptr);
35   sen_ctx_close(resource);
36 }
37
38 int le_sen_db;
39 void sen_db_dtor(zend_rsrc_list_entry *rsrc TSRMLS_DC)
40 {
41   sen_db * resource = (sen_db *)(rsrc->ptr);
42   sen_db_close(resource);
43 }
44
45
46 function_entry sen_ctx_functions[] = {
47   PHP_FE(sen_db_create       , NULL)
48   PHP_FE(sen_db_open         , NULL)
49   PHP_FE(sen_db_close        , NULL)
50   PHP_FE(sen_ctx_open        , NULL)
51   PHP_FE(sen_ctx_connect     , NULL)
52   PHP_FE(sen_ctx_pconnect    , NULL)
53   PHP_FE(sen_ctx_load        , NULL)
54   PHP_FE(sen_ctx_send        , NULL)
55   PHP_FE(sen_ctx_recv        , NULL)
56   PHP_FE(sen_ctx_close       , NULL)
57   PHP_FE(sen_ctx_info_get    , NULL)
58   { NULL, NULL, NULL }
59 };
60
61
62 zend_module_entry sen_ctx_module_entry = {
63   STANDARD_MODULE_HEADER,
64   "sen_ctx",
65   sen_ctx_functions,
66   PHP_MINIT(sen_ctx),     /* Replace with NULL if there is nothing to do at php startup   */
67   PHP_MSHUTDOWN(sen_ctx), /* Replace with NULL if there is nothing to do at php shutdown  */
68   PHP_RINIT(sen_ctx),     /* Replace with NULL if there is nothing to do at request start */
69   PHP_RSHUTDOWN(sen_ctx), /* Replace with NULL if there is nothing to do at request end   */
70   PHP_MINFO(sen_ctx),
71   "0.0.1",
72   STANDARD_MODULE_PROPERTIES
73 };
74
75 #ifdef COMPILE_DL_SEN_CTX
76 ZEND_GET_MODULE(sen_ctx)
77 #endif
78
79
80 PHP_MINIT_FUNCTION(sen_ctx)
81 {
82   REGISTER_LONG_CONSTANT("SEN_SYM_WITH_SIS", SEN_SYM_WITH_SIS, CONST_PERSISTENT | CONST_CS);
83   REGISTER_LONG_CONSTANT("SEN_CTX_MORE", SEN_CTX_MORE, CONST_PERSISTENT | CONST_CS);
84   REGISTER_LONG_CONSTANT("SEN_CTX_USEQL", SEN_CTX_USEQL, CONST_PERSISTENT | CONST_CS);
85   REGISTER_LONG_CONSTANT("SEN_CTX_BATCHMODE", SEN_CTX_BATCHMODE, CONST_PERSISTENT | CONST_CS);
86   REGISTER_LONG_CONSTANT("SEN_ENC_DEFAULT", 0, CONST_PERSISTENT | CONST_CS);
87   REGISTER_LONG_CONSTANT("SEN_ENC_NONE", 1, CONST_PERSISTENT | CONST_CS);
88   REGISTER_LONG_CONSTANT("SEN_ENC_EUC_JP", 2, CONST_PERSISTENT | CONST_CS);
89   REGISTER_LONG_CONSTANT("SEN_ENC_UTF8", 3, CONST_PERSISTENT | CONST_CS);
90   REGISTER_LONG_CONSTANT("SEN_ENC_SJIS", 4, CONST_PERSISTENT | CONST_CS);
91   REGISTER_LONG_CONSTANT("SEN_ENC_LATIN1", 5, CONST_PERSISTENT | CONST_CS);
92   REGISTER_LONG_CONSTANT("SEN_ENC_KOI8R", 6, CONST_PERSISTENT | CONST_CS);
93   le_sen_ctx = zend_register_list_destructors_ex(sen_ctx_dtor,
94                NULL, "sen_ctx", module_number);
95   le_sen_pctx = zend_register_list_destructors_ex(NULL,
96                sen_pctx_dtor, "sen_pctx", module_number);
97   le_sen_db = zend_register_list_destructors_ex(sen_db_dtor,
98                NULL, "sen_db", module_number);
99   sen_init();
100   return SUCCESS;
101 }
102
103
104 PHP_MSHUTDOWN_FUNCTION(sen_ctx)
105 {
106   sen_fin();
107   return SUCCESS;
108 }
109
110
111 PHP_RINIT_FUNCTION(sen_ctx)
112 {
113   return SUCCESS;
114 }
115
116
117 PHP_RSHUTDOWN_FUNCTION(sen_ctx)
118 {
119   return SUCCESS;
120 }
121
122
123 PHP_MINFO_FUNCTION(sen_ctx)
124 {
125   php_info_print_box_start(0);
126   php_printf("<p>senna binding for PHP.</p>\n");
127   php_printf("<p>Version 0.1-devel (2007-08-16)</p>\n");
128   php_info_print_box_end();
129 }
130
131
132 PHP_FUNCTION(sen_db_create)
133 {
134   sen_db * return_res;
135   long return_res_id = -1;
136   const char * path = NULL;
137   int path_len = 0;
138   long flags = 0;
139   long encoding = 0;
140
141   if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "sll", &path, &path_len, &flags, &encoding) == FAILURE) {
142     return;
143   }
144   do {
145     return_res = sen_db_create(path,flags,encoding);
146     if(!return_res) RETURN_FALSE;
147   } while (0);
148   return_res_id = ZEND_REGISTER_RESOURCE(return_value, return_res, le_sen_db);
149 }
150
151
152 PHP_FUNCTION(sen_db_open)
153 {
154   sen_db * return_res;
155   long return_res_id = -1;
156   const char * path = NULL;
157   int path_len = 0;
158
159   if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", &path, &path_len) == FAILURE) {
160     return;
161   }
162   return_res = sen_db_open(path);
163   if(!return_res) RETURN_FALSE;
164   return_res_id = ZEND_REGISTER_RESOURCE(return_value, return_res, le_sen_db);
165 }
166
167
168 PHP_FUNCTION(sen_db_close)
169 {
170   zval * res = NULL;
171   int res_id = -1;
172   sen_db * res_res;
173
174   if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "r", &res) == FAILURE) {
175     return;
176   }
177   ZEND_FETCH_RESOURCE(res_res, sen_db *, &res, res_id, "sen_db", le_sen_db);
178         FREE_RESOURCE(res);
179   RETURN_TRUE;
180 }
181
182
183 PHP_FUNCTION(sen_ctx_open)
184 {
185   sen_ctx * return_res;
186   long return_res_id = -1;
187   zval * res = NULL;
188   int res_id = -1;
189   sen_db * res_res;
190   long flags = 0;
191
192   if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "rl", &res, &flags) == FAILURE) {
193     return;
194   }
195   ZEND_FETCH_RESOURCE(res_res, sen_db *, &res, res_id, "sen_db", le_sen_db);
196
197   return_res = sen_ctx_open(res_res,flags);
198   if(!return_res) RETURN_FALSE;
199
200   return_res_id = ZEND_REGISTER_RESOURCE(return_value, return_res, le_sen_ctx);
201 }
202
203
204 PHP_FUNCTION(sen_ctx_connect)
205 {
206   sen_ctx * return_res;
207   long return_res_id = -1;
208   const char * host = NULL;
209   int host_len = 0;
210   long port = 0;
211   long flags = 0;
212
213   if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "sll", &host, &host_len, &port, &flags) == FAILURE) {
214     return;
215   }
216   return_res = sen_ctx_connect(host,port,flags);
217   if(!return_res) RETURN_FALSE;
218   return_res_id = ZEND_REGISTER_RESOURCE(return_value, return_res, le_sen_ctx);
219 }
220
221
222 PHP_FUNCTION(sen_ctx_pconnect)
223 {
224   sen_ctx * return_res;
225   long return_res_id = -1;
226   const char * host = NULL;
227   char *hash_key = NULL;
228   int hash_key_len;
229   int host_len = 0;
230   long port = 0;
231   long flags = 0;
232   zend_rsrc_list_entry *le;
233
234   if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "sll", &host, &host_len, &port, &flags) == FAILURE) {
235     return;
236   }
237
238   hash_key_len = spprintf(&hash_key, 0, "ctx_%s_%d_%d", host, port, flags);
239
240   if (zend_hash_find(&EG(persistent_list), hash_key, hash_key_len+1, (void **) &le)==FAILURE) {
241     zend_rsrc_list_entry new_le;
242           return_res = sen_ctx_connect(host,port,flags);
243     Z_TYPE(new_le) = le_sen_pctx;
244     new_le.ptr = return_res;
245     if (zend_hash_update(&EG(persistent_list), hash_key, hash_key_len+1, (void *) &new_le, sizeof(zend_rsrc_list_entry), NULL)==FAILURE) {
246       sen_ctx_close(return_res);
247       efree(hash_key);
248       RETURN_FALSE;
249     }
250   } else {
251     if (Z_TYPE_P(le) != le_sen_pctx) {
252       RETURN_FALSE;
253     }
254     return_res = (sen_ctx *) le->ptr;
255   }
256   if(!return_res) RETURN_FALSE;
257   return_res_id = ZEND_REGISTER_RESOURCE(return_value, return_res, le_sen_pctx);
258 }
259
260
261 PHP_FUNCTION(sen_ctx_load)
262 {
263   zval * res = NULL;
264   int res_id = -1;
265   sen_ctx * res_res;
266   const char * path = NULL;
267   int path_len = 0;
268
269   if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "rs", &res, &path, &path_len) == FAILURE) {
270     return;
271   }
272   ZEND_FETCH_RESOURCE2(res_res, sen_ctx *, &res, res_id, "sen_ctx", le_sen_ctx, le_sen_pctx);
273
274   int rc;
275   rc = sen_ctx_load(res_res,path);
276   RETURN_LONG(rc);
277 }
278
279
280 PHP_FUNCTION(sen_ctx_send)
281 {
282   zval * res = NULL;
283   int res_id = -1;
284   sen_ctx * res_res;
285   char * query = NULL;
286   int query_len = 0;
287   long flags = 0;
288
289   if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "rsl", &res, &query, &query_len, &flags) == FAILURE) {
290     return;
291   }
292   ZEND_FETCH_RESOURCE2(res_res, sen_ctx *, &res, res_id, "sen_ctx", le_sen_ctx, le_sen_pctx);
293
294   int rc;
295   rc = sen_ctx_send(res_res, query, query_len, flags);
296   RETURN_LONG(rc);
297 }
298
299
300 PHP_FUNCTION(sen_ctx_recv)
301 {
302   zval * res = NULL;
303   int res_id = -1;
304   sen_ctx * res_res;
305
306   if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "r", &res) == FAILURE) {
307     return;
308   }
309   ZEND_FETCH_RESOURCE2(res_res, sen_ctx *, &res, res_id, "sen_ctx", le_sen_ctx, le_sen_pctx);
310
311
312   array_init(return_value);
313
314   char *str;
315   int rc,str_len,flags;
316   rc=sen_ctx_recv(res_res, &str, &str_len, &flags);
317   add_next_index_long(return_value, rc);
318   add_next_index_stringl(return_value, str, str_len, 1);
319   add_next_index_long(return_value, flags);
320 }
321
322
323 PHP_FUNCTION(sen_ctx_close)
324 {
325   zval * res = NULL;
326   int res_id = -1;
327   sen_ctx * res_res;
328
329   if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "r", &res) == FAILURE) {
330     return;
331   }
332   ZEND_FETCH_RESOURCE2(res_res, sen_ctx *, &res, res_id, "sen_ctx", le_sen_ctx, le_sen_pctx);
333
334         FREE_RESOURCE(res);
335   RETURN_TRUE;
336 }
337
338
339 PHP_FUNCTION(sen_ctx_info_get)
340 {
341   zval * res = NULL;
342   int res_id = -1;
343   sen_ctx * res_res;
344
345   if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "r", &res) == FAILURE) {
346     return;
347   }
348   ZEND_FETCH_RESOURCE2(res_res, sen_ctx *, &res, res_id, "sen_ctx", le_sen_ctx, le_sen_pctx);
349
350
351   array_init(return_value);
352
353   int rc;
354   sen_ctx_info info;
355   rc=sen_ctx_info_get(res_res, &info);
356   add_assoc_long(return_value,"rc",rc);
357   add_assoc_long(return_value,"fd",info.fd);
358   add_assoc_long(return_value,"status",info.com_status);
359   add_assoc_long(return_value,"info",info.com_info);
360 }
361
362 #endif /* HAVE_SEN_CTX */