ee46786e84f705511308250039126a5e9857854b
[m6w6/ext-http] / php_http_strlist.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-2011, Michael Wallner <mike@php.net> |
10 +--------------------------------------------------------------------+
11 */
12
13 #include "php_http.h"
14
15 PHP_HTTP_API php_http_strlist_iterator_t *php_http_strlist_iterator_init(php_http_strlist_iterator_t *iter, const char list[], unsigned factor)
16 {
17 if (!iter) {
18 iter = emalloc(sizeof(*iter));
19 }
20 memset(iter, 0, sizeof(*iter));
21
22 iter->p = &list[0];
23 iter->factor = factor;
24
25 return iter;
26 }
27
28 PHP_HTTP_API const char *php_http_strlist_iterator_this(php_http_strlist_iterator_t *iter, unsigned *id)
29 {
30 if (id) {
31 *id = iter->major * iter->factor + iter->minor;
32 }
33
34 return iter->p;
35 }
36
37 PHP_HTTP_API const char *php_http_strlist_iterator_next(php_http_strlist_iterator_t *iter)
38 {
39 if (*iter->p) {
40 while (*iter->p) {
41 ++iter->p;
42 }
43 ++iter->p;
44 ++iter->minor;
45
46 if (!*iter->p) {
47 ++iter->p;
48 ++iter->major;
49 }
50 }
51
52 return iter->p;
53 }
54
55 PHP_HTTP_API void php_http_strlist_iterator_dtor(php_http_strlist_iterator_t *iter)
56 {
57
58 }
59
60 PHP_HTTP_API void php_http_strlist_iterator_free(php_http_strlist_iterator_t **iter)
61 {
62 if (*iter) {
63 efree(*iter);
64 *iter = NULL;
65 }
66 }
67
68 PHP_HTTP_API const char *php_http_strlist_find(const char list[], unsigned factor, unsigned item)
69 {
70 unsigned M = 0, m = 0, major, minor;
71 const char *p = &list[0];
72
73 if (factor) {
74 major = (item / factor) - 1;
75 minor = item % factor;
76 } else {
77 major = 0;
78 minor = item;
79 }
80 while (*p && major != M++) {
81 while (*p) {
82 while (*p) {
83 ++p;
84 }
85 ++p;
86 }
87 ++p;
88 }
89
90 while (*p && minor != m++) {
91 while (*p) {
92 ++p;
93 }
94 ++p;
95 }
96
97 return p;
98 }
99
100 /*
101 * Local variables:
102 * tab-width: 4
103 * c-basic-offset: 4
104 * End:
105 * vim600: noet sw=4 ts=4 fdm=marker
106 * vim<600: noet sw=4 ts=4
107 */
108