|
|
1.1 root 1: // This is a part of the Microsoft Foundation Classes C++ library.
2: // Copyright (C) 1992 Microsoft Corporation
3: // All rights reserved.
4: //
5: // This source code is only intended as a supplement to the
6: // Microsoft Foundation Classes Reference and Microsoft
7: // QuickHelp documentation provided with the library.
8: // See these sources for detailed information regarding the
9: // Microsoft Foundation Classes product.
10:
11:
12: #include "afx.h"
13: #pragma hdrstop
14: #include <limits.h>
15:
16: #ifdef AFX_AUX_SEG
17: #pragma code_seg(AFX_AUX_SEG)
18: #endif
19:
20: #ifdef _DEBUG
21: #undef THIS_FILE
22: static char BASED_CODE THIS_FILE[] = __FILE__;
23: #endif
24:
25: #define new DEBUG_NEW
26:
27: extern char _afxChNil;
28:
29: //////////////////////////////////////////////////////////////////////////////
30: // More sophisticated construction
31:
32: CString::CString(char ch, int nRepeat)
33: {
34: if (nRepeat < 1)
35: // return empty string if invalid repeat count
36: Init();
37: else
38: {
39: AllocBuffer(nRepeat);
40: memset(m_pchData, ch, nRepeat);
41: }
42: }
43:
44:
45: CString::CString(const char* pch, int nLen)
46: {
47: if (nLen == 0)
48: Init();
49: else
50: {
51: AllocBuffer(nLen);
52: memcpy(m_pchData, pch, nLen);
53: }
54: }
55:
56: //////////////////////////////////////////////////////////////////////////////
57: // Additional constructors for far string data
58:
59: #ifdef _NEARDATA
60:
61: CString::CString(const char FAR* lpsz)
62: {
63: int nLen;
64: if (lpsz == NULL || (nLen = _fstrlen(lpsz)) == 0)
65: Init();
66: else
67: {
68: AllocBuffer(nLen);
69: _fmemcpy(m_pchData, lpsz, nLen);
70: }
71: }
72:
73: CString::CString(const char FAR* lpch, int nLen)
74: {
75: if (nLen == 0)
76: Init();
77: else
78: {
79: AllocBuffer(nLen);
80: _fmemcpy(m_pchData, lpch, nLen);
81: }
82: }
83:
84: #endif // need far overloads
85:
86: //////////////////////////////////////////////////////////////////////////////
87: // Assignment operators
88: const CString&
89: CString::operator =(char ch)
90: {
91: AssignCopy(1, &ch);
92: return *this;
93: }
94:
95:
96:
97: //////////////////////////////////////////////////////////////////////////////
98: // concatenation
99: const CString&
100: CString::operator +=(char ch)
101: {
102: ConcatInPlace(1, &ch);
103: return *this;
104: }
105:
106:
107: CString
108: operator +(const CString& string1, char ch)
109: {
110: CString s;
111: s.ConcatCopy(string1.m_nDataLength, string1.m_pchData, 1, &ch);
112: return s;
113: }
114:
115:
116: CString
117: operator +(char ch, const CString& string)
118: {
119: CString s;
120: s.ConcatCopy(1, &ch, string.m_nDataLength, string.m_pchData);
121: return s;
122: }
123:
124:
125: //////////////////////////////////////////////////////////////////////////////
126: // Very simple sub-string extraction
127:
128: CString
129: CString::Mid(int nFirst) const
130: {
131: return Mid(nFirst, m_nDataLength - nFirst);
132: }
133:
134: CString
135: CString::Mid(int nFirst, int nCount) const
136: {
137: ASSERT(nFirst >= 0);
138: ASSERT(nCount >= 0);
139:
140: // out-of-bounds requests return sensible things
141: if (nFirst + nCount > m_nDataLength)
142: nCount = m_nDataLength - nFirst;
143: if (nFirst > m_nDataLength)
144: nCount = 0;
145:
146: CString dest;
147: AllocCopy(dest, nCount, nFirst, 0);
148: return dest;
149: }
150:
151: CString
152: CString::Right(int nCount) const
153: {
154: ASSERT(nCount >= 0);
155:
156: if (nCount > m_nDataLength)
157: nCount = m_nDataLength;
158:
159: CString dest;
160: AllocCopy(dest, nCount, m_nDataLength-nCount, 0);
161: return dest;
162: }
163:
164: CString
165: CString::Left(int nCount) const
166: {
167: ASSERT(nCount >= 0);
168:
169: if (nCount > m_nDataLength)
170: nCount = m_nDataLength;
171:
172: CString dest;
173: AllocCopy(dest, nCount, 0, 0);
174: return dest;
175: }
176:
177: CString
178: CString::SpanIncluding(const char* pszCharSet) const
179: {
180: // strspn equivalent
181: return Left(strspn(m_pchData, pszCharSet));
182: }
183:
184:
185: CString
186: CString::SpanExcluding(const char* pszCharSet) const
187: {
188: // strcspn equivalent
189: return Left(strcspn(m_pchData, pszCharSet));
190: }
191:
192: //////////////////////////////////////////////////////////////////////////////
193: // Finding
194:
195: int CString::Find(char ch) const
196: {
197: // find a single character (strchr)
198:
199: register char* psz;
200: psz = (char*) strchr(m_pchData, ch);
201: return (psz == NULL) ? -1 : psz - m_pchData;
202: }
203:
204:
205: int CString::ReverseFind(char ch) const
206: {
207: // find a single character (start backwards, strrchr)
208:
209: register char* psz;
210: psz = (char*) strrchr(m_pchData, ch);
211: return (psz == NULL) ? -1 : psz - m_pchData;
212: }
213:
214:
215: int CString::FindOneOf(const char* pszCharSet) const
216: {
217: // like single character find, but look for any of the characters
218: // in the string "pszCharSet", like strpbrk
219:
220: char* psz = (char*) strpbrk(m_pchData, pszCharSet);
221: return (psz == NULL) ? -1 : (psz-m_pchData);
222: }
223:
224:
225: int CString::Find(const char* pszSub) const
226: {
227: // find a sub-string (like strstr)
228:
229: char* psz = (char*) strstr(m_pchData, pszSub);
230: return (psz == NULL) ? -1 : (psz-m_pchData);
231: }
232:
233: ///////////////////////////////////////////////////////////////////////////////
234: // Advanced access
235:
236: char* CString::GetBuffer(int nMinBufLength)
237: {
238: if (nMinBufLength > m_nAllocLength)
239: {
240: // we have to grow the buffer
241: char* pszOldData = m_pchData;
242: int nOldLen = m_nDataLength; // AllocBuffer will tromp it
243:
244: AllocBuffer(nMinBufLength);
245: memcpy(m_pchData, pszOldData, nOldLen);
246: m_nDataLength = nOldLen;
247: m_pchData[m_nDataLength] = '\0';
248:
249: ASSERT(pszOldData != NULL);
250: if (pszOldData != &_afxChNil)
251: delete [] pszOldData;
252: }
253:
254: // return a pointer to the character storage for this string
255: return m_pchData;
256: }
257:
258: void CString::ReleaseBuffer(int nNewLength)
259: {
260: if (nNewLength == -1)
261: nNewLength = strlen(m_pchData); // zero terminated
262:
263: ASSERT(nNewLength <= m_nAllocLength);
264: m_nDataLength = nNewLength;
265: m_pchData[m_nDataLength] = '\0';
266: }
267:
268: char* CString::GetBufferSetLength(int nNewLength)
269: {
270: GetBuffer(nNewLength);
271: m_nDataLength = nNewLength;
272: m_pchData[m_nDataLength] = '\0';
273: return m_pchData;
274: }
275:
276: ///////////////////////////////////////////////////////////////////////////////
277:
This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.