flush WIP
[m6w6/ext-http] / 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 efree(opt);
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.name = zend_string_init(name_str, name_len, registry->persistent);
88 opt.type = type;
89 opt.option = option;
90
91 switch ((opt.type = type)) {
92 case IS_TRUE:
93 ZVAL_TRUE(&opt.defval);
94 break;
95
96 case IS_FALSE:
97 ZVAL_FALSE(&opt.defval);
98 break;
99
100 case IS_LONG:
101 ZVAL_LONG(&opt.defval, 0);
102 break;
103
104 case IS_STRING:
105 ZVAL_EMPTY_STRING(&opt.defval);
106 break;
107
108 case IS_DOUBLE:
109 ZVAL_DOUBLE(&opt.defval, 0);
110 break;
111
112 default:
113 ZVAL_NULL(&opt.defval);
114 break;
115 }
116
117 return zend_hash_update_mem(&registry->options, opt.name, &opt, sizeof(opt));
118 }
119
120 zval *php_http_option_get(php_http_option_t *opt, HashTable *options, void *userdata)
121 {
122 if (options) {
123 return zend_hash_find(options, opt->name);
124 }
125
126 return NULL;
127 }
128
129
130 /*
131 * Local variables:
132 * tab-width: 4
133 * c-basic-offset: 4
134 * End:
135 * vim600: noet sw=4 ts=4 fdm=marker
136 * vim<600: noet sw=4 ts=4
137 */