2 +--------------------------------------------------------------------+
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 +--------------------------------------------------------------------+
18 #include <ext/date/php_date.h>
19 #include <ext/standard/php_string.h>
21 #include <Zend/zend_interfaces.h>
23 #include <libpq/libpq-fs.h>
26 #include "php_pqexc.h"
27 #include "php_pq_misc.h"
29 #include "php_pq_type.h"
31 char *php_pq_rtrim(char *e
)
35 while (l
-- > 0 && e
[l
] == '\n') {
41 const char *php_pq_strmode(long mode
)
43 switch (mode
& (INV_READ
|INV_WRITE
)) {
44 case INV_READ
|INV_WRITE
:
55 int php_pq_compare_index(const void *lptr
, const void *rptr
)
57 zend_ulong l
= ((const Bucket
*) lptr
)->h
;
58 zend_ulong r
= ((const Bucket
*) rptr
)->h
;
69 void php_pq_hash_ptr_dtor(zval
*p
)
74 zend_class_entry
*php_pqdt_class_entry
;
76 ZEND_BEGIN_ARG_INFO_EX(ai_pqdt_to_string
, 0, 0, 0)
78 static PHP_METHOD(pqdt
, __toString
)
83 zend_call_method_with_1_params(getThis(), php_pqdt_class_entry
, NULL
, "format", &rv
,
84 zend_read_property(php_pqdt_class_entry
, getThis(), ZEND_STRL("format"), 0, &tmp
));
85 RETVAL_ZVAL(&rv
, 1, 1);
88 ZEND_BEGIN_ARG_INFO_EX(ai_pqdt_create_from_format
, 0, 0, 2)
89 ZEND_ARG_INFO(0, format
)
90 ZEND_ARG_INFO(0, datetime
)
91 ZEND_ARG_INFO(0, timezone
)
93 static PHP_METHOD(pqdt
, createFromFormat
)
95 zend_error_handling zeh
;
96 char *fmt_str
, *dt_str
;
97 size_t fmt_len
, dt_len
;
101 zend_replace_error_handling(EH_THROW
, exce(EX_INVALID_ARGUMENT
), &zeh
);
102 rv
= zend_parse_parameters(ZEND_NUM_ARGS(), "ss|O", &fmt_str
, &fmt_len
, &dt_str
, &dt_len
, &ztz
, php_date_get_timezone_ce());
103 zend_restore_error_handling(&zeh
);
106 php_pqdt_from_string(return_value
, fmt_str
, dt_str
, dt_len
, "Y-m-d H:i:s.uO", ztz
);
110 static zend_function_entry php_pqdt_methods
[] = {
111 PHP_ME(pqdt
, createFromFormat
, ai_pqdt_create_from_format
, ZEND_ACC_PUBLIC
|ZEND_ACC_STATIC
)
112 PHP_ME(pqdt
, __toString
, ai_pqdt_to_string
, ZEND_ACC_PUBLIC
)
113 PHP_MALIAS(pqdt
, jsonSerialize
, __toString
, ai_pqdt_to_string
, ZEND_ACC_PUBLIC
)
117 zval
*php_pqdt_from_string(zval
*zv
, char *input_fmt
, char *dt_str
, size_t dt_len
, char *output_fmt
, zval
*ztimezone
)
121 php_date_instantiate(php_pqdt_class_entry
, zv
);
122 dobj
= php_date_obj_from_obj(Z_OBJ_P(zv
));
123 if (!php_date_initialize(dobj
, dt_str
, dt_len
, input_fmt
, ztimezone
, 1)) {
126 } else if (output_fmt
) {
127 zend_update_property_string(php_pqdt_class_entry
, zv
, ZEND_STRL("format"), output_fmt
);
133 zend_string
*php_pqdt_to_string(zval
*zdt
, const char *format
)
139 if (Z_OBJ_HT_P(zdt
)->cast_object
140 && SUCCESS
== Z_OBJ_HT_P(zdt
)->cast_object(zdt
, &rv
, IS_STRING
)
143 } else if (instanceof_function(Z_OBJCE_P(zdt
), php_date_get_date_ce())) {
147 ZVAL_STRING(&zfmt
, format
);
148 zend_call_method_with_1_params(zdt
, Z_OBJCE_P(zdt
), NULL
, "format", &rv
, &zfmt
);
149 zval_ptr_dtor(&zfmt
);
151 if (Z_TYPE(rv
) == IS_STRING
) {
160 zend_class_entry
*php_pqconv_class_entry
;
162 ZEND_BEGIN_ARG_INFO_EX(ai_pqconv_convert_types
, 0, 0, 0)
165 ZEND_BEGIN_ARG_INFO_EX(ai_pqconv_convert_from_string
, 0, 0, 2)
166 ZEND_ARG_INFO(0, data
)
167 ZEND_ARG_INFO(0, type
)
170 ZEND_BEGIN_ARG_INFO_EX(ai_pqconv_convert_to_string
, 0, 0, 2)
171 ZEND_ARG_INFO(0, data
)
172 ZEND_ARG_INFO(0, type
)
175 zend_function_entry php_pqconv_methods
[] = {
176 PHP_ABSTRACT_ME(pqconv
, convertTypes
, ai_pqconv_convert_types
)
177 PHP_ABSTRACT_ME(pqconv
, convertFromString
, ai_pqconv_convert_from_string
)
178 PHP_ABSTRACT_ME(pqconv
, convertToString
, ai_pqconv_convert_to_string
)
183 PHP_MINIT_FUNCTION(pq_misc
)
185 zend_class_entry
*json
, ce
= {0};
187 INIT_NS_CLASS_ENTRY(ce
, "pq", "Converter", php_pqconv_methods
);
188 php_pqconv_class_entry
= zend_register_internal_interface(&ce
);
190 memset(&ce
, 0, sizeof(ce
));
191 INIT_NS_CLASS_ENTRY(ce
,"pq", "DateTime", php_pqdt_methods
);
192 php_pqdt_class_entry
= zend_register_internal_class_ex(&ce
, php_date_get_date_ce());
194 zend_declare_property_stringl(php_pqdt_class_entry
, ZEND_STRL("format"), ZEND_STRL("Y-m-d H:i:s.uO"), ZEND_ACC_PUBLIC
);
196 /* stop reading this file right here! */
197 if ((json
= zend_hash_str_find_ptr(CG(class_table
), ZEND_STRL("jsonserializable")))) {
198 zend_class_implements(php_pqdt_class_entry
, 1, json
);
204 typedef struct _HashTableList
{
206 struct _HashTableList
*parent
;
209 typedef struct _ArrayParserState
{
210 const char *ptr
, *end
;
218 static char caa(ArrayParserState
*a
, const char *any
, unsigned advance
)
229 php_error_docref(NULL
, E_WARNING
, "Failed to parse array: expected one of '%s', got '%c'", any
, *a
->ptr
); \
233 static ZEND_RESULT_CODE
add_element(ArrayParserState
*a
, const char *start
)
236 zend_string
*zstr
= zend_string_init(start
, a
->ptr
- start
, 0);
239 php_stripslashes(zstr
);
240 ZVAL_STR(&zelem
, zstr
);
241 } else if (!zend_string_equals_literal(zstr
, "NULL")) {
242 ZVAL_STR(&zelem
, zstr
);
244 zend_string_release(zstr
);
248 if (!ZVAL_IS_NULL(&zelem
)) {
249 php_pqres_typed_zval(a
->res
, a
->typ
, &zelem
);
252 add_next_index_zval(&a
->list
->arr
, &zelem
);
256 static ZEND_RESULT_CODE
parse_array(ArrayParserState
*a
);
258 static ZEND_RESULT_CODE
parse_element(ArrayParserState
*a
, char delim
)
264 return parse_array(a
);
272 for (el
= a
->ptr
; a
->ptr
< a
->end
; ++a
->ptr
) {
275 a
->escaped
= !a
->escaped
;
281 } else if (a
->quotes
) {
282 if (SUCCESS
!= add_element(a
, el
)) {
289 php_error_docref(NULL
, E_WARNING
, "Failed to parse element, unexpected quote: '%.*s'", (int) (a
->ptr
- el
), el
);
295 if (delim
!= *a
->ptr
) {
302 return add_element(a
, el
);
309 php_error_docref(NULL
, E_WARNING
, "Failed to parse element, reached end of input");
313 static ZEND_RESULT_CODE
parse_elements(ArrayParserState
*a
)
315 char delims
[] = {'}', PHP_PQ_DELIM_OF_ARRAY(a
->typ
), 0};
317 while (SUCCESS
== parse_element(a
, delims
[1])) {
318 switch (caa(a
, delims
, 0)) {
327 php_error_docref(NULL
, E_WARNING
, "Failed to parse elements, reached end of input");
337 static ZEND_RESULT_CODE
parse_array(ArrayParserState
*a
)
341 if (!caa(a
, "{", 1)) {
345 list
= ecalloc(1, sizeof(*list
));
346 array_init(&list
->arr
);
349 add_next_index_zval(&a
->list
->arr
, &list
->arr
);
350 list
->parent
= a
->list
;
354 if (SUCCESS
!= parse_elements(a
)) {
358 if (!caa(a
, "}", 1)) {
362 /* step one level back up */
363 if (a
->list
->parent
) {
364 HashTableList
*l
= a
->list
->parent
;
373 HashTable
*php_pq_parse_array(php_pqres_t
*res
, const char *val_str
, size_t val_len
, Oid typ TSRMLS_DC
)
375 HashTable
*ht
= NULL
;
376 ArrayParserState a
= {0};
380 a
.end
= val_str
+ val_len
;
383 if (SUCCESS
!= parse_array(&a
)) {
385 HashTableList
*l
= a
.list
->parent
;
387 zval_dtor(&a
.list
->arr
);
395 php_error_docref(NULL
, E_NOTICE
, "Trailing input: '%s'", a
.ptr
);
399 HashTableList
*l
= a
.list
->parent
;
401 ht
= Z_ARRVAL(a
.list
->arr
);
415 * vim600: noet sw=4 ts=4 fdm=marker
416 * vim<600: noet sw=4 ts=4