]>
Commit | Line | Data |
---|---|---|
fd3f686c | 1 | //////////////////////////////////////////////////////////////////////////////// |
7ec69821 | 2 | // Name: src/common/list.cpp |
c801d85f KB |
3 | // Purpose: wxList implementation |
4 | // Author: Julian Smart | |
fd3f686c | 5 | // Modified by: VZ at 16/11/98: WX_DECLARE_LIST() and typesafe lists added |
c801d85f | 6 | // Created: 04/01/98 |
55d99c7a | 7 | // Copyright: (c) Julian Smart |
65571936 | 8 | // Licence: wxWindows licence |
fd3f686c | 9 | //////////////////////////////////////////////////////////////////////////////// |
c801d85f | 10 | |
fd3f686c VZ |
11 | // ============================================================================= |
12 | // declarations | |
13 | // ============================================================================= | |
14 | ||
15 | // ----------------------------------------------------------------------------- | |
16 | // headers | |
17 | // ----------------------------------------------------------------------------- | |
7daab453 | 18 | |
c801d85f KB |
19 | // For compilers that support precompilation, includes "wx.h". |
20 | #include "wx/wxprec.h" | |
21 | ||
22 | #ifdef __BORLANDC__ | |
fd3f686c | 23 | #pragma hdrstop |
c801d85f KB |
24 | #endif |
25 | ||
fd3f686c VZ |
26 | #include <stdarg.h> |
27 | #include <stdlib.h> | |
28 | #include <string.h> | |
29 | ||
c801d85f | 30 | #ifndef WX_PRECOMP |
fd3f686c | 31 | #include "wx/list.h" |
062ccd65 | 32 | #include "wx/crt.h" |
c801d85f KB |
33 | #endif |
34 | ||
01871bf6 | 35 | #if !wxUSE_STD_CONTAINERS |
df5168c4 | 36 | |
fd3f686c VZ |
37 | // ============================================================================= |
38 | // implementation | |
39 | // ============================================================================= | |
c801d85f | 40 | |
ff528365 VZ |
41 | // ----------------------------------------------------------------------------- |
42 | // wxListKey | |
43 | // ----------------------------------------------------------------------------- | |
8a2c6ef8 JS |
44 | wxListKey wxDefaultListKey; |
45 | ||
ff528365 VZ |
46 | bool wxListKey::operator==(wxListKeyValue value) const |
47 | { | |
48 | switch ( m_keyType ) | |
49 | { | |
50 | default: | |
223d09f6 | 51 | wxFAIL_MSG(wxT("bad key type.")); |
ff528365 VZ |
52 | // let compiler optimize the line above away in release build |
53 | // by not putting return here... | |
54 | ||
55 | case wxKEY_STRING: | |
81727065 | 56 | return *m_key.string == *value.string; |
ff528365 VZ |
57 | |
58 | case wxKEY_INTEGER: | |
59 | return m_key.integer == value.integer; | |
60 | } | |
77c5eefb | 61 | } |
ff528365 | 62 | |
fd3f686c VZ |
63 | // ----------------------------------------------------------------------------- |
64 | // wxNodeBase | |
65 | // ----------------------------------------------------------------------------- | |
c801d85f | 66 | |
fd3f686c VZ |
67 | wxNodeBase::wxNodeBase(wxListBase *list, |
68 | wxNodeBase *previous, wxNodeBase *next, | |
69 | void *data, const wxListKey& key) | |
c801d85f | 70 | { |
fd3f686c VZ |
71 | m_list = list; |
72 | m_data = data; | |
73 | m_previous = previous; | |
74 | m_next = next; | |
77c5eefb | 75 | |
fd3f686c VZ |
76 | switch ( key.GetKeyType() ) |
77 | { | |
78 | case wxKEY_NONE: | |
79 | break; | |
77c5eefb | 80 | |
fd3f686c VZ |
81 | case wxKEY_INTEGER: |
82 | m_key.integer = key.GetNumber(); | |
83 | break; | |
77c5eefb | 84 | |
fd3f686c VZ |
85 | case wxKEY_STRING: |
86 | // to be free()d later | |
81727065 | 87 | m_key.string = new wxString(key.GetString()); |
fd3f686c | 88 | break; |
77c5eefb | 89 | |
fd3f686c | 90 | default: |
223d09f6 | 91 | wxFAIL_MSG(wxT("invalid key type")); |
fd3f686c | 92 | } |
77c5eefb | 93 | |
fd3f686c VZ |
94 | if ( previous ) |
95 | previous->m_next = this; | |
77c5eefb | 96 | |
fd3f686c VZ |
97 | if ( next ) |
98 | next->m_previous = this; | |
99 | } | |
c801d85f | 100 | |
fd3f686c VZ |
101 | wxNodeBase::~wxNodeBase() |
102 | { | |
103 | // handle the case when we're being deleted from the list by the user (i.e. | |
104 | // not by the list itself from DeleteNode) - we must do it for | |
105 | // compatibility with old code | |
106 | if ( m_list != NULL ) | |
107 | { | |
09914df7 VZ |
108 | if ( m_list->m_keyType == wxKEY_STRING ) |
109 | { | |
81727065 | 110 | delete m_key.string; |
09914df7 VZ |
111 | } |
112 | ||
fd3f686c VZ |
113 | m_list->DetachNode(this); |
114 | } | |
c801d85f KB |
115 | } |
116 | ||
77c5eefb VZ |
117 | int wxNodeBase::IndexOf() const |
118 | { | |
223d09f6 | 119 | wxCHECK_MSG( m_list, wxNOT_FOUND, wxT("node doesn't belong to a list in IndexOf")); |
77c5eefb VZ |
120 | |
121 | // It would be more efficient to implement IndexOf() completely inside | |
122 | // wxListBase (only traverse the list once), but this is probably a more | |
123 | // reusable way of doing it. Can always be optimized at a later date (since | |
124 | // IndexOf() resides in wxListBase as well) if efficiency is a problem. | |
125 | int i; | |
126 | wxNodeBase *prev = m_previous; | |
127 | ||
128 | for( i = 0; prev; i++ ) | |
129 | { | |
130 | prev = prev->m_previous; | |
131 | } | |
132 | ||
133 | return i; | |
134 | } | |
135 | ||
fd3f686c VZ |
136 | // ----------------------------------------------------------------------------- |
137 | // wxListBase | |
138 | // ----------------------------------------------------------------------------- | |
139 | ||
62448488 | 140 | void wxListBase::Init(wxKeyType keyType) |
c801d85f | 141 | { |
fd3f686c | 142 | m_nodeFirst = |
d3b9f782 | 143 | m_nodeLast = NULL; |
fd3f686c | 144 | m_count = 0; |
f644b28c | 145 | m_destroy = false; |
fd3f686c VZ |
146 | m_keyType = keyType; |
147 | } | |
c801d85f | 148 | |
fd3f686c VZ |
149 | wxListBase::wxListBase(size_t count, void *elements[]) |
150 | { | |
151 | Init(); | |
c801d85f | 152 | |
fd3f686c VZ |
153 | for ( size_t n = 0; n < count; n++ ) |
154 | { | |
155 | Append(elements[n]); | |
156 | } | |
c801d85f KB |
157 | } |
158 | ||
fd3f686c | 159 | void wxListBase::DoCopy(const wxListBase& list) |
c801d85f | 160 | { |
fd3f686c | 161 | wxASSERT_MSG( !list.m_destroy, |
223d09f6 | 162 | wxT("copying list which owns it's elements is a bad idea") ); |
c801d85f | 163 | |
fd3f686c VZ |
164 | m_destroy = list.m_destroy; |
165 | m_keyType = list.m_keyType; | |
166 | m_nodeFirst = | |
d3b9f782 | 167 | m_nodeLast = NULL; |
c801d85f | 168 | |
3d0145a5 VZ |
169 | switch (m_keyType) |
170 | { | |
ecf9e593 VS |
171 | case wxKEY_INTEGER: |
172 | { | |
ecf9e593 VS |
173 | for ( wxNodeBase *node = list.GetFirst(); node; node = node->GetNext() ) |
174 | { | |
9ed406c3 | 175 | Append(node->GetKeyInteger(), node->GetData()); |
ecf9e593 VS |
176 | } |
177 | break; | |
178 | } | |
179 | ||
180 | case wxKEY_STRING: | |
181 | { | |
ecf9e593 VS |
182 | for ( wxNodeBase *node = list.GetFirst(); node; node = node->GetNext() ) |
183 | { | |
9ed406c3 | 184 | Append(node->GetKeyString(), node->GetData()); |
ecf9e593 VS |
185 | } |
186 | break; | |
187 | } | |
188 | ||
189 | default: | |
190 | { | |
191 | for ( wxNodeBase *node = list.GetFirst(); node; node = node->GetNext() ) | |
192 | { | |
193 | Append(node->GetData()); | |
194 | } | |
195 | break; | |
196 | } | |
fd3f686c | 197 | } |
f6bcfd97 | 198 | |
9a83f860 | 199 | wxASSERT_MSG( m_count == list.m_count, wxT("logic error in wxList::DoCopy") ); |
c801d85f KB |
200 | } |
201 | ||
fd3f686c | 202 | wxListBase::~wxListBase() |
c801d85f | 203 | { |
fd3f686c VZ |
204 | wxNodeBase *each = m_nodeFirst; |
205 | while ( each != NULL ) | |
206 | { | |
207 | wxNodeBase *next = each->GetNext(); | |
208 | DoDeleteNode(each); | |
209 | each = next; | |
210 | } | |
c801d85f KB |
211 | } |
212 | ||
fd3f686c | 213 | wxNodeBase *wxListBase::AppendCommon(wxNodeBase *node) |
c801d85f | 214 | { |
fd3f686c VZ |
215 | if ( !m_nodeFirst ) |
216 | { | |
217 | m_nodeFirst = node; | |
218 | m_nodeLast = m_nodeFirst; | |
219 | } | |
220 | else | |
221 | { | |
222 | m_nodeLast->m_next = node; | |
223 | m_nodeLast = node; | |
224 | } | |
225 | ||
226 | m_count++; | |
227 | ||
228 | return node; | |
c801d85f KB |
229 | } |
230 | ||
fd3f686c | 231 | wxNodeBase *wxListBase::Append(void *object) |
c801d85f | 232 | { |
fd3f686c | 233 | // all objects in a keyed list should have a key |
d3b9f782 | 234 | wxCHECK_MSG( m_keyType == wxKEY_NONE, NULL, |
223d09f6 | 235 | wxT("need a key for the object to append") ); |
c801d85f | 236 | |
7daab453 VZ |
237 | // we use wxDefaultListKey even though it is the default parameter value |
238 | // because gcc under Mac OS X seems to miscompile this call otherwise | |
d3b9f782 | 239 | wxNodeBase *node = CreateNode(m_nodeLast, NULL, object, |
7daab453 | 240 | wxDefaultListKey); |
fd3f686c VZ |
241 | |
242 | return AppendCommon(node); | |
c801d85f KB |
243 | } |
244 | ||
fd3f686c | 245 | wxNodeBase *wxListBase::Append(long key, void *object) |
c801d85f | 246 | { |
fd3f686c VZ |
247 | wxCHECK_MSG( (m_keyType == wxKEY_INTEGER) || |
248 | (m_keyType == wxKEY_NONE && m_count == 0), | |
d3b9f782 | 249 | NULL, |
223d09f6 | 250 | wxT("can't append object with numeric key to this list") ); |
fd3f686c | 251 | |
d3b9f782 | 252 | wxNodeBase *node = CreateNode(m_nodeLast, NULL, object, key); |
fd3f686c | 253 | return AppendCommon(node); |
c801d85f KB |
254 | } |
255 | ||
81727065 | 256 | wxNodeBase *wxListBase::Append (const wxString& key, void *object) |
c801d85f | 257 | { |
fd3f686c VZ |
258 | wxCHECK_MSG( (m_keyType == wxKEY_STRING) || |
259 | (m_keyType == wxKEY_NONE && m_count == 0), | |
d3b9f782 | 260 | NULL, |
223d09f6 | 261 | wxT("can't append object with string key to this list") ); |
fd3f686c | 262 | |
d3b9f782 | 263 | wxNodeBase *node = CreateNode(m_nodeLast, NULL, object, key); |
fd3f686c VZ |
264 | return AppendCommon(node); |
265 | } | |
c801d85f | 266 | |
fd3f686c VZ |
267 | wxNodeBase *wxListBase::Insert(wxNodeBase *position, void *object) |
268 | { | |
269 | // all objects in a keyed list should have a key | |
d3b9f782 | 270 | wxCHECK_MSG( m_keyType == wxKEY_NONE, NULL, |
223d09f6 | 271 | wxT("need a key for the object to insert") ); |
c801d85f | 272 | |
d3b9f782 | 273 | wxCHECK_MSG( !position || position->m_list == this, NULL, |
223d09f6 | 274 | wxT("can't insert before a node from another list") ); |
ff528365 VZ |
275 | |
276 | // previous and next node for the node being inserted | |
277 | wxNodeBase *prev, *next; | |
fd3f686c | 278 | if ( position ) |
ff528365 | 279 | { |
fd3f686c | 280 | prev = position->GetPrevious(); |
ff528365 VZ |
281 | next = position; |
282 | } | |
283 | else | |
284 | { | |
285 | // inserting in the beginning of the list | |
d3b9f782 | 286 | prev = NULL; |
ff528365 VZ |
287 | next = m_nodeFirst; |
288 | } | |
c801d85f | 289 | |
7daab453 VZ |
290 | // wxDefaultListKey: see comment in Append() above |
291 | wxNodeBase *node = CreateNode(prev, next, object, wxDefaultListKey); | |
fd3f686c | 292 | if ( !m_nodeFirst ) |
c801d85f | 293 | { |
fd3f686c | 294 | m_nodeLast = node; |
c801d85f | 295 | } |
c801d85f | 296 | |
fd3f686c | 297 | if ( prev == NULL ) |
c801d85f | 298 | { |
fd3f686c | 299 | m_nodeFirst = node; |
c801d85f | 300 | } |
c801d85f | 301 | |
fd3f686c VZ |
302 | m_count++; |
303 | ||
c801d85f KB |
304 | return node; |
305 | } | |
306 | ||
fd3f686c | 307 | wxNodeBase *wxListBase::Item(size_t n) const |
c801d85f | 308 | { |
fd3f686c | 309 | for ( wxNodeBase *current = GetFirst(); current; current = current->GetNext() ) |
c801d85f | 310 | { |
fd3f686c VZ |
311 | if ( n-- == 0 ) |
312 | { | |
313 | return current; | |
314 | } | |
c801d85f | 315 | } |
c801d85f | 316 | |
223d09f6 | 317 | wxFAIL_MSG( wxT("invalid index in wxListBase::Item") ); |
c801d85f | 318 | |
d3b9f782 | 319 | return NULL; |
c801d85f KB |
320 | } |
321 | ||
fd3f686c | 322 | wxNodeBase *wxListBase::Find(const wxListKey& key) const |
c801d85f | 323 | { |
fd3f686c | 324 | wxASSERT_MSG( m_keyType == key.GetKeyType(), |
223d09f6 | 325 | wxT("this list is not keyed on the type of this key") ); |
c801d85f | 326 | |
fd3f686c VZ |
327 | for ( wxNodeBase *current = GetFirst(); current; current = current->GetNext() ) |
328 | { | |
329 | if ( key == current->m_key ) | |
330 | { | |
331 | return current; | |
332 | } | |
333 | } | |
c801d85f | 334 | |
fd3f686c | 335 | // not found |
d3b9f782 | 336 | return NULL; |
c801d85f KB |
337 | } |
338 | ||
22d080f3 | 339 | wxNodeBase *wxListBase::Find(const void *object) const |
c801d85f | 340 | { |
fd3f686c | 341 | for ( wxNodeBase *current = GetFirst(); current; current = current->GetNext() ) |
c801d85f | 342 | { |
fd3f686c VZ |
343 | if ( current->GetData() == object ) |
344 | return current; | |
c801d85f | 345 | } |
fd3f686c VZ |
346 | |
347 | // not found | |
d3b9f782 | 348 | return NULL; |
c801d85f KB |
349 | } |
350 | ||
77c5eefb VZ |
351 | int wxListBase::IndexOf(void *object) const |
352 | { | |
353 | wxNodeBase *node = Find( object ); | |
354 | ||
3c67202d | 355 | return node ? node->IndexOf() : wxNOT_FOUND; |
77c5eefb VZ |
356 | } |
357 | ||
fd3f686c | 358 | void wxListBase::DoDeleteNode(wxNodeBase *node) |
c801d85f | 359 | { |
fd3f686c VZ |
360 | // free node's data |
361 | if ( m_keyType == wxKEY_STRING ) | |
c801d85f | 362 | { |
fd3f686c | 363 | free(node->m_key.string); |
c801d85f | 364 | } |
c801d85f | 365 | |
fd3f686c | 366 | if ( m_destroy ) |
c801d85f | 367 | { |
fd3f686c | 368 | node->DeleteData(); |
c801d85f | 369 | } |
c801d85f | 370 | |
09914df7 VZ |
371 | // so that the node knows that it's being deleted by the list |
372 | node->m_list = NULL; | |
fd3f686c | 373 | delete node; |
c801d85f KB |
374 | } |
375 | ||
fd3f686c | 376 | wxNodeBase *wxListBase::DetachNode(wxNodeBase *node) |
c801d85f | 377 | { |
223d09f6 | 378 | wxCHECK_MSG( node, NULL, wxT("detaching NULL wxNodeBase") ); |
fd3f686c | 379 | wxCHECK_MSG( node->m_list == this, NULL, |
223d09f6 | 380 | wxT("detaching node which is not from this list") ); |
c801d85f | 381 | |
fd3f686c VZ |
382 | // update the list |
383 | wxNodeBase **prevNext = node->GetPrevious() ? &node->GetPrevious()->m_next | |
384 | : &m_nodeFirst; | |
385 | wxNodeBase **nextPrev = node->GetNext() ? &node->GetNext()->m_previous | |
386 | : &m_nodeLast; | |
c801d85f | 387 | |
fd3f686c VZ |
388 | *prevNext = node->GetNext(); |
389 | *nextPrev = node->GetPrevious(); | |
c801d85f | 390 | |
fd3f686c | 391 | m_count--; |
c801d85f | 392 | |
fd3f686c VZ |
393 | // mark the node as not belonging to this list any more |
394 | node->m_list = NULL; | |
c801d85f | 395 | |
fd3f686c | 396 | return node; |
c801d85f KB |
397 | } |
398 | ||
fd3f686c | 399 | bool wxListBase::DeleteNode(wxNodeBase *node) |
c801d85f | 400 | { |
fd3f686c | 401 | if ( !DetachNode(node) ) |
f644b28c | 402 | return false; |
fd3f686c VZ |
403 | |
404 | DoDeleteNode(node); | |
405 | ||
f644b28c | 406 | return true; |
c801d85f KB |
407 | } |
408 | ||
fd3f686c | 409 | bool wxListBase::DeleteObject(void *object) |
c801d85f | 410 | { |
fd3f686c VZ |
411 | for ( wxNodeBase *current = GetFirst(); current; current = current->GetNext() ) |
412 | { | |
413 | if ( current->GetData() == object ) | |
414 | { | |
415 | DeleteNode(current); | |
f644b28c | 416 | return true; |
fd3f686c VZ |
417 | } |
418 | } | |
419 | ||
420 | // not found | |
f644b28c | 421 | return false; |
c801d85f KB |
422 | } |
423 | ||
fd3f686c | 424 | void wxListBase::Clear() |
c801d85f | 425 | { |
fd3f686c VZ |
426 | wxNodeBase *current = m_nodeFirst; |
427 | while ( current ) | |
c801d85f | 428 | { |
fd3f686c VZ |
429 | wxNodeBase *next = current->GetNext(); |
430 | DoDeleteNode(current); | |
431 | current = next; | |
c801d85f | 432 | } |
fd3f686c VZ |
433 | |
434 | m_nodeFirst = | |
d3b9f782 | 435 | m_nodeLast = NULL; |
fd3f686c VZ |
436 | |
437 | m_count = 0; | |
c801d85f KB |
438 | } |
439 | ||
fd3f686c | 440 | void wxListBase::ForEach(wxListIterateFunction F) |
c801d85f | 441 | { |
fd3f686c VZ |
442 | for ( wxNodeBase *current = GetFirst(); current; current = current->GetNext() ) |
443 | { | |
444 | (*F)(current->GetData()); | |
c801d85f KB |
445 | } |
446 | } | |
fd3f686c VZ |
447 | |
448 | void *wxListBase::FirstThat(wxListIterateFunction F) | |
c801d85f | 449 | { |
fd3f686c VZ |
450 | for ( wxNodeBase *current = GetFirst(); current; current = current->GetNext() ) |
451 | { | |
452 | if ( (*F)(current->GetData()) ) | |
453 | return current->GetData(); | |
c801d85f | 454 | } |
fd3f686c | 455 | |
d3b9f782 | 456 | return NULL; |
c801d85f | 457 | } |
fd3f686c VZ |
458 | |
459 | void *wxListBase::LastThat(wxListIterateFunction F) | |
c801d85f | 460 | { |
fd3f686c VZ |
461 | for ( wxNodeBase *current = GetLast(); current; current = current->GetPrevious() ) |
462 | { | |
463 | if ( (*F)(current->GetData()) ) | |
464 | return current->GetData(); | |
c801d85f | 465 | } |
fd3f686c | 466 | |
d3b9f782 | 467 | return NULL; |
c801d85f KB |
468 | } |
469 | ||
470 | // (stefan.hammes@urz.uni-heidelberg.de) | |
471 | // | |
472 | // function for sorting lists. the concept is borrowed from 'qsort'. | |
473 | // by giving a sort function, arbitrary lists can be sorted. | |
474 | // method: | |
475 | // - put wxObject pointers into an array | |
476 | // - sort the array with qsort | |
477 | // - put back the sorted wxObject pointers into the list | |
478 | // | |
479 | // CAVE: the sort function receives pointers to wxObject pointers (wxObject **), | |
480 | // so dereference right! | |
481 | // EXAMPLE: | |
482 | // int listcompare(const void *arg1, const void *arg2) | |
483 | // { | |
484 | // return(compare(**(wxString **)arg1, | |
485 | // **(wxString **)arg2)); | |
486 | // } | |
487 | // | |
488 | // void main() | |
fd3f686c VZ |
489 | // { |
490 | // wxListBase list; | |
c801d85f KB |
491 | // |
492 | // list.Append(new wxString("DEF")); | |
493 | // list.Append(new wxString("GHI")); | |
494 | // list.Append(new wxString("ABC")); | |
495 | // list.Sort(listcompare); | |
496 | // } | |
497 | ||
fd3f686c | 498 | void wxListBase::Sort(const wxSortCompareFunction compfunc) |
c801d85f | 499 | { |
fd3f686c VZ |
500 | // allocate an array for the wxObject pointers of the list |
501 | const size_t num = GetCount(); | |
502 | void **objArray = new void *[num]; | |
503 | void **objPtr = objArray; | |
504 | ||
505 | // go through the list and put the pointers into the array | |
506 | wxNodeBase *node; | |
b1d4dd7a | 507 | for ( node = GetFirst(); node; node = node->GetNext() ) |
fd3f686c | 508 | { |
b1d4dd7a | 509 | *objPtr++ = node->GetData(); |
fd3f686c VZ |
510 | } |
511 | ||
512 | // sort the array | |
1c193821 JS |
513 | qsort((void *)objArray,num,sizeof(wxObject *), |
514 | #ifdef __WXWINCE__ | |
515 | (int (__cdecl *)(const void *,const void *)) | |
516 | #endif | |
517 | compfunc); | |
fd3f686c VZ |
518 | |
519 | // put the sorted pointers back into the list | |
520 | objPtr = objArray; | |
b1d4dd7a | 521 | for ( node = GetFirst(); node; node = node->GetNext() ) |
fd3f686c VZ |
522 | { |
523 | node->SetData(*objPtr++); | |
524 | } | |
525 | ||
526 | // free the array | |
527 | delete[] objArray; | |
c801d85f KB |
528 | } |
529 | ||
df5168c4 MB |
530 | void wxListBase::Reverse() |
531 | { | |
532 | wxNodeBase* node = m_nodeFirst; | |
533 | wxNodeBase* tmp; | |
534 | ||
535 | while (node) | |
536 | { | |
537 | // swap prev and next pointers | |
538 | tmp = node->m_next; | |
539 | node->m_next = node->m_previous; | |
540 | node->m_previous = tmp; | |
541 | ||
542 | // this is the node that was next before swapping | |
543 | node = tmp; | |
544 | } | |
545 | ||
546 | // swap first and last node | |
547 | tmp = m_nodeFirst; m_nodeFirst = m_nodeLast; m_nodeLast = tmp; | |
548 | } | |
549 | ||
550 | void wxListBase::DeleteNodes(wxNodeBase* first, wxNodeBase* last) | |
551 | { | |
552 | wxNodeBase* node = first; | |
553 | ||
554 | while (node != last) | |
555 | { | |
556 | wxNodeBase* next = node->GetNext(); | |
557 | DeleteNode(node); | |
558 | node = next; | |
559 | } | |
560 | } | |
561 | ||
f98bd52a VZ |
562 | // ============================================================================ |
563 | // compatibility section from now on | |
564 | // ============================================================================ | |
565 | ||
566 | #ifdef wxLIST_COMPATIBILITY | |
567 | ||
fd3f686c VZ |
568 | // ----------------------------------------------------------------------------- |
569 | // wxList (a.k.a. wxObjectList) | |
570 | // ----------------------------------------------------------------------------- | |
c801d85f | 571 | |
b1d4dd7a RL |
572 | wxList::wxList( int key_type ) |
573 | : wxObjectList( (wxKeyType)key_type ) | |
574 | { | |
575 | } | |
576 | ||
fd3f686c | 577 | void wxObjectListNode::DeleteData() |
c801d85f | 578 | { |
fd3f686c | 579 | delete (wxObject *)GetData(); |
c801d85f KB |
580 | } |
581 | ||
f526f752 | 582 | // ---------------------------------------------------------------------------- |
fd3f686c | 583 | // wxStringList |
f526f752 MB |
584 | // ---------------------------------------------------------------------------- |
585 | ||
f526f752 MB |
586 | static inline wxChar* MYcopystring(const wxChar* s) |
587 | { | |
588 | wxChar* copy = new wxChar[wxStrlen(s) + 1]; | |
589 | return wxStrcpy(copy, s); | |
590 | } | |
fd3f686c VZ |
591 | |
592 | // instead of WX_DEFINE_LIST(wxStringListBase) we define this function | |
593 | // ourselves | |
594 | void wxStringListNode::DeleteData() | |
341287bf | 595 | { |
fd3f686c | 596 | delete [] (char *)GetData(); |
341287bf JS |
597 | } |
598 | ||
50920146 | 599 | bool wxStringList::Delete(const wxChar *s) |
f0824a5a VZ |
600 | { |
601 | wxStringListNode *current; | |
602 | ||
603 | for ( current = GetFirst(); current; current = current->GetNext() ) | |
604 | { | |
50920146 | 605 | if ( wxStrcmp(current->GetData(), s) == 0 ) |
f0824a5a VZ |
606 | { |
607 | DeleteNode(current); | |
f644b28c | 608 | return true; |
f0824a5a VZ |
609 | } |
610 | } | |
611 | ||
612 | // not found | |
f644b28c | 613 | return false; |
f0824a5a VZ |
614 | } |
615 | ||
db9504c5 VZ |
616 | void wxStringList::DoCopy(const wxStringList& other) |
617 | { | |
618 | wxASSERT( GetCount() == 0 ); // this list must be empty before copying! | |
619 | ||
620 | size_t count = other.GetCount(); | |
621 | for ( size_t n = 0; n < count; n++ ) | |
622 | { | |
77c5eefb | 623 | Add(other.Item(n)->GetData()); |
db9504c5 VZ |
624 | } |
625 | } | |
626 | ||
b1d4dd7a RL |
627 | wxStringList::wxStringList() |
628 | { | |
f644b28c | 629 | DeleteContents(true); |
b1d4dd7a RL |
630 | } |
631 | ||
c801d85f KB |
632 | // Variable argument list, terminated by a zero |
633 | // Makes new storage for the strings | |
50920146 | 634 | wxStringList::wxStringList (const wxChar *first, ...) |
c801d85f | 635 | { |
f644b28c | 636 | DeleteContents(true); |
fd3f686c | 637 | if ( !first ) |
c801d85f KB |
638 | return; |
639 | ||
640 | va_list ap; | |
fd3f686c | 641 | va_start(ap, first); |
c801d85f | 642 | |
50920146 | 643 | const wxChar *s = first; |
c801d85f | 644 | for (;;) |
fd3f686c VZ |
645 | { |
646 | Add(s); | |
647 | ||
2cfcf22d VZ |
648 | // icc gives this warning in its own va_arg() macro, argh |
649 | #ifdef __INTELC__ | |
650 | #pragma warning(push) | |
651 | #pragma warning(disable: 1684) | |
652 | #endif | |
653 | ||
50920146 | 654 | s = va_arg(ap, const wxChar *); |
2cfcf22d VZ |
655 | |
656 | #ifdef __INTELC__ | |
657 | #pragma warning(pop) | |
c801d85f | 658 | #endif |
2cfcf22d VZ |
659 | |
660 | if ( !s ) | |
fd3f686c VZ |
661 | break; |
662 | } | |
c801d85f | 663 | |
fd3f686c | 664 | va_end(ap); |
341287bf JS |
665 | } |
666 | ||
f644b28c | 667 | // Only makes new strings if arg is true |
50920146 | 668 | wxChar **wxStringList::ListToArray(bool new_copies) const |
341287bf | 669 | { |
50920146 | 670 | wxChar **string_array = new wxChar *[GetCount()]; |
fd3f686c VZ |
671 | wxStringListNode *node = GetFirst(); |
672 | for (size_t i = 0; i < GetCount(); i++) | |
c801d85f | 673 | { |
50920146 | 674 | wxChar *s = node->GetData(); |
fd3f686c | 675 | if ( new_copies ) |
f526f752 | 676 | string_array[i] = MYcopystring(s); |
fd3f686c VZ |
677 | else |
678 | string_array[i] = s; | |
679 | node = node->GetNext(); | |
c801d85f | 680 | } |
c801d85f | 681 | |
fd3f686c | 682 | return string_array; |
c801d85f KB |
683 | } |
684 | ||
fd3f686c | 685 | // Checks whether s is a member of the list |
50920146 | 686 | bool wxStringList::Member(const wxChar *s) const |
c801d85f | 687 | { |
fd3f686c | 688 | for ( wxStringListNode *node = GetFirst(); node; node = node->GetNext() ) |
c801d85f | 689 | { |
50920146 OK |
690 | const wxChar *s1 = node->GetData(); |
691 | if (s == s1 || wxStrcmp (s, s1) == 0) | |
f644b28c | 692 | return true; |
c801d85f | 693 | } |
fd3f686c | 694 | |
f644b28c | 695 | return false; |
c801d85f KB |
696 | } |
697 | ||
1c193821 | 698 | #ifdef __WXWINCE__ |
7d7b3f69 FM |
699 | extern "C" |
700 | { | |
701 | static int __cdecl | |
1c193821 | 702 | #else |
7d7b3f69 FM |
703 | extern "C" |
704 | { | |
705 | static int LINKAGEMODE | |
1c193821 JS |
706 | #endif |
707 | ||
fd3f686c | 708 | wx_comparestrings(const void *arg1, const void *arg2) |
c801d85f | 709 | { |
50920146 OK |
710 | wxChar **s1 = (wxChar **) arg1; |
711 | wxChar **s2 = (wxChar **) arg2; | |
c801d85f | 712 | |
50920146 | 713 | return wxStrcmp (*s1, *s2); |
c801d85f KB |
714 | } |
715 | ||
7d7b3f69 FM |
716 | } // end of extern "C" (required because of GCC Bug c++/33078 |
717 | ||
c801d85f | 718 | // Sort a list of strings - deallocates old nodes, allocates new |
fd3f686c | 719 | void wxStringList::Sort() |
c801d85f | 720 | { |
fd3f686c | 721 | size_t N = GetCount(); |
50920146 | 722 | wxChar **array = new wxChar *[N]; |
2a040d3f | 723 | wxStringListNode *node; |
c801d85f | 724 | |
fd3f686c | 725 | size_t i = 0; |
2a040d3f | 726 | for ( node = GetFirst(); node; node = node->GetNext() ) |
c801d85f | 727 | { |
fd3f686c | 728 | array[i++] = node->GetData(); |
c801d85f | 729 | } |
341287bf | 730 | |
50920146 | 731 | qsort (array, N, sizeof (wxChar *), wx_comparestrings); |
341287bf | 732 | |
9257d0b7 VZ |
733 | i = 0; |
734 | for ( node = GetFirst(); node; node = node->GetNext() ) | |
735 | node->SetData( array[i++] ); | |
341287bf | 736 | |
9257d0b7 | 737 | delete [] array; |
341287bf | 738 | } |
f98bd52a | 739 | |
f526f752 MB |
740 | wxNode *wxStringList::Add(const wxChar *s) |
741 | { | |
d4d8988c VZ |
742 | return (wxNode *)(wxStringListBase::Node *) |
743 | wxStringListBase::Append(MYcopystring(s)); | |
f526f752 | 744 | } |
f644b28c | 745 | |
f526f752 MB |
746 | wxNode *wxStringList::Prepend(const wxChar *s) |
747 | { | |
d4d8988c VZ |
748 | return (wxNode *)(wxStringListBase::Node *) |
749 | wxStringListBase::Insert(MYcopystring(s)); | |
f526f752 MB |
750 | } |
751 | ||
f98bd52a VZ |
752 | #endif // wxLIST_COMPATIBILITY |
753 | ||
01871bf6 | 754 | #else // wxUSE_STD_CONTAINERS = 1 |
ed1288c1 | 755 | |
7ec69821 | 756 | #include "wx/listimpl.cpp" |
412e0d47 | 757 | WX_DEFINE_LIST(wxObjectList) |
3543c79e | 758 | |
01871bf6 | 759 | // with wxUSE_STD_CONTAINERS wxStringList contains wxString objects, not pointers |
30a29593 | 760 | void _WX_LIST_HELPER_wxStringListBase::DeleteFunction( wxString WXUNUSED(X) ) |
3543c79e MB |
761 | { |
762 | } | |
ed1288c1 | 763 | |
1bc5ddea | 764 | _WX_LIST_HELPER_wxStringListBase::BaseListType _WX_LIST_HELPER_wxStringListBase::EmptyList; |
1d300f25 | 765 | |
01871bf6 | 766 | #endif // !wxUSE_STD_CONTAINERS |