You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

240 lines
10 KiB

  1. From fb31b1cf4da2bfd0830d0a83754f5ecb125d1c4a Mon Sep 17 00:00:00 2001
  2. From: Michael Heimpold <mhei@heimpold.de>
  3. Date: Thu, 14 Jul 2016 00:38:52 +0200
  4. Subject: [PATCH 10/16] Replace dio_convert_to_long
  5. Signed-off-by: Michael Heimpold <mhei@heimpold.de>
  6. ---
  7. dio_common.c | 106 +++++++++++++++++++++++--------------------------------
  8. php_dio_common.h | 2 --
  9. 2 files changed, 44 insertions(+), 64 deletions(-)
  10. diff --git a/dio_common.c b/dio_common.c
  11. index c50f56d..a5f4c63 100644
  12. --- a/dio_common.c
  13. +++ b/dio_common.c
  14. @@ -52,29 +52,12 @@ void dio_init_stream_data(php_dio_stream_data *data) {
  15. }
  16. /* }}} */
  17. -/* {{{ dio_convert_to_long
  18. - * Returns as a long, the value of the zval regardless of its type.
  19. - */
  20. -long dio_convert_to_long(zval *val) {
  21. - zval *copyval;
  22. - long longval;
  23. -
  24. - ALLOC_INIT_ZVAL(copyval);
  25. - *copyval = *val;
  26. - convert_to_long(copyval);
  27. - longval = Z_LVAL_P(copyval);
  28. - zval_ptr_dtor(&copyval);
  29. -
  30. - return longval;
  31. -}
  32. -/* }}} */
  33. -
  34. /* {{{ dio_assoc_array_get_basic_options
  35. * Retrieves the basic open option values from an associative array
  36. */
  37. void dio_assoc_array_get_basic_options(zval *options, php_dio_stream_data *data TSRMLS_DC) {
  38. #if defined(DIO_HAS_FILEPERMS) || defined(DIO_NONBLOCK)
  39. - zval **tmpzval;
  40. + zval *tmpzval;
  41. HashTable *opthash;
  42. opthash = HASH_OF(options);
  43. @@ -82,8 +65,8 @@ void dio_assoc_array_get_basic_options(zval *options, php_dio_stream_data *data
  44. #ifdef DIO_HAS_FILEPERMS
  45. /* This is the file mode flags used by open(). */
  46. - if (zend_hash_find(opthash, "perms", sizeof("perms"), (void **)&tmpzval) == SUCCESS && tmpzval && *tmpzval) {
  47. - data->perms = (int)dio_convert_to_long(*tmpzval);
  48. + if ((tmpzval = zend_hash_str_find(opthash, "perms", sizeof("perms"))) != NULL) {
  49. + data->perms = (int)zval_get_long(tmpzval);
  50. data->has_perms = 1;
  51. }
  52. #endif
  53. @@ -91,20 +74,20 @@ void dio_assoc_array_get_basic_options(zval *options, php_dio_stream_data *data
  54. #ifdef DIO_NONBLOCK
  55. /* This sets the underlying stream to be blocking/non
  56. block (i.e. O_NONBLOCK) */
  57. - if (zend_hash_find(opthash, "is_blocking", sizeof("is_blocking"), (void **)&tmpzval) == SUCCESS && tmpzval && *tmpzval) {
  58. - data->is_blocking = dio_convert_to_long(*tmpzval) ? 1 : 0;
  59. + if ((tmpzval = zend_hash_str_find(opthash, "is_blocking", sizeof("is_blocking"))) != NULL) {
  60. + data->is_blocking = zval_get_long(tmpzval) ? 1 : 0;
  61. }
  62. /* This is the timeout value for reads in seconds. Only one of
  63. timeout_secs or timeout_usecs need be defined to define a timeout. */
  64. - if (zend_hash_find(opthash, "timeout_secs", sizeof("timeout_secs"), (void **)&tmpzval) == SUCCESS && tmpzval && *tmpzval) {
  65. - data->timeout_sec = dio_convert_to_long(*tmpzval);
  66. + if ((tmpzval = zend_hash_str_find(opthash, "timeout_secs", sizeof("timeout_secs"))) != NULL) {
  67. + data->timeout_sec = zval_get_long(tmpzval);
  68. }
  69. /* This is the timeout value for reads in microseconds. Only one of
  70. timeout_secs or timeout_usecs need be defined to define a timeout. */
  71. - if (zend_hash_find(opthash, "timeout_usecs", sizeof("timeout_usecs"), (void **)&tmpzval) == SUCCESS && tmpzval && *tmpzval) {
  72. - data->timeout_usec = dio_convert_to_long(*tmpzval);
  73. + if ((tmpzval = zend_hash_str_find(opthash, "timeout_usecs", sizeof("timeout_usecs"))) != NULL) {
  74. + data->timeout_usec = zval_get_long(tmpzval);
  75. }
  76. data->has_timeout = (data->timeout_sec | data->timeout_usec) ? 1 : 0;
  77. @@ -116,33 +99,33 @@ void dio_assoc_array_get_basic_options(zval *options, php_dio_stream_data *data
  78. * Retrieves the serial open option values from an associative array
  79. */
  80. void dio_assoc_array_get_serial_options(zval *options, php_dio_stream_data *data TSRMLS_DC) {
  81. - zval **tmpzval;
  82. + zval *tmpzval;
  83. HashTable *opthash;
  84. opthash = HASH_OF(options);
  85. - if (zend_hash_find(opthash, "data_rate", sizeof("data_rate"), (void **)&tmpzval) == SUCCESS && tmpzval && *tmpzval) {
  86. - data->data_rate = dio_convert_to_long(*tmpzval);
  87. + if ((tmpzval = zend_hash_str_find(opthash, "data_rate", sizeof("data_rate"))) != NULL) {
  88. + data->data_rate = zval_get_long(tmpzval);
  89. }
  90. - if (zend_hash_find(opthash, "data_bits", sizeof("data_bits"), (void **)&tmpzval) == SUCCESS && tmpzval && *tmpzval) {
  91. - data->data_bits = (int)dio_convert_to_long(*tmpzval);
  92. + if ((tmpzval = zend_hash_str_find(opthash, "data_bits", sizeof("data_bits"))) != NULL) {
  93. + data->data_bits = (int)zval_get_long(tmpzval);
  94. }
  95. - if (zend_hash_find(opthash, "stop_bits", sizeof("stop_bits"), (void **)&tmpzval) == SUCCESS && tmpzval && *tmpzval) {
  96. - data->stop_bits = (int)dio_convert_to_long(*tmpzval);
  97. + if ((tmpzval = zend_hash_str_find(opthash, "stop_bits", sizeof("stop_bits"))) != NULL) {
  98. + data->stop_bits = (int)zval_get_long(tmpzval);
  99. }
  100. - if (zend_hash_find(opthash, "parity", sizeof("parity"), (void **)&tmpzval) == SUCCESS && tmpzval && *tmpzval) {
  101. - data->parity = (int)dio_convert_to_long(*tmpzval);
  102. + if ((tmpzval = zend_hash_str_find(opthash, "parity", sizeof("parity"))) != NULL) {
  103. + data->parity = (int)zval_get_long(tmpzval);
  104. }
  105. - if (zend_hash_find(opthash, "flow_control", sizeof("flow_control"), (void **)&tmpzval) == SUCCESS && tmpzval && *tmpzval) {
  106. - data->flow_control = (int)(dio_convert_to_long(*tmpzval) ? 1 : 0);
  107. + if ((tmpzval = zend_hash_str_find(opthash, "flow_control", sizeof("flow_control"))) != NULL) {
  108. + data->flow_control = zval_get_long(tmpzval) ? 1 : 0;
  109. }
  110. - if (zend_hash_find(opthash, "is_canonical", sizeof("is_canonical"), (void **)&tmpzval) == SUCCESS && tmpzval && *tmpzval) {
  111. - data->canonical = (int)(dio_convert_to_long(*tmpzval) ? 1 : 0);
  112. + if ((tmpzval = zend_hash_str_find(opthash, "is_canonical", sizeof("is_canonical"))) != NULL) {
  113. + data->canonical = zval_get_long(tmpzval) ? 1 : 0;
  114. }
  115. }
  116. /* }}} */
  117. @@ -152,13 +135,13 @@ void dio_assoc_array_get_serial_options(zval *options, php_dio_stream_data *data
  118. */
  119. void dio_stream_context_get_basic_options(php_stream_context *context, php_dio_stream_data *data TSRMLS_DC) {
  120. #if defined(DIO_HAS_FILEPERMS) || defined(DIO_NONBLOCK)
  121. - zval **tmpzval;
  122. + zval *tmpzval;
  123. #endif
  124. #ifdef DIO_HAS_FILEPERMS
  125. /* This is the file mode flags used by open(). */
  126. - if (php_stream_context_get_option(context, "dio", "perms", &tmpzval) == SUCCESS && tmpzval && *tmpzval) {
  127. - data->perms = (int)dio_convert_to_long(*tmpzval);
  128. + if ((tmpzval = php_stream_context_get_option(context, "dio", "perms")) != NULL) {
  129. + data->perms = (int)zval_get_long(tmpzval);
  130. data->has_perms = 1;
  131. }
  132. #endif
  133. @@ -166,20 +149,20 @@ void dio_stream_context_get_basic_options(php_stream_context *context, php_dio_s
  134. #ifdef DIO_NONBLOCK
  135. /* This sets the underlying stream to be blocking/non
  136. block (i.e. O_NONBLOCK) */
  137. - if (php_stream_context_get_option(context, "dio", "is_blocking", &tmpzval) == SUCCESS && tmpzval && *tmpzval) {
  138. - data->is_blocking = dio_convert_to_long(*tmpzval) ? 1 : 0;
  139. + if ((tmpzval = php_stream_context_get_option(context, "dio", "is_blocking")) != NULL) {
  140. + data->is_blocking = zval_get_long(tmpzval) ? 1 : 0;
  141. }
  142. /* This is the timeout value for reads in seconds. Only one of
  143. timeout_secs or timeout_usecs need be defined to define a timeout. */
  144. - if (php_stream_context_get_option(context, "dio", "timeout_secs", &tmpzval) == SUCCESS && tmpzval && *tmpzval) {
  145. - data->timeout_sec = dio_convert_to_long(*tmpzval);
  146. + if ((tmpzval = php_stream_context_get_option(context, "dio", "timeout_secs")) != NULL) {
  147. + data->timeout_sec = zval_get_long(tmpzval);
  148. }
  149. /* This is the timeout value for reads in microseconds. Only one of
  150. timeout_secs or timeout_usecs need be defined to define a timeout. */
  151. - if (php_stream_context_get_option(context, "dio", "timeout_usecs", &tmpzval) == SUCCESS && tmpzval && *tmpzval) {
  152. - data->timeout_usec = dio_convert_to_long(*tmpzval);
  153. + if ((tmpzval = php_stream_context_get_option(context, "dio", "timeout_usecs")) != NULL) {
  154. + data->timeout_usec = zval_get_long(tmpzval);
  155. }
  156. data->has_timeout = (data->timeout_sec | data->timeout_usec) ? 1 : 0;
  157. @@ -191,30 +174,30 @@ void dio_stream_context_get_basic_options(php_stream_context *context, php_dio_s
  158. * Extracts the option values for dio.serial mode from a context
  159. */
  160. void dio_stream_context_get_serial_options(php_stream_context *context, php_dio_stream_data *data TSRMLS_DC) {
  161. - zval **tmpzval;
  162. + zval *tmpzval;
  163. - if (php_stream_context_get_option(context, "dio", "data_rate", &tmpzval) == SUCCESS && tmpzval && *tmpzval) {
  164. - data->data_rate = dio_convert_to_long(*tmpzval);
  165. + if ((tmpzval = php_stream_context_get_option(context, "dio", "data_rate")) != NULL) {
  166. + data->data_rate = zval_get_long(tmpzval);
  167. }
  168. - if (php_stream_context_get_option(context, "dio", "data_bits", &tmpzval) == SUCCESS && tmpzval && *tmpzval) {
  169. - data->data_bits = (int)dio_convert_to_long(*tmpzval);
  170. + if ((tmpzval = php_stream_context_get_option(context, "dio", "data_bits")) != NULL) {
  171. + data->data_bits = (int)zval_get_long(tmpzval);
  172. }
  173. - if (php_stream_context_get_option(context, "dio", "stop_bits", &tmpzval) == SUCCESS && tmpzval && *tmpzval) {
  174. - data->stop_bits = (int)dio_convert_to_long(*tmpzval);
  175. + if ((tmpzval = php_stream_context_get_option(context, "dio", "stop_bits")) != NULL) {
  176. + data->stop_bits = (int)zval_get_long(tmpzval);
  177. }
  178. - if (php_stream_context_get_option(context, "dio", "parity", &tmpzval) == SUCCESS && tmpzval && *tmpzval) {
  179. - data->parity = (int)dio_convert_to_long(*tmpzval);
  180. + if ((tmpzval = php_stream_context_get_option(context, "dio", "parity")) != NULL) {
  181. + data->parity = (int)zval_get_long(tmpzval);
  182. }
  183. - if (php_stream_context_get_option(context, "dio", "flow_control", &tmpzval) == SUCCESS && tmpzval && *tmpzval) {
  184. - data->flow_control = (int)(dio_convert_to_long(*tmpzval) ? 1 : 0);
  185. + if ((tmpzval = php_stream_context_get_option(context, "dio", "flow_control")) != NULL) {
  186. + data->flow_control = zval_get_long(tmpzval) ? 1 : 0;
  187. }
  188. - if (php_stream_context_get_option(context, "dio", "is_canonical", &tmpzval) == SUCCESS && tmpzval && *tmpzval) {
  189. - data->canonical = (int)(dio_convert_to_long(*tmpzval) ? 1 : 0);
  190. + if ((tmpzval = php_stream_context_get_option(context, "dio", "is_canonical")) != NULL) {
  191. + data->canonical = zval_get_long(tmpzval) ? 1 : 0;
  192. }
  193. }
  194. /* }}} */
  195. @@ -227,4 +210,3 @@ void dio_stream_context_get_serial_options(php_stream_context *context, php_dio_
  196. * vim600: fdm=marker
  197. * vim: sw=4 ts=4 noet
  198. */
  199. -
  200. diff --git a/php_dio_common.h b/php_dio_common.h
  201. index f6e4d98..6b14040 100644
  202. --- a/php_dio_common.h
  203. +++ b/php_dio_common.h
  204. @@ -35,8 +35,6 @@
  205. #define DIO_STREAM_TYPE_RAW 1
  206. #define DIO_STREAM_TYPE_SERIAL 2
  207. -long dio_convert_to_long(zval *val);
  208. -
  209. php_dio_stream_data * dio_create_stream_data(void);
  210. void dio_init_stream_data(php_dio_stream_data *data);
  211. --
  212. 2.5.0