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