1 /* GNU Mailutils -- a suite of utilities for electronic mail
2 Copyright (C) 2004-2021 Free Software Foundation, Inc.
3
4 This library is free software; you can redistribute it and/or
5 modify it under the terms of the GNU Lesser General Public
6 License as published by the Free Software Foundation; either
7 version 3 of the License, or (at your option) any later version.
8
9 This library is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 Lesser General Public License for more details.
13
14 You should have received a copy of the GNU Lesser General
15 Public License along with this library. If not, see
16 <http://www.gnu.org/licenses/>. */
17
18 #include <mailutils/cpp/list.h>
19
20 using namespace mailutils;
21
22 std::list<void*>
mulist_to_stl(mu_list_t mu_list)23 mailutils :: mulist_to_stl (mu_list_t mu_list)
24 {
25 size_t list_count;
26 std::list<void *> list;
27
28 if (!mu_list)
29 return list;
30
31 int status = mu_list_count (mu_list, &list_count);
32 if (status)
33 return list;
34
35 for (int i = 0; i < list_count; i++)
36 {
37 void *item = NULL;
38 status = mu_list_get (mu_list, i, &item);
39 if (!status && item)
40 list.push_back (item);
41 }
42 return list;
43 }
44
45 //
46 // List
47 //
48
List()49 List :: List ()
50 {
51 int status = mu_list_create (&mu_list);
52 if (status)
53 throw Exception ("List::List", status);
54 }
55
List(const mu_list_t lst)56 List :: List (const mu_list_t lst)
57 {
58 if (lst == 0)
59 throw Exception ("List::List", EINVAL);
60
61 this->mu_list = lst;
62 }
63
~List()64 List :: ~List ()
65 {
66 mu_list_destroy (&mu_list);
67 }
68
69 void
append(void * item)70 List :: append (void* item)
71 {
72 int status = mu_list_append (mu_list, item);
73 if (status)
74 throw Exception ("List::append", status);
75 }
76
77 void
prepend(void * item)78 List :: prepend (void* item)
79 {
80 int status = mu_list_prepend (mu_list, item);
81 if (status)
82 throw Exception ("List::prepend", status);
83 }
84
85 void
insert(void * item,void * new_item,int insert_before)86 List :: insert (void* item, void* new_item, int insert_before)
87 {
88 int status = mu_list_insert (mu_list, item, new_item, insert_before);
89 if (status)
90 throw Exception ("List::insert", status);
91 }
92
93 void
remove(void * item)94 List :: remove (void* item)
95 {
96 int status = mu_list_remove (mu_list, item);
97 if (status)
98 throw Exception ("List::remove", status);
99 }
100
101 void
replace(void * old_item,void * new_item)102 List :: replace (void* old_item, void* new_item)
103 {
104 int status = mu_list_replace (mu_list, old_item, new_item);
105 if (status)
106 throw Exception ("List::replace", status);
107 }
108
109 void
get(size_t index,void ** pitem)110 List :: get (size_t index, void** pitem)
111 {
112 int status = mu_list_get (mu_list, index, pitem);
113 if (status)
114 throw Exception ("List::get", status);
115 }
116
117 void*
get(size_t index)118 List :: get (size_t index)
119 {
120 void* pitem;
121
122 int status = mu_list_get (mu_list, index, &pitem);
123 if (status)
124 throw Exception ("List::get", status);
125
126 return pitem;
127 }
128
129 inline void*
front()130 List :: front ()
131 {
132 return this->get (0);
133 }
134
135 void*
back()136 List :: back ()
137 {
138 size_t count = this->count ();
139 if (count)
140 return this->get (count - 1);
141 else
142 return NULL;
143 }
144
145 Iterator
begin()146 List :: begin ()
147 {
148 mu_iterator_t mu_iter;
149 int status = mu_list_get_iterator (this->mu_list, &mu_iter);
150 if (status)
151 throw Exception ("Iterator::begin", status);
152
153 Iterator itr = Iterator (mu_iter);
154
155 this->iter = &itr;
156 this->iter->first ();
157 return itr;
158 }
159
160 void
to_array(void ** array,size_t count,size_t * pcount)161 List :: to_array (void** array, size_t count, size_t* pcount)
162 {
163 int status = mu_list_to_array (mu_list, array, count, pcount);
164 if (status)
165 throw Exception ("List::to_array", status);
166 }
167
168 void
locate(void * item,void ** ret_item)169 List :: locate (void* item, void** ret_item)
170 {
171 int status = mu_list_locate (mu_list, item, ret_item);
172 if (status)
173 throw Exception ("List::locate", status);
174 }
175
176 bool
is_empty()177 List :: is_empty ()
178 {
179 return (bool) mu_list_is_empty (mu_list);
180 }
181
182 size_t
count()183 List :: count ()
184 {
185 size_t count = 0;
186
187 int status = mu_list_count (mu_list, &count);
188 if (status)
189 throw Exception ("List::count", status);
190
191 return count;
192 }
193
194 inline size_t
size()195 List :: size ()
196 {
197 return this->count ();
198 }
199
200 void
apply(mu_list_action_t action,void * cbdata)201 List :: apply (mu_list_action_t action, void* cbdata)
202 {
203 int status = mu_list_foreach (mu_list, action, cbdata);
204 if (status)
205 throw Exception ("List::apply", status);
206 }
207
208 mu_list_comparator_t
set_comparator(mu_list_comparator_t comp)209 List :: set_comparator (mu_list_comparator_t comp)
210 {
211 return mu_list_set_comparator (mu_list, comp);
212 }
213
214 mu_list_destroy_item_t
set_destroy_item(mu_list_destroy_item_t mu_destroy_item)215 List :: set_destroy_item (mu_list_destroy_item_t mu_destroy_item)
216 {
217 return mu_list_set_destroy_item (mu_list, mu_destroy_item);
218 }
219
220 std::list<void*>
to_stl()221 List :: to_stl ()
222 {
223 return mulist_to_stl (mu_list);
224 }
225
226