simplify
[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-2014, Michael Wallner <mike@php.net> |
10 +--------------------------------------------------------------------+
11 */
12
13 #include "php_http_api.h"
14
15 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 const char *php_http_strlist_iterator_this(php_http_strlist_iterator_t *iter, unsigned *id)
29 {
30 if (id) {
31 *id = (iter->major + 1) * iter->factor + iter->minor;
32 }
33
34 return iter->p;
35 }
36
37 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 iter->minor = 0;
50 }
51 }
52
53 return iter->p;
54 }
55
56 void php_http_strlist_iterator_dtor(php_http_strlist_iterator_t *iter)
57 {
58
59 }
60
61 void php_http_strlist_iterator_free(php_http_strlist_iterator_t **iter)
62 {
63 if (*iter) {
64 efree(*iter);
65 *iter = NULL;
66 }
67 }
68
69 const char *php_http_strlist_find(const char list[], unsigned factor, unsigned item)
70 {
71 unsigned M = 0, m = 0, major, minor;
72 const char *p = &list[0];
73
74 if (factor) {
75 major = (item / factor) - 1;
76 minor = item % factor;
77 } else {
78 major = 0;
79 minor = item;
80 }
81 while (*p && major != M++) {
82 while (*p) {
83 while (*p) {
84 ++p;
85 }
86 ++p;
87 }
88 ++p;
89 }
90
91 while (*p && minor != m++) {
92 while (*p) {
93 ++p;
94 }
95 ++p;
96 }
97
98 return p;
99 }
100
101 /*
102 * Local variables:
103 * tab-width: 4
104 * c-basic-offset: 4
105 * End:
106 * vim600: noet sw=4 ts=4 fdm=marker
107 * vim<600: noet sw=4 ts=4
108 */
109