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 1 : php_http_strlist_iterator_t *php_http_strlist_iterator_init(php_http_strlist_iterator_t *iter, const char list[], unsigned factor)
16 : {
17 1 : if (!iter) {
18 0 : iter = emalloc(sizeof(*iter));
19 : }
20 1 : memset(iter, 0, sizeof(*iter));
21 :
22 1 : iter->p = &list[0];
23 1 : iter->factor = factor;
24 :
25 1 : return iter;
26 : }
27 :
28 84 : const char *php_http_strlist_iterator_this(php_http_strlist_iterator_t *iter, unsigned *id)
29 : {
30 84 : if (id) {
31 84 : *id = (iter->major + 1) * iter->factor + iter->minor;
32 : }
33 :
34 84 : return iter->p;
35 : }
36 :
37 83 : const char *php_http_strlist_iterator_next(php_http_strlist_iterator_t *iter)
38 : {
39 83 : if (*iter->p) {
40 1264 : while (*iter->p) {
41 1098 : ++iter->p;
42 : }
43 83 : ++iter->p;
44 83 : ++iter->minor;
45 :
46 83 : if (!*iter->p) {
47 5 : ++iter->p;
48 5 : ++iter->major;
49 5 : iter->minor = 0;
50 : }
51 : }
52 :
53 83 : return iter->p;
54 : }
55 :
56 0 : void php_http_strlist_iterator_dtor(php_http_strlist_iterator_t *iter)
57 : {
58 :
59 0 : }
60 :
61 0 : void php_http_strlist_iterator_free(php_http_strlist_iterator_t **iter)
62 : {
63 0 : if (*iter) {
64 0 : efree(*iter);
65 0 : *iter = NULL;
66 : }
67 0 : }
68 :
69 641 : const char *php_http_strlist_find(const char list[], unsigned factor, unsigned item)
70 : {
71 641 : unsigned M = 0, m = 0, major, minor;
72 641 : const char *p = &list[0];
73 :
74 641 : if (factor) {
75 641 : major = (item / factor) - 1;
76 641 : minor = item % factor;
77 : } else {
78 0 : major = 0;
79 0 : minor = item;
80 : }
81 2829 : while (*p && major != M++) {
82 25933 : while (*p) {
83 320123 : while (*p) {
84 274445 : ++p;
85 : }
86 22839 : ++p;
87 : }
88 1547 : ++p;
89 : }
90 :
91 8577 : while (*p && minor != m++) {
92 111358 : while (*p) {
93 96768 : ++p;
94 : }
95 7295 : ++p;
96 : }
97 :
98 641 : 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 :
|