clean up test
[m6w6/ext-http] / src / php_http_options.c
1 /*
2 +--------------------------------------------------------------------+
3 | PECL :: http |
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) 2004-2014, Michael Wallner <mike@php.net> |
10 +--------------------------------------------------------------------+
11 */
12
13 #include "php_http_api.h"
14
15 static void php_http_options_hash_dtor(zval *pData)
16 {
17 php_http_option_t *opt = Z_PTR_P(pData);
18
19 zval_ptr_dtor(&opt->defval);
20 zend_hash_destroy(&opt->suboptions.options);
21 zend_string_release(opt->name);
22 pefree(opt, opt->persistent);
23 }
24
25 php_http_options_t *php_http_options_init(php_http_options_t *registry, zend_bool persistent)
26 {
27 if (!registry) {
28 registry = pecalloc(1, sizeof(*registry), persistent);
29 } else {
30 memset(registry, 0, sizeof(*registry));
31 }
32
33 registry->persistent = persistent;
34 zend_hash_init(&registry->options, 0, NULL, php_http_options_hash_dtor, persistent);
35
36 return registry;
37 }
38
39 ZEND_RESULT_CODE php_http_options_apply(php_http_options_t *registry, HashTable *options, void *userdata)
40 {
41 zval *entry, *val;
42 php_http_option_t *opt;
43
44 ZEND_HASH_FOREACH_VAL(&registry->options, entry)
45 {
46 opt = Z_PTR_P(entry);
47 if (!(val = registry->getter(opt, options, userdata))) {
48 val = &opt->defval;
49 }
50 if (registry->setter) {
51 if (SUCCESS != registry->setter(opt, val, userdata)) {
52 return FAILURE;
53 }
54 } else if (!opt->setter || SUCCESS != opt->setter(opt, val, userdata)) {
55 return FAILURE;
56 }
57 }
58 ZEND_HASH_FOREACH_END();
59
60 return SUCCESS;
61 }
62
63 void php_http_options_dtor(php_http_options_t *registry)
64 {
65 zend_hash_destroy(&registry->options);
66 }
67
68 void php_http_options_free(php_http_options_t **registry)
69 {
70 if (*registry) {
71 php_http_options_dtor(*registry);
72 pefree(*registry, (*registry)->persistent);
73 *registry = NULL;
74 }
75 }
76
77 php_http_option_t *php_http_option_register(php_http_options_t *registry, const char *name_str, size_t name_len, ulong option, zend_uchar type)
78 {
79 php_http_option_t opt;
80
81 memset(&opt, 0, sizeof(opt));
82
83 php_http_options_init(&opt.suboptions, registry->persistent);
84 opt.suboptions.getter = registry->getter;
85 opt.suboptions.setter = registry->setter;
86
87 opt.persistent = registry->persistent;
88 opt.name = zend_string_init(name_str, name_len, registry->persistent);
89 opt.type = type;
90 opt.option = option;
91
92 switch ((opt.type = type)) {
93 case IS_TRUE:
94 ZVAL_TRUE(&opt.defval);
95 break;
96
97 case IS_FALSE:
98 ZVAL_FALSE(&opt.defval);
99 break;
100
101 case IS_LONG:
102 ZVAL_LONG(&opt.defval, 0);
103 break;
104
105 case IS_DOUBLE:
106 ZVAL_DOUBLE(&opt.defval, 0);
107 break;
108
109 default:
110 ZVAL_NULL(&opt.defval);
111 break;
112 }
113
114 return zend_hash_update_mem(&registry->options, opt.name, &opt, sizeof(opt));
115 }
116
117 zval *php_http_option_get(php_http_option_t *opt, HashTable *options, void *userdata)
118 {
119 if (options) {
120 return zend_hash_find(options, opt->name);
121 }
122
123 return NULL;
124 }
125
126
127 /*
128 * Local variables:
129 * tab-width: 4
130 * c-basic-offset: 4
131 * End:
132 * vim600: noet sw=4 ts=4 fdm=marker
133 * vim<600: noet sw=4 ts=4
134 */