2 * Part of Very Secure FTPd
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
18 struct mystr_list_node
21 struct mystr sort_key_str
;
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
);
32 str_list_free(struct mystr_list
* p_list
)
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
);
41 p_list
->alloc_len
= 0;
44 vsf_sysutil_free(p_list
->p_nodes
);
50 str_list_get_length(const struct mystr_list
* p_list
)
52 return p_list
->list_len
;
56 str_list_contains_str(const struct mystr_list
* p_list
,
57 const struct mystr
* p_str
)
60 for (i
=0; i
< p_list
->list_len
; ++i
)
62 if (str_equal(p_str
, &p_list
->p_nodes
[i
].str
))
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
));
86 p_list
->alloc_len
*= 2;
87 p_list
->p_nodes
= vsf_sysutil_realloc(p_list
->p_nodes
,
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
);
98 str_copy(&p_node
->sort_key_str
, p_sort_key_str
);
104 str_list_sort(struct mystr_list
* p_list
, int reverse
)
108 vsf_sysutil_qsort(p_list
->p_nodes
, p_list
->list_len
,
109 sizeof(struct mystr_list_node
), sort_compare_func
);
113 vsf_sysutil_qsort(p_list
->p_nodes
, p_list
->list_len
,
114 sizeof(struct mystr_list_node
),
115 sort_compare_func_reverse
);
120 sort_compare_func(const void* p1
, const void* p2
)
122 return sort_compare_common(p1
, p2
, 0);
126 sort_compare_func_reverse(const void* p1
, const void* p2
)
128 return sort_compare_common(p1
, p2
, 1);
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
;
144 p_cmp1
= &p_node1
->str
;
146 if (!str_isempty(&p_node2
->sort_key_str
))
148 p_cmp2
= &p_node2
->sort_key_str
;
152 p_cmp2
= &p_node2
->str
;
157 return str_strcmp(p_cmp2
, p_cmp1
);
161 return str_strcmp(p_cmp1
, p_cmp2
);
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
;