17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * CDDL HEADER START
37c478bd9Sstevel@tonic-gate  *
47c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
57c478bd9Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
67c478bd9Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
77c478bd9Sstevel@tonic-gate  * with the License.
87c478bd9Sstevel@tonic-gate  *
97c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
107c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
117c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
127c478bd9Sstevel@tonic-gate  * and limitations under the License.
137c478bd9Sstevel@tonic-gate  *
147c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
157c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
167c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
177c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
187c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
197c478bd9Sstevel@tonic-gate  *
207c478bd9Sstevel@tonic-gate  * CDDL HEADER END
217c478bd9Sstevel@tonic-gate  */
227c478bd9Sstevel@tonic-gate /*
237c478bd9Sstevel@tonic-gate  * Copyright (c) 2000 by Sun Microsystems, Inc.
247c478bd9Sstevel@tonic-gate  * All rights reserved.
257c478bd9Sstevel@tonic-gate  */
267c478bd9Sstevel@tonic-gate 
277c478bd9Sstevel@tonic-gate #include <string.h>
287c478bd9Sstevel@tonic-gate #include <stdio.h>
297c478bd9Sstevel@tonic-gate 
307c478bd9Sstevel@tonic-gate #include "Str.h"
317c478bd9Sstevel@tonic-gate 
Str()327c478bd9Sstevel@tonic-gate Str::Str()
337c478bd9Sstevel@tonic-gate 	: str_(strcpy(new char[strlen("")+1], "")),
347c478bd9Sstevel@tonic-gate     nextTok_(str_)
357c478bd9Sstevel@tonic-gate {}
367c478bd9Sstevel@tonic-gate 
Str(const char * str)377c478bd9Sstevel@tonic-gate Str::Str(const char *str)
387c478bd9Sstevel@tonic-gate 	: str_(strcpy(new char[strlen(str)+1], str)),
397c478bd9Sstevel@tonic-gate     nextTok_(str_)
407c478bd9Sstevel@tonic-gate {}
417c478bd9Sstevel@tonic-gate 
Str(const char * str,int len)427c478bd9Sstevel@tonic-gate Str::Str(const char *str, int len)
437c478bd9Sstevel@tonic-gate 	: str_(new char[len+1]),
447c478bd9Sstevel@tonic-gate     nextTok_(str_)
457c478bd9Sstevel@tonic-gate {
467c478bd9Sstevel@tonic-gate 	strlcpy(str_, str, len+1);
477c478bd9Sstevel@tonic-gate }
487c478bd9Sstevel@tonic-gate 
Str(const Str & rhs)497c478bd9Sstevel@tonic-gate Str::Str(const Str& rhs)
507c478bd9Sstevel@tonic-gate 	: str_(strcpy(new char[strlen(rhs.str_)+1], rhs.str_)),
517c478bd9Sstevel@tonic-gate     nextTok_(str_)
527c478bd9Sstevel@tonic-gate {}
537c478bd9Sstevel@tonic-gate 
~Str()547c478bd9Sstevel@tonic-gate Str::~Str()
557c478bd9Sstevel@tonic-gate {
567c478bd9Sstevel@tonic-gate 	delete[] str_;
577c478bd9Sstevel@tonic-gate }
587c478bd9Sstevel@tonic-gate 
597c478bd9Sstevel@tonic-gate void
operator =(const Str & rhs)607c478bd9Sstevel@tonic-gate Str::operator = (const Str& rhs)
617c478bd9Sstevel@tonic-gate {
627c478bd9Sstevel@tonic-gate 	delete[] str_;
637c478bd9Sstevel@tonic-gate 	str_ = strcpy(new char[strlen(rhs.str_)+1], rhs.str_);
647c478bd9Sstevel@tonic-gate 	// pointer arithmetic very BAD I know...
657c478bd9Sstevel@tonic-gate 	nextTok_ = str_ + (rhs.nextTok_ - rhs.str_);
667c478bd9Sstevel@tonic-gate }
677c478bd9Sstevel@tonic-gate 
687c478bd9Sstevel@tonic-gate void
operator =(const char * str)697c478bd9Sstevel@tonic-gate Str::operator = (const char *str)
707c478bd9Sstevel@tonic-gate {
717c478bd9Sstevel@tonic-gate 	delete[] str_;
727c478bd9Sstevel@tonic-gate 	str_ = strcpy(new char[strlen(str)+1], str);
737c478bd9Sstevel@tonic-gate 	nextTok_ = str_;
747c478bd9Sstevel@tonic-gate }
757c478bd9Sstevel@tonic-gate 
767c478bd9Sstevel@tonic-gate int
operator ==(const Str & rhs) const777c478bd9Sstevel@tonic-gate Str::operator == (const Str& rhs) const
787c478bd9Sstevel@tonic-gate {
797c478bd9Sstevel@tonic-gate 	return (strcmp(str_, rhs.str_) == 0);
807c478bd9Sstevel@tonic-gate }
817c478bd9Sstevel@tonic-gate 
827c478bd9Sstevel@tonic-gate int
operator !=(const Str & rhs) const837c478bd9Sstevel@tonic-gate Str::operator != (const Str& rhs) const
847c478bd9Sstevel@tonic-gate {
857c478bd9Sstevel@tonic-gate 	return (strcmp(str_, rhs.str_) != 0);
867c478bd9Sstevel@tonic-gate }
877c478bd9Sstevel@tonic-gate 
887c478bd9Sstevel@tonic-gate char&
operator [](int index) const897c478bd9Sstevel@tonic-gate Str::operator[](int index) const
907c478bd9Sstevel@tonic-gate {
917c478bd9Sstevel@tonic-gate 	return (str_[index]);
927c478bd9Sstevel@tonic-gate }
937c478bd9Sstevel@tonic-gate 
947c478bd9Sstevel@tonic-gate Str&
operator <<(Str rhs)957c478bd9Sstevel@tonic-gate Str::operator<<(Str rhs)
967c478bd9Sstevel@tonic-gate {
977c478bd9Sstevel@tonic-gate 	char *tmp = new char[strlen(str_)+strlen(rhs.peak())+1];
987c478bd9Sstevel@tonic-gate 	strcpy(tmp, str_);
997c478bd9Sstevel@tonic-gate 	delete[] str_;
1007c478bd9Sstevel@tonic-gate 	str_ = tmp;
1017c478bd9Sstevel@tonic-gate 	strcat(str_, rhs.peak());
1027c478bd9Sstevel@tonic-gate 	return (*this);
1037c478bd9Sstevel@tonic-gate }
1047c478bd9Sstevel@tonic-gate 
1057c478bd9Sstevel@tonic-gate Str&
operator <<(long long i)1067c478bd9Sstevel@tonic-gate Str::operator<<(long long i)
1077c478bd9Sstevel@tonic-gate {
1087c478bd9Sstevel@tonic-gate 	char msg[256];
1097c478bd9Sstevel@tonic-gate 	sprintf(msg, "%lld", i);
1107c478bd9Sstevel@tonic-gate 	return (*this << msg);
1117c478bd9Sstevel@tonic-gate }
1127c478bd9Sstevel@tonic-gate 
1137c478bd9Sstevel@tonic-gate Str&
operator <<(long i)1147c478bd9Sstevel@tonic-gate Str::operator<<(long i)
1157c478bd9Sstevel@tonic-gate {
1167c478bd9Sstevel@tonic-gate 	char msg[256];
1177c478bd9Sstevel@tonic-gate 	sprintf(msg, "%ld", i);
1187c478bd9Sstevel@tonic-gate 	return (*this << msg);
1197c478bd9Sstevel@tonic-gate }
1207c478bd9Sstevel@tonic-gate 
1217c478bd9Sstevel@tonic-gate Str&
operator <<(int i)1227c478bd9Sstevel@tonic-gate Str::operator<<(int i)
1237c478bd9Sstevel@tonic-gate {
1247c478bd9Sstevel@tonic-gate 	char msg[256];
1257c478bd9Sstevel@tonic-gate 	sprintf(msg, "%d", i);
1267c478bd9Sstevel@tonic-gate 	return (*this << msg);
1277c478bd9Sstevel@tonic-gate }
1287c478bd9Sstevel@tonic-gate 
1297c478bd9Sstevel@tonic-gate Str&
operator <<(char c)1307c478bd9Sstevel@tonic-gate Str::operator<<(char c)
1317c478bd9Sstevel@tonic-gate {
1327c478bd9Sstevel@tonic-gate 	char msg[256];
1337c478bd9Sstevel@tonic-gate 	sprintf(msg, "%c", c);
1347c478bd9Sstevel@tonic-gate 	return (*this << msg);
1357c478bd9Sstevel@tonic-gate }
1367c478bd9Sstevel@tonic-gate 
1377c478bd9Sstevel@tonic-gate // normal "C" strcmp
1387c478bd9Sstevel@tonic-gate int
compare(const Str & rhs) const1397c478bd9Sstevel@tonic-gate Str::compare(const Str& rhs) const
1407c478bd9Sstevel@tonic-gate {
1417c478bd9Sstevel@tonic-gate 	return (strcmp(str_, rhs.str_));
1427c478bd9Sstevel@tonic-gate }
1437c478bd9Sstevel@tonic-gate 
1447c478bd9Sstevel@tonic-gate int
length(void) const1457c478bd9Sstevel@tonic-gate Str::length(void) const
1467c478bd9Sstevel@tonic-gate {
1477c478bd9Sstevel@tonic-gate 	return (strlen(str_));
1487c478bd9Sstevel@tonic-gate }
1497c478bd9Sstevel@tonic-gate 
1507c478bd9Sstevel@tonic-gate char
tokenize(Str & token,const Str & separators,Str & remainder)1517c478bd9Sstevel@tonic-gate Str::tokenize(Str& token, const Str& separators, Str& remainder)
1527c478bd9Sstevel@tonic-gate {
1537c478bd9Sstevel@tonic-gate 	int i = 0;
1547c478bd9Sstevel@tonic-gate 	int j = 0;
1557c478bd9Sstevel@tonic-gate 	for (i = 0; nextTok_[i] != '\0'; i++) {
1567c478bd9Sstevel@tonic-gate 		for (j = 0; j < separators.length(); j++) {
1577c478bd9Sstevel@tonic-gate 			if (nextTok_[i] == separators[j]) {
1587c478bd9Sstevel@tonic-gate 				Str rc(nextTok_, i);
1597c478bd9Sstevel@tonic-gate 				token = rc;
1607c478bd9Sstevel@tonic-gate 				nextTok_ = &(nextTok_[i+1]);
1617c478bd9Sstevel@tonic-gate 				// Str remain(nextTok_);
1627c478bd9Sstevel@tonic-gate 				remainder = nextTok_;
1637c478bd9Sstevel@tonic-gate 				return (separators[j]);
1647c478bd9Sstevel@tonic-gate 			}
1657c478bd9Sstevel@tonic-gate 		}
1667c478bd9Sstevel@tonic-gate 	}
1677c478bd9Sstevel@tonic-gate 
1687c478bd9Sstevel@tonic-gate 	token = "";
1697c478bd9Sstevel@tonic-gate 	remainder = nextTok_;
1707c478bd9Sstevel@tonic-gate 	// remainder = *this;
1717c478bd9Sstevel@tonic-gate 	// did not find it!
172*132157d7SToomas Soome 	return ('\0');
1737c478bd9Sstevel@tonic-gate }
1747c478bd9Sstevel@tonic-gate 
1757c478bd9Sstevel@tonic-gate void
resetToken(void)1767c478bd9Sstevel@tonic-gate Str::resetToken(void)
1777c478bd9Sstevel@tonic-gate {
1787c478bd9Sstevel@tonic-gate 	nextTok_ = str_;
1797c478bd9Sstevel@tonic-gate }
1807c478bd9Sstevel@tonic-gate 
1817c478bd9Sstevel@tonic-gate const char *
peak(void) const1827c478bd9Sstevel@tonic-gate Str::peak(void) const
1837c478bd9Sstevel@tonic-gate {
1847c478bd9Sstevel@tonic-gate 	return (str_);
1857c478bd9Sstevel@tonic-gate }
1867c478bd9Sstevel@tonic-gate 
1877c478bd9Sstevel@tonic-gate void
replaceAll(char c,char newc)1887c478bd9Sstevel@tonic-gate Str::replaceAll(char c, char newc)
1897c478bd9Sstevel@tonic-gate {
1907c478bd9Sstevel@tonic-gate 	for (int i = 0; i < strlen(str_); i++) {
1917c478bd9Sstevel@tonic-gate 		if (str_[i] == c) {
1927c478bd9Sstevel@tonic-gate 			str_[i] = newc;
1937c478bd9Sstevel@tonic-gate 		}
1947c478bd9Sstevel@tonic-gate 	}
1957c478bd9Sstevel@tonic-gate }
1967c478bd9Sstevel@tonic-gate // oh look an extra line!!!
197