usbmodeswitch: Updated to v.1.2.6 from shibby's branch.
[tomato.git] / release / src / router / vsftpd / strlist.c
blob47d747605a944b7d68c69ffce1a51b2d4b9843ca
1 /*
2 * Part of Very Secure FTPd
3 * Licence: GPL v2
4 * Author: Chris Evans
5 * strlist.c
6 */
8 /* Anti-lamer measures deployed, sir! */
9 #define PRIVATE_HANDS_OFF_alloc_len alloc_len
10 #define PRIVATE_HANDS_OFF_list_len list_len
11 #define PRIVATE_HANDS_OFF_p_nodes p_nodes
12 #include "strlist.h"
14 #include "str.h"
15 #include "utility.h"
16 #include "sysutil.h"
18 struct mystr_list_node
20 struct mystr str;
21 struct mystr sort_key_str;
24 /* File locals */
25 static struct mystr s_null_str;
27 static int sort_compare_func(const void* p1, const void* p2);
28 static int sort_compare_func_reverse(const void* p1, const void* p2);
29 static int sort_compare_common(const void* p1, const void* p2, int reverse);
31 void
32 str_list_free(struct mystr_list* p_list)
34 unsigned int i;
35 for (i=0; i < p_list->list_len; ++i)
37 str_free(&p_list->p_nodes[i].str);
38 str_free(&p_list->p_nodes[i].sort_key_str);
40 p_list->list_len = 0;
41 p_list->alloc_len = 0;
42 if (p_list->p_nodes)
44 vsf_sysutil_free(p_list->p_nodes);
45 p_list->p_nodes = 0;
49 int
50 str_list_get_length(const struct mystr_list* p_list)
52 return p_list->list_len;
55 int
56 str_list_contains_str(const struct mystr_list* p_list,
57 const struct mystr* p_str)
59 unsigned int i;
60 for (i=0; i < p_list->list_len; ++i)
62 if (str_equal(p_str, &p_list->p_nodes[i].str))
64 return 1;
67 return 0;
70 void
71 str_list_add(struct mystr_list* p_list, const struct mystr* p_str,
72 const struct mystr* p_sort_key_str)
74 struct mystr_list_node* p_node;
75 /* Expand the node allocation if we have to */
76 if (p_list->list_len == p_list->alloc_len)
78 if (p_list->alloc_len == 0)
80 p_list->alloc_len = 32;
81 p_list->p_nodes = vsf_sysutil_malloc(p_list->alloc_len *
82 sizeof(struct mystr_list_node));
84 else
86 p_list->alloc_len *= 2;
87 p_list->p_nodes = vsf_sysutil_realloc(p_list->p_nodes,
88 p_list->alloc_len *
89 sizeof(struct mystr_list_node));
92 p_node = &p_list->p_nodes[p_list->list_len];
93 p_node->str = s_null_str;
94 p_node->sort_key_str = s_null_str;
95 str_copy(&p_node->str, p_str);
96 if (p_sort_key_str)
98 str_copy(&p_node->sort_key_str, p_sort_key_str);
100 p_list->list_len++;
103 void
104 str_list_sort(struct mystr_list* p_list, int reverse)
106 if (!reverse)
108 vsf_sysutil_qsort(p_list->p_nodes, p_list->list_len,
109 sizeof(struct mystr_list_node), sort_compare_func);
111 else
113 vsf_sysutil_qsort(p_list->p_nodes, p_list->list_len,
114 sizeof(struct mystr_list_node),
115 sort_compare_func_reverse);
119 static int
120 sort_compare_func(const void* p1, const void* p2)
122 return sort_compare_common(p1, p2, 0);
125 static int
126 sort_compare_func_reverse(const void* p1, const void* p2)
128 return sort_compare_common(p1, p2, 1);
131 static int
132 sort_compare_common(const void* p1, const void* p2, int reverse)
134 const struct mystr* p_cmp1;
135 const struct mystr* p_cmp2;
136 const struct mystr_list_node* p_node1 = (const struct mystr_list_node*) p1;
137 const struct mystr_list_node* p_node2 = (const struct mystr_list_node*) p2;
138 if (!str_isempty(&p_node1->sort_key_str))
140 p_cmp1 = &p_node1->sort_key_str;
142 else
144 p_cmp1 = &p_node1->str;
146 if (!str_isempty(&p_node2->sort_key_str))
148 p_cmp2 = &p_node2->sort_key_str;
150 else
152 p_cmp2 = &p_node2->str;
155 if (reverse)
157 return str_strcmp(p_cmp2, p_cmp1);
159 else
161 return str_strcmp(p_cmp1, p_cmp2);
165 const struct mystr*
166 str_list_get_pstr(const struct mystr_list* p_list, unsigned int indexx)
168 if (indexx >= p_list->list_len)
170 bug("indexx out of range in str_list_get_str");
172 return &p_list->p_nodes[indexx].str;