[libvirt] [libvirt-php PATCH 1/3] Define macros for looping php hash tables.

Dawid Zamirski dzamirski at datto.com
Wed Jul 6 21:42:59 UTC 2016


Since PHP5 and 7 differ slightly on how looping over php hash tables is
done, those macros were added to abstract away those differences and
avoid using ifdefs for each php version.
---
 src/libvirt-php.c | 31 +++++++++++++++++++++++++++++++
 src/libvirt-php.h |  6 ++++++
 2 files changed, 37 insertions(+)

diff --git a/src/libvirt-php.c b/src/libvirt-php.c
index 85bfcc2..a105dd3 100644
--- a/src/libvirt-php.c
+++ b/src/libvirt-php.c
@@ -83,6 +83,21 @@ typedef size_t strsize_t;
 #define VIRT_ADD_ASSOC_STRING_EX(_arg, _key, _key_len, _value) \
     add_assoc_string_ex(_arg, _key, _key_len, _value)
 
+#define VIRT_FOREACH(_ht, _pos, _zv) \
+    for (zend_hash_internal_pointer_reset_ex(_ht, &_pos); \
+         (_zv = zend_hash_get_current_data_ex(_ht, &_pos)) != NULL; \
+         zend_hash_move_forward_ex(_ht, &_pos)) \
+
+#define VIRT_FOREACH_END(_dummy)
+
+#define VIRT_HASH_CURRENT_KEY_INFO(_ht, _pos, _idx, _info) \
+    do { \
+    zend_string *tmp_key_info; \
+    _info.type = zend_hash_get_current_key_ex(_ht, &tmp_key_info, &_idx, &_pos); \
+    _info.name = ZSTR_VAL(tmp_key_info); \
+    _info.length = ZSTR_LEN(tmp_key_info); \
+    } while(0)
+
 #else /* PHP_MAJOR_VERSION < 7 */
 typedef int strsize_t;
 typedef long zend_long;
@@ -110,6 +125,22 @@ typedef unsigned long zend_ulong;
 #define VIRT_ADD_ASSOC_STRING_EX(_arg, _key, _key_len, _value) \
     add_assoc_string_ex(_arg, _key, _key_len, _value, 1)
 
+#define VIRT_FOREACH(_ht, _pos, _zv) \
+    { \
+    zval **pzv = &_zv; \
+    for (zend_hash_internal_pointer_reset_ex(_ht, &_pos); \
+         zend_hash_get_current_data_ex(_ht, (void **) &pzv, &_pos) == SUCCESS; \
+         zend_hash_move_forward_ex(_ht, &_pos)) { \
+        _zv = *pzv;
+
+#define VIRT_FOREACH_END(_dummy) \
+    }}
+
+#define VIRT_HASH_CURRENT_KEY_INFO(_ht, _pos, _idx, _info) \
+    do { \
+    _info.type = zend_hash_get_current_key_ex(_ht, &_info.name, &_info.length, &_idx, 0, &_pos); \
+    } while(0)
+
 #endif /* PHP_MAJOR_VERSION < 7 */
 
 /* ZEND thread safe per request globals definition */
diff --git a/src/libvirt-php.h b/src/libvirt-php.h
index f1ba9c9..6e61fea 100644
--- a/src/libvirt-php.h
+++ b/src/libvirt-php.h
@@ -315,6 +315,12 @@ typedef struct _php_libvirt_cred_value {
     unsigned int    resultlen;
 } php_libvirt_cred_value;
 
+typedef struct _php_libvirt_hash_key_info {
+    char *name;
+    uint length;
+    uint type;
+} php_libvirt_hash_key_info;
+
 /* Private definitions */
 int vnc_refresh_screen(char *server, char *port, int scancode);
 int vnc_send_keys(char *server, char *port, char *keys);
-- 
2.7.4




More information about the libvir-list mailing list