change array_list API
[m6w6/ext-http] / php_http_misc.c
index c52f83f91fddc6f840e3b52964830a9a55294a36..f14354abfab28291799129062f97f8a46553d108 100644 (file)
@@ -6,17 +6,14 @@
     | modification, are permitted provided that the conditions mentioned |
     | in the accompanying LICENSE file are met.                          |
     +--------------------------------------------------------------------+
-    | Copyright (c) 2004-2010, Michael Wallner <mike@php.net>            |
+    | Copyright (c) 2004-2011, Michael Wallner <mike@php.net>            |
     +--------------------------------------------------------------------+
 */
 
-/* $Id $ */
-
-#include "php_http.h"
+#include "php_http_api.h"
 
 #include <ext/standard/php_lcg.h>
-#include <ext/standard/php_string.h>
-#include <Zend/zend_exceptions.h>
+#include <zend_exceptions.h>
 
 /* SLEEP */
 
@@ -39,7 +36,7 @@ PHP_HTTP_API void php_http_sleep(double s)
 #else
        struct timeval timeout;
 
-       timeout.tv.sec = (time_t) s;
+       timeout.tv_sec = (time_t) s;
        timeout.tv_usec = PHP_HTTP_USEC(s) % PHP_HTTP_MCROSEC;
 
        select(0, NULL, NULL, NULL, &timeout);
@@ -51,7 +48,11 @@ PHP_HTTP_API void php_http_sleep(double s)
 
 int php_http_match(const char *haystack_str, const char *needle_str, int flags)
 {
-       int result;
+       int result = 0;
+
+       if (!haystack_str || !needle_str) {
+               return result;
+       }
 
        if (flags & PHP_HTTP_MATCH_FULL) {
                if (flags & PHP_HTTP_MATCH_CASE) {
@@ -112,11 +113,58 @@ char *php_http_pretty_key(char *key, size_t key_len, zend_bool uctitle, zend_boo
 
 size_t php_http_boundary(char *buf, size_t buf_len TSRMLS_DC)
 {
-       return snprintf(buf, buf_len, "%lu%0.9f", (ulong) PHP_HTTP_G->env.request.time, (float) php_combined_lcg(TSRMLS_C));
+       return snprintf(buf, buf_len, "%15.15F", PHP_HTTP_G->env.request.time * php_combined_lcg(TSRMLS_C));
+}
+
+int php_http_select_str(const char *cmp, int argc, ...)
+{
+       va_list argv;
+       int match = -1;
+
+       if (cmp && argc > 0) {
+               int i;
+
+               va_start(argv, argc);
+               for (i = 0; i < argc; ++i) {
+                       const char *test = va_arg(argv, const char *);
+
+                       if (!strcasecmp(cmp, test)) {
+                               match = i;
+                               break;
+                       }
+               }
+               va_end(argv);
+       }
+
+       return match;
 }
 
+
 /* ARRAYS */
 
+PHP_HTTP_API unsigned php_http_array_list(HashTable *ht TSRMLS_DC, unsigned argc, ...)
+{
+       HashPosition pos;
+       unsigned argl = 0;
+       va_list argv;
+
+       va_start(argv, argc);
+       for (   zend_hash_internal_pointer_reset_ex(ht, &pos);
+                       SUCCESS == zend_hash_has_more_elements_ex(ht, &pos) && (argl < argc);
+                       zend_hash_move_forward_ex(ht, &pos))
+       {
+               zval **data, ***argp = (zval ***) va_arg(argv, zval ***);
+
+               if (SUCCESS == zend_hash_get_current_data_ex(ht, (void *) &data, &pos)) {
+                       *argp = data;
+                       ++argl;
+               }
+       }
+       va_end(argv);
+
+       return argl;
+}
+
 int php_http_array_apply_append_func(void *pDest TSRMLS_DC, int num_args, va_list args, zend_hash_key *hash_key)
 {
        int flags;
@@ -142,7 +190,7 @@ int php_http_array_apply_append_func(void *pDest TSRMLS_DC, int num_args, va_lis
                        }
                        add_next_index_zval(*data, *value);
                } else if (key) {
-                       zend_hash_add(dst, key, hash_key->nKeyLength, value, sizeof(zval *), NULL);
+                       zend_symtable_update(dst, key, hash_key->nKeyLength, value, sizeof(zval *), NULL);
                } else {
                        zend_hash_quick_add(dst, hash_key->arKey, hash_key->nKeyLength, hash_key->h, value, sizeof(zval *), NULL);
                }
@@ -181,10 +229,20 @@ int php_http_array_apply_merge_func(void *pDest TSRMLS_DC, int num_args, va_list
 
 /* PASS CALLBACK */
 
-PHP_HTTP_API size_t php_http_pass_wrapper(php_http_pass_callback_arg_t *cb, const char *str, size_t len)
+PHP_HTTP_API size_t php_http_pass_fcall_callback(void *cb_arg, const char *str, size_t len)
 {
-       TSRMLS_FETCH();
-       return cb->cb_zts(cb->cb_arg, str, len TSRMLS_CC);
+       php_http_pass_fcall_arg_t *fcd = cb_arg;
+       zval *zdata;
+       TSRMLS_FETCH_FROM_CTX(fcd->ts);
+
+       MAKE_STD_ZVAL(zdata);
+       ZVAL_STRINGL(zdata, str, len, 1);
+       if (SUCCESS == zend_fcall_info_argn(&fcd->fci TSRMLS_CC, 2, &fcd->fcz, &zdata)) {
+               zend_fcall_info_call(&fcd->fci, &fcd->fcc, NULL, NULL TSRMLS_CC);
+               zend_fcall_info_args_clear(&fcd->fci, 0);
+       }
+       zval_ptr_dtor(&zdata);
+       return len;
 }
 
 /* ERROR */
@@ -195,7 +253,7 @@ static inline int scope_error_handling(long type TSRMLS_DC)
                return EH_THROW;
        }
 
-       if (EG(This) && instanceof_function(Z_OBJCE_P(EG(This)), php_http_object_class_entry)) {
+       if (EG(This) && instanceof_function(Z_OBJCE_P(EG(This)), php_http_object_get_class_entry() TSRMLS_CC)) {
                return php_http_object_get_error_handling(EG(This) TSRMLS_CC);
        }
 
@@ -210,12 +268,11 @@ void php_http_error(long type TSRMLS_DC, long code, const char *format, ...)
        switch (scope_error_handling(type TSRMLS_CC)) {
                case EH_THROW: {
                        char *message;
-                       zend_class_entry *ce;
+                       zend_class_entry *ce = php_http_exception_get_class_entry();
 
-                       if (0&& EG(exception_class) && instanceof_function(EG(exception_class), php_http_exception_class_entry)) {
+                       /*  FIXME wat? */
+                       if (0&& EG(exception_class) && instanceof_function(EG(exception_class), ce TSRMLS_CC)) {
                                ce = EG(exception_class);
-                       } else {
-                               ce = php_http_exception_get_for_code(code);
                        }
 
                        vspprintf(&message, 0, format, args);