]> git.saurik.com Git - wxWidgets.git/blob - src/common/string.cpp
added check for GTK 1.2
[wxWidgets.git] / src / common / string.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: string.cpp
3 // Purpose: wxString class
4 // Author: Vadim Zeitlin
5 // Modified by:
6 // Created: 29/01/98
7 // RCS-ID: $Id$
8 // Copyright: (c) 1998 Vadim Zeitlin <zeitlin@dptmaths.ens-cachan.fr>
9 // Licence: wxWindows license
10 /////////////////////////////////////////////////////////////////////////////
11
12 #ifdef __GNUG__
13 #pragma implementation "string.h"
14 #endif
15
16 /*
17 * About ref counting:
18 * 1) all empty strings use g_strEmpty, nRefs = -1 (set in Init())
19 * 2) AllocBuffer() sets nRefs to 1, Lock() increments it by one
20 * 3) Unlock() decrements nRefs and frees memory if it goes to 0
21 */
22
23 // ===========================================================================
24 // headers, declarations, constants
25 // ===========================================================================
26
27 // For compilers that support precompilation, includes "wx.h".
28 #include "wx/wxprec.h"
29
30 #ifdef __BORLANDC__
31 #pragma hdrstop
32 #endif
33
34 #ifndef WX_PRECOMP
35 #include "wx/defs.h"
36 #include "wx/string.h"
37 #include "wx/intl.h"
38 #if wxUSE_THREADS
39 #include <wx/thread.h>
40 #endif
41 #endif
42
43 #include <ctype.h>
44 #include <string.h>
45 #include <stdlib.h>
46
47 #ifdef __SALFORDC__
48 #include <clib.h>
49 #endif
50
51 #if wxUSE_WCSRTOMBS
52 #include <wchar.h> // for wcsrtombs(), see comments where it's used
53 #endif // GNU
54
55 #ifdef WXSTRING_IS_WXOBJECT
56 IMPLEMENT_DYNAMIC_CLASS(wxString, wxObject)
57 #endif //WXSTRING_IS_WXOBJECT
58
59 // allocating extra space for each string consumes more memory but speeds up
60 // the concatenation operations (nLen is the current string's length)
61 // NB: EXTRA_ALLOC must be >= 0!
62 #define EXTRA_ALLOC (19 - nLen % 16)
63
64 // ---------------------------------------------------------------------------
65 // static class variables definition
66 // ---------------------------------------------------------------------------
67
68 #ifdef wxSTD_STRING_COMPATIBILITY
69 const size_t wxString::npos = wxSTRING_MAXLEN;
70 #endif // wxSTD_STRING_COMPATIBILITY
71
72 // ----------------------------------------------------------------------------
73 // static data
74 // ----------------------------------------------------------------------------
75
76 // for an empty string, GetStringData() will return this address: this
77 // structure has the same layout as wxStringData and it's data() method will
78 // return the empty string (dummy pointer)
79 static const struct
80 {
81 wxStringData data;
82 wxChar dummy;
83 } g_strEmpty = { {-1, 0, 0}, _T('\0') };
84
85 // empty C style string: points to 'string data' byte of g_strEmpty
86 extern const wxChar WXDLLEXPORT *g_szNul = &g_strEmpty.dummy;
87
88 // ----------------------------------------------------------------------------
89 // conditional compilation
90 // ----------------------------------------------------------------------------
91
92 // we want to find out if the current platform supports vsnprintf()-like
93 // function: for Unix this is done with configure, for Windows we test the
94 // compiler explicitly.
95 #ifdef __WXMSW__
96 #ifdef __VISUALC__
97 #define wxVsnprintf _vsnprintf
98 #endif
99 #else // !Windows
100 #ifdef HAVE_VSNPRINTF
101 #define wxVsnprintf vsnprintf
102 #endif
103 #endif // Windows/!Windows
104
105 #ifndef wxVsnprintf
106 // in this case we'll use vsprintf() (which is ANSI and thus should be
107 // always available), but it's unsafe because it doesn't check for buffer
108 // size - so give a warning
109 #define wxVsnprintf(buffer,len,format,argptr) vsprintf(buffer,format, argptr)
110
111 #if defined(__VISUALC__)
112 #pragma message("Using sprintf() because no snprintf()-like function defined")
113 #elif defined(__GNUG__) && !defined(__UNIX__)
114 #warning "Using sprintf() because no snprintf()-like function defined"
115 #elif defined(__MWERKS__)
116 #warning "Using sprintf() because no snprintf()-like function defined"
117 #endif //compiler
118 #endif // no vsnprintf
119
120 #ifdef _AIX
121 // AIX has vsnprintf, but there's no prototype in the system headers.
122 extern "C" int vsnprintf(char* str, size_t n, const char* format, va_list ap);
123 #endif
124
125 // ----------------------------------------------------------------------------
126 // global functions
127 // ----------------------------------------------------------------------------
128
129 #ifdef wxSTD_STRING_COMPATIBILITY
130
131 // MS Visual C++ version 5.0 provides the new STL headers as well as the old
132 // iostream ones.
133 //
134 // ATTN: you can _not_ use both of these in the same program!
135
136 istream& operator>>(istream& is, wxString& WXUNUSED(str))
137 {
138 #if 0
139 int w = is.width(0);
140 if ( is.ipfx(0) ) {
141 streambuf *sb = is.rdbuf();
142 str.erase();
143 while ( true ) {
144 int ch = sb->sbumpc ();
145 if ( ch == EOF ) {
146 is.setstate(ios::eofbit);
147 break;
148 }
149 else if ( isspace(ch) ) {
150 sb->sungetc();
151 break;
152 }
153
154 str += ch;
155 if ( --w == 1 )
156 break;
157 }
158 }
159
160 is.isfx();
161 if ( str.length() == 0 )
162 is.setstate(ios::failbit);
163 #endif
164 return is;
165 }
166
167 #endif //std::string compatibility
168
169 // ----------------------------------------------------------------------------
170 // private classes
171 // ----------------------------------------------------------------------------
172
173 // this small class is used to gather statistics for performance tuning
174 //#define WXSTRING_STATISTICS
175 #ifdef WXSTRING_STATISTICS
176 class Averager
177 {
178 public:
179 Averager(const char *sz) { m_sz = sz; m_nTotal = m_nCount = 0; }
180 ~Averager()
181 { printf("wxString: average %s = %f\n", m_sz, ((float)m_nTotal)/m_nCount); }
182
183 void Add(size_t n) { m_nTotal += n; m_nCount++; }
184
185 private:
186 size_t m_nCount, m_nTotal;
187 const char *m_sz;
188 } g_averageLength("allocation size"),
189 g_averageSummandLength("summand length"),
190 g_averageConcatHit("hit probability in concat"),
191 g_averageInitialLength("initial string length");
192
193 #define STATISTICS_ADD(av, val) g_average##av.Add(val)
194 #else
195 #define STATISTICS_ADD(av, val)
196 #endif // WXSTRING_STATISTICS
197
198 // ===========================================================================
199 // wxString class core
200 // ===========================================================================
201
202 // ---------------------------------------------------------------------------
203 // construction
204 // ---------------------------------------------------------------------------
205
206 // constructs string of <nLength> copies of character <ch>
207 wxString::wxString(wxChar ch, size_t nLength)
208 {
209 Init();
210
211 if ( nLength > 0 ) {
212 AllocBuffer(nLength);
213
214 #if wxUSE_UNICODE
215 // memset only works on char
216 for (size_t n=0; n<nLength; n++) m_pchData[n] = ch;
217 #else
218 memset(m_pchData, ch, nLength);
219 #endif
220 }
221 }
222
223 // takes nLength elements of psz starting at nPos
224 void wxString::InitWith(const wxChar *psz, size_t nPos, size_t nLength)
225 {
226 Init();
227
228 wxASSERT( nPos <= wxStrlen(psz) );
229
230 if ( nLength == wxSTRING_MAXLEN )
231 nLength = wxStrlen(psz + nPos);
232
233 STATISTICS_ADD(InitialLength, nLength);
234
235 if ( nLength > 0 ) {
236 // trailing '\0' is written in AllocBuffer()
237 AllocBuffer(nLength);
238 memcpy(m_pchData, psz + nPos, nLength*sizeof(wxChar));
239 }
240 }
241
242 #ifdef wxSTD_STRING_COMPATIBILITY
243
244 // poor man's iterators are "void *" pointers
245 wxString::wxString(const void *pStart, const void *pEnd)
246 {
247 InitWith((const wxChar *)pStart, 0,
248 (const wxChar *)pEnd - (const wxChar *)pStart);
249 }
250
251 #endif //std::string compatibility
252
253 #if wxUSE_UNICODE
254
255 // from multibyte string
256 wxString::wxString(const char *psz, wxMBConv& conv, size_t nLength)
257 {
258 // first get necessary size
259 size_t nLen = psz ? conv.MB2WC((wchar_t *) NULL, psz, 0) : 0;
260
261 // nLength is number of *Unicode* characters here!
262 if ((nLen != (size_t)-1) && (nLen > nLength))
263 nLen = nLength;
264
265 // empty?
266 if ( (nLen != 0) && (nLen != (size_t)-1) ) {
267 AllocBuffer(nLen);
268 conv.MB2WC(m_pchData, psz, nLen);
269 }
270 else {
271 Init();
272 }
273 }
274
275 #else
276
277 // from wide string
278 wxString::wxString(const wchar_t *pwz)
279 {
280 // first get necessary size
281 size_t nLen = pwz ? wxWC2MB((char *) NULL, pwz, 0) : 0;
282
283 // empty?
284 if ( (nLen != 0) && (nLen != (size_t)-1) ) {
285 AllocBuffer(nLen);
286 wxWC2MB(m_pchData, pwz, nLen);
287 }
288 else {
289 Init();
290 }
291 }
292
293 #endif
294
295 // ---------------------------------------------------------------------------
296 // memory allocation
297 // ---------------------------------------------------------------------------
298
299 // allocates memory needed to store a C string of length nLen
300 void wxString::AllocBuffer(size_t nLen)
301 {
302 wxASSERT( nLen > 0 ); //
303 wxASSERT( nLen <= INT_MAX-1 ); // max size (enough room for 1 extra)
304
305 STATISTICS_ADD(Length, nLen);
306
307 // allocate memory:
308 // 1) one extra character for '\0' termination
309 // 2) sizeof(wxStringData) for housekeeping info
310 wxStringData* pData = (wxStringData*)
311 malloc(sizeof(wxStringData) + (nLen + EXTRA_ALLOC + 1)*sizeof(wxChar));
312 pData->nRefs = 1;
313 pData->nDataLength = nLen;
314 pData->nAllocLength = nLen + EXTRA_ALLOC;
315 m_pchData = pData->data(); // data starts after wxStringData
316 m_pchData[nLen] = _T('\0');
317 }
318
319 // must be called before changing this string
320 void wxString::CopyBeforeWrite()
321 {
322 wxStringData* pData = GetStringData();
323
324 if ( pData->IsShared() ) {
325 pData->Unlock(); // memory not freed because shared
326 size_t nLen = pData->nDataLength;
327 AllocBuffer(nLen);
328 memcpy(m_pchData, pData->data(), nLen*sizeof(wxChar));
329 }
330
331 wxASSERT( !GetStringData()->IsShared() ); // we must be the only owner
332 }
333
334 // must be called before replacing contents of this string
335 void wxString::AllocBeforeWrite(size_t nLen)
336 {
337 wxASSERT( nLen != 0 ); // doesn't make any sense
338
339 // must not share string and must have enough space
340 wxStringData* pData = GetStringData();
341 if ( pData->IsShared() || (nLen > pData->nAllocLength) ) {
342 // can't work with old buffer, get new one
343 pData->Unlock();
344 AllocBuffer(nLen);
345 }
346 else {
347 // update the string length
348 pData->nDataLength = nLen;
349 }
350
351 wxASSERT( !GetStringData()->IsShared() ); // we must be the only owner
352 }
353
354 // allocate enough memory for nLen characters
355 void wxString::Alloc(size_t nLen)
356 {
357 wxStringData *pData = GetStringData();
358 if ( pData->nAllocLength <= nLen ) {
359 if ( pData->IsEmpty() ) {
360 nLen += EXTRA_ALLOC;
361
362 wxStringData* pData = (wxStringData*)
363 malloc(sizeof(wxStringData) + (nLen + 1)*sizeof(wxChar));
364 pData->nRefs = 1;
365 pData->nDataLength = 0;
366 pData->nAllocLength = nLen;
367 m_pchData = pData->data(); // data starts after wxStringData
368 m_pchData[0u] = _T('\0');
369 }
370 else if ( pData->IsShared() ) {
371 pData->Unlock(); // memory not freed because shared
372 size_t nOldLen = pData->nDataLength;
373 AllocBuffer(nLen);
374 memcpy(m_pchData, pData->data(), nOldLen*sizeof(wxChar));
375 }
376 else {
377 nLen += EXTRA_ALLOC;
378
379 wxStringData *p = (wxStringData *)
380 realloc(pData, sizeof(wxStringData) + (nLen + 1)*sizeof(wxChar));
381
382 if ( p == NULL ) {
383 // @@@ what to do on memory error?
384 return;
385 }
386
387 // it's not important if the pointer changed or not (the check for this
388 // is not faster than assigning to m_pchData in all cases)
389 p->nAllocLength = nLen;
390 m_pchData = p->data();
391 }
392 }
393 //else: we've already got enough
394 }
395
396 // shrink to minimal size (releasing extra memory)
397 void wxString::Shrink()
398 {
399 wxStringData *pData = GetStringData();
400
401 // this variable is unused in release build, so avoid the compiler warning by
402 // just not declaring it
403 #ifdef __WXDEBUG__
404 void *p =
405 #endif
406 realloc(pData, sizeof(wxStringData) + (pData->nDataLength + 1)*sizeof(wxChar));
407
408 wxASSERT( p != NULL ); // can't free memory?
409 wxASSERT( p == pData ); // we're decrementing the size - block shouldn't move!
410 }
411
412 // get the pointer to writable buffer of (at least) nLen bytes
413 wxChar *wxString::GetWriteBuf(size_t nLen)
414 {
415 AllocBeforeWrite(nLen);
416
417 wxASSERT( GetStringData()->nRefs == 1 );
418 GetStringData()->Validate(FALSE);
419
420 return m_pchData;
421 }
422
423 // put string back in a reasonable state after GetWriteBuf
424 void wxString::UngetWriteBuf()
425 {
426 GetStringData()->nDataLength = wxStrlen(m_pchData);
427 GetStringData()->Validate(TRUE);
428 }
429
430 // ---------------------------------------------------------------------------
431 // data access
432 // ---------------------------------------------------------------------------
433
434 // all functions are inline in string.h
435
436 // ---------------------------------------------------------------------------
437 // assignment operators
438 // ---------------------------------------------------------------------------
439
440 // helper function: does real copy
441 void wxString::AssignCopy(size_t nSrcLen, const wxChar *pszSrcData)
442 {
443 if ( nSrcLen == 0 ) {
444 Reinit();
445 }
446 else {
447 AllocBeforeWrite(nSrcLen);
448 memcpy(m_pchData, pszSrcData, nSrcLen*sizeof(wxChar));
449 GetStringData()->nDataLength = nSrcLen;
450 m_pchData[nSrcLen] = _T('\0');
451 }
452 }
453
454 // assigns one string to another
455 wxString& wxString::operator=(const wxString& stringSrc)
456 {
457 wxASSERT( stringSrc.GetStringData()->IsValid() );
458
459 // don't copy string over itself
460 if ( m_pchData != stringSrc.m_pchData ) {
461 if ( stringSrc.GetStringData()->IsEmpty() ) {
462 Reinit();
463 }
464 else {
465 // adjust references
466 GetStringData()->Unlock();
467 m_pchData = stringSrc.m_pchData;
468 GetStringData()->Lock();
469 }
470 }
471
472 return *this;
473 }
474
475 // assigns a single character
476 wxString& wxString::operator=(wxChar ch)
477 {
478 AssignCopy(1, &ch);
479 return *this;
480 }
481
482 // assigns C string
483 wxString& wxString::operator=(const wxChar *psz)
484 {
485 AssignCopy(wxStrlen(psz), psz);
486 return *this;
487 }
488
489 #if !wxUSE_UNICODE
490
491 // same as 'signed char' variant
492 wxString& wxString::operator=(const unsigned char* psz)
493 {
494 *this = (const char *)psz;
495 return *this;
496 }
497
498 wxString& wxString::operator=(const wchar_t *pwz)
499 {
500 wxString str(pwz);
501 *this = str;
502 return *this;
503 }
504
505 #endif
506
507 // ---------------------------------------------------------------------------
508 // string concatenation
509 // ---------------------------------------------------------------------------
510
511 // add something to this string
512 void wxString::ConcatSelf(int nSrcLen, const wxChar *pszSrcData)
513 {
514 STATISTICS_ADD(SummandLength, nSrcLen);
515
516 // concatenating an empty string is a NOP
517 if ( nSrcLen > 0 ) {
518 wxStringData *pData = GetStringData();
519 size_t nLen = pData->nDataLength;
520 size_t nNewLen = nLen + nSrcLen;
521
522 // alloc new buffer if current is too small
523 if ( pData->IsShared() ) {
524 STATISTICS_ADD(ConcatHit, 0);
525
526 // we have to allocate another buffer
527 wxStringData* pOldData = GetStringData();
528 AllocBuffer(nNewLen);
529 memcpy(m_pchData, pOldData->data(), nLen*sizeof(wxChar));
530 pOldData->Unlock();
531 }
532 else if ( nNewLen > pData->nAllocLength ) {
533 STATISTICS_ADD(ConcatHit, 0);
534
535 // we have to grow the buffer
536 Alloc(nNewLen);
537 }
538 else {
539 STATISTICS_ADD(ConcatHit, 1);
540
541 // the buffer is already big enough
542 }
543
544 // should be enough space
545 wxASSERT( nNewLen <= GetStringData()->nAllocLength );
546
547 // fast concatenation - all is done in our buffer
548 memcpy(m_pchData + nLen, pszSrcData, nSrcLen*sizeof(wxChar));
549
550 m_pchData[nNewLen] = _T('\0'); // put terminating '\0'
551 GetStringData()->nDataLength = nNewLen; // and fix the length
552 }
553 //else: the string to append was empty
554 }
555
556 /*
557 * concatenation functions come in 5 flavours:
558 * string + string
559 * char + string and string + char
560 * C str + string and string + C str
561 */
562
563 wxString operator+(const wxString& string1, const wxString& string2)
564 {
565 wxASSERT( string1.GetStringData()->IsValid() );
566 wxASSERT( string2.GetStringData()->IsValid() );
567
568 wxString s = string1;
569 s += string2;
570
571 return s;
572 }
573
574 wxString operator+(const wxString& string, wxChar ch)
575 {
576 wxASSERT( string.GetStringData()->IsValid() );
577
578 wxString s = string;
579 s += ch;
580
581 return s;
582 }
583
584 wxString operator+(wxChar ch, const wxString& string)
585 {
586 wxASSERT( string.GetStringData()->IsValid() );
587
588 wxString s = ch;
589 s += string;
590
591 return s;
592 }
593
594 wxString operator+(const wxString& string, const wxChar *psz)
595 {
596 wxASSERT( string.GetStringData()->IsValid() );
597
598 wxString s;
599 s.Alloc(wxStrlen(psz) + string.Len());
600 s = string;
601 s += psz;
602
603 return s;
604 }
605
606 wxString operator+(const wxChar *psz, const wxString& string)
607 {
608 wxASSERT( string.GetStringData()->IsValid() );
609
610 wxString s;
611 s.Alloc(wxStrlen(psz) + string.Len());
612 s = psz;
613 s += string;
614
615 return s;
616 }
617
618 // ===========================================================================
619 // other common string functions
620 // ===========================================================================
621
622 // ---------------------------------------------------------------------------
623 // simple sub-string extraction
624 // ---------------------------------------------------------------------------
625
626 // helper function: clone the data attached to this string
627 void wxString::AllocCopy(wxString& dest, int nCopyLen, int nCopyIndex) const
628 {
629 if ( nCopyLen == 0 ) {
630 dest.Init();
631 }
632 else {
633 dest.AllocBuffer(nCopyLen);
634 memcpy(dest.m_pchData, m_pchData + nCopyIndex, nCopyLen*sizeof(wxChar));
635 }
636 }
637
638 // extract string of length nCount starting at nFirst
639 wxString wxString::Mid(size_t nFirst, size_t nCount) const
640 {
641 wxStringData *pData = GetStringData();
642 size_t nLen = pData->nDataLength;
643
644 // default value of nCount is wxSTRING_MAXLEN and means "till the end"
645 if ( nCount == wxSTRING_MAXLEN )
646 {
647 nCount = nLen - nFirst;
648 }
649
650 // out-of-bounds requests return sensible things
651 if ( nFirst + nCount > nLen )
652 {
653 nCount = nLen - nFirst;
654 }
655
656 if ( nFirst > nLen )
657 {
658 // AllocCopy() will return empty string
659 nCount = 0;
660 }
661
662 wxString dest;
663 AllocCopy(dest, nCount, nFirst);
664
665 return dest;
666 }
667
668 // extract nCount last (rightmost) characters
669 wxString wxString::Right(size_t nCount) const
670 {
671 if ( nCount > (size_t)GetStringData()->nDataLength )
672 nCount = GetStringData()->nDataLength;
673
674 wxString dest;
675 AllocCopy(dest, nCount, GetStringData()->nDataLength - nCount);
676 return dest;
677 }
678
679 // get all characters after the last occurence of ch
680 // (returns the whole string if ch not found)
681 wxString wxString::AfterLast(wxChar ch) const
682 {
683 wxString str;
684 int iPos = Find(ch, TRUE);
685 if ( iPos == wxNOT_FOUND )
686 str = *this;
687 else
688 str = c_str() + iPos + 1;
689
690 return str;
691 }
692
693 // extract nCount first (leftmost) characters
694 wxString wxString::Left(size_t nCount) const
695 {
696 if ( nCount > (size_t)GetStringData()->nDataLength )
697 nCount = GetStringData()->nDataLength;
698
699 wxString dest;
700 AllocCopy(dest, nCount, 0);
701 return dest;
702 }
703
704 // get all characters before the first occurence of ch
705 // (returns the whole string if ch not found)
706 wxString wxString::BeforeFirst(wxChar ch) const
707 {
708 wxString str;
709 for ( const wxChar *pc = m_pchData; *pc != _T('\0') && *pc != ch; pc++ )
710 str += *pc;
711
712 return str;
713 }
714
715 /// get all characters before the last occurence of ch
716 /// (returns empty string if ch not found)
717 wxString wxString::BeforeLast(wxChar ch) const
718 {
719 wxString str;
720 int iPos = Find(ch, TRUE);
721 if ( iPos != wxNOT_FOUND && iPos != 0 )
722 str = wxString(c_str(), iPos);
723
724 return str;
725 }
726
727 /// get all characters after the first occurence of ch
728 /// (returns empty string if ch not found)
729 wxString wxString::AfterFirst(wxChar ch) const
730 {
731 wxString str;
732 int iPos = Find(ch);
733 if ( iPos != wxNOT_FOUND )
734 str = c_str() + iPos + 1;
735
736 return str;
737 }
738
739 // replace first (or all) occurences of some substring with another one
740 size_t wxString::Replace(const wxChar *szOld, const wxChar *szNew, bool bReplaceAll)
741 {
742 size_t uiCount = 0; // count of replacements made
743
744 size_t uiOldLen = wxStrlen(szOld);
745
746 wxString strTemp;
747 const wxChar *pCurrent = m_pchData;
748 const wxChar *pSubstr;
749 while ( *pCurrent != _T('\0') ) {
750 pSubstr = wxStrstr(pCurrent, szOld);
751 if ( pSubstr == NULL ) {
752 // strTemp is unused if no replacements were made, so avoid the copy
753 if ( uiCount == 0 )
754 return 0;
755
756 strTemp += pCurrent; // copy the rest
757 break; // exit the loop
758 }
759 else {
760 // take chars before match
761 strTemp.ConcatSelf(pSubstr - pCurrent, pCurrent);
762 strTemp += szNew;
763 pCurrent = pSubstr + uiOldLen; // restart after match
764
765 uiCount++;
766
767 // stop now?
768 if ( !bReplaceAll ) {
769 strTemp += pCurrent; // copy the rest
770 break; // exit the loop
771 }
772 }
773 }
774
775 // only done if there were replacements, otherwise would have returned above
776 *this = strTemp;
777
778 return uiCount;
779 }
780
781 bool wxString::IsAscii() const
782 {
783 const wxChar *s = (const wxChar*) *this;
784 while(*s){
785 if(!isascii(*s)) return(FALSE);
786 s++;
787 }
788 return(TRUE);
789 }
790
791 bool wxString::IsWord() const
792 {
793 const wxChar *s = (const wxChar*) *this;
794 while(*s){
795 if(!wxIsalpha(*s)) return(FALSE);
796 s++;
797 }
798 return(TRUE);
799 }
800
801 bool wxString::IsNumber() const
802 {
803 const wxChar *s = (const wxChar*) *this;
804 while(*s){
805 if(!wxIsdigit(*s)) return(FALSE);
806 s++;
807 }
808 return(TRUE);
809 }
810
811 wxString wxString::Strip(stripType w) const
812 {
813 wxString s = *this;
814 if ( w & leading ) s.Trim(FALSE);
815 if ( w & trailing ) s.Trim(TRUE);
816 return s;
817 }
818
819 // ---------------------------------------------------------------------------
820 // case conversion
821 // ---------------------------------------------------------------------------
822
823 wxString& wxString::MakeUpper()
824 {
825 CopyBeforeWrite();
826
827 for ( wxChar *p = m_pchData; *p; p++ )
828 *p = (wxChar)wxToupper(*p);
829
830 return *this;
831 }
832
833 wxString& wxString::MakeLower()
834 {
835 CopyBeforeWrite();
836
837 for ( wxChar *p = m_pchData; *p; p++ )
838 *p = (wxChar)wxTolower(*p);
839
840 return *this;
841 }
842
843 // ---------------------------------------------------------------------------
844 // trimming and padding
845 // ---------------------------------------------------------------------------
846
847 // trims spaces (in the sense of isspace) from left or right side
848 wxString& wxString::Trim(bool bFromRight)
849 {
850 // first check if we're going to modify the string at all
851 if ( !IsEmpty() &&
852 (
853 (bFromRight && wxIsspace(GetChar(Len() - 1))) ||
854 (!bFromRight && wxIsspace(GetChar(0u)))
855 )
856 )
857 {
858 // ok, there is at least one space to trim
859 CopyBeforeWrite();
860
861 if ( bFromRight )
862 {
863 // find last non-space character
864 wxChar *psz = m_pchData + GetStringData()->nDataLength - 1;
865 while ( wxIsspace(*psz) && (psz >= m_pchData) )
866 psz--;
867
868 // truncate at trailing space start
869 *++psz = _T('\0');
870 GetStringData()->nDataLength = psz - m_pchData;
871 }
872 else
873 {
874 // find first non-space character
875 const wxChar *psz = m_pchData;
876 while ( wxIsspace(*psz) )
877 psz++;
878
879 // fix up data and length
880 int nDataLength = GetStringData()->nDataLength - (psz - (const wxChar*) m_pchData);
881 memmove(m_pchData, psz, (nDataLength + 1)*sizeof(wxChar));
882 GetStringData()->nDataLength = nDataLength;
883 }
884 }
885
886 return *this;
887 }
888
889 // adds nCount characters chPad to the string from either side
890 wxString& wxString::Pad(size_t nCount, wxChar chPad, bool bFromRight)
891 {
892 wxString s(chPad, nCount);
893
894 if ( bFromRight )
895 *this += s;
896 else
897 {
898 s += *this;
899 *this = s;
900 }
901
902 return *this;
903 }
904
905 // truncate the string
906 wxString& wxString::Truncate(size_t uiLen)
907 {
908 if ( uiLen < Len() ) {
909 CopyBeforeWrite();
910
911 *(m_pchData + uiLen) = _T('\0');
912 GetStringData()->nDataLength = uiLen;
913 }
914 //else: nothing to do, string is already short enough
915
916 return *this;
917 }
918
919 // ---------------------------------------------------------------------------
920 // finding (return wxNOT_FOUND if not found and index otherwise)
921 // ---------------------------------------------------------------------------
922
923 // find a character
924 int wxString::Find(wxChar ch, bool bFromEnd) const
925 {
926 const wxChar *psz = bFromEnd ? wxStrrchr(m_pchData, ch) : wxStrchr(m_pchData, ch);
927
928 return (psz == NULL) ? wxNOT_FOUND : psz - (const wxChar*) m_pchData;
929 }
930
931 // find a sub-string (like strstr)
932 int wxString::Find(const wxChar *pszSub) const
933 {
934 const wxChar *psz = wxStrstr(m_pchData, pszSub);
935
936 return (psz == NULL) ? wxNOT_FOUND : psz - (const wxChar*) m_pchData;
937 }
938
939 // ---------------------------------------------------------------------------
940 // stream-like operators
941 // ---------------------------------------------------------------------------
942 wxString& wxString::operator<<(int i)
943 {
944 wxString res;
945 res.Printf(_T("%d"), i);
946
947 return (*this) << res;
948 }
949
950 wxString& wxString::operator<<(float f)
951 {
952 wxString res;
953 res.Printf(_T("%f"), f);
954
955 return (*this) << res;
956 }
957
958 wxString& wxString::operator<<(double d)
959 {
960 wxString res;
961 res.Printf(_T("%g"), d);
962
963 return (*this) << res;
964 }
965
966 // ---------------------------------------------------------------------------
967 // formatted output
968 // ---------------------------------------------------------------------------
969 int wxString::Printf(const wxChar *pszFormat, ...)
970 {
971 va_list argptr;
972 va_start(argptr, pszFormat);
973
974 int iLen = PrintfV(pszFormat, argptr);
975
976 va_end(argptr);
977
978 return iLen;
979 }
980
981 int wxString::PrintfV(const wxChar* pszFormat, va_list argptr)
982 {
983 // static buffer to avoid dynamic memory allocation each time
984 static char s_szScratch[1024];
985 #if wxUSE_THREADS
986 // protect the static buffer
987 static wxCriticalSection critsect;
988 wxCriticalSectionLocker lock(critsect);
989 #endif
990
991 #if 1 // the new implementation
992
993 Reinit();
994 for (size_t n = 0; pszFormat[n]; n++)
995 if (pszFormat[n] == _T('%')) {
996 static char s_szFlags[256] = "%";
997 size_t flagofs = 1;
998 bool adj_left = FALSE, in_prec = FALSE,
999 prec_dot = FALSE, done = FALSE;
1000 int ilen = 0;
1001 size_t min_width = 0, max_width = wxSTRING_MAXLEN;
1002 do {
1003 #define CHECK_PREC if (in_prec && !prec_dot) { s_szFlags[flagofs++] = '.'; prec_dot = TRUE; }
1004 switch (pszFormat[++n]) {
1005 case _T('\0'):
1006 done = TRUE;
1007 break;
1008 case _T('%'):
1009 *this += _T('%');
1010 done = TRUE;
1011 break;
1012 case _T('#'):
1013 case _T('0'):
1014 case _T(' '):
1015 case _T('+'):
1016 case _T('\''):
1017 CHECK_PREC
1018 s_szFlags[flagofs++] = pszFormat[n];
1019 break;
1020 case _T('-'):
1021 CHECK_PREC
1022 adj_left = TRUE;
1023 s_szFlags[flagofs++] = pszFormat[n];
1024 break;
1025 case _T('.'):
1026 CHECK_PREC
1027 in_prec = TRUE;
1028 prec_dot = FALSE;
1029 max_width = 0;
1030 // dot will be auto-added to s_szFlags if non-negative number follows
1031 break;
1032 case _T('h'):
1033 ilen = -1;
1034 CHECK_PREC
1035 s_szFlags[flagofs++] = pszFormat[n];
1036 break;
1037 case _T('l'):
1038 ilen = 1;
1039 CHECK_PREC
1040 s_szFlags[flagofs++] = pszFormat[n];
1041 break;
1042 case _T('q'):
1043 case _T('L'):
1044 ilen = 2;
1045 CHECK_PREC
1046 s_szFlags[flagofs++] = pszFormat[n];
1047 break;
1048 case _T('Z'):
1049 ilen = 3;
1050 CHECK_PREC
1051 s_szFlags[flagofs++] = pszFormat[n];
1052 break;
1053 case _T('*'):
1054 {
1055 int len = va_arg(argptr, int);
1056 if (in_prec) {
1057 if (len<0) break;
1058 CHECK_PREC
1059 max_width = len;
1060 } else {
1061 if (len<0) {
1062 adj_left = !adj_left;
1063 s_szFlags[flagofs++] = '-';
1064 len = -len;
1065 }
1066 min_width = len;
1067 }
1068 flagofs += ::sprintf(s_szFlags+flagofs,"%d",len);
1069 }
1070 break;
1071 case _T('1'): case _T('2'): case _T('3'):
1072 case _T('4'): case _T('5'): case _T('6'):
1073 case _T('7'): case _T('8'): case _T('9'):
1074 {
1075 int len = 0;
1076 CHECK_PREC
1077 while ((pszFormat[n]>=_T('0')) && (pszFormat[n]<=_T('9'))) {
1078 s_szFlags[flagofs++] = pszFormat[n];
1079 len = len*10 + (pszFormat[n] - _T('0'));
1080 n++;
1081 }
1082 if (in_prec) max_width = len;
1083 else min_width = len;
1084 n--; // the main loop pre-increments n again
1085 }
1086 break;
1087 case _T('d'):
1088 case _T('i'):
1089 case _T('o'):
1090 case _T('u'):
1091 case _T('x'):
1092 case _T('X'):
1093 CHECK_PREC
1094 s_szFlags[flagofs++] = pszFormat[n];
1095 s_szFlags[flagofs] = '\0';
1096 if (ilen == 0 ) {
1097 int val = va_arg(argptr, int);
1098 ::sprintf(s_szScratch, s_szFlags, val);
1099 }
1100 else if (ilen == -1) {
1101 short int val = va_arg(argptr, short int);
1102 ::sprintf(s_szScratch, s_szFlags, val);
1103 }
1104 else if (ilen == 1) {
1105 long int val = va_arg(argptr, long int);
1106 ::sprintf(s_szScratch, s_szFlags, val);
1107 }
1108 else if (ilen == 2) {
1109 #if SIZEOF_LONG_LONG
1110 long long int val = va_arg(argptr, long long int);
1111 ::sprintf(s_szScratch, s_szFlags, val);
1112 #else
1113 long int val = va_arg(argptr, long int);
1114 ::sprintf(s_szScratch, s_szFlags, val);
1115 #endif
1116 }
1117 else if (ilen == 3) {
1118 size_t val = va_arg(argptr, size_t);
1119 ::sprintf(s_szScratch, s_szFlags, val);
1120 }
1121 *this += wxString(s_szScratch);
1122 done = TRUE;
1123 break;
1124 case _T('e'):
1125 case _T('E'):
1126 case _T('f'):
1127 case _T('g'):
1128 case _T('G'):
1129 CHECK_PREC
1130 s_szFlags[flagofs++] = pszFormat[n];
1131 s_szFlags[flagofs] = '\0';
1132 if (ilen == 2) {
1133 long double val = va_arg(argptr, long double);
1134 ::sprintf(s_szScratch, s_szFlags, val);
1135 } else {
1136 double val = va_arg(argptr, double);
1137 ::sprintf(s_szScratch, s_szFlags, val);
1138 }
1139 *this += wxString(s_szScratch);
1140 done = TRUE;
1141 break;
1142 case _T('p'):
1143 {
1144 void *val = va_arg(argptr, void *);
1145 CHECK_PREC
1146 s_szFlags[flagofs++] = pszFormat[n];
1147 s_szFlags[flagofs] = '\0';
1148 ::sprintf(s_szScratch, s_szFlags, val);
1149 *this += wxString(s_szScratch);
1150 done = TRUE;
1151 }
1152 break;
1153 case _T('c'):
1154 {
1155 wxChar val = va_arg(argptr, int);
1156 // we don't need to honor padding here, do we?
1157 *this += val;
1158 done = TRUE;
1159 }
1160 break;
1161 case _T('s'):
1162 if (ilen == -1) {
1163 // wx extension: we'll let %hs mean non-Unicode strings
1164 char *val = va_arg(argptr, char *);
1165 #if wxUSE_UNICODE
1166 // ASCII->Unicode constructor handles max_width right
1167 wxString s(val, wxConv_libc, max_width);
1168 #else
1169 size_t len = wxSTRING_MAXLEN;
1170 if (val) {
1171 for (len = 0; val[len] && (len<max_width); len++);
1172 } else val = _T("(null)");
1173 wxString s(val, len);
1174 #endif
1175 if (s.Len() < min_width)
1176 s.Pad(min_width - s.Len(), _T(' '), adj_left);
1177 *this += s;
1178 } else {
1179 wxChar *val = va_arg(argptr, wxChar *);
1180 size_t len = wxSTRING_MAXLEN;
1181 if (val) {
1182 for (len = 0; val[len] && (len<max_width); len++);
1183 } else val = _T("(null)");
1184 wxString s(val, len);
1185 if (s.Len() < min_width)
1186 s.Pad(min_width - s.Len(), _T(' '), adj_left);
1187 *this += s;
1188 }
1189 done = TRUE;
1190 break;
1191 case _T('n'):
1192 if (ilen == 0) {
1193 int *val = va_arg(argptr, int *);
1194 *val = Len();
1195 }
1196 else if (ilen == -1) {
1197 short int *val = va_arg(argptr, short int *);
1198 *val = Len();
1199 }
1200 else if (ilen >= 1) {
1201 long int *val = va_arg(argptr, long int *);
1202 *val = Len();
1203 }
1204 done = TRUE;
1205 break;
1206 default:
1207 if (wxIsalpha(pszFormat[n]))
1208 // probably some flag not taken care of here yet
1209 s_szFlags[flagofs++] = pszFormat[n];
1210 else {
1211 // bad format
1212 *this += _T('%'); // just to pass the glibc tst-printf.c
1213 n--;
1214 done = TRUE;
1215 }
1216 break;
1217 }
1218 #undef CHECK_PREC
1219 } while (!done);
1220 } else *this += pszFormat[n];
1221
1222 #else
1223 // NB: wxVsnprintf() may return either less than the buffer size or -1 if there
1224 // is not enough place depending on implementation
1225 int iLen = wxVsnprintf(s_szScratch, WXSIZEOF(s_szScratch), pszFormat, argptr);
1226 char *buffer;
1227 if ( iLen < (int)WXSIZEOF(s_szScratch) ) {
1228 buffer = s_szScratch;
1229 }
1230 else {
1231 int size = WXSIZEOF(s_szScratch) * 2;
1232 buffer = (char *)malloc(size);
1233 while ( buffer != NULL ) {
1234 iLen = wxVsnprintf(buffer, WXSIZEOF(s_szScratch), pszFormat, argptr);
1235 if ( iLen < size ) {
1236 // ok, there was enough space
1237 break;
1238 }
1239
1240 // still not enough, double it again
1241 buffer = (char *)realloc(buffer, size *= 2);
1242 }
1243
1244 if ( !buffer ) {
1245 // out of memory
1246 return -1;
1247 }
1248 }
1249
1250 wxString s(buffer);
1251 *this = s;
1252
1253 if ( buffer != s_szScratch )
1254 free(buffer);
1255 #endif
1256
1257 return Len();
1258 }
1259
1260 // ----------------------------------------------------------------------------
1261 // misc other operations
1262 // ----------------------------------------------------------------------------
1263 bool wxString::Matches(const wxChar *pszMask) const
1264 {
1265 // check char by char
1266 const wxChar *pszTxt;
1267 for ( pszTxt = c_str(); *pszMask != _T('\0'); pszMask++, pszTxt++ ) {
1268 switch ( *pszMask ) {
1269 case _T('?'):
1270 if ( *pszTxt == _T('\0') )
1271 return FALSE;
1272
1273 pszTxt++;
1274 pszMask++;
1275 break;
1276
1277 case _T('*'):
1278 {
1279 // ignore special chars immediately following this one
1280 while ( *pszMask == _T('*') || *pszMask == _T('?') )
1281 pszMask++;
1282
1283 // if there is nothing more, match
1284 if ( *pszMask == _T('\0') )
1285 return TRUE;
1286
1287 // are there any other metacharacters in the mask?
1288 size_t uiLenMask;
1289 const wxChar *pEndMask = wxStrpbrk(pszMask, _T("*?"));
1290
1291 if ( pEndMask != NULL ) {
1292 // we have to match the string between two metachars
1293 uiLenMask = pEndMask - pszMask;
1294 }
1295 else {
1296 // we have to match the remainder of the string
1297 uiLenMask = wxStrlen(pszMask);
1298 }
1299
1300 wxString strToMatch(pszMask, uiLenMask);
1301 const wxChar* pMatch = wxStrstr(pszTxt, strToMatch);
1302 if ( pMatch == NULL )
1303 return FALSE;
1304
1305 // -1 to compensate "++" in the loop
1306 pszTxt = pMatch + uiLenMask - 1;
1307 pszMask += uiLenMask - 1;
1308 }
1309 break;
1310
1311 default:
1312 if ( *pszMask != *pszTxt )
1313 return FALSE;
1314 break;
1315 }
1316 }
1317
1318 // match only if nothing left
1319 return *pszTxt == _T('\0');
1320 }
1321
1322 // Count the number of chars
1323 int wxString::Freq(wxChar ch) const
1324 {
1325 int count = 0;
1326 int len = Len();
1327 for (int i = 0; i < len; i++)
1328 {
1329 if (GetChar(i) == ch)
1330 count ++;
1331 }
1332 return count;
1333 }
1334
1335 // convert to upper case, return the copy of the string
1336 wxString wxString::Upper() const
1337 { wxString s(*this); return s.MakeUpper(); }
1338
1339 // convert to lower case, return the copy of the string
1340 wxString wxString::Lower() const { wxString s(*this); return s.MakeLower(); }
1341
1342 int wxString::sprintf(const wxChar *pszFormat, ...)
1343 {
1344 va_list argptr;
1345 va_start(argptr, pszFormat);
1346 int iLen = PrintfV(pszFormat, argptr);
1347 va_end(argptr);
1348 return iLen;
1349 }
1350
1351 // ---------------------------------------------------------------------------
1352 // standard C++ library string functions
1353 // ---------------------------------------------------------------------------
1354 #ifdef wxSTD_STRING_COMPATIBILITY
1355
1356 wxString& wxString::insert(size_t nPos, const wxString& str)
1357 {
1358 wxASSERT( str.GetStringData()->IsValid() );
1359 wxASSERT( nPos <= Len() );
1360
1361 if ( !str.IsEmpty() ) {
1362 wxString strTmp;
1363 wxChar *pc = strTmp.GetWriteBuf(Len() + str.Len());
1364 wxStrncpy(pc, c_str(), nPos);
1365 wxStrcpy(pc + nPos, str);
1366 wxStrcpy(pc + nPos + str.Len(), c_str() + nPos);
1367 strTmp.UngetWriteBuf();
1368 *this = strTmp;
1369 }
1370
1371 return *this;
1372 }
1373
1374 size_t wxString::find(const wxString& str, size_t nStart) const
1375 {
1376 wxASSERT( str.GetStringData()->IsValid() );
1377 wxASSERT( nStart <= Len() );
1378
1379 const wxChar *p = wxStrstr(c_str() + nStart, str);
1380
1381 return p == NULL ? npos : p - c_str();
1382 }
1383
1384 // VC++ 1.5 can't cope with the default argument in the header.
1385 #if !defined(__VISUALC__) || defined(__WIN32__)
1386 size_t wxString::find(const wxChar* sz, size_t nStart, size_t n) const
1387 {
1388 return find(wxString(sz, n == npos ? 0 : n), nStart);
1389 }
1390 #endif // VC++ 1.5
1391
1392 // Gives a duplicate symbol (presumably a case-insensitivity problem)
1393 #if !defined(__BORLANDC__)
1394 size_t wxString::find(wxChar ch, size_t nStart) const
1395 {
1396 wxASSERT( nStart <= Len() );
1397
1398 const wxChar *p = wxStrchr(c_str() + nStart, ch);
1399
1400 return p == NULL ? npos : p - c_str();
1401 }
1402 #endif
1403
1404 size_t wxString::rfind(const wxString& str, size_t nStart) const
1405 {
1406 wxASSERT( str.GetStringData()->IsValid() );
1407 wxASSERT( nStart <= Len() );
1408
1409 // # could be quicker than that
1410 const wxChar *p = c_str() + (nStart == npos ? Len() : nStart);
1411 while ( p >= c_str() + str.Len() ) {
1412 if ( wxStrncmp(p - str.Len(), str, str.Len()) == 0 )
1413 return p - str.Len() - c_str();
1414 p--;
1415 }
1416
1417 return npos;
1418 }
1419
1420 // VC++ 1.5 can't cope with the default argument in the header.
1421 #if !defined(__VISUALC__) || defined(__WIN32__)
1422 size_t wxString::rfind(const wxChar* sz, size_t nStart, size_t n) const
1423 {
1424 return rfind(wxString(sz, n == npos ? 0 : n), nStart);
1425 }
1426
1427 size_t wxString::rfind(wxChar ch, size_t nStart) const
1428 {
1429 wxASSERT( nStart <= Len() );
1430
1431 const wxChar *p = wxStrrchr(c_str() + nStart, ch);
1432
1433 return p == NULL ? npos : p - c_str();
1434 }
1435 #endif // VC++ 1.5
1436
1437 wxString wxString::substr(size_t nStart, size_t nLen) const
1438 {
1439 // npos means 'take all'
1440 if ( nLen == npos )
1441 nLen = 0;
1442
1443 wxASSERT( nStart + nLen <= Len() );
1444
1445 return wxString(c_str() + nStart, nLen == npos ? 0 : nLen);
1446 }
1447
1448 wxString& wxString::erase(size_t nStart, size_t nLen)
1449 {
1450 wxString strTmp(c_str(), nStart);
1451 if ( nLen != npos ) {
1452 wxASSERT( nStart + nLen <= Len() );
1453
1454 strTmp.append(c_str() + nStart + nLen);
1455 }
1456
1457 *this = strTmp;
1458 return *this;
1459 }
1460
1461 wxString& wxString::replace(size_t nStart, size_t nLen, const wxChar *sz)
1462 {
1463 wxASSERT( nStart + nLen <= wxStrlen(sz) );
1464
1465 wxString strTmp;
1466 if ( nStart != 0 )
1467 strTmp.append(c_str(), nStart);
1468 strTmp += sz;
1469 strTmp.append(c_str() + nStart + nLen);
1470
1471 *this = strTmp;
1472 return *this;
1473 }
1474
1475 wxString& wxString::replace(size_t nStart, size_t nLen, size_t nCount, wxChar ch)
1476 {
1477 return replace(nStart, nLen, wxString(ch, nCount));
1478 }
1479
1480 wxString& wxString::replace(size_t nStart, size_t nLen,
1481 const wxString& str, size_t nStart2, size_t nLen2)
1482 {
1483 return replace(nStart, nLen, str.substr(nStart2, nLen2));
1484 }
1485
1486 wxString& wxString::replace(size_t nStart, size_t nLen,
1487 const wxChar* sz, size_t nCount)
1488 {
1489 return replace(nStart, nLen, wxString(sz, nCount));
1490 }
1491
1492 #endif //std::string compatibility
1493
1494 // ============================================================================
1495 // ArrayString
1496 // ============================================================================
1497
1498 // size increment = max(50% of current size, ARRAY_MAXSIZE_INCREMENT)
1499 #define ARRAY_MAXSIZE_INCREMENT 4096
1500 #ifndef ARRAY_DEFAULT_INITIAL_SIZE // also defined in dynarray.h
1501 #define ARRAY_DEFAULT_INITIAL_SIZE (16)
1502 #endif
1503
1504 #define STRING(p) ((wxString *)(&(p)))
1505
1506 // ctor
1507 wxArrayString::wxArrayString()
1508 {
1509 m_nSize =
1510 m_nCount = 0;
1511 m_pItems = (wxChar **) NULL;
1512 }
1513
1514 // copy ctor
1515 wxArrayString::wxArrayString(const wxArrayString& src)
1516 {
1517 m_nSize =
1518 m_nCount = 0;
1519 m_pItems = (wxChar **) NULL;
1520
1521 *this = src;
1522 }
1523
1524 // assignment operator
1525 wxArrayString& wxArrayString::operator=(const wxArrayString& src)
1526 {
1527 if ( m_nSize > 0 )
1528 Clear();
1529
1530 if ( src.m_nCount > ARRAY_DEFAULT_INITIAL_SIZE )
1531 Alloc(src.m_nCount);
1532
1533 // we can't just copy the pointers here because otherwise we would share
1534 // the strings with another array
1535 for ( size_t n = 0; n < src.m_nCount; n++ )
1536 Add(src[n]);
1537
1538 if ( m_nCount != 0 )
1539 memcpy(m_pItems, src.m_pItems, m_nCount*sizeof(wxChar *));
1540
1541 return *this;
1542 }
1543
1544 // grow the array
1545 void wxArrayString::Grow()
1546 {
1547 // only do it if no more place
1548 if( m_nCount == m_nSize ) {
1549 if( m_nSize == 0 ) {
1550 // was empty, alloc some memory
1551 m_nSize = ARRAY_DEFAULT_INITIAL_SIZE;
1552 m_pItems = new wxChar *[m_nSize];
1553 }
1554 else {
1555 // otherwise when it's called for the first time, nIncrement would be 0
1556 // and the array would never be expanded
1557 wxASSERT( ARRAY_DEFAULT_INITIAL_SIZE != 0 );
1558
1559 // add 50% but not too much
1560 size_t nIncrement = m_nSize < ARRAY_DEFAULT_INITIAL_SIZE
1561 ? ARRAY_DEFAULT_INITIAL_SIZE : m_nSize >> 1;
1562 if ( nIncrement > ARRAY_MAXSIZE_INCREMENT )
1563 nIncrement = ARRAY_MAXSIZE_INCREMENT;
1564 m_nSize += nIncrement;
1565 wxChar **pNew = new wxChar *[m_nSize];
1566
1567 // copy data to new location
1568 memcpy(pNew, m_pItems, m_nCount*sizeof(wxChar *));
1569
1570 // delete old memory (but do not release the strings!)
1571 wxDELETEA(m_pItems);
1572
1573 m_pItems = pNew;
1574 }
1575 }
1576 }
1577
1578 void wxArrayString::Free()
1579 {
1580 for ( size_t n = 0; n < m_nCount; n++ ) {
1581 STRING(m_pItems[n])->GetStringData()->Unlock();
1582 }
1583 }
1584
1585 // deletes all the strings from the list
1586 void wxArrayString::Empty()
1587 {
1588 Free();
1589
1590 m_nCount = 0;
1591 }
1592
1593 // as Empty, but also frees memory
1594 void wxArrayString::Clear()
1595 {
1596 Free();
1597
1598 m_nSize =
1599 m_nCount = 0;
1600
1601 wxDELETEA(m_pItems);
1602 }
1603
1604 // dtor
1605 wxArrayString::~wxArrayString()
1606 {
1607 Free();
1608
1609 wxDELETEA(m_pItems);
1610 }
1611
1612 // pre-allocates memory (frees the previous data!)
1613 void wxArrayString::Alloc(size_t nSize)
1614 {
1615 wxASSERT( nSize > 0 );
1616
1617 // only if old buffer was not big enough
1618 if ( nSize > m_nSize ) {
1619 Free();
1620 wxDELETEA(m_pItems);
1621 m_pItems = new wxChar *[nSize];
1622 m_nSize = nSize;
1623 }
1624
1625 m_nCount = 0;
1626 }
1627
1628 // minimizes the memory usage by freeing unused memory
1629 void wxArrayString::Shrink()
1630 {
1631 // only do it if we have some memory to free
1632 if( m_nCount < m_nSize ) {
1633 // allocates exactly as much memory as we need
1634 wxChar **pNew = new wxChar *[m_nCount];
1635
1636 // copy data to new location
1637 memcpy(pNew, m_pItems, m_nCount*sizeof(wxChar *));
1638 delete [] m_pItems;
1639 m_pItems = pNew;
1640 }
1641 }
1642
1643 // searches the array for an item (forward or backwards)
1644 int wxArrayString::Index(const wxChar *sz, bool bCase, bool bFromEnd) const
1645 {
1646 if ( bFromEnd ) {
1647 if ( m_nCount > 0 ) {
1648 size_t ui = m_nCount;
1649 do {
1650 if ( STRING(m_pItems[--ui])->IsSameAs(sz, bCase) )
1651 return ui;
1652 }
1653 while ( ui != 0 );
1654 }
1655 }
1656 else {
1657 for( size_t ui = 0; ui < m_nCount; ui++ ) {
1658 if( STRING(m_pItems[ui])->IsSameAs(sz, bCase) )
1659 return ui;
1660 }
1661 }
1662
1663 return wxNOT_FOUND;
1664 }
1665
1666 // add item at the end
1667 void wxArrayString::Add(const wxString& str)
1668 {
1669 wxASSERT( str.GetStringData()->IsValid() );
1670
1671 Grow();
1672
1673 // the string data must not be deleted!
1674 str.GetStringData()->Lock();
1675 m_pItems[m_nCount++] = (wxChar *)str.c_str();
1676 }
1677
1678 // add item at the given position
1679 void wxArrayString::Insert(const wxString& str, size_t nIndex)
1680 {
1681 wxASSERT( str.GetStringData()->IsValid() );
1682
1683 wxCHECK_RET( nIndex <= m_nCount, _("bad index in wxArrayString::Insert") );
1684
1685 Grow();
1686
1687 memmove(&m_pItems[nIndex + 1], &m_pItems[nIndex],
1688 (m_nCount - nIndex)*sizeof(wxChar *));
1689
1690 str.GetStringData()->Lock();
1691 m_pItems[nIndex] = (wxChar *)str.c_str();
1692
1693 m_nCount++;
1694 }
1695
1696 // removes item from array (by index)
1697 void wxArrayString::Remove(size_t nIndex)
1698 {
1699 wxCHECK_RET( nIndex <= m_nCount, _("bad index in wxArrayString::Remove") );
1700
1701 // release our lock
1702 Item(nIndex).GetStringData()->Unlock();
1703
1704 memmove(&m_pItems[nIndex], &m_pItems[nIndex + 1],
1705 (m_nCount - nIndex - 1)*sizeof(wxChar *));
1706 m_nCount--;
1707 }
1708
1709 // removes item from array (by value)
1710 void wxArrayString::Remove(const wxChar *sz)
1711 {
1712 int iIndex = Index(sz);
1713
1714 wxCHECK_RET( iIndex != wxNOT_FOUND,
1715 _("removing inexistent element in wxArrayString::Remove") );
1716
1717 Remove(iIndex);
1718 }
1719
1720 // ----------------------------------------------------------------------------
1721 // sorting
1722 // ----------------------------------------------------------------------------
1723
1724 // we can only sort one array at a time with the quick-sort based
1725 // implementation
1726 #if wxUSE_THREADS
1727 // need a critical section to protect access to gs_compareFunction and
1728 // gs_sortAscending variables
1729 static wxCriticalSection *gs_critsectStringSort = NULL;
1730
1731 // call this before the value of the global sort vars is changed/after
1732 // you're finished with them
1733 #define START_SORT() wxASSERT( !gs_critsectStringSort ); \
1734 gs_critsectStringSort = new wxCriticalSection; \
1735 gs_critsectStringSort->Enter()
1736 #define END_SORT() gs_critsectStringSort->Leave(); \
1737 delete gs_critsectStringSort; \
1738 gs_critsectStringSort = NULL
1739 #else // !threads
1740 #define START_SORT()
1741 #define END_SORT()
1742 #endif // wxUSE_THREADS
1743
1744 // function to use for string comparaison
1745 static wxArrayString::CompareFunction gs_compareFunction = NULL;
1746
1747 // if we don't use the compare function, this flag tells us if we sort the
1748 // array in ascending or descending order
1749 static bool gs_sortAscending = TRUE;
1750
1751 // function which is called by quick sort
1752 static int wxStringCompareFunction(const void *first, const void *second)
1753 {
1754 wxString *strFirst = (wxString *)first;
1755 wxString *strSecond = (wxString *)second;
1756
1757 if ( gs_compareFunction ) {
1758 return gs_compareFunction(*strFirst, *strSecond);
1759 }
1760 else {
1761 // maybe we should use wxStrcoll
1762 int result = wxStrcmp(strFirst->c_str(), strSecond->c_str());
1763
1764 return gs_sortAscending ? result : -result;
1765 }
1766 }
1767
1768 // sort array elements using passed comparaison function
1769 void wxArrayString::Sort(CompareFunction compareFunction)
1770 {
1771 START_SORT();
1772
1773 wxASSERT( !gs_compareFunction ); // must have been reset to NULL
1774 gs_compareFunction = compareFunction;
1775
1776 DoSort();
1777
1778 END_SORT();
1779 }
1780
1781 void wxArrayString::Sort(bool reverseOrder)
1782 {
1783 START_SORT();
1784
1785 wxASSERT( !gs_compareFunction ); // must have been reset to NULL
1786 gs_sortAscending = !reverseOrder;
1787
1788 DoSort();
1789
1790 END_SORT();
1791 }
1792
1793 void wxArrayString::DoSort()
1794 {
1795 // just sort the pointers using qsort() - of course it only works because
1796 // wxString() *is* a pointer to its data
1797 qsort(m_pItems, m_nCount, sizeof(wxChar *), wxStringCompareFunction);
1798 }
1799
1800 // ============================================================================
1801 // MBConv
1802 // ============================================================================
1803
1804 #if wxUSE_WCHAR_T
1805 WXDLLEXPORT_DATA(wxMBConv *) wxConv_current = &wxConv_libc;
1806
1807 // ----------------------------------------------------------------------------
1808 // standard libc conversion
1809 // ----------------------------------------------------------------------------
1810
1811 WXDLLEXPORT_DATA(wxMBConv) wxConv_libc;
1812
1813 size_t wxMBConv::MB2WC(wchar_t *buf, const char *psz, size_t n) const
1814 {
1815 return wxMB2WC(buf, psz, n);
1816 }
1817
1818 size_t wxMBConv::WC2MB(char *buf, const wchar_t *psz, size_t n) const
1819 {
1820 return wxWC2MB(buf, psz, n);
1821 }
1822
1823 // ----------------------------------------------------------------------------
1824 // standard file conversion
1825 // ----------------------------------------------------------------------------
1826
1827 WXDLLEXPORT_DATA(wxMBConvFile) wxConvFile;
1828
1829 // just use the libc conversion for now
1830 size_t wxMBConvFile::MB2WC(wchar_t *buf, const char *psz, size_t n) const
1831 {
1832 return wxMB2WC(buf, psz, n);
1833 }
1834
1835 size_t wxMBConvFile::WC2MB(char *buf, const wchar_t *psz, size_t n) const
1836 {
1837 return wxWC2MB(buf, psz, n);
1838 }
1839
1840 // ----------------------------------------------------------------------------
1841 // standard gdk conversion
1842 // ----------------------------------------------------------------------------
1843
1844 #ifdef __WXGTK12__
1845 WXDLLEXPORT_DATA(wxMBConvGdk) wxConvGdk;
1846
1847 #include <gdk/gdk.h>
1848
1849 size_t wxMBConvGdk::MB2WC(wchar_t *buf, const char *psz, size_t n) const
1850 {
1851 if (buf) {
1852 return gdk_mbstowcs((GdkWChar *)buf, psz, n);
1853 } else {
1854 GdkWChar *nbuf = new GdkWChar[n=strlen(psz)];
1855 size_t len = gdk_mbstowcs(nbuf, psz, n);
1856 delete [] nbuf;
1857 return len;
1858 }
1859 }
1860
1861 size_t wxMBConvGdk::WC2MB(char *buf, const wchar_t *psz, size_t n) const
1862 {
1863 char *mbstr = gdk_wcstombs((GdkWChar *)psz);
1864 size_t len = mbstr ? strlen(mbstr) : 0;
1865 if (buf) {
1866 if (len > n) len = n;
1867 memcpy(buf, psz, len);
1868 if (len < n) buf[len] = 0;
1869 }
1870 return len;
1871 }
1872 #endif // GTK > 1.0
1873
1874 // ----------------------------------------------------------------------------
1875 // UTF-7
1876 // ----------------------------------------------------------------------------
1877
1878 WXDLLEXPORT_DATA(wxMBConvUTF7) wxConvUTF7;
1879
1880 #if 0
1881 static char utf7_setD[]="ABCDEFGHIJKLMNOPQRSTUVWXYZ"
1882 "abcdefghijklmnopqrstuvwxyz"
1883 "0123456789'(),-./:?";
1884 static char utf7_setO[]="!\"#$%&*;<=>@[]^_`{|}";
1885 static char utf7_setB[]="ABCDEFGHIJKLMNOPQRSTUVWXYZ"
1886 "abcdefghijklmnopqrstuvwxyz"
1887 "0123456789+/";
1888 #endif
1889
1890 // TODO: write actual implementations of UTF-7 here
1891 size_t wxMBConvUTF7::MB2WC(wchar_t *buf, const char *psz, size_t n) const
1892 {
1893 return 0;
1894 }
1895
1896 size_t wxMBConvUTF7::WC2MB(char *buf, const wchar_t *psz, size_t n) const
1897 {
1898 return 0;
1899 }
1900
1901 // ----------------------------------------------------------------------------
1902 // UTF-8
1903 // ----------------------------------------------------------------------------
1904
1905 WXDLLEXPORT_DATA(wxMBConvUTF8) wxConvUTF8;
1906
1907 static unsigned long utf8_max[]={0x7f,0x7ff,0xffff,0x1fffff,0x3ffffff,0x7fffffff,0xffffffff};
1908
1909 size_t wxMBConvUTF8::MB2WC(wchar_t *buf, const char *psz, size_t n) const
1910 {
1911 size_t len = 0;
1912
1913 while (*psz && ((!buf) || (len<n))) {
1914 unsigned char cc=*psz++, fc=cc;
1915 unsigned cnt;
1916 for (cnt=0; fc&0x80; cnt++) fc<<=1;
1917 if (!cnt) {
1918 // plain ASCII char
1919 if (buf) *buf++=cc;
1920 len++;
1921 } else {
1922 cnt--;
1923 if (!cnt) {
1924 // invalid UTF-8 sequence
1925 return (size_t)-1;
1926 } else {
1927 unsigned ocnt=cnt-1;
1928 unsigned long res=cc&(0x3f>>cnt);
1929 while (cnt--) {
1930 cc = *psz++;
1931 if ((cc&0xC0)!=0x80) {
1932 // invalid UTF-8 sequence
1933 return (size_t)-1;
1934 }
1935 res=(res<<6)|(cc&0x3f);
1936 }
1937 if (res<=utf8_max[ocnt]) {
1938 // illegal UTF-8 encoding
1939 return (size_t)-1;
1940 }
1941 if (buf) *buf++=res;
1942 len++;
1943 }
1944 }
1945 }
1946 if (buf && (len<n)) *buf = 0;
1947 return len;
1948 }
1949
1950 size_t wxMBConvUTF8::WC2MB(char *buf, const wchar_t *psz, size_t n) const
1951 {
1952 size_t len = 0;
1953
1954 while (*psz && ((!buf) || (len<n))) {
1955 unsigned long cc=(*psz++)&0x7fffffff;
1956 unsigned cnt;
1957 for (cnt=0; cc>utf8_max[cnt]; cnt++);
1958 if (!cnt) {
1959 // plain ASCII char
1960 if (buf) *buf++=cc;
1961 len++;
1962 } else {
1963 len+=cnt+1;
1964 if (buf) {
1965 *buf++=(-128>>cnt)|((cc>>(cnt*6))&(0x3f>>cnt));
1966 while (cnt--)
1967 *buf++=0x80|((cc>>(cnt*6))&0x3f);
1968 }
1969 }
1970 }
1971 if (buf && (len<n)) *buf = 0;
1972 return len;
1973 }
1974
1975 // ----------------------------------------------------------------------------
1976 // specified character set
1977 // ----------------------------------------------------------------------------
1978
1979 class wxCharacterSet
1980 {
1981 public:
1982 wxArrayString names;
1983 wchar_t *data;
1984 };
1985
1986 #ifndef WX_PRECOMP
1987 #include "wx/dynarray.h"
1988 #include "wx/filefn.h"
1989 #include "wx/textfile.h"
1990 #include "wx/tokenzr.h"
1991 #include "wx/utils.h"
1992 #endif
1993
1994 WX_DECLARE_OBJARRAY(wxCharacterSet, wxCSArray);
1995 #include "wx/arrimpl.cpp"
1996 WX_DEFINE_OBJARRAY(wxCSArray);
1997
1998 static wxCSArray wxCharsets;
1999
2000 static void wxLoadCharacterSets(void)
2001 {
2002 static bool already_loaded = FALSE;
2003
2004 if (already_loaded) return;
2005
2006 already_loaded = TRUE;
2007 #if defined(__UNIX__)
2008 // search through files in /usr/share/i18n/charmaps
2009 wxString fname;
2010 for (fname = ::wxFindFirstFile(_T("/usr/share/i18n/charmaps/*"));
2011 !fname.IsEmpty();
2012 fname = ::wxFindNextFile()) {
2013 wxTextFile cmap(fname);
2014 if (cmap.Open()) {
2015 wxCharacterSet *cset = new wxCharacterSet;
2016 wxString comchar,escchar;
2017 bool in_charset = FALSE;
2018
2019 // wxFprintf(stderr,_T("Loaded: %s\n"),fname.c_str());
2020
2021 wxString line;
2022 for (line = cmap.GetFirstLine();
2023 !cmap.Eof();
2024 line = cmap.GetNextLine()) {
2025 // wxFprintf(stderr,_T("line contents: %s\n"),line.c_str());
2026 wxStringTokenizer token(line);
2027 wxString cmd = token.GetNextToken();
2028 if (cmd == comchar) {
2029 if (token.GetNextToken() == _T("alias"))
2030 cset->names.Add(token.GetNextToken());
2031 }
2032 else if (cmd == _T("<code_set_name>"))
2033 cset->names.Add(token.GetNextToken());
2034 else if (cmd == _T("<comment_char>"))
2035 comchar = token.GetNextToken();
2036 else if (cmd == _T("<escape_char>"))
2037 escchar = token.GetNextToken();
2038 else if (cmd == _T("<mb_cur_min>")) {
2039 delete cset;
2040 cset = (wxCharacterSet *) NULL;
2041 break; // we don't support multibyte charsets ourselves (yet)
2042 }
2043 else if (cmd == _T("CHARMAP")) {
2044 cset->data = (wchar_t *)calloc(256, sizeof(wchar_t));
2045 in_charset = TRUE;
2046 }
2047 else if (cmd == _T("END")) {
2048 if (token.GetNextToken() == _T("CHARMAP"))
2049 in_charset = FALSE;
2050 }
2051 else if (in_charset) {
2052 // format: <NUL> /x00 <U0000> NULL (NUL)
2053 // <A> /x41 <U0041> LATIN CAPITAL LETTER A
2054 wxString hex = token.GetNextToken();
2055 // skip whitespace (why doesn't wxStringTokenizer do this?)
2056 while (wxIsEmpty(hex) && token.HasMoreTokens()) hex = token.GetNextToken();
2057 wxString uni = token.GetNextToken();
2058 // skip whitespace again
2059 while (wxIsEmpty(uni) && token.HasMoreTokens()) uni = token.GetNextToken();
2060
2061 if ((hex.Len() > 2) && (hex.GetChar(0) == escchar) && (hex.GetChar(1) == _T('x')) &&
2062 (uni.Left(2) == _T("<U"))) {
2063 hex.MakeUpper(); uni.MakeUpper();
2064 int pos = ::wxHexToDec(hex.Mid(2,2));
2065 if (pos>=0) {
2066 unsigned long uni1 = ::wxHexToDec(uni.Mid(2,2));
2067 unsigned long uni2 = ::wxHexToDec(uni.Mid(4,2));
2068 cset->data[pos] = (uni1 << 16) | uni2;
2069 // wxFprintf(stderr,_T("char %02x mapped to %04x (%c)\n"),pos,cset->data[pos],cset->data[pos]);
2070 }
2071 }
2072 }
2073 }
2074 if (cset) {
2075 cset->names.Shrink();
2076 wxCharsets.Add(cset);
2077 }
2078 }
2079 }
2080 #endif
2081 wxCharsets.Shrink();
2082 }
2083
2084 static wxCharacterSet *wxFindCharacterSet(const wxChar *charset)
2085 {
2086 if (!charset) return (wxCharacterSet *)NULL;
2087 wxLoadCharacterSets();
2088 for (size_t n=0; n<wxCharsets.GetCount(); n++)
2089 if (wxCharsets[n].names.Index(charset) != wxNOT_FOUND)
2090 return &(wxCharsets[n]);
2091 return (wxCharacterSet *)NULL;
2092 }
2093
2094 WXDLLEXPORT_DATA(wxCSConv) wxConvLocal((const wxChar *)NULL);
2095
2096 wxCSConv::wxCSConv(const wxChar *charset)
2097 {
2098 m_name = (wxChar *) NULL;
2099 m_cset = (wxCharacterSet *) NULL;
2100 m_deferred = TRUE;
2101 SetName(charset);
2102 }
2103
2104 wxCSConv::~wxCSConv()
2105 {
2106 if (m_name) free(m_name);
2107 }
2108
2109 void wxCSConv::SetName(const wxChar *charset)
2110 {
2111 if (charset) {
2112 #ifdef __UNIX__
2113 // first, convert the character set name to standard form
2114 wxString codeset;
2115 if (wxString(charset,3).CmpNoCase(_T("ISO")) == 0) {
2116 // make sure it's represented in the standard form: ISO_8859-1
2117 codeset = _T("ISO_");
2118 charset += 3;
2119 if ((*charset == _T('-')) || (*charset == _T('_'))) charset++;
2120 if (wxStrlen(charset)>4) {
2121 if (wxString(charset,4) == _T("8859")) {
2122 codeset << _T("8859-");
2123 if (*charset == _T('-')) charset++;
2124 }
2125 }
2126 }
2127 codeset << charset;
2128 codeset.MakeUpper();
2129 m_name = wxStrdup(codeset.c_str());
2130 m_deferred = TRUE;
2131 #endif
2132 }
2133 }
2134
2135 void wxCSConv::LoadNow()
2136 {
2137 // wxPrintf(_T("Conversion request\n"));
2138 if (m_deferred) {
2139 if (!m_name) {
2140 #ifdef __UNIX__
2141 wxChar *lang = wxGetenv(_T("LANG"));
2142 wxChar *dot = lang ? wxStrchr(lang, _T('.')) : (wxChar *)NULL;
2143 if (dot) SetName(dot+1);
2144 #endif
2145 }
2146 m_cset = wxFindCharacterSet(m_name);
2147 m_deferred = FALSE;
2148 }
2149 }
2150
2151 size_t wxCSConv::MB2WC(wchar_t *buf, const char *psz, size_t n) const
2152 {
2153 ((wxCSConv *)this)->LoadNow(); // discard constness
2154 if (buf) {
2155 if (m_cset) {
2156 for (size_t c=0; c<n; c++)
2157 buf[c] = m_cset->data[(unsigned char)(psz[c])];
2158 } else {
2159 // latin-1 (direct)
2160 for (size_t c=0; c<n; c++)
2161 buf[c] = (unsigned char)(psz[c]);
2162 }
2163 return n;
2164 }
2165 return strlen(psz);
2166 }
2167
2168 size_t wxCSConv::WC2MB(char *buf, const wchar_t *psz, size_t n) const
2169 {
2170 ((wxCSConv *)this)->LoadNow(); // discard constness
2171 if (buf) {
2172 if (m_cset) {
2173 for (size_t c=0; c<n; c++) {
2174 size_t n;
2175 for (n=0; (n<256) && (m_cset->data[n] != psz[c]); n++);
2176 buf[c] = (n>0xff) ? '?' : n;
2177 }
2178 } else {
2179 // latin-1 (direct)
2180 for (size_t c=0; c<n; c++)
2181 buf[c] = (psz[c]>0xff) ? '?' : psz[c];
2182 }
2183 return n;
2184 }
2185 return wcslen(psz);
2186 }
2187
2188 #endif//wxUSE_WCHAR_T