wxString& operator<<(double d);
// string comparison
- // case-sensitive comparison: return 0 if =, +1 if > or -1 if <
+ // case-sensitive comparison (returns a value < 0, = 0 or > 0)
int Cmp(const char *psz) const { return strcmp(c_str(), psz); }
// same as Cmp() but not case-sensitive
int CmpNoCase(const char *psz) const { return Stricmp(c_str(), psz); }
class WXDLLEXPORT wxArrayString
{
public:
+ // type of function used by wxArrayString::Sort()
+ typedef int (*CompareFunction)(const wxString& first,
+ const wxString& second);
+
// constructors and destructor
// default ctor
wxArrayString();
// remove item by index
void Remove(size_t nIndex);
- // sort array elements
- void Sort(bool bCase = TRUE, bool bReverse = FALSE);
+ // sorting
+ // sort array elements in alphabetical order (or reversed alphabetical
+ // order if reverseOrder parameter is TRUE)
+ void Sort(bool reverseOrder = FALSE);
+ // sort array elements using specified comparaison function
+ void Sort(CompareFunction compareFunction);
private:
void Grow(); // makes array bigger if needed
void Free(); // free the string stored
+ void DoSort(); // common part of all Sort() variants
+
size_t m_nSize, // current size of the array
m_nCount; // current number of elements
/////////////////////////////////////////////////////////////////////////////
#ifdef __GNUG__
-#pragma implementation "string.h"
+ #pragma implementation "string.h"
#endif
/*
#include "wx/wxprec.h"
#ifdef __BORLANDC__
-#pragma hdrstop
+ #pragma hdrstop
#endif
#ifndef WX_PRECOMP
#include <stdlib.h>
#ifdef __SALFORDC__
-#include <clib.h>
+ #include <clib.h>
#endif
#if wxUSE_WCSRTOMBS
Remove(iIndex);
}
+// ----------------------------------------------------------------------------
+// sorting
+// ----------------------------------------------------------------------------
+
+// we can only sort one array at a time with the quick-sort based
+// implementation
+#if wxUSE_THREADS
+ #include <wx/thread.h>
+
+ // need a critical section to protect access to gs_compareFunction and
+ // gs_sortAscending variables
+ static wxCriticalSection gs_critsectStringSort;
+
+ // call this before the value of the global sort vars is changed/after
+ // you're finished with them
+ #define START_SORT() gs_critsectStringSort.Enter()
+ #define END_SORT() gs_critsectStringSort.Leave()
+#else // !threads
+ #define START_SORT()
+ #define END_SORT()
+#endif // wxUSE_THREADS
+
+// function to use for string comparaison
+static wxArrayString::CompareFunction gs_compareFunction = NULL;
+
+// if we don't use the compare function, this flag tells us if we sort the
+// array in ascending or descending order
+static bool gs_sortAscending = TRUE;
+
+// function which is called by quick sort
+static int wxStringCompareFunction(const void *first, const void *second)
+{
+ wxString *strFirst = (wxString *)first;
+ wxString *strSecond = (wxString *)second;
+
+ if ( gs_compareFunction )
+ return gs_compareFunction(*strFirst, *strSecond);
+ else {
+ int result = strcmp(strFirst->c_str(), strSecond->c_str());
+
+ return gs_sortAscending ? result : -result;
+ }
+}
+
// sort array elements using passed comparaison function
+void wxArrayString::Sort(CompareFunction compareFunction)
+{
+ START_SORT();
+
+ wxASSERT( !gs_compareFunction ); // must have been reset to NULL
+ gs_compareFunction = compareFunction;
+
+ DoSort();
+
+ END_SORT();
+}
+
+void wxArrayString::Sort(bool reverseOrder)
+{
+ START_SORT();
+
+ wxASSERT( !gs_compareFunction ); // must have been reset to NULL
+ gs_sortAscending = !reverseOrder;
+
+ DoSort();
+
+ END_SORT();
+}
-void wxArrayString::Sort(bool WXUNUSED(bCase), bool WXUNUSED(bReverse) )
+void wxArrayString::DoSort()
{
- //@@@@ TO DO
- //qsort(m_pItems, m_nCount, sizeof(char *), fCmp);
+ // just sort the pointers using qsort() - of course it only works because
+ // wxString() *is* a pointer to its data
+ qsort(m_pItems, m_nCount, sizeof(char *), wxStringCompareFunction);
}