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