transform event handlers to zvals;
[m6w6/ext-pq] / src / php_pq_misc.c
1 /*
2 +--------------------------------------------------------------------+
3 | PECL :: pq |
4 +--------------------------------------------------------------------+
5 | Redistribution and use in source and binary forms, with or without |
6 | modification, are permitted provided that the conditions mentioned |
7 | in the accompanying LICENSE file are met. |
8 +--------------------------------------------------------------------+
9 | Copyright (c) 2013, Michael Wallner <mike@php.net> |
10 +--------------------------------------------------------------------+
11 */
12
13 #ifdef HAVE_CONFIG_H
14 # include "config.h"
15 #endif
16
17 #include <php.h>
18
19 #include <libpq/libpq-fs.h>
20
21 #include "php_pq.h"
22 #include "php_pq_misc.h"
23
24 char *rtrim(char *e)
25 {
26 size_t l = strlen(e);
27
28 while (l-- > 0 && e[l] == '\n') {
29 e[l] = '\0';
30 }
31 return e;
32 }
33
34 const char *strmode(long mode)
35 {
36 switch (mode & (INV_READ|INV_WRITE)) {
37 case INV_READ|INV_WRITE:
38 return "rw";
39 case INV_READ:
40 return "r";
41 case INV_WRITE:
42 return "w";
43 default:
44 return "-";
45 }
46 }
47
48 int compare_index(const void *lptr, const void *rptr TSRMLS_DC)
49 {
50 const Bucket *l = *(const Bucket **) lptr;
51 const Bucket *r = *(const Bucket **) rptr;
52
53 if (l->h < r->h) {
54 return -1;
55 }
56 if (l->h > r->h) {
57 return 1;
58 }
59 return 0;
60 }
61
62 static int apply_to_oid(void *p, void *arg TSRMLS_DC)
63 {
64 Oid **types = arg;
65 zval **ztype = p;
66
67 if (Z_TYPE_PP(ztype) != IS_LONG) {
68 convert_to_long_ex(ztype);
69 }
70
71 **types = Z_LVAL_PP(ztype);
72 ++*types;
73
74 if (*ztype != *(zval **)p) {
75 zval_ptr_dtor(ztype);
76 }
77 return ZEND_HASH_APPLY_KEEP;
78 }
79
80 static int apply_to_param(void *p TSRMLS_DC, int argc, va_list argv, zend_hash_key *key)
81 {
82 char ***params;
83 HashTable *zdtor;
84 zval **zparam = p;
85
86 params = (char ***) va_arg(argv, char ***);
87 zdtor = (HashTable *) va_arg(argv, HashTable *);
88
89 if (Z_TYPE_PP(zparam) == IS_NULL) {
90 **params = NULL;
91 ++*params;
92 } else {
93 if (Z_TYPE_PP(zparam) != IS_STRING) {
94 convert_to_string_ex(zparam);
95 }
96
97 **params = Z_STRVAL_PP(zparam);
98 ++*params;
99
100 if (*zparam != *(zval **)p) {
101 zend_hash_next_index_insert(zdtor, zparam, sizeof(zval *), NULL);
102 }
103 }
104 return ZEND_HASH_APPLY_KEEP;
105 }
106
107 int php_pq_types_to_array(HashTable *ht, Oid **types TSRMLS_DC)
108 {
109 int count = zend_hash_num_elements(ht);
110
111 *types = NULL;
112
113 if (count) {
114 Oid *tmp;
115
116 /* +1 for when less types than params are specified */
117 *types = tmp = ecalloc(count + 1, sizeof(**types));
118 zend_hash_apply_with_argument(ht, apply_to_oid, &tmp TSRMLS_CC);
119 }
120
121 return count;
122 }
123
124 int php_pq_params_to_array(HashTable *ht, char ***params, HashTable *zdtor TSRMLS_DC)
125 {
126 int count = zend_hash_num_elements(ht);
127
128 *params = NULL;
129
130 if (count) {
131 char **tmp;
132
133 *params = tmp = ecalloc(count, sizeof(char *));
134 zend_hash_apply_with_arguments(ht TSRMLS_CC, apply_to_param, 2, &tmp, zdtor);
135 }
136
137 return count;
138 }
139
140 /*
141 Oid *php_pq_ntypes_to_array(zend_bool fill, int argc, ...)
142 {
143 int i;
144 Oid *oids = ecalloc(argc + 1, sizeof(*oids));
145 va_list argv;
146
147 va_start(argv, argc);
148 for (i = 0; i < argc; ++i) {
149 if (!fill || !i) {
150 oids[i] = va_arg(argv, Oid);
151 } else {
152 oids[i] = oids[0];
153 }
154 }
155 va_end(argv);
156
157 return oids;
158 }
159 */
160
161 /*
162 * Local variables:
163 * tab-width: 4
164 * c-basic-offset: 4
165 * End:
166 * vim600: noet sw=4 ts=4 fdm=marker
167 * vim<600: noet sw=4 ts=4
168 */