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