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 27336 : php_http_options_t *php_http_options_init(php_http_options_t *registry, zend_bool persistent)
16 : {
17 27336 : if (!registry) {
18 0 : registry = pecalloc(1, sizeof(*registry), persistent);
19 : } else {
20 27336 : memset(registry, 0, sizeof(*registry));
21 : }
22 :
23 27336 : registry->persistent = persistent;
24 27336 : zend_hash_init(®istry->options, 0, NULL, (dtor_func_t) zend_hash_destroy, persistent);
25 :
26 27336 : return registry;
27 : }
28 :
29 44 : 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 2084 : FOREACH_HASH_VAL(pos, ®istry->options, opt) {
36 2040 : if (!(val = registry->getter(opt, options, userdata))) {
37 2037 : val = &opt->defval;
38 : }
39 2040 : if (registry->setter) {
40 2040 : 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 44 : return SUCCESS;
48 : }
49 :
50 408 : void php_http_options_dtor(php_http_options_t *registry)
51 : {
52 408 : zend_hash_destroy(®istry->options);
53 408 : }
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 26928 : 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 26928 : php_http_option_t opt, *dst = NULL;
67 :
68 26928 : memset(&opt, 0, sizeof(opt));
69 :
70 26928 : php_http_options_init(&opt.suboptions, registry->persistent);
71 26928 : opt.suboptions.getter = registry->getter;
72 26928 : opt.suboptions.setter = registry->setter;
73 :
74 26928 : opt.name.h = zend_hash_func(opt.name.s = name_str, opt.name.l = name_len + 1);
75 26928 : opt.type = type;
76 26928 : opt.option = option;
77 :
78 26928 : INIT_ZVAL(opt.defval);
79 26928 : switch ((opt.type = type)) {
80 : case IS_BOOL:
81 6120 : ZVAL_BOOL(&opt.defval, 0);
82 6120 : break;
83 :
84 : case IS_LONG:
85 8160 : ZVAL_LONG(&opt.defval, 0);
86 8160 : break;
87 :
88 : case IS_STRING:
89 8976 : ZVAL_STRINGL(&opt.defval, NULL, 0, 0);
90 8976 : break;
91 :
92 : case IS_DOUBLE:
93 1632 : ZVAL_DOUBLE(&opt.defval, 0);
94 1632 : break;
95 :
96 : default:
97 2040 : ZVAL_NULL(&opt.defval);
98 2040 : break;
99 : }
100 :
101 26928 : zend_hash_quick_update(®istry->options, opt.name.s, opt.name.l, opt.name.h, (void *) &opt, sizeof(opt), (void *) &dst);
102 26928 : return dst;
103 : }
104 :
105 2040 : zval *php_http_option_get(php_http_option_t *opt, HashTable *options, void *userdata)
106 : {
107 2040 : if (options) {
108 : zval **zoption;
109 :
110 2040 : if (SUCCESS == zend_hash_quick_find(options, opt->name.s, opt->name.l, opt->name.h, (void *) &zoption)) {
111 3 : return *zoption;
112 : }
113 : }
114 :
115 2037 : 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 : */
|