define off_t
[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 php_http_options_t *php_http_options_init(php_http_options_t *registry, zend_bool persistent)
16 {
17 if (!registry) {
18 registry = pecalloc(1, sizeof(*registry), persistent);
19 } else {
20 memset(registry, 0, sizeof(*registry));
21 }
22
23 registry->persistent = persistent;
24 zend_hash_init(&registry->options, 0, NULL, (dtor_func_t) zend_hash_destroy, persistent);
25
26 return registry;
27 }
28
29 STATUS php_http_options_apply(php_http_options_t *registry, HashTable *options, void *userdata)
30 {
31 HashPosition pos;
32 zval *val;
33 php_http_option_t *opt;
34
35 FOREACH_HASH_VAL(pos, &registry->options, opt) {
36 if (!(val = registry->getter(opt, options, userdata))) {
37 val = &opt->defval;
38 }
39 if (registry->setter) {
40 if (SUCCESS != registry->setter(opt, val, userdata)) {
41 return FAILURE;
42 }
43 } else if (!opt->setter || SUCCESS != opt->setter(opt, val, userdata)) {
44 return FAILURE;
45 }
46 }
47 return SUCCESS;
48 }
49
50 void php_http_options_dtor(php_http_options_t *registry)
51 {
52 zend_hash_destroy(&registry->options);
53 }
54
55 void php_http_options_free(php_http_options_t **registry)
56 {
57 if (*registry) {
58 php_http_options_dtor(*registry);
59 pefree(*registry, (*registry)->persistent);
60 *registry = NULL;
61 }
62 }
63
64 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)
65 {
66 php_http_option_t opt, *dst = NULL;
67
68 memset(&opt, 0, sizeof(opt));
69
70 php_http_options_init(&opt.suboptions, registry->persistent);
71 opt.suboptions.getter = registry->getter;
72 opt.suboptions.setter = registry->setter;
73
74 opt.name.h = zend_hash_func(opt.name.s = name_str, opt.name.l = name_len + 1);
75 opt.type = type;
76 opt.option = option;
77
78 INIT_ZVAL(opt.defval);
79 switch ((opt.type = type)) {
80 case IS_BOOL:
81 ZVAL_BOOL(&opt.defval, 0);
82 break;
83
84 case IS_LONG:
85 ZVAL_LONG(&opt.defval, 0);
86 break;
87
88 case IS_STRING:
89 ZVAL_STRINGL(&opt.defval, NULL, 0, 0);
90 break;
91
92 case IS_DOUBLE:
93 ZVAL_DOUBLE(&opt.defval, 0);
94 break;
95
96 default:
97 ZVAL_NULL(&opt.defval);
98 break;
99 }
100
101 zend_hash_quick_update(&registry->options, opt.name.s, opt.name.l, opt.name.h, (void *) &opt, sizeof(opt), (void *) &dst);
102 return dst;
103 }
104
105 zval *php_http_option_get(php_http_option_t *opt, HashTable *options, void *userdata)
106 {
107 if (options) {
108 zval **zoption;
109
110 if (SUCCESS == zend_hash_quick_find(options, opt->name.s, opt->name.l, opt->name.h, (void *) &zoption)) {
111 return *zoption;
112 }
113 }
114
115 return NULL;
116 }
117
118
119 /*
120 * Local variables:
121 * tab-width: 4
122 * c-basic-offset: 4
123 * End:
124 * vim600: noet sw=4 ts=4 fdm=marker
125 * vim<600: noet sw=4 ts=4
126 */