]> git.saurik.com Git - wxWidgets.git/blame - include/wx/arrstr.h
Prevent seg fault for older GTK+
[wxWidgets.git] / include / wx / arrstr.h
CommitLineData
df5168c4
MB
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>
65571936 9// Licence: wxWindows licence
df5168c4
MB
10///////////////////////////////////////////////////////////////////////////////
11
12#ifndef _WX_ARRSTR_H
13#define _WX_ARRSTR_H
14
15#include "wx/defs.h"
16#include "wx/string.h"
17
eae4425d
JS
18WXDLLIMPEXP_BASE int wxCMPFUNC_CONV wxStringSortAscending(wxString*, wxString*);
19WXDLLIMPEXP_BASE int wxCMPFUNC_CONV wxStringSortDescending(wxString*, wxString*);
df5168c4
MB
20
21#if wxUSE_STL
22
23#include "wx/dynarray.h"
24
39318636 25typedef int (wxCMPFUNC_CONV *CMPFUNCwxString)(wxString*, wxString*);
f700f98c
MB
26typedef wxString _wxArraywxBaseArrayStringBase;
27_WX_DECLARE_BASEARRAY_2(_wxArraywxBaseArrayStringBase, wxBaseArrayStringBase,
28 wxArray_SortFunction<wxString>,
29 class WXDLLIMPEXP_BASE);
4629016d 30WX_DEFINE_USER_EXPORTED_TYPEARRAY(wxString, wxArrayStringBase,
116270c6 31 wxBaseArrayStringBase, WXDLLIMPEXP_BASE);
4629016d 32_WX_DEFINE_SORTED_TYPEARRAY_2(wxString, wxSortedArrayStringBase,
df5168c4
MB
33 wxBaseArrayStringBase, = wxStringSortAscending,
34 class WXDLLIMPEXP_BASE, CMPFUNCwxString);
35
36class WXDLLIMPEXP_BASE wxArrayString : public wxArrayStringBase
37{
38public:
4e75b65f
MB
39 // type of function used by wxArrayString::Sort()
40 typedef int (wxCMPFUNC_CONV *CompareFunction)(const wxString& first,
41 const wxString& second);
42
df5168c4
MB
43 wxArrayString() { }
44 wxArrayString(const wxArrayString& a) : wxArrayStringBase(a) { }
d44d0cbd
JS
45 wxArrayString(size_t sz, const wxChar** a);
46 wxArrayString(size_t sz, const wxString* a);
2da2f941 47
86501081 48 int Index(const wxString& str, bool bCase = true, bool bFromEnd = false) const;
4e75b65f
MB
49
50 void Sort(bool reverseOrder = false);
51 void Sort(CompareFunction function);
14baddc9 52 void Sort(CMPFUNCwxString function) { wxArrayStringBase::Sort(function); }
555d0d7e
MB
53
54 size_t Add(const wxString& string, size_t copies = 1)
55 {
56 wxArrayStringBase::Add(string, copies);
57 return size() - copies;
58 }
6580df1b 59};
df5168c4
MB
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 }
2da2f941 77
86501081 78 int Index(const wxString& str, bool bCase = true, bool bFromEnd = false) const;
df5168c4
MB
79};
80
81#else // if !wxUSE_STL
82
df5168c4
MB
83class WXDLLIMPEXP_BASE wxArrayString
84{
85public:
86 // type of function used by wxArrayString::Sort()
39318636 87 typedef int (wxCMPFUNC_CONV *CompareFunction)(const wxString& first,
df5168c4
MB
88 const wxString& second);
89 // type of function used by wxArrayString::Sort(), for compatibility with
90 // wxArray
39318636 91 typedef int (wxCMPFUNC_CONV *CompareFunction2)(wxString* first,
df5168c4
MB
92 wxString* second);
93
94 // constructors and destructor
95 // default ctor
584ad2a3
MB
96 wxArrayString() { Init(false); }
97 // if autoSort is true, the array is always sorted (in alphabetical order)
df5168c4
MB
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...
584ad2a3 105 wxArrayString(int autoSort) { Init(autoSort != 0); }
d44d0cbd 106 // C string array ctor
cfa3d7ba
VS
107 wxArrayString(size_t sz, const char** a);
108 wxArrayString(size_t sz, const wchar_t** a);
d44d0cbd
JS
109 // wxString string array ctor
110 wxArrayString(size_t sz, const wxString* a);
df5168c4
MB
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
b7452b3a 143 return m_pItems[nIndex];
df5168c4
MB
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") );
b4a980f4 153 return Item(GetCount() - 1);
df5168c4
MB
154 }
155
df5168c4
MB
156
157 // item management
158 // Search the element in the array, starting from the beginning if
584ad2a3 159 // bFromEnd is false or from end otherwise. If bCase, comparison is case
df5168c4
MB
160 // sensitive (default). Returns index of the first item matched or
161 // wxNOT_FOUND
86501081 162 int Index (const wxString& str, bool bCase = true, bool bFromEnd = false) const;
df5168c4
MB
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
cfa3d7ba 171 void Remove(const wxString& sz);
df5168c4 172 // remove item by index
df5168c4
MB
173 void RemoveAt(size_t nIndex, size_t nRemove = 1);
174
175 // sorting
176 // sort array elements in alphabetical order (or reversed alphabetical
584ad2a3
MB
177 // order if reverseOrder parameter is true)
178 void Sort(bool reverseOrder = false);
df5168c4
MB
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
3c7896fd 200 // TODO: this code duplicates the one in dynarray.h
df5168c4
MB
201 class reverse_iterator
202 {
37589419
MB
203 typedef wxString value_type;
204 typedef value_type* pointer;
205 typedef value_type& reference;
df5168c4
MB
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; }
60d8e886
VZ
218 itor& operator++() { --m_ptr; return *this; }
219 const itor operator++(int)
df5168c4 220 { reverse_iterator tmp = *this; --m_ptr; return tmp; }
60d8e886
VZ
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; }
df5168c4
MB
225 };
226
227 class const_reverse_iterator
228 {
37589419
MB
229 typedef wxString value_type;
230 typedef const value_type* pointer;
231 typedef const value_type& reference;
df5168c4
MB
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; }
60d8e886
VZ
245 itor& operator++() { --m_ptr; return *this; }
246 const itor operator++(int)
df5168c4 247 { itor tmp = *this; --m_ptr; return tmp; }
60d8e886
VZ
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; }
df5168c4
MB
252 };
253
584ad2a3
MB
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); }
df5168c4
MB
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); }
b7452b3a
VS
262 iterator begin() { return m_pItems; }
263 const_iterator begin() const { return m_pItems; }
df5168c4
MB
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
df5168c4
MB
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
b7452b3a 306 wxString *m_pItems; // pointer to data
df5168c4 307
584ad2a3 308 bool m_autoSort; // if true, keep the array always sorted
df5168c4
MB
309};
310
311class WXDLLIMPEXP_BASE wxSortedArrayString : public wxArrayString
312{
313public:
584ad2a3 314 wxSortedArrayString() : wxArrayString(true)
df5168c4 315 { }
584ad2a3 316 wxSortedArrayString(const wxArrayString& array) : wxArrayString(true)
df5168c4
MB
317 { Copy(array); }
318};
319
320#endif // !wxUSE_STL
321
584ad2a3
MB
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
abbb59e8
VZ
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