943aa57ffce27eaee0791adf0cc89b4b8196d28e
[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 #include <ext/date/php_date.h>
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 switch (Z_TYPE_PP(zparam)) {
90 case IS_NULL:
91 **params = NULL;
92 ++*params;
93 break;
94
95 case IS_BOOL:
96 **params = Z_BVAL_PP(zparam) ? "t" : "f";
97 ++*params;
98 break;
99
100 case IS_DOUBLE:
101 SEPARATE_ZVAL(zparam);
102 Z_TYPE_PP(zparam) = IS_STRING;
103 Z_STRLEN_PP(zparam) = spprintf(&Z_STRVAL_PP(zparam), 0, "%F", Z_DVAL_PP((zval **)p));
104 /* no break */
105
106 default:
107 convert_to_string_ex(zparam);
108 /* no break */
109
110 case IS_STRING:
111 **params = Z_STRVAL_PP(zparam);
112 ++*params;
113
114 if (*zparam != *(zval **)p) {
115 zend_hash_next_index_insert(zdtor, zparam, sizeof(zval *), NULL);
116 }
117 break;
118 }
119
120 return ZEND_HASH_APPLY_KEEP;
121 }
122
123 int php_pq_types_to_array(HashTable *ht, Oid **types TSRMLS_DC)
124 {
125 int count = zend_hash_num_elements(ht);
126
127 *types = NULL;
128
129 if (count) {
130 Oid *tmp;
131
132 /* +1 for when less types than params are specified */
133 *types = tmp = ecalloc(count + 1, sizeof(**types));
134 zend_hash_apply_with_argument(ht, apply_to_oid, &tmp TSRMLS_CC);
135 }
136
137 return count;
138 }
139
140 int php_pq_params_to_array(HashTable *ht, char ***params, HashTable *zdtor TSRMLS_DC)
141 {
142 int count = zend_hash_num_elements(ht);
143
144 *params = NULL;
145
146 if (count) {
147 char **tmp;
148
149 *params = tmp = ecalloc(count, sizeof(char *));
150 zend_hash_apply_with_arguments(ht TSRMLS_CC, apply_to_param, 2, &tmp, zdtor);
151 }
152
153 return count;
154 }
155
156 /*
157 Oid *php_pq_ntypes_to_array(zend_bool fill, int argc, ...)
158 {
159 int i;
160 Oid *oids = ecalloc(argc + 1, sizeof(*oids));
161 va_list argv;
162
163 va_start(argv, argc);
164 for (i = 0; i < argc; ++i) {
165 if (!fill || !i) {
166 oids[i] = va_arg(argv, Oid);
167 } else {
168 oids[i] = oids[0];
169 }
170 }
171 va_end(argv);
172
173 return oids;
174 }
175 */
176
177 zval *php_pq_date_from_string(char *datetime_str, size_t datetime_len, zval *zv TSRMLS_DC)
178 {
179 php_date_obj *dobj;
180
181 if (!zv) {
182 MAKE_STD_ZVAL(zv);
183 }
184
185 php_date_instantiate(php_date_get_date_ce(), zv TSRMLS_CC);
186 dobj = zend_object_store_get_object(zv TSRMLS_CC);
187 if (!php_date_initialize(dobj, datetime_str, datetime_len, NULL, NULL, 1 TSRMLS_CC)) {
188 zval_dtor(zv);
189 ZVAL_NULL(zv);
190 }
191
192 return zv;
193 }
194
195 /*
196 * Local variables:
197 * tab-width: 4
198 * c-basic-offset: 4
199 * End:
200 * vim600: noet sw=4 ts=4 fdm=marker
201 * vim<600: noet sw=4 ts=4
202 */