File: php_http_options.c

package info (click to toggle)
php-pecl-http 2.0.4-1
  • links: PTS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 1,436 kB
  • ctags: 1,839
  • sloc: ansic: 15,322; php: 726; xml: 254; makefile: 10; pascal: 3
file content (126 lines) | stat: -rw-r--r-- 3,271 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
/*
    +--------------------------------------------------------------------+
    | PECL :: http                                                       |
    +--------------------------------------------------------------------+
    | Redistribution and use in source and binary forms, with or without |
    | modification, are permitted provided that the conditions mentioned |
    | in the accompanying LICENSE file are met.                          |
    +--------------------------------------------------------------------+
    | Copyright (c) 2004-2014, Michael Wallner <mike@php.net>            |
    +--------------------------------------------------------------------+
*/

#include "php_http_api.h"

php_http_options_t *php_http_options_init(php_http_options_t *registry, zend_bool persistent)
{
	if (!registry) {
		registry = pecalloc(1, sizeof(*registry), persistent);
	} else {
		memset(registry, 0, sizeof(*registry));
	}

	registry->persistent = persistent;
	zend_hash_init(&registry->options, 0, NULL, (dtor_func_t) zend_hash_destroy, persistent);

	return registry;
}

STATUS php_http_options_apply(php_http_options_t *registry, HashTable *options, void *userdata)
{
	HashPosition pos;
	zval *val;
	php_http_option_t *opt;

	FOREACH_HASH_VAL(pos, &registry->options, opt) {
		if (!(val = registry->getter(opt, options, userdata))) {
			val = &opt->defval;
		}
		if (registry->setter) {
			if (SUCCESS != registry->setter(opt, val, userdata)) {
				return FAILURE;
			}
		} else if (!opt->setter || SUCCESS != opt->setter(opt, val, userdata)) {
			return FAILURE;
		}
	}
	return SUCCESS;
}

void php_http_options_dtor(php_http_options_t *registry)
{
	zend_hash_destroy(&registry->options);
}

void php_http_options_free(php_http_options_t **registry)
{
	if (*registry) {
		php_http_options_dtor(*registry);
		pefree(*registry, (*registry)->persistent);
		*registry = NULL;
	}
}

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)
{
	php_http_option_t opt, *dst = NULL;

	memset(&opt, 0, sizeof(opt));

	php_http_options_init(&opt.suboptions, registry->persistent);
	opt.suboptions.getter = registry->getter;
	opt.suboptions.setter = registry->setter;

	opt.name.h = zend_hash_func(opt.name.s = name_str, opt.name.l = name_len + 1);
	opt.type = type;
	opt.option = option;

	INIT_ZVAL(opt.defval);
	switch ((opt.type = type)) {
	case IS_BOOL:
		ZVAL_BOOL(&opt.defval, 0);
		break;

	case IS_LONG:
		ZVAL_LONG(&opt.defval, 0);
		break;

	case IS_STRING:
		ZVAL_STRINGL(&opt.defval, NULL, 0, 0);
		break;

	case IS_DOUBLE:
		ZVAL_DOUBLE(&opt.defval, 0);
		break;

	default:
		ZVAL_NULL(&opt.defval);
		break;
	}

	zend_hash_quick_update(&registry->options, opt.name.s, opt.name.l, opt.name.h, (void *) &opt, sizeof(opt), (void *) &dst);
	return dst;
}

zval *php_http_option_get(php_http_option_t *opt, HashTable *options, void *userdata)
{
	if (options) {
		zval **zoption;

		if (SUCCESS == zend_hash_quick_find(options, opt->name.s, opt->name.l, opt->name.h, (void *) &zoption)) {
			return *zoption;
		}
	}

	return NULL;
}


/*
 * Local variables:
 * tab-width: 4
 * c-basic-offset: 4
 * End:
 * vim600: noet sw=4 ts=4 fdm=marker
 * vim<600: noet sw=4 ts=4
 */