Line data Source code
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 31416 : php_http_options_t *php_http_options_init(php_http_options_t *registry, zend_bool persistent)
16 : {
17 31416 : if (!registry) {
18 0 : registry = pecalloc(1, sizeof(*registry), persistent);
19 : } else {
20 31416 : memset(registry, 0, sizeof(*registry));
21 : }
22 :
23 31416 : registry->persistent = persistent;
24 31416 : zend_hash_init(®istry->options, 0, NULL, (dtor_func_t) zend_hash_destroy, persistent);
25 :
26 31416 : return registry;
27 : }
28 :
29 73 : 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 3364 : FOREACH_HASH_VAL(pos, ®istry->options, opt) {
36 3291 : if (!(val = registry->getter(opt, options, userdata))) {
37 3224 : val = &opt->defval;
38 : }
39 3291 : if (registry->setter) {
40 3291 : if (SUCCESS != registry->setter(opt, val, userdata)) {
41 0 : return FAILURE;
42 : }
43 0 : } else if (!opt->setter || SUCCESS != opt->setter(opt, val, userdata)) {
44 0 : return FAILURE;
45 : }
46 : }
47 73 : return SUCCESS;
48 : }
49 :
50 748 : void php_http_options_dtor(php_http_options_t *registry)
51 : {
52 748 : zend_hash_destroy(®istry->options);
53 748 : }
54 :
55 0 : void php_http_options_free(php_http_options_t **registry)
56 : {
57 0 : if (*registry) {
58 0 : php_http_options_dtor(*registry);
59 0 : pefree(*registry, (*registry)->persistent);
60 0 : *registry = NULL;
61 : }
62 0 : }
63 :
64 30668 : 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 30668 : php_http_option_t opt, *dst = NULL;
67 :
68 30668 : memset(&opt, 0, sizeof(opt));
69 :
70 30668 : php_http_options_init(&opt.suboptions, registry->persistent);
71 30668 : opt.suboptions.getter = registry->getter;
72 30668 : opt.suboptions.setter = registry->setter;
73 :
74 30668 : opt.name.h = zend_hash_func(opt.name.s = name_str, opt.name.l = name_len + 1);
75 30668 : opt.type = type;
76 30668 : opt.option = option;
77 :
78 30668 : INIT_ZVAL(opt.defval);
79 30668 : switch ((opt.type = type)) {
80 : case IS_BOOL:
81 6358 : ZVAL_BOOL(&opt.defval, 0);
82 6358 : break;
83 :
84 : case IS_LONG:
85 10098 : ZVAL_LONG(&opt.defval, 0);
86 10098 : break;
87 :
88 : case IS_STRING:
89 9724 : ZVAL_STRINGL(&opt.defval, NULL, 0, 0);
90 9724 : break;
91 :
92 : case IS_DOUBLE:
93 1496 : ZVAL_DOUBLE(&opt.defval, 0);
94 1496 : break;
95 :
96 : default:
97 2992 : ZVAL_NULL(&opt.defval);
98 2992 : break;
99 : }
100 :
101 30668 : zend_hash_quick_update(®istry->options, opt.name.s, opt.name.l, opt.name.h, (void *) &opt, sizeof(opt), (void *) &dst);
102 30668 : return dst;
103 : }
104 :
105 3291 : zval *php_http_option_get(php_http_option_t *opt, HashTable *options, void *userdata)
106 : {
107 3291 : if (options) {
108 : zval **zoption;
109 :
110 3291 : if (SUCCESS == zend_hash_quick_find(options, opt->name.s, opt->name.l, opt->name.h, (void *) &zoption)) {
111 67 : return *zoption;
112 : }
113 : }
114 :
115 3224 : 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 : */
|