Chameleon

Chameleon Svn Source Tree

Root/branches/xZenu/src/util/doxygen/qtools/qcstring.cpp

Source at commit 1322 created 9 years 5 months ago.
By meklort, Add doxygen to utils folder
1/******************************************************************************
2 *
3 * Copyright (C) 1997-2004 by Dimitri van Heesch.
4 *
5 * Permission to use, copy, modify, and distribute this software and its
6 * documentation under the terms of the GNU General Public License is hereby
7 * granted. No representations are made about the suitability of this software
8 * for any purpose. It is provided "as is" without express or implied warranty.
9 * See the GNU General Public License for more details.
10 *
11 * Documents produced by Doxygen are derivative works derived from the
12 * input used in their production; they are not affected by this license.
13 *
14 */
15
16#include "qcstring.h"
17#include "qgstring.h"
18
19#include <qstring.h>
20#include <stdlib.h>
21#include <stdio.h>
22#include <stdarg.h>
23#include <ctype.h>
24#include <qregexp.h>
25#include <qdatastream.h>
26
27
28QCString::QCString(int size)
29{
30 if (size>0)
31 {
32 m_data = (char *)malloc(size);
33 if (m_data)
34 {
35 if (size>1) memset(m_data,' ',size-1);
36 m_data[size-1]='\0';
37 }
38 }
39 else
40 {
41 m_data=0;
42 }
43}
44
45QCString::QCString( const QCString &s )
46{
47 duplicate(s);
48}
49
50QCString::QCString( const char *str )
51{
52 duplicate(str);
53}
54
55QCString::QCString( const char *str, uint maxlen )
56{
57 uint l;
58 if (str && ( l = QMIN(qstrlen(str),maxlen) ))
59 {
60 m_data=(char *)malloc(l+1);
61 strncpy(m_data,str,l+1);
62 m_data[l]='\0';
63 }
64 else
65 {
66 m_data=0;
67 }
68}
69
70QCString::~QCString()
71{
72 if (m_data) free(m_data);
73 m_data=0;
74}
75
76QCString &QCString::assign( const char *str )
77{
78 if (m_data) free(m_data);
79 duplicate(str);
80 return *this;
81}
82
83bool QCString::resize( uint newlen )
84{
85 if (newlen==0)
86 {
87 if (m_data) { free(m_data); m_data=0; }
88 return TRUE;
89 }
90 if (m_data==0) // newlen>0
91 {
92 m_data = (char *)malloc(newlen);
93 }
94 else
95 {
96 m_data = (char *)realloc(m_data,newlen);
97 }
98 if (m_data==0) return FALSE;
99 m_data[newlen-1]='\0';
100 return TRUE;
101}
102
103bool QCString::fill( char c, int len )
104{
105 uint l=length();
106 if (len<0) len=l;
107 if ((uint)len!=l)
108 {
109 if (m_data) free(m_data);
110 if (len>0)
111 {
112 m_data=(char *)malloc(len+1);
113 if (m_data==0) return FALSE;
114 m_data[len]='\0';
115 }
116 else
117 {
118 m_data=0;
119 }
120 }
121 if (len>0)
122 {
123 uint i;
124 for (i=0;i<(uint)len;i++) m_data[i]=c;
125 }
126 return TRUE;
127}
128
129QCString &QCString::sprintf( const char *format, ... )
130{
131 va_list ap;
132 va_start( ap, format );
133 uint l = length();
134 const uint minlen=256;
135 if (l<minlen)
136 {
137 if (m_data)
138 m_data = (char *)realloc(m_data,minlen);
139 else
140 m_data = (char *)malloc(minlen);
141 }
142 vsprintf( m_data, format, ap );
143 resize( qstrlen(m_data) + 1 ); // truncate
144 va_end( ap );
145 return *this;
146}
147
148
149int QCString::find( char c, int index, bool cs ) const
150{
151 uint len = length();
152 if ( m_data==0 || (uint)index>len ) // index outside string
153 return -1;
154 register const char *d;
155 if ( cs ) // case sensitive
156 {
157 d = strchr( m_data+index, c );
158 }
159 else
160 {
161 d = m_data+index;
162 c = tolower( (uchar) c );
163 while ( *d && tolower((uchar) *d) != c )
164 d++;
165 if ( !*d && c ) // not found
166 d = 0;
167 }
168 return d ? (int)(d - m_data) : -1;
169}
170
171int QCString::find( const char *str, int index, bool cs ) const
172{
173 uint l = length();
174 if ( m_data==0 || (uint)index > l ) // index outside string
175 return -1;
176 if ( !str ) // no search string
177 return -1;
178 if ( !*str ) // zero-length search string
179 return index;
180 register const char *d;
181 if ( cs ) // case sensitive
182 {
183 d = strstr( m_data+index, str );
184 }
185 else // case insensitive
186 {
187 d = m_data+index;
188 int len = qstrlen( str );
189 while ( *d )
190 {
191 if ( qstrnicmp(d, str, len) == 0 )
192 break;
193 d++;
194 }
195 if ( !*d ) // not found
196 d = 0;
197 }
198 return d ? (int)(d - m_data) : -1;
199}
200
201int QCString::find( const QCString &str,int index,bool cs) const
202{
203 return find(str.data(),index,cs);
204}
205
206int QCString::find( const QRegExp &rx, int index ) const
207{
208 QString d = QString::fromLatin1( m_data );
209 return d.find( rx, index );
210}
211
212int QCString::findRev( char c, int index, bool cs) const
213{
214 const char *b = m_data;
215 const char *d;
216 uint len = length();
217 if ( b == 0 ) return -1; // empty string
218 if ( index < 0 ) // neg index ==> start from end
219 {
220 if ( len == 0 ) return -1;
221 if ( cs )
222 {
223 d = strrchr( b, c );
224 return d ? (int)(d - b) : -1;
225 }
226 index = len;
227 }
228 else if ( (uint)index > len ) // bad index
229 {
230 return -1;
231 }
232 d = b+index;
233 if ( cs ) // case sensitive
234 {
235 while ( d >= b && *d != c )
236 d--;
237 }
238 else // case insensitive
239 {
240 c = tolower( (uchar) c );
241 while ( d >= b && tolower((uchar) *d) != c )
242 d--;
243 }
244 return d >= b ? (int)(d - b) : -1;
245}
246
247int QCString::findRev( const char *str, int index, bool cs) const
248{
249 int slen = qstrlen(str);
250 uint len = length();
251 if ( index < 0 ) // neg index ==> start from end
252 index = len-slen;
253 else if ( (uint)index > len ) // bad index
254 return -1;
255 else if ( (uint)(index + slen) > len ) // str would be too long
256 index = len - slen;
257 if ( index < 0 )
258 return -1;
259
260 register char *d = m_data + index;
261 if ( cs ) // case sensitive
262 {
263 for ( int i=index; i>=0; i-- )
264 if ( qstrncmp(d--,str,slen)==0 )
265 return i;
266 }
267 else // case insensitive
268 {
269 for ( int i=index; i>=0; i-- )
270 if ( qstrnicmp(d--,str,slen)==0 )
271 return i;
272 }
273 return -1;
274
275}
276
277int QCString::findRev( const QRegExp &rx, int index ) const
278{
279 QString d = QString::fromLatin1( m_data );
280 return d.findRev( rx, index );
281}
282
283int QCString::contains( char c, bool cs ) const
284{
285 int count = 0;
286 char *d = m_data;
287 if ( !d )
288 return 0;
289 if ( cs ) // case sensitive
290 {
291 while ( *d )
292 if ( *d++ == c )
293 count++;
294 }
295 else // case insensitive
296 {
297 c = tolower( (uchar) c );
298 while ( *d ) {
299 if ( tolower((uchar) *d) == c )
300 count++;
301 d++;
302 }
303 }
304 return count;
305}
306
307int QCString::contains( const char *str, bool cs ) const
308{
309 int count = 0;
310 char *d = data();
311 if ( !d )
312 return 0;
313 int len = qstrlen( str );
314 while ( *d ) // counts overlapping strings
315 {
316 if ( cs )
317 {
318 if ( qstrncmp( d, str, len ) == 0 )
319 count++;
320 }
321 else
322 {
323 if ( qstrnicmp(d, str, len) == 0 )
324 count++;
325 }
326 d++;
327 }
328 return count;
329}
330
331int QCString::contains( const QRegExp &rx ) const
332{
333 QString d = QString::fromLatin1( m_data );
334 return d.contains( rx );
335}
336
337QCString QCString::left( uint len ) const
338{
339 if ( isEmpty() )
340 {
341 return QCString();
342 }
343 else if ( len >= length() )
344 {
345 return *this;
346 }
347 else
348 {
349 QCString s( len+1 );
350 strncpy( s.data(), m_data, len );
351 *(s.data()+len) = '\0';
352 return s;
353 }
354}
355
356QCString QCString::right( uint len ) const
357{
358 if ( isEmpty() )
359 {
360 return QCString();
361 }
362 else
363 {
364 uint l = length();
365 if ( len > l ) len = l;
366 char *p = m_data + (l - len);
367 return QCString( p );
368 }
369}
370
371QCString QCString::mid( uint index, uint len) const
372{
373 uint slen = length();
374 if ( len == 0xffffffff ) len = slen-index;
375 if ( isEmpty() || index >= slen )
376 {
377 return QCString();
378 }
379 else
380 {
381 register char *p = data()+index;
382 QCString s( len+1 );
383 strncpy( s.data(), p, len );
384 *(s.data()+len) = '\0';
385 return s;
386 }
387}
388
389QCString QCString::lower() const
390{
391 QCString s( m_data );
392 register char *p = s.data();
393 if ( p )
394 {
395 while ( *p )
396 {
397 *p = tolower((uchar) *p);
398 p++;
399 }
400 }
401 return s;
402}
403
404QCString QCString::upper() const
405{
406 QCString s( m_data );
407 register char *p = s.data();
408 if ( p ) {
409 while ( *p ) {
410 *p = toupper((uchar)*p);
411 p++;
412 }
413 }
414 return s;
415}
416
417QCString QCString::stripWhiteSpace()const
418{
419 if ( isEmpty() ) // nothing to do
420 return *this;
421
422 register char *s = m_data;
423 int reslen = length();
424 if ( !isspace((uchar) s[0]) && !isspace((uchar) s[reslen-1]) )
425 return *this; // returns a copy
426
427 QCString result(s);
428 s = result.data();
429 int start = 0;
430 int end = reslen - 1;
431 while ( isspace((uchar) s[start]) ) // skip white space from start
432 start++;
433 if ( s[start] == '\0' )
434 { // only white space
435 return QCString();
436 }
437 while ( end && isspace((uchar) s[end]) ) // skip white space from end
438 end--;
439 end -= start - 1;
440 memmove( result.data(), &s[start], end );
441 result.resize( end + 1 );
442 return result;
443}
444
445QCString QCString::simplifyWhiteSpace() const
446{
447 if ( isEmpty() ) // nothing to do
448 return *this;
449
450 QCString result( length()+1 );
451 char *from = data();
452 char *to = result.data();
453 char *first = to;
454 while ( TRUE )
455 {
456 while ( *from && isspace((uchar) *from) )
457 from++;
458 while ( *from && !isspace((uchar)*from) )
459 *to++ = *from++;
460 if ( *from )
461 *to++ = 0x20; // ' '
462 else
463 break;
464 }
465 if ( to > first && *(to-1) == 0x20 )
466 to--;
467 *to = '\0';
468 result.resize( (int)((long)to - (long)result.data()) + 1 );
469 return result;
470}
471
472QCString &QCString::insert( uint index, const char *s )
473{
474 int len = s ? qstrlen(s) : 0;
475 if ( len == 0 )
476 return *this;
477 uint olen = length();
478 int nlen = olen + len;
479 if ( index >= olen ) // insert after end of string
480 {
481 m_data = (char *)realloc(m_data,nlen+index-olen+1);
482 if ( m_data )
483 {
484 memset( m_data+olen, ' ', index-olen );
485 memcpy( m_data+index, s, len+1 );
486 }
487 }
488 else if ( (m_data = (char *)realloc(m_data,nlen+1)) ) // normal insert
489 {
490 memmove( m_data+index+len, m_data+index, olen-index+1 );
491 memcpy( m_data+index, s, len );
492 }
493 return *this;
494}
495
496QCString &QCString::insert( uint index, char c ) // insert char
497{
498 char buf[2];
499 buf[0] = c;
500 buf[1] = '\0';
501 return insert( index, buf );
502}
503
504QCString& QCString::operator+=( const char *str )
505{
506 if ( !str ) return *this; // nothing to append
507 uint len1 = length();
508 uint len2 = qstrlen(str);
509 char *newData = (char *)realloc( m_data, len1 + len2 + 1 );
510 if (newData)
511 {
512 m_data = newData;
513 memcpy( m_data + len1, str, len2 + 1 );
514 }
515 return *this;
516}
517
518QCString &QCString::operator+=( char c )
519{
520 uint len = length();
521 char *newData = (char *)realloc( m_data, length()+2 );
522 if (newData)
523 {
524 m_data = newData;
525 m_data[len] = c;
526 m_data[len+1] = '\0';
527 }
528 return *this;
529}
530
531QCString &QCString::remove( uint index, uint len )
532{
533 uint olen = length();
534 if ( index + len >= olen ) // range problems
535 {
536 if ( index < olen ) // index ok
537 {
538 resize( index+1 );
539 }
540 }
541 else if ( len != 0 )
542 {
543 memmove( m_data+index, m_data+index+len, olen-index-len+1 );
544 resize( olen-len+1 );
545 }
546 return *this;
547}
548
549QCString &QCString::replace( uint index, uint len, const char *s )
550{
551 remove( index, len );
552 insert( index, s );
553 return *this;
554}
555
556QCString &QCString::replace( const QRegExp &rx, const char *str )
557{
558 QString d = QString::fromLatin1( m_data );
559 QString r = QString::fromLatin1( str );
560 d.replace( rx, r );
561 operator=( d.ascii() );
562 return *this;
563}
564
565long QCString::toLong( bool *ok ) const
566{
567 QString s(m_data);
568 return s.toLong(ok);
569}
570
571ulong QCString::toULong( bool *ok ) const
572{
573 QString s(m_data);
574 return s.toULong(ok);
575}
576
577short QCString::toShort( bool *ok )const
578{
579 QString s(m_data);
580 return s.toShort(ok);
581}
582
583ushort QCString::toUShort( bool *ok ) const
584{
585 QString s(m_data);
586 return s.toUShort(ok);
587}
588
589int QCString::toInt( bool *ok ) const
590{
591 QString s(m_data);
592 return s.toInt(ok);
593}
594
595uint QCString::toUInt( bool *ok ) const
596{
597 QString s(m_data);
598 return s.toUInt(ok);
599}
600
601QCString &QCString::setNum( long n )
602{
603 char buf[20];
604 register char *p = &buf[19];
605 bool neg;
606 if ( n < 0 )
607 {
608 neg = TRUE;
609 n = -n;
610 }
611 else
612 {
613 neg = FALSE;
614 }
615 *p = '\0';
616 do
617 {
618 *--p = ((int)(n%10)) + '0';
619 n /= 10;
620 } while ( n );
621 if ( neg ) *--p = '-';
622 operator=( p );
623 return *this;
624}
625
626QCString &QCString::setNum( ulong n )
627{
628 char buf[20];
629 register char *p = &buf[19];
630 *p = '\0';
631 do
632 {
633 *--p = ((int)(n%10)) + '0';
634 n /= 10;
635 } while ( n );
636 operator=( p );
637 return *this;
638}
639
640void QCString::msg_index( uint index )
641{
642#if defined(CHECK_RANGE)
643 qWarning( "QCString::at: Absolute index %d out of range", index );
644#else
645 Q_UNUSED( index )
646#endif
647}
648
649bool QCString::stripPrefix(const char *prefix)
650{
651 if (prefix==0) return FALSE;
652 uint plen = qstrlen(prefix);
653 if (m_data && qstrncmp(prefix,m_data,plen)==0) // prefix matches
654 {
655 uint len = qstrlen(m_data);
656 uint newlen = len-plen+1;
657 qmemmove(m_data,m_data+plen,newlen);
658 resize(newlen);
659 return TRUE;
660 }
661 return FALSE;
662}
663
664//---------------------------------------------------------------------------
665
666void *qmemmove( void *dst, const void *src, uint len )
667{
668 register char *d;
669 register char *s;
670 if ( dst > src ) {
671d = (char *)dst + len - 1;
672s = (char *)src + len - 1;
673while ( len-- )
674 *d-- = *s--;
675 } else if ( dst < src ) {
676d = (char *)dst;
677s = (char *)src;
678while ( len-- )
679 *d++ = *s++;
680 }
681 return dst;
682}
683
684char *qstrdup( const char *str )
685{
686 if ( !str )
687return 0;
688 char *dst = new char[strlen(str)+1];
689 CHECK_PTR( dst );
690 return strcpy( dst, str );
691}
692
693char *qstrncpy( char *dst, const char *src, uint len )
694{
695 if ( !src )
696return 0;
697 strncpy( dst, src, len );
698 if ( len > 0 )
699dst[len-1] = '\0';
700 return dst;
701}
702
703int qstricmp( const char *str1, const char *str2 )
704{
705 register const uchar *s1 = (const uchar *)str1;
706 register const uchar *s2 = (const uchar *)str2;
707 int res;
708 uchar c;
709 if ( !s1 || !s2 )
710return s1 == s2 ? 0 : (int)((long)s2 - (long)s1);
711 for ( ; !(res = (c=tolower(*s1)) - tolower(*s2)); s1++, s2++ )
712if ( !c )// strings are equal
713 break;
714 return res;
715}
716
717int qstrnicmp( const char *str1, const char *str2, uint len )
718{
719 register const uchar *s1 = (const uchar *)str1;
720 register const uchar *s2 = (const uchar *)str2;
721 int res;
722 uchar c;
723 if ( !s1 || !s2 )
724return (int)((long)s2 - (long)s1);
725 for ( ; len--; s1++, s2++ ) {
726if ( (res = (c=tolower(*s1)) - tolower(*s2)) )
727 return res;
728if ( !c )// strings are equal
729 break;
730 }
731 return 0;
732}
733
734#ifndef QT_NO_DATASTREAM
735
736QDataStream &operator<<( QDataStream &s, const QByteArray &a )
737{
738 return s.writeBytes( a.data(), a.size() );
739}
740
741QDataStream &operator>>( QDataStream &s, QByteArray &a )
742{
743 Q_UINT32 len;
744 s >> len;// read size of array
745 if ( len == 0 || s.eof() ) {// end of file reached
746a.resize( 0 );
747return s;
748 }
749 if ( !a.resize( (uint)len ) ) {// resize array
750#if defined(CHECK_NULL)
751qWarning( "QDataStream: Not enough memory to read QByteArray" );
752#endif
753len = 0;
754 }
755 if ( len > 0 )// not null array
756s.readRawBytes( a.data(), (uint)len );
757 return s;
758}
759
760QDataStream &operator<<( QDataStream &s, const QCString &str )
761{
762 return s.writeBytes( str.data(), str.size() );
763}
764
765QDataStream &operator>>( QDataStream &s, QCString &str )
766{
767 Q_UINT32 len;
768 s >> len;// read size of string
769 if ( len == 0 || s.eof() ) {// end of file reached
770str.resize( 0 );
771return s;
772 }
773 if ( !str.resize( (uint)len )) {// resize string
774#if defined(CHECK_NULL)
775qWarning( "QDataStream: Not enough memory to read QCString" );
776#endif
777len = 0;
778 }
779 if ( len > 0 )// not null array
780s.readRawBytes( str.data(), (uint)len );
781 return s;
782}
783
784#endif //QT_NO_DATASTREAM
785
786inline QCString operator+( const QCString &s1, const QGString &s2 )
787{
788 QCString tmp(s1);
789 tmp += s2.data();
790 return tmp;
791}
792
793inline QCString operator+( const QGString &s1, const QCString &s2 )
794{
795 QCString tmp(s1.data());
796 tmp += s2;
797 return tmp;
798}
799
800

Archive Download this file

Revision: 1322