From fb31b1cf4da2bfd0830d0a83754f5ecb125d1c4a Mon Sep 17 00:00:00 2001
From: Michael Heimpold <mhei@heimpold.de>
Date: Thu, 14 Jul 2016 00:38:52 +0200
Subject: [PATCH 10/16] Replace dio_convert_to_long

Signed-off-by: Michael Heimpold <mhei@heimpold.de>
---
 dio_common.c     | 106 +++++++++++++++++++++++--------------------------------
 php_dio_common.h |   2 --
 2 files changed, 44 insertions(+), 64 deletions(-)

diff --git a/dio_common.c b/dio_common.c
index c50f56d..a5f4c63 100644
--- a/dio_common.c
+++ b/dio_common.c
@@ -52,29 +52,12 @@ void dio_init_stream_data(php_dio_stream_data *data) {
 }
 /* }}} */
 
-/* {{{ dio_convert_to_long
- * Returns as a long, the value of the zval regardless of its type.
- */
-long dio_convert_to_long(zval *val) {
-	zval *copyval;
-	long  longval;
-
-	ALLOC_INIT_ZVAL(copyval);
-	*copyval = *val;
-	convert_to_long(copyval);
-	longval = Z_LVAL_P(copyval);
-	zval_ptr_dtor(&copyval);
-
-	return longval;
-}
-/* }}} */
-
 /* {{{ dio_assoc_array_get_basic_options
  * Retrieves the basic open option values from an associative array
  */
 void dio_assoc_array_get_basic_options(zval *options, php_dio_stream_data *data TSRMLS_DC) {
 #if defined(DIO_HAS_FILEPERMS) || defined(DIO_NONBLOCK)
-	zval **tmpzval;
+	zval *tmpzval;
 	HashTable *opthash;
 
 	opthash = HASH_OF(options);
@@ -82,8 +65,8 @@ void dio_assoc_array_get_basic_options(zval *options, php_dio_stream_data *data
 
 #ifdef DIO_HAS_FILEPERMS
 	/* This is the file mode flags used by open(). */
-	if (zend_hash_find(opthash, "perms", sizeof("perms"), (void **)&tmpzval) == SUCCESS && tmpzval && *tmpzval) {
-		data->perms = (int)dio_convert_to_long(*tmpzval);
+	if ((tmpzval = zend_hash_str_find(opthash, "perms", sizeof("perms"))) != NULL) {
+		data->perms = (int)zval_get_long(tmpzval);
 		data->has_perms = 1;
 	}
 #endif
@@ -91,20 +74,20 @@ void dio_assoc_array_get_basic_options(zval *options, php_dio_stream_data *data
 #ifdef DIO_NONBLOCK
 	/* This sets the underlying stream to be blocking/non
 	   block (i.e. O_NONBLOCK) */
-	if (zend_hash_find(opthash, "is_blocking", sizeof("is_blocking"), (void **)&tmpzval) == SUCCESS && tmpzval && *tmpzval) {
-		data->is_blocking = dio_convert_to_long(*tmpzval) ? 1 : 0;
+	if ((tmpzval = zend_hash_str_find(opthash, "is_blocking", sizeof("is_blocking"))) != NULL) {
+		data->is_blocking = zval_get_long(tmpzval) ? 1 : 0;
 	}
 
 	/* This is the timeout value for reads in seconds.  Only one of
 	   timeout_secs or timeout_usecs need be defined to define a timeout. */
-	if (zend_hash_find(opthash, "timeout_secs", sizeof("timeout_secs"), (void **)&tmpzval) == SUCCESS && tmpzval && *tmpzval) {
-		data->timeout_sec = dio_convert_to_long(*tmpzval);
+	if ((tmpzval = zend_hash_str_find(opthash, "timeout_secs", sizeof("timeout_secs"))) != NULL) {
+		data->timeout_sec = zval_get_long(tmpzval);
 	}
 
 	/* This is the timeout value for reads in microseconds.  Only one of
 	   timeout_secs or timeout_usecs need be defined to define a timeout. */
-	if (zend_hash_find(opthash, "timeout_usecs", sizeof("timeout_usecs"), (void **)&tmpzval) == SUCCESS && tmpzval && *tmpzval) {
-		data->timeout_usec = dio_convert_to_long(*tmpzval);
+	if ((tmpzval = zend_hash_str_find(opthash, "timeout_usecs", sizeof("timeout_usecs"))) != NULL) {
+		data->timeout_usec = zval_get_long(tmpzval);
 	}
 
 	data->has_timeout = (data->timeout_sec | data->timeout_usec) ? 1 : 0;
@@ -116,33 +99,33 @@ void dio_assoc_array_get_basic_options(zval *options, php_dio_stream_data *data
  * Retrieves the serial open option values from an associative array
  */
 void dio_assoc_array_get_serial_options(zval *options, php_dio_stream_data *data TSRMLS_DC) {
-	zval **tmpzval;
+	zval *tmpzval;
 	HashTable *opthash;
 
 	opthash = HASH_OF(options);
 
-	if (zend_hash_find(opthash, "data_rate", sizeof("data_rate"), (void **)&tmpzval) == SUCCESS && tmpzval && *tmpzval) {
-		data->data_rate = dio_convert_to_long(*tmpzval);
+	if ((tmpzval = zend_hash_str_find(opthash, "data_rate", sizeof("data_rate"))) != NULL) {
+		data->data_rate = zval_get_long(tmpzval);
 	}
 
-	if (zend_hash_find(opthash, "data_bits", sizeof("data_bits"), (void **)&tmpzval) == SUCCESS && tmpzval && *tmpzval) {
-		data->data_bits = (int)dio_convert_to_long(*tmpzval);
+	if ((tmpzval = zend_hash_str_find(opthash, "data_bits", sizeof("data_bits"))) != NULL) {
+		data->data_bits = (int)zval_get_long(tmpzval);
 	}
 
-	if (zend_hash_find(opthash, "stop_bits", sizeof("stop_bits"), (void **)&tmpzval) == SUCCESS && tmpzval && *tmpzval) {
-		data->stop_bits = (int)dio_convert_to_long(*tmpzval);
+	if ((tmpzval = zend_hash_str_find(opthash, "stop_bits", sizeof("stop_bits"))) != NULL) {
+		data->stop_bits = (int)zval_get_long(tmpzval);
 	}
 
-	if (zend_hash_find(opthash, "parity", sizeof("parity"), (void **)&tmpzval) == SUCCESS && tmpzval && *tmpzval) {
-		data->parity = (int)dio_convert_to_long(*tmpzval);
+	if ((tmpzval = zend_hash_str_find(opthash, "parity", sizeof("parity"))) != NULL) {
+		data->parity = (int)zval_get_long(tmpzval);
 	}
 
-	if (zend_hash_find(opthash, "flow_control", sizeof("flow_control"), (void **)&tmpzval) == SUCCESS && tmpzval && *tmpzval) {
-		data->flow_control = (int)(dio_convert_to_long(*tmpzval) ? 1 : 0);
+	if ((tmpzval = zend_hash_str_find(opthash, "flow_control", sizeof("flow_control"))) != NULL) {
+		data->flow_control = zval_get_long(tmpzval) ? 1 : 0;
 	}
 
-	if (zend_hash_find(opthash, "is_canonical", sizeof("is_canonical"), (void **)&tmpzval) == SUCCESS && tmpzval && *tmpzval) {
-		data->canonical = (int)(dio_convert_to_long(*tmpzval) ? 1 : 0);
+	if ((tmpzval = zend_hash_str_find(opthash, "is_canonical", sizeof("is_canonical"))) != NULL) {
+		data->canonical = zval_get_long(tmpzval) ? 1 : 0;
 	}
 }
 /* }}} */
@@ -152,13 +135,13 @@ void dio_assoc_array_get_serial_options(zval *options, php_dio_stream_data *data
  */
 void dio_stream_context_get_basic_options(php_stream_context *context, php_dio_stream_data *data TSRMLS_DC) {
 #if defined(DIO_HAS_FILEPERMS) || defined(DIO_NONBLOCK)
-	zval **tmpzval;
+	zval *tmpzval;
 #endif
 
 #ifdef DIO_HAS_FILEPERMS
 	/* This is the file mode flags used by open(). */
-	if (php_stream_context_get_option(context, "dio", "perms", &tmpzval) == SUCCESS && tmpzval && *tmpzval) {
-		data->perms = (int)dio_convert_to_long(*tmpzval);
+	if ((tmpzval = php_stream_context_get_option(context, "dio", "perms")) != NULL) {
+		data->perms = (int)zval_get_long(tmpzval);
 		data->has_perms = 1;
 	}
 #endif
@@ -166,20 +149,20 @@ void dio_stream_context_get_basic_options(php_stream_context *context, php_dio_s
 #ifdef DIO_NONBLOCK
 	/* This sets the underlying stream to be blocking/non
 	   block (i.e. O_NONBLOCK) */
-	if (php_stream_context_get_option(context, "dio", "is_blocking", &tmpzval) == SUCCESS && tmpzval && *tmpzval) {
-		data->is_blocking = dio_convert_to_long(*tmpzval) ? 1 : 0;
+	if ((tmpzval = php_stream_context_get_option(context, "dio", "is_blocking")) != NULL) {
+		data->is_blocking = zval_get_long(tmpzval) ? 1 : 0;
 	}
 
 	/* This is the timeout value for reads in seconds.  Only one of
 	   timeout_secs or timeout_usecs need be defined to define a timeout. */
-	if (php_stream_context_get_option(context, "dio", "timeout_secs", &tmpzval) == SUCCESS && tmpzval && *tmpzval) {
-		data->timeout_sec = dio_convert_to_long(*tmpzval);
+	if ((tmpzval = php_stream_context_get_option(context, "dio", "timeout_secs")) != NULL) {
+		data->timeout_sec = zval_get_long(tmpzval);
 	}
 
 	/* This is the timeout value for reads in microseconds.  Only one of
 	   timeout_secs or timeout_usecs need be defined to define a timeout. */
-	if (php_stream_context_get_option(context, "dio", "timeout_usecs", &tmpzval) == SUCCESS && tmpzval && *tmpzval) {
-		data->timeout_usec = dio_convert_to_long(*tmpzval);
+	if ((tmpzval = php_stream_context_get_option(context, "dio", "timeout_usecs")) != NULL) {
+		data->timeout_usec = zval_get_long(tmpzval);
 	}
 
 	data->has_timeout = (data->timeout_sec | data->timeout_usec) ? 1 : 0;
@@ -191,30 +174,30 @@ void dio_stream_context_get_basic_options(php_stream_context *context, php_dio_s
  * Extracts the option values for dio.serial mode from a context
  */
 void dio_stream_context_get_serial_options(php_stream_context *context, php_dio_stream_data *data TSRMLS_DC) {
-	zval **tmpzval;
+	zval *tmpzval;
 
-	if (php_stream_context_get_option(context, "dio", "data_rate", &tmpzval) == SUCCESS && tmpzval && *tmpzval) {
-		data->data_rate = dio_convert_to_long(*tmpzval);
+	if ((tmpzval = php_stream_context_get_option(context, "dio", "data_rate")) != NULL) {
+		data->data_rate = zval_get_long(tmpzval);
 	}
 
-	if (php_stream_context_get_option(context, "dio", "data_bits", &tmpzval) == SUCCESS && tmpzval && *tmpzval) {
-		data->data_bits = (int)dio_convert_to_long(*tmpzval);
+	if ((tmpzval = php_stream_context_get_option(context, "dio", "data_bits")) != NULL) {
+		data->data_bits = (int)zval_get_long(tmpzval);
 	}
 
-	if (php_stream_context_get_option(context, "dio", "stop_bits", &tmpzval) == SUCCESS && tmpzval && *tmpzval) {
-		data->stop_bits = (int)dio_convert_to_long(*tmpzval);
+	if ((tmpzval = php_stream_context_get_option(context, "dio", "stop_bits")) != NULL) {
+		data->stop_bits = (int)zval_get_long(tmpzval);
 	}
 
-	if (php_stream_context_get_option(context, "dio", "parity", &tmpzval) == SUCCESS && tmpzval && *tmpzval) {
-		data->parity = (int)dio_convert_to_long(*tmpzval);
+	if ((tmpzval = php_stream_context_get_option(context, "dio", "parity")) != NULL) {
+		data->parity = (int)zval_get_long(tmpzval);
 	}
 
-	if (php_stream_context_get_option(context, "dio", "flow_control", &tmpzval) == SUCCESS && tmpzval && *tmpzval) {
-		data->flow_control = (int)(dio_convert_to_long(*tmpzval) ? 1 : 0);
+	if ((tmpzval = php_stream_context_get_option(context, "dio", "flow_control")) != NULL) {
+		data->flow_control = zval_get_long(tmpzval) ? 1 : 0;
 	}
 
-	if (php_stream_context_get_option(context, "dio", "is_canonical", &tmpzval) == SUCCESS && tmpzval && *tmpzval) {
-		data->canonical = (int)(dio_convert_to_long(*tmpzval) ? 1 : 0);
+	if ((tmpzval = php_stream_context_get_option(context, "dio", "is_canonical")) != NULL) {
+		data->canonical = zval_get_long(tmpzval) ? 1 : 0;
 	}
 }
 /* }}} */
@@ -227,4 +210,3 @@ void dio_stream_context_get_serial_options(php_stream_context *context, php_dio_
  * vim600: fdm=marker
  * vim: sw=4 ts=4 noet
  */
-
diff --git a/php_dio_common.h b/php_dio_common.h
index f6e4d98..6b14040 100644
--- a/php_dio_common.h
+++ b/php_dio_common.h
@@ -35,8 +35,6 @@
 #define DIO_STREAM_TYPE_RAW    1
 #define DIO_STREAM_TYPE_SERIAL 2
 
-long dio_convert_to_long(zval *val);
-
 php_dio_stream_data * dio_create_stream_data(void);
 
 void dio_init_stream_data(php_dio_stream_data *data);
-- 
2.5.0

