push a load of changes before holidays
[m6w6/ext-http] / php_http_strlist.c
1
2 #include "php_http.h"
3
4 PHP_HTTP_API php_http_strlist_iterator_t *php_http_strlist_iterator_init(php_http_strlist_iterator_t *iter, const char list[], unsigned factor)
5 {
6 if (!iter) {
7 iter = emalloc(sizeof(*iter));
8 }
9 memset(iter, 0, sizeof(*iter));
10
11 iter->p = &list[0];
12 iter->factor = factor;
13
14 return iter;
15 }
16
17 PHP_HTTP_API const char *php_http_strlist_iterator_this(php_http_strlist_iterator_t *iter, unsigned *id)
18 {
19 if (id) {
20 *id = iter->major * iter->factor + iter->minor;
21 }
22
23 return iter->p;
24 }
25
26 PHP_HTTP_API const char *php_http_strlist_iterator_next(php_http_strlist_iterator_t *iter)
27 {
28 if (*iter->p) {
29 while (*iter->p) {
30 ++iter->p;
31 }
32 ++iter->p;
33 ++iter->minor;
34
35 if (!*iter->p) {
36 ++iter->p;
37 ++iter->major;
38 }
39 }
40
41 return iter->p;
42 }
43
44 PHP_HTTP_API void php_http_strlist_iterator_dtor(php_http_strlist_iterator_t *iter)
45 {
46
47 }
48
49 PHP_HTTP_API void php_http_strlist_iterator_free(php_http_strlist_iterator_t **iter)
50 {
51 if (*iter) {
52 efree(*iter);
53 *iter = NULL;
54 }
55 }
56
57 PHP_HTTP_API const char *php_http_strlist_find(const char list[], unsigned factor, unsigned item)
58 {
59 unsigned M = 0, m = 0, major, minor;
60 const char *p = &list[0];
61
62 if (factor) {
63 major = (item / factor) - 1;
64 minor = item % factor;
65 } else {
66 major = 0;
67 minor = item;
68 }
69 while (*p && major != M++) {
70 while (*p) {
71 while (*p) {
72 ++p;
73 }
74 ++p;
75 }
76 ++p;
77 }
78
79 while (*p && minor != m++) {
80 while (*p) {
81 ++p;
82 }
83 ++p;
84 }
85
86 return p;
87 }