Commits

Ruslan Osmanov committed 52a846b

Del: EventBufferPosition class and the stuff related to it

  • Participants
  • Parent commits 0deace7

Comments (0)

Files changed (11)

 #include "src/util.h"
 #include "src/priv.h"
 
+/* {{{ _get_start_pos */
+static int _get_pos_from_zval(struct evbuffer_ptr *out_ptr, const zval *pz, struct evbuffer *buf TSRMLS_CC)
+{
+	if (!pz) {
+		return FAILURE;
+	}
+
+	if (Z_TYPE_P(pz) == IS_LONG) {
+		if (evbuffer_ptr_set(buf, out_ptr, Z_LVAL_P(pz), EVBUFFER_PTR_SET) == -1) {
+			php_error_docref(NULL TSRMLS_CC, E_WARNING,
+					"Failed to set position to %ld", Z_LVAL_P(pz));
+			return FAILURE;
+		}
+	} else {
+		php_error_docref(NULL TSRMLS_CC, E_WARNING,
+				"Invalid start position argument passed");
+		return FAILURE;
+	}
+
+	return SUCCESS;
+}
+/* }}} */
+
+
 /* {{{ proto EventBuffer EventBuffer::__construct(void); */
 PHP_METHOD(EventBuffer, __construct)
 {
 }
 /* }}} */
 
-/* {{{ proto EventBufferPosition EventBuffer::search(string what[, EventBufferPosition start = NULL[, EventBufferPosition end = NULL]]);
+/* {{{ proto int EventBuffer::search(int what[, int start = NULL[, int end = NULL]]);
  *
  * Scans the buffer for an occurrence of the len-character string what. It
  * returns object representing the position of the string, or NULL if the
  * of the string. If end argument provided, the search is performed between
  * start and end buffer positions.
  *
- * Returns EventBufferPosition representing position of the first occurance of the string
- * in the buffer, or NULL if string is not found
+ * Returns position of the first occurance of the string
+ * in the buffer, or -1 if string is not found.
  */
 PHP_METHOD(EventBuffer, search)
 {
 	char                   *what;
 	int                     what_len;
 	php_event_buffer_t     *b;
-	php_event_buffer_pos_t *start_pos  = NULL;
-	php_event_buffer_pos_t *end_pos    = NULL;
-	php_event_buffer_pos_t *res_pos;
 
-	if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s|O!O!",
+	struct evbuffer_ptr ptr_start, ptr_end, ptr_res;
+
+
+	if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s|z!z!",
 				&what, &what_len,
-				&zstart_pos, php_event_buffer_pos_ce,
-				&zend_pos, php_event_buffer_pos_ce) == FAILURE) {
+				&zstart_pos,
+				&zend_pos) == FAILURE) {
 		return;
 	}
 
 	PHP_EVENT_FETCH_BUFFER(b, zbuf);
 
-	PHP_EVENT_INIT_CLASS_OBJECT(return_value, php_event_buffer_pos_ce);
-	PHP_EVENT_FETCH_BUFFER_POS(res_pos, return_value);
-
-	if (zstart_pos) {
-		PHP_EVENT_FETCH_BUFFER_POS(start_pos, zstart_pos);
+	if (zstart_pos != NULL
+			&& _get_pos_from_zval(&ptr_start, zstart_pos, b->buf TSRMLS_CC) == FAILURE) {
+		zstart_pos = NULL;
+	}
+	if (zend_pos != NULL
+			&& _get_pos_from_zval(&ptr_end, zend_pos, b->buf TSRMLS_CC) == FAILURE) {
+		zend_pos = NULL;
 	}
 
 	if (zend_pos) {
-		PHP_EVENT_FETCH_BUFFER_POS(end_pos, zend_pos);
-
-		res_pos->p = evbuffer_search_range(b->buf, what, (size_t) what_len,
-				&start_pos->p, &end_pos->p);
+		ptr_res = evbuffer_search_range(b->buf, what, (size_t) what_len,
+				(zstart_pos ? &ptr_start : NULL), &ptr_end);
 	} else {
-		res_pos->p = evbuffer_search(b->buf, what, (size_t) what_len, (start_pos ? &start_pos->p : NULL));
+		ptr_res = evbuffer_search(b->buf, what, (size_t) what_len,
+				(zstart_pos ? &ptr_start : NULL));
 	}
 
-	if (res_pos->p.pos == -1) {
-		RETURN_NULL();
-	}
-}
-/* }}} */
-
-/* {{{ proto bool EventBuffer::setPosition(EventBufferPosition &pos, int value, int how);
- *
- * Manipulates the position pos within buffer. If how is EventBuffer::PTR_SET,
- * the pointer is moved to an absolute position position within the buffer. If
- * it is EventBuffer::PTR_ADD, the pointer moves position bytes forward.
- */
-PHP_METHOD(EventBuffer, setPosition)
-{
-	zval                *zbuf  = getThis();
-	zval                *zpos;
-	long                 value;
-	long                 how;
-	php_event_buffer_t  *b;
-	php_event_buffer_pos_t *pos   = NULL;
-
-	if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "Oll",
-				&zpos, php_event_buffer_pos_ce, &value, &how) == FAILURE) {
-		return;
-	}
-
-	if (how & ~(EVBUFFER_PTR_SET | EVBUFFER_PTR_ADD)) {
-		php_error_docref(NULL TSRMLS_CC, E_WARNING, "Invalid mask");
-		RETURN_FALSE;
-	}
-
-	PHP_EVENT_FETCH_BUFFER(b, zbuf);
-	PHP_EVENT_FETCH_BUFFER_POS(pos, zpos);
-
-	if (evbuffer_ptr_set(b->buf, &pos->p, value, how)) {
-		RETURN_FALSE;
-	}
-
-	RETVAL_TRUE;
+	RETVAL_LONG(ptr_res.pos);
 }
 /* }}} */
 

classes/buffer_pos.c

-/*
-   +----------------------------------------------------------------------+
-   | PHP Version 5                                                        |
-   +----------------------------------------------------------------------+
-   | Copyright (c) 1997-2013 The PHP Group                                |
-   +----------------------------------------------------------------------+
-   | This source file is subject to version 3.01 of the PHP license,      |
-   | that is bundled with this package in the file LICENSE, and is        |
-   | available through the world-wide-web at the following url:           |
-   | http://www.php.net/license/3_01.txt                                  |
-   | If you did not receive a copy of the PHP license and are unable to   |
-   | obtain it through the world-wide-web, please send a note to          |
-   | license@php.net so we can mail you a copy immediately.               |
-   +----------------------------------------------------------------------+
-   | Author: Ruslan Osmanov <osmanov@php.net>                             |
-   +----------------------------------------------------------------------+
-*/
-#include "src/common.h"
-#include "src/util.h"
-#include "src/priv.h"
-
-/* {{{ proto EventBufferPosition EventBufferPosition::__construct(void); */
-PHP_METHOD(EventBufferPosition, __construct)
-{
-	php_event_buffer_pos_t *p;
-
-	if (zend_parse_parameters_none() == FAILURE) {
-		return;
-	}
-
-	PHP_EVENT_FETCH_BUFFER_POS(p, getThis());
-	p->p.pos = 0;
-}
-/* }}} */
-
-/*
- * Local variables:
- * tab-width: 4
- * c-basic-offset: 4
- * End:
- * vim600: noet sw=4 ts=4 sts=4 fdm=marker
- * vim<600: noet sw=4 ts=4 sts=4
- */
     classes/event_config.c \
     classes/buffer_event.c \
     classes/buffer.c \
-    classes/buffer_pos.c \
     classes/event_util.c"
   dnl }}}
 
     <email>osmanov@php.net</email>
     <active>yes</active>
   </lead>
-  <date>2013-03-15</date>
+  <date>2013-03-19</date>
   <!--{{{ Current version -->
   <version>
-    <release>1.4.1</release>
-    <api>1.4.0</api>
+    <release>1.5.0</release>
+    <api>1.5.0</api>
   </version>
   <stability>
     <release>beta</release>
   <license uri="http://www.php.net/license">PHP</license>
   <notes><![CDATA[
   Fix: uninitialized socket caused segmentation fault in EventBufferEvent methods
+  Del: EventBufferPosition class removed, EventBuffer::search now accepts and returns integers
   ]]></notes>
   <!--}}}-->
   <!--{{{ Contents -->
   <!--{{{ changelog-->
   <changelog>
     <release>
-      <!--{{{ 1.4.1-beta -->
+      <!--{{{ 1.5.0-beta -->
       <version>
-        <release>1.4.1</release>
-        <api>1.4.0</api>
+        <release>1.5.0</release>
+        <api>1.5.0</api>
       </version>
       <stability>
         <release>beta</release>
       <license uri="http://www.php.net/license">PHP</license>
       <notes><![CDATA[
         Fix: uninitialized socket caused segmentation fault in EventBufferEvent methods
+        Del: EventBufferPosition class removed, EventBuffer::search now accepts and returns integers
         ]]></notes>
       <!--}}}-->
     </release>
 zend_class_entry *php_event_bevent_ce;
 zend_class_entry *php_event_buffer_ce;
 zend_class_entry *php_event_util_ce;
-zend_class_entry *php_event_buffer_pos_ce;
 #ifdef HAVE_EVENT_OPENSSL_LIB
 zend_class_entry *php_event_ssl_context_ce;
 #endif
 static HashTable event_properties;
 static HashTable event_bevent_properties;
 static HashTable event_buffer_properties;
-static HashTable event_buffer_pos_properties;
 #ifdef HAVE_EVENT_OPENSSL_LIB
 static HashTable event_ssl_context_properties;
 int php_event_ssl_data_index;
 
 #endif /* HAVE_EVENT_EXTRA_LIB */
 
-/* {{{ event_buffer_pos_object_free_storage */
-static void event_buffer_pos_object_free_storage(void *ptr TSRMLS_DC)
-{
-	event_generic_object_free_storage(ptr TSRMLS_CC);
-}
-/* }}} */
-
 
 #ifdef HAVE_EVENT_OPENSSL_LIB
 /* {{{ event_ssl_context_object_free_storage */
 }
 /* }}} */
 
-/* {{{ event_buffer_pos_object_create
- * EventBufferPosition object ctor */
-static zend_object_value event_buffer_pos_object_create(zend_class_entry *ce TSRMLS_DC)
-{
-	php_event_abstract_object_t *obj = (php_event_abstract_object_t *) object_new(ce, sizeof(php_event_buffer_pos_t) TSRMLS_CC);
-
-	return register_object(ce, (void *) obj, (zend_objects_store_dtor_t) zend_objects_destroy_object,
-			event_buffer_pos_object_free_storage TSRMLS_CC);
-}
-/* }}} */
-
 #ifdef HAVE_EVENT_OPENSSL_LIB
 /* {{{ event_ssl_context_object_create
- * EventBufferPosition object ctor */
+ * EventSslContext object ctor */
 static zend_object_value event_ssl_context_object_create(zend_class_entry *ce TSRMLS_DC)
 {
 	php_event_abstract_object_t *obj = (php_event_abstract_object_t *)
 	zend_hash_add(&classes, ce->name, ce->name_length + 1, &event_buffer_properties,
 			sizeof(event_buffer_properties), NULL);
 
-	PHP_EVENT_REGISTER_CLASS("EventBufferPosition", event_buffer_pos_object_create,
-			php_event_buffer_pos_ce,
-			php_event_buffer_pos_ce_functions);
-	ce = php_event_buffer_pos_ce;
-	ce->ce_flags |= ZEND_ACC_FINAL_CLASS;
-	zend_hash_init(&event_buffer_pos_properties, 0, NULL, NULL, 1);
-	PHP_EVENT_ADD_CLASS_PROPERTIES(&event_buffer_pos_properties, event_buffer_pos_property_entries);
-	PHP_EVENT_DECL_CLASS_PROPERTIES(ce, event_buffer_pos_property_entry_info);
-	zend_hash_add(&classes, ce->name, ce->name_length + 1, &event_buffer_pos_properties,
-			sizeof(event_buffer_pos_properties), NULL);
-
 #if HAVE_EVENT_EXTRA_LIB
 	PHP_EVENT_REGISTER_CLASS("EventDnsBase", event_dns_base_object_create, php_event_dns_base_ce,
 			php_event_dns_base_ce_functions);
 	zend_hash_destroy(&event_properties);
 	zend_hash_destroy(&event_bevent_properties);
 	zend_hash_destroy(&event_buffer_properties);
-	zend_hash_destroy(&event_buffer_pos_properties);
 #ifdef HAVE_EVENT_OPENSSL_LIB
 	zend_hash_destroy(&event_ssl_context_properties);
 #endif
 	PHP_ME(EventBuffer, copyout,       arginfo_evbuffer_remove,        ZEND_ACC_PUBLIC)
 	PHP_ME(EventBuffer, readLine,      arginfo_evbuffer_read_line,     ZEND_ACC_PUBLIC)
 	PHP_ME(EventBuffer, search,        arginfo_evbuffer_search,        ZEND_ACC_PUBLIC)
-	PHP_ME(EventBuffer, setPosition,   arginfo_evbuffer_set_position,  ZEND_ACC_PUBLIC)
 	PHP_ME(EventBuffer, pullup,        arginfo_evbuffer_pullup,        ZEND_ACC_PUBLIC)
 
 	PHP_FE_END
 };
 /* }}} */
 
-const zend_function_entry php_event_buffer_pos_ce_functions[] = {/* {{{ */
-	PHP_ME(EventBufferPosition, __construct, arginfo_event__void, ZEND_ACC_PUBLIC | ZEND_ACC_CTOR)
-
-	PHP_FE_END
-};
 /* }}} */
 
 #if HAVE_EVENT_EXTRA_LIB
 PHP_METHOD(EventBuffer, copyout);
 PHP_METHOD(EventBuffer, readLine);
 PHP_METHOD(EventBuffer, search);
-PHP_METHOD(EventBuffer, setPosition);
 PHP_METHOD(EventBuffer, pullup);
 
 PHP_METHOD(EventUtil, getLastSocketErrno);
 }
 /* }}} */
 
-/* {{{ event_buffer_pos_position_prop_read */
-static int event_buffer_pos_position_prop_read(php_event_abstract_object_t *obj, zval **retval TSRMLS_DC)
-{
-	php_event_buffer_pos_t *pos = (php_event_buffer_pos_t *) obj;
-
-	MAKE_STD_ZVAL(*retval);
-	ZVAL_LONG(*retval, pos->p.pos);
-
-	return SUCCESS;
-}
-/* }}} */
-
 /* {{{ event_bevent_priority_prop_write*/
 static int event_bevent_priority_prop_write(php_event_abstract_object_t *obj, zval *value TSRMLS_DC)
 {
 	{"contiguous_space", sizeof("contiguous_space") - 1, event_buffer_contiguous_space_prop_read, NULL, NULL},
     {NULL, 0, NULL, NULL, NULL}
 };
-const php_event_property_entry_t event_buffer_pos_property_entries[] = {
-	{"position", sizeof("position") - 1, event_buffer_pos_position_prop_read, NULL, NULL},
-    {NULL, 0, NULL, NULL, NULL}
-};
 #ifdef HAVE_EVENT_OPENSSL_LIB
 const php_event_property_entry_t event_ssl_context_property_entries[] = {
 	{"local_cert", sizeof("local_cert") - 1, event_ssl_context_local_cert_prop_read, event_ssl_context_local_cert_prop_write, NULL},
 	{ZEND_ACC_PUBLIC, "contiguous_space", sizeof("contiguous_space") - 1, -1, 0, NULL, 0, NULL},
 	{0, NULL, 0, -1, 0, NULL, 0, NULL}
 };
-const zend_property_info event_buffer_pos_property_entry_info[] = {
-	{ZEND_ACC_PUBLIC, "position", sizeof("position") - 1, -1, 0, NULL, 0, NULL},
-	{0, NULL, 0, -1, 0, NULL, 0, NULL}
-};
 #ifdef HAVE_EVENT_OPENSSL_LIB
 const zend_property_info event_ssl_context_property_entry_info[] = {
 	{ZEND_ACC_PUBLIC, "local_cert", sizeof("local_cert") - 1, -1, 0, NULL, 0, NULL},
 extern const zend_function_entry php_event_bevent_ce_functions[];
 extern const zend_function_entry php_event_buffer_ce_functions[];
 extern const zend_function_entry php_event_util_ce_functions[];
-extern const zend_function_entry php_event_buffer_pos_ce_functions[];
 extern const zend_function_entry php_event_ssl_context_ce_functions[];
 
 extern zend_class_entry *php_event_ce;
 extern zend_class_entry *php_event_config_ce;
 extern zend_class_entry *php_event_bevent_ce;
 extern zend_class_entry *php_event_buffer_ce;
-extern zend_class_entry *php_event_buffer_pos_ce;
 extern zend_class_entry *php_event_util_ce;
 #ifdef HAVE_EVENT_OPENSSL_LIB
 extern zend_class_entry *php_event_ssl_context_ce;
 extern const php_event_property_entry_t event_property_entries[];
 extern const php_event_property_entry_t event_bevent_property_entries[];
 extern const php_event_property_entry_t event_buffer_property_entries[];
-extern const php_event_property_entry_t event_buffer_pos_property_entries[];
 #ifdef HAVE_EVENT_OPENSSL_LIB
 extern const php_event_property_entry_t event_ssl_context_property_entries[];
 #endif
 extern const zend_property_info event_property_entry_info[];
 extern const zend_property_info event_bevent_property_entry_info[];
 extern const zend_property_info event_buffer_property_entry_info[];
-extern const zend_property_info event_buffer_pos_property_entry_info[];
 #ifdef HAVE_EVENT_OPENSSL_LIB
 extern const zend_property_info event_ssl_context_property_entry_info[];
 #endif
 
 #endif/* HAVE_EVENT_EXTRA_LIB }}} */
 
-typedef struct _php_event_buffer_pos_t {
-	PHP_EVENT_OBJECT_HEAD;
-
-	struct evbuffer_ptr p;
-} php_event_buffer_pos_t;
-
 #ifdef HAVE_EVENT_OPENSSL_LIB
 
 enum {

tests/05-buffer-pos.phpt

 /* Count total occurances of 'str' in 'buf' */
 function count_instances($buf, $str) {
     $total = 0;
-	$p = new EventBufferPosition();
-
-    $buf->setPosition($p, 0, EventBuffer::PTR_SET);
+	$p = 0;
 
 	$i = 0;
     while (1) {
         $p = $buf->search($str, $p);
-        if (!$p) {
+        if ($p == -1) {
             break;
 		}
         ++$total;
-        $buf->setPosition($p, 1, EventBuffer::PTR_ADD);
+		++$p;
     }
 
     return $total;