2 * CDE - Common Desktop Environment
4 * Copyright (c) 1993-2012, The Open Group. All rights reserved.
6 * These libraries and programs are free software; you can
7 * redistribute them and/or modify them under the terms of the GNU
8 * Lesser General Public License as published by the Free Software
9 * Foundation; either version 2 of the License, or (at your option)
12 * These libraries and programs are distributed in the hope that
13 * they will be useful, but WITHOUT ANY WARRANTY; without even the
14 * implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
15 * PURPOSE. See the GNU Lesser General Public License for more
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with these librararies and programs; if not, write
20 * to the Free Software Foundation, Inc., 51 Franklin Street, Fifth
21 * Floor, Boston, MA 02110-1301 USA
23 /* $XConsortium: cstring.C /main/2 1995/07/17 14:10:22 drk $ */
24 /*******************************************************************
25 ** (c) Copyright Hewlett-Packard Company, 1990, 1991, 1992, 1993.
26 ** All rights are reserved. Copying or other reproduction of this
27 ** program except for archival purposes is prohibited without prior
28 ** written consent of Hewlett-Packard Company.
29 ********************************************************************
30 ****************************<+>*************************************/
45 ) : skipWhiteSpace(ws)
48 contents = new char [strlen(s) + 1];
52 contents = new char [1];
62 ) : skipWhiteSpace(ws)
64 contents = new char [2];
79 contents = new char [s.length() + 1];
80 strcpy(contents,s.data());
81 skipWhiteSpace = s.skipWhiteSpace;
84 char & CString::operator[]
92 if (index > strlen(contents))
93 return contents[strlen(contents)-1];
95 return contents[index];
98 int CString::operator!=
103 return !(*this == s);
106 int CString::operator==
111 if (isNull() && s.isNull())
114 if (isNull() || s.isNull())
117 if (strcmp(contents,s.data()) == 0)
123 CString & CString::operator=
130 contents = new char [s.length() + 1];
131 strcpy(contents,s.data());
136 CString & CString::operator=
142 contents = new char [strlen(s) + 1];
147 CString & CString::operator+=
156 temp = new char [strlen(contents) + strlen(cs) + 1];
162 temp = new char [strlen(cs) + 1];
169 strcat(temp,contents);
180 CString & CString::operator+=
189 temp = new char [strlen(contents) + s.length() + 1];
195 temp = new char [s.length() + 1];
202 strcat(temp,contents);
204 strcat(temp,s.data());
252 CString CString::copy
261 for (int i = 0; i < strlen(delim); i++) {
263 if (q = strchr(contents,delim[i])) {
266 CString result(&contents[start]);
274 CString CString::copy
283 for (int i = 0; i < strlen(delim1); i++) {
285 if (q = strchr(contents,delim1[i])) {
286 for (int j = 0; j < strlen(delim2); j++) {
288 if (p = strchr(q+1,delim2[j])) {
303 CString CString::find
313 if (q = strstr(contents,cs))
319 int CString::contains
326 return contains(s.data(), leader, trailer);
329 int CString::contains
336 // first see if there is anything in contents
341 // or if the strings are exactly the same
343 if (strcmp(cs,data()) == 0)
346 // now check to see if it is embedded in the string
348 CString search(leader);
352 if (strstr (contents, search.data()))
355 // if skipWhiteSpace is on then check for spaces on one or
356 // both sides of the target in the source.
358 if (skipWhiteSpace) {
360 // first, a space in front, delimiter on the end
365 if (strstr (contents, search.data()))
368 // second, a delimiter on the front, space on the end
373 if (strstr (contents, search.data()))
376 // third, spaces before and after
381 if (strstr (contents, search.data()))
385 // now check to see if it is the start of the string
389 if (strncmp (contents, search.data(), search.length()) == 0)
392 // now check to see if it is the start of the string followed
397 if (strncmp (contents, search.data(), search.length()) == 0)
400 // last, check to see if it is on the end of the string using
401 // the delimiter and a space
408 while (q = strstr(p, search.data())) {
409 r = q + strlen(leader);
412 if (r && strcmp(r,cs) == 0)
415 if (search[0] == ' ')
424 void CString::replace
426 const CString & to_be_replaced,
427 const CString & replacee
435 if (q = strstr(contents,to_be_replaced.data())) {
437 char * prefix = new char [strlen(contents) + 1];
438 strcpy(prefix,contents);
439 q += to_be_replaced.length();
440 char * suffix = new char [strlen(q) + 1];
443 contents = new char [strlen(prefix) +
446 strcpy(contents,prefix);
447 strcat(contents,replacee.data());
448 strcat(contents,suffix);
454 int CString::isNull() const
459 if (strlen(contents) == 0)
466 CTokenizedString::CTokenizedString()
467 : CString(""), finished(0), cursor(0), delimiter(0)
471 CTokenizedString::CTokenizedString
473 const CTokenizedString & s
477 cursor = new char [strlen(s.cursor) + 1];
478 strcpy(cursor,s.cursor);
479 delimiter = new char [strlen(s.delimiter) + 1];
480 strcpy(delimiter,s.delimiter);
481 finished = s.finished;
482 skipWhiteSpace = s.skipWhiteSpace;
483 contents = new char [strlen(s.contents) + 1];
484 strcpy(contents,s.contents);
488 CTokenizedString::CTokenizedString
493 ) : CString(s), finished(0)
495 cursor = new char [s.length() + 1];
496 strcpy(cursor,s.data());
497 delimiter = new char [strlen(d) + 1];
503 CTokenizedString::~CTokenizedString()
510 CString CTokenizedString::next()
517 if (strlen(delimiter) == 1)
518 q = strchr(cursor,delimiter[0]);
520 for (i = 0; i < strlen(cursor); i++)
521 if (strchr(delimiter,cursor[i])) {
530 // a delimiter has been found. Create the return token,
535 // eliminate trailing white space
537 if (skipWhiteSpace) {
538 for (p = q; isspace(*(p-1)); p--);
541 CString result(cursor);
543 // eliminate leading white space
546 for (; isspace(*(q+1)); q++);
548 char * temp = new char [strlen(q+1) + 1];
556 // no delimiter found. Return what is left of the string
558 CString result(cursor);
569 #if defined(linux) || defined(CSRG_BASED) || defined(sun)
570 std::ostream & operator<<
584 return os << "(null)";
586 return os << s.data();