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