]> git.saurik.com Git - wxWidgets.git/blame_incremental - include/wx/arrstr.h
Prevent seg fault for older GTK+
[wxWidgets.git] / include / wx / arrstr.h
... / ...
CommitLineData
1///////////////////////////////////////////////////////////////////////////////
2// Name: include/wx/arrstr.h
3// Purpose: wxArrayString class
4// Author: Mattia Barbon and Vadim Zeitlin
5// Modified by:
6// Created: 07/07/03
7// RCS-ID: $Id$
8// Copyright: (c) 2003 Vadim Zeitlin <zeitlin@dptmaths.ens-cachan.fr>
9// Licence: wxWindows licence
10///////////////////////////////////////////////////////////////////////////////
11
12#ifndef _WX_ARRSTR_H
13#define _WX_ARRSTR_H
14
15#include "wx/defs.h"
16#include "wx/string.h"
17
18WXDLLIMPEXP_BASE int wxCMPFUNC_CONV wxStringSortAscending(wxString*, wxString*);
19WXDLLIMPEXP_BASE int wxCMPFUNC_CONV wxStringSortDescending(wxString*, wxString*);
20
21#if wxUSE_STL
22
23#include "wx/dynarray.h"
24
25typedef int (wxCMPFUNC_CONV *CMPFUNCwxString)(wxString*, wxString*);
26typedef wxString _wxArraywxBaseArrayStringBase;
27_WX_DECLARE_BASEARRAY_2(_wxArraywxBaseArrayStringBase, wxBaseArrayStringBase,
28 wxArray_SortFunction<wxString>,
29 class WXDLLIMPEXP_BASE);
30WX_DEFINE_USER_EXPORTED_TYPEARRAY(wxString, wxArrayStringBase,
31 wxBaseArrayStringBase, WXDLLIMPEXP_BASE);
32_WX_DEFINE_SORTED_TYPEARRAY_2(wxString, wxSortedArrayStringBase,
33 wxBaseArrayStringBase, = wxStringSortAscending,
34 class WXDLLIMPEXP_BASE, CMPFUNCwxString);
35
36class WXDLLIMPEXP_BASE wxArrayString : public wxArrayStringBase
37{
38public:
39 // type of function used by wxArrayString::Sort()
40 typedef int (wxCMPFUNC_CONV *CompareFunction)(const wxString& first,
41 const wxString& second);
42
43 wxArrayString() { }
44 wxArrayString(const wxArrayString& a) : wxArrayStringBase(a) { }
45 wxArrayString(size_t sz, const wxChar** a);
46 wxArrayString(size_t sz, const wxString* a);
47
48 int Index(const wxString& str, bool bCase = true, bool bFromEnd = false) const;
49
50 void Sort(bool reverseOrder = false);
51 void Sort(CompareFunction function);
52 void Sort(CMPFUNCwxString function) { wxArrayStringBase::Sort(function); }
53
54 size_t Add(const wxString& string, size_t copies = 1)
55 {
56 wxArrayStringBase::Add(string, copies);
57 return size() - copies;
58 }
59};
60
61class WXDLLIMPEXP_BASE wxSortedArrayString : public wxSortedArrayStringBase
62{
63public:
64 wxSortedArrayString() : wxSortedArrayStringBase(wxStringSortAscending)
65 { }
66 wxSortedArrayString(const wxSortedArrayString& array)
67 : wxSortedArrayStringBase(array)
68 { }
69 wxSortedArrayString(const wxArrayString& src)
70 : wxSortedArrayStringBase(wxStringSortAscending)
71 {
72 reserve(src.size());
73
74 for ( size_t n = 0; n < src.size(); n++ )
75 Add(src[n]);
76 }
77
78 int Index(const wxString& str, bool bCase = true, bool bFromEnd = false) const;
79};
80
81#else // if !wxUSE_STL
82
83class WXDLLIMPEXP_BASE wxArrayString
84{
85public:
86 // type of function used by wxArrayString::Sort()
87 typedef int (wxCMPFUNC_CONV *CompareFunction)(const wxString& first,
88 const wxString& second);
89 // type of function used by wxArrayString::Sort(), for compatibility with
90 // wxArray
91 typedef int (wxCMPFUNC_CONV *CompareFunction2)(wxString* first,
92 wxString* second);
93
94 // constructors and destructor
95 // default ctor
96 wxArrayString() { Init(false); }
97 // if autoSort is true, the array is always sorted (in alphabetical order)
98 //
99 // NB: the reason for using int and not bool is that like this we can avoid
100 // using this ctor for implicit conversions from "const char *" (which
101 // we'd like to be implicitly converted to wxString instead!)
102 //
103 // of course, using explicit would be even better - if all compilers
104 // supported it...
105 wxArrayString(int autoSort) { Init(autoSort != 0); }
106 // C string array ctor
107 wxArrayString(size_t sz, const char** a);
108 wxArrayString(size_t sz, const wchar_t** a);
109 // wxString string array ctor
110 wxArrayString(size_t sz, const wxString* a);
111 // copy ctor
112 wxArrayString(const wxArrayString& array);
113 // assignment operator
114 wxArrayString& operator=(const wxArrayString& src);
115 // not virtual, this class should not be derived from
116 ~wxArrayString();
117
118 // memory management
119 // empties the list, but doesn't release memory
120 void Empty();
121 // empties the list and releases memory
122 void Clear();
123 // preallocates memory for given number of items
124 void Alloc(size_t nCount);
125 // minimzes the memory usage (by freeing all extra memory)
126 void Shrink();
127
128 // simple accessors
129 // number of elements in the array
130 size_t GetCount() const { return m_nCount; }
131 // is it empty?
132 bool IsEmpty() const { return m_nCount == 0; }
133 // number of elements in the array (GetCount is preferred API)
134 size_t Count() const { return m_nCount; }
135
136 // items access (range checking is done in debug version)
137 // get item at position uiIndex
138 wxString& Item(size_t nIndex) const
139 {
140 wxASSERT_MSG( nIndex < m_nCount,
141 _T("wxArrayString: index out of bounds") );
142
143 return m_pItems[nIndex];
144 }
145
146 // same as Item()
147 wxString& operator[](size_t nIndex) const { return Item(nIndex); }
148 // get last item
149 wxString& Last() const
150 {
151 wxASSERT_MSG( !IsEmpty(),
152 _T("wxArrayString: index out of bounds") );
153 return Item(GetCount() - 1);
154 }
155
156
157 // item management
158 // Search the element in the array, starting from the beginning if
159 // bFromEnd is false or from end otherwise. If bCase, comparison is case
160 // sensitive (default). Returns index of the first item matched or
161 // wxNOT_FOUND
162 int Index (const wxString& str, bool bCase = true, bool bFromEnd = false) const;
163 // add new element at the end (if the array is not sorted), return its
164 // index
165 size_t Add(const wxString& str, size_t nInsert = 1);
166 // add new element at given position
167 void Insert(const wxString& str, size_t uiIndex, size_t nInsert = 1);
168 // expand the array to have count elements
169 void SetCount(size_t count);
170 // remove first item matching this value
171 void Remove(const wxString& sz);
172 // remove item by index
173 void RemoveAt(size_t nIndex, size_t nRemove = 1);
174
175 // sorting
176 // sort array elements in alphabetical order (or reversed alphabetical
177 // order if reverseOrder parameter is true)
178 void Sort(bool reverseOrder = false);
179 // sort array elements using specified comparaison function
180 void Sort(CompareFunction compareFunction);
181 void Sort(CompareFunction2 compareFunction);
182
183 // comparison
184 // compare two arrays case sensitively
185 bool operator==(const wxArrayString& a) const;
186 // compare two arrays case sensitively
187 bool operator!=(const wxArrayString& a) const { return !(*this == a); }
188
189 // STL-like interface
190 typedef wxString value_type;
191 typedef value_type* pointer;
192 typedef const value_type* const_pointer;
193 typedef value_type* iterator;
194 typedef const value_type* const_iterator;
195 typedef value_type& reference;
196 typedef const value_type& const_reference;
197 typedef int difference_type;
198 typedef size_t size_type;
199
200 // TODO: this code duplicates the one in dynarray.h
201 class reverse_iterator
202 {
203 typedef wxString value_type;
204 typedef value_type* pointer;
205 typedef value_type& reference;
206 typedef reverse_iterator itor;
207 friend itor operator+(int o, const itor& it);
208 friend itor operator+(const itor& it, int o);
209 friend itor operator-(const itor& it, int o);
210 friend difference_type operator -(const itor& i1, const itor& i2);
211 public:
212 pointer m_ptr;
213 reverse_iterator() : m_ptr(NULL) { }
214 reverse_iterator(pointer ptr) : m_ptr(ptr) { }
215 reverse_iterator(const itor& it) : m_ptr(it.m_ptr) { }
216 reference operator*() const { return *m_ptr; }
217 pointer operator->() const { return m_ptr; }
218 itor& operator++() { --m_ptr; return *this; }
219 const itor operator++(int)
220 { reverse_iterator tmp = *this; --m_ptr; return tmp; }
221 itor& operator--() { ++m_ptr; return *this; }
222 const itor operator--(int) { itor tmp = *this; ++m_ptr; return tmp; }
223 bool operator ==(const itor& it) const { return m_ptr == it.m_ptr; }
224 bool operator !=(const itor& it) const { return m_ptr != it.m_ptr; }
225 };
226
227 class const_reverse_iterator
228 {
229 typedef wxString value_type;
230 typedef const value_type* pointer;
231 typedef const value_type& reference;
232 typedef const_reverse_iterator itor;
233 friend itor operator+(int o, const itor& it);
234 friend itor operator+(const itor& it, int o);
235 friend itor operator-(const itor& it, int o);
236 friend difference_type operator -(const itor& i1, const itor& i2);
237 public:
238 pointer m_ptr;
239 const_reverse_iterator() : m_ptr(NULL) { }
240 const_reverse_iterator(pointer ptr) : m_ptr(ptr) { }
241 const_reverse_iterator(const itor& it) : m_ptr(it.m_ptr) { }
242 const_reverse_iterator(const reverse_iterator& it) : m_ptr(it.m_ptr) { }
243 reference operator*() const { return *m_ptr; }
244 pointer operator->() const { return m_ptr; }
245 itor& operator++() { --m_ptr; return *this; }
246 const itor operator++(int)
247 { itor tmp = *this; --m_ptr; return tmp; }
248 itor& operator--() { ++m_ptr; return *this; }
249 const itor operator--(int) { itor tmp = *this; ++m_ptr; return tmp; }
250 bool operator ==(const itor& it) const { return m_ptr == it.m_ptr; }
251 bool operator !=(const itor& it) const { return m_ptr != it.m_ptr; }
252 };
253
254 wxArrayString(const_iterator first, const_iterator last)
255 { Init(false); assign(first, last); }
256 wxArrayString(size_type n, const_reference v) { Init(false); assign(n, v); }
257 void assign(const_iterator first, const_iterator last);
258 void assign(size_type n, const_reference v)
259 { clear(); Add(v, n); }
260 reference back() { return *(end() - 1); }
261 const_reference back() const { return *(end() - 1); }
262 iterator begin() { return m_pItems; }
263 const_iterator begin() const { return m_pItems; }
264 size_type capacity() const { return m_nSize; }
265 void clear() { Clear(); }
266 bool empty() const { return IsEmpty(); }
267 iterator end() { return begin() + GetCount(); }
268 const_iterator end() const { return begin() + GetCount(); }
269 iterator erase(iterator first, iterator last)
270 {
271 size_t idx = first - begin();
272 RemoveAt(idx, last - first);
273 return begin() + idx;
274 }
275 iterator erase(iterator it) { return erase(it, it + 1); }
276 reference front() { return *begin(); }
277 const_reference front() const { return *begin(); }
278 void insert(iterator it, size_type n, const_reference v)
279 { Insert(v, it - begin(), n); }
280 iterator insert(iterator it, const_reference v = value_type())
281 { size_t idx = it - begin(); Insert(v, idx); return begin() + idx; }
282 void insert(iterator it, const_iterator first, const_iterator last);
283 size_type max_size() const { return INT_MAX; }
284 void pop_back() { RemoveAt(GetCount() - 1); }
285 void push_back(const_reference v) { Add(v); }
286 reverse_iterator rbegin() { return reverse_iterator(end() - 1); }
287 const_reverse_iterator rbegin() const;
288 reverse_iterator rend() { return reverse_iterator(begin() - 1); }
289 const_reverse_iterator rend() const;
290 void reserve(size_type n) /* base::reserve*/;
291 void resize(size_type n, value_type v = value_type());
292 size_type size() const { return GetCount(); }
293
294protected:
295 void Init(bool autoSort); // common part of all ctors
296 void Copy(const wxArrayString& src); // copies the contents of another array
297
298private:
299 void Grow(size_t nIncrement = 0); // makes array bigger if needed
300
301 void DoSort(); // common part of all Sort() variants
302
303 size_t m_nSize, // current size of the array
304 m_nCount; // current number of elements
305
306 wxString *m_pItems; // pointer to data
307
308 bool m_autoSort; // if true, keep the array always sorted
309};
310
311class WXDLLIMPEXP_BASE wxSortedArrayString : public wxArrayString
312{
313public:
314 wxSortedArrayString() : wxArrayString(true)
315 { }
316 wxSortedArrayString(const wxArrayString& array) : wxArrayString(true)
317 { Copy(array); }
318};
319
320#endif // !wxUSE_STL
321
322// this class provides a temporary wxString* from a
323// wxArrayString
324class WXDLLIMPEXP_BASE wxCArrayString
325{
326public:
327 wxCArrayString( const wxArrayString& array )
328 : m_array( array ), m_strings( NULL )
329 { }
330 ~wxCArrayString() { delete[] m_strings; }
331
332 size_t GetCount() const { return m_array.GetCount(); }
333 wxString* GetStrings()
334 {
335 if( m_strings ) return m_strings;
336 size_t count = m_array.GetCount();
337 m_strings = new wxString[count];
338 for( size_t i = 0; i < count; ++i )
339 m_strings[i] = m_array[i];
340 return m_strings;
341 }
342private:
343 const wxArrayString& m_array;
344 wxString* m_strings;
345};
346
347
348// ----------------------------------------------------------------------------
349// helper functions for working with arrays
350// ----------------------------------------------------------------------------
351
352// by default, these functions use the escape character to escape the
353// separators occuring inside the string to be joined, this can be disabled by
354// passing '\0' as escape
355
356WXDLLIMPEXP_BASE wxString wxJoin(const wxArrayString& arr,
357 const wxChar sep,
358 const wxChar escape = wxT('\\'));
359
360WXDLLIMPEXP_BASE wxArrayString wxSplit(const wxString& str,
361 const wxChar sep,
362 const wxChar escape = wxT('\\'));
363
364#endif // _WX_ARRSTR_H