1 | /***
|
---|
2 | This file belongs to the Gotcha! distribution.
|
---|
3 | Copyright (C) 1998-2002 Thorsten Thielen <thth@c2226.de>
|
---|
4 |
|
---|
5 | This program is free software; you can redistribute it and/or modify
|
---|
6 | it under the terms of the GNU General Public License as published by
|
---|
7 | the Free Software Foundation; either version 2 of the License, or
|
---|
8 | (at your option) any later version.
|
---|
9 |
|
---|
10 | This program is distributed in the hope that it will be useful,
|
---|
11 | but WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
---|
13 | GNU General Public License for more details.
|
---|
14 |
|
---|
15 | You should have received a copy of the GNU General Public License
|
---|
16 | along with this program; if not, write to the Free Software
|
---|
17 | Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
---|
18 | ***/
|
---|
19 |
|
---|
20 | #include "thth_string.h"
|
---|
21 |
|
---|
22 | PSZ ththString :: replace( PSZ _pszSearch, PSZ _pszReplace )
|
---|
23 | {
|
---|
24 | if( !_pszSearch || !_pszReplace )
|
---|
25 | return getPSZ();
|
---|
26 |
|
---|
27 | // Count (non-overlapping) instances of pattern in string.
|
---|
28 | PSZ pszTmp = psz;
|
---|
29 | ULONG c = 0;
|
---|
30 | while( ( pszTmp = strstr( pszTmp, _pszReplace ) ) ) {
|
---|
31 | c++;
|
---|
32 | }
|
---|
33 |
|
---|
34 | // See how many additional bytes we need (or how many we can throw away)
|
---|
35 | // and allocate a new buffer.
|
---|
36 | LONG cb = c * ( strlen( _pszReplace ) - strlen( _pszSearch ) );
|
---|
37 | PSZ pszNew = new char[ strlen( getPSZ() ) + 1 + cb ];
|
---|
38 |
|
---|
39 | // Replace and write into new buffer.
|
---|
40 | pszTmp = psz;
|
---|
41 | PSZ pszWrite = pszNew;
|
---|
42 | PSZ pszOccurence;
|
---|
43 | while( ( pszOccurence = strstr( pszTmp, _pszReplace ) ) ) {
|
---|
44 | // Write part of old string before pattern.
|
---|
45 | cb = pszOccurence - pszTmp;
|
---|
46 | strncpy( pszWrite, pszTmp, cb );
|
---|
47 | pszWrite += cb;
|
---|
48 | // Write replace pattern.
|
---|
49 | cb = strlen( _pszReplace );
|
---|
50 | strncat( pszWrite, _pszReplace, cb );
|
---|
51 | pszWrite += cb;
|
---|
52 | // Search on.
|
---|
53 | pszTmp = pszOccurence + strlen( _pszSearch );
|
---|
54 | }
|
---|
55 |
|
---|
56 | // Replace psz instance.
|
---|
57 | PSZ pszOld = psz;
|
---|
58 | psz = pszNew;
|
---|
59 | delete pszOld;
|
---|
60 |
|
---|
61 | return getPSZ();
|
---|
62 | }
|
---|
63 |
|
---|
64 | PSZ ththString :: replace( PSZ _pszSearch, LONG _lReplace )
|
---|
65 | {
|
---|
66 | int cb = strlen( _pszSearch );
|
---|
67 | PSZ pszTmp = new char[ cb + 1 ];
|
---|
68 | sprintf( pszTmp, "%0*ld", cb, _lReplace );
|
---|
69 |
|
---|
70 | replace( _pszSearch, pszTmp );
|
---|
71 |
|
---|
72 | delete pszTmp;
|
---|
73 |
|
---|
74 | return getPSZ();
|
---|
75 | }
|
---|
76 |
|
---|
77 | PSZ ththString :: contains( PSZ _pszSearch )
|
---|
78 | {
|
---|
79 | return strstr( getPSZ(), _pszSearch );
|
---|
80 | }
|
---|
81 |
|
---|