1*0Sstevel@tonic-gate /*
2*0Sstevel@tonic-gate * CDDL HEADER START
3*0Sstevel@tonic-gate *
4*0Sstevel@tonic-gate * The contents of this file are subject to the terms of the
5*0Sstevel@tonic-gate * Common Development and Distribution License, Version 1.0 only
6*0Sstevel@tonic-gate * (the "License"). You may not use this file except in compliance
7*0Sstevel@tonic-gate * with the License.
8*0Sstevel@tonic-gate *
9*0Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10*0Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing.
11*0Sstevel@tonic-gate * See the License for the specific language governing permissions
12*0Sstevel@tonic-gate * and limitations under the License.
13*0Sstevel@tonic-gate *
14*0Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each
15*0Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16*0Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the
17*0Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying
18*0Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner]
19*0Sstevel@tonic-gate *
20*0Sstevel@tonic-gate * CDDL HEADER END
21*0Sstevel@tonic-gate */
22*0Sstevel@tonic-gate /*
23*0Sstevel@tonic-gate * Copyright (c) 2000 by Sun Microsystems, Inc.
24*0Sstevel@tonic-gate * All rights reserved.
25*0Sstevel@tonic-gate */
26*0Sstevel@tonic-gate
27*0Sstevel@tonic-gate #pragma ident "%Z%%M% %I% %E% SMI"
28*0Sstevel@tonic-gate
29*0Sstevel@tonic-gate #include <string.h>
30*0Sstevel@tonic-gate #include <stdio.h>
31*0Sstevel@tonic-gate
32*0Sstevel@tonic-gate #include "Str.h"
33*0Sstevel@tonic-gate
Str()34*0Sstevel@tonic-gate Str::Str()
35*0Sstevel@tonic-gate : str_(strcpy(new char[strlen("")+1], "")),
36*0Sstevel@tonic-gate nextTok_(str_)
37*0Sstevel@tonic-gate {}
38*0Sstevel@tonic-gate
Str(const char * str)39*0Sstevel@tonic-gate Str::Str(const char *str)
40*0Sstevel@tonic-gate : str_(strcpy(new char[strlen(str)+1], str)),
41*0Sstevel@tonic-gate nextTok_(str_)
42*0Sstevel@tonic-gate {}
43*0Sstevel@tonic-gate
Str(const char * str,int len)44*0Sstevel@tonic-gate Str::Str(const char *str, int len)
45*0Sstevel@tonic-gate : str_(new char[len+1]),
46*0Sstevel@tonic-gate nextTok_(str_)
47*0Sstevel@tonic-gate {
48*0Sstevel@tonic-gate strlcpy(str_, str, len+1);
49*0Sstevel@tonic-gate }
50*0Sstevel@tonic-gate
Str(const Str & rhs)51*0Sstevel@tonic-gate Str::Str(const Str& rhs)
52*0Sstevel@tonic-gate : str_(strcpy(new char[strlen(rhs.str_)+1], rhs.str_)),
53*0Sstevel@tonic-gate nextTok_(str_)
54*0Sstevel@tonic-gate {}
55*0Sstevel@tonic-gate
~Str()56*0Sstevel@tonic-gate Str::~Str()
57*0Sstevel@tonic-gate {
58*0Sstevel@tonic-gate delete[] str_;
59*0Sstevel@tonic-gate }
60*0Sstevel@tonic-gate
61*0Sstevel@tonic-gate void
operator =(const Str & rhs)62*0Sstevel@tonic-gate Str::operator = (const Str& rhs)
63*0Sstevel@tonic-gate {
64*0Sstevel@tonic-gate delete[] str_;
65*0Sstevel@tonic-gate str_ = strcpy(new char[strlen(rhs.str_)+1], rhs.str_);
66*0Sstevel@tonic-gate // pointer arithmetic very BAD I know...
67*0Sstevel@tonic-gate nextTok_ = str_ + (rhs.nextTok_ - rhs.str_);
68*0Sstevel@tonic-gate }
69*0Sstevel@tonic-gate
70*0Sstevel@tonic-gate void
operator =(const char * str)71*0Sstevel@tonic-gate Str::operator = (const char *str)
72*0Sstevel@tonic-gate {
73*0Sstevel@tonic-gate delete[] str_;
74*0Sstevel@tonic-gate str_ = strcpy(new char[strlen(str)+1], str);
75*0Sstevel@tonic-gate nextTok_ = str_;
76*0Sstevel@tonic-gate }
77*0Sstevel@tonic-gate
78*0Sstevel@tonic-gate int
operator ==(const Str & rhs) const79*0Sstevel@tonic-gate Str::operator == (const Str& rhs) const
80*0Sstevel@tonic-gate {
81*0Sstevel@tonic-gate return (strcmp(str_, rhs.str_) == 0);
82*0Sstevel@tonic-gate }
83*0Sstevel@tonic-gate
84*0Sstevel@tonic-gate int
operator !=(const Str & rhs) const85*0Sstevel@tonic-gate Str::operator != (const Str& rhs) const
86*0Sstevel@tonic-gate {
87*0Sstevel@tonic-gate return (strcmp(str_, rhs.str_) != 0);
88*0Sstevel@tonic-gate }
89*0Sstevel@tonic-gate
90*0Sstevel@tonic-gate char&
operator [](int index) const91*0Sstevel@tonic-gate Str::operator[](int index) const
92*0Sstevel@tonic-gate {
93*0Sstevel@tonic-gate return (str_[index]);
94*0Sstevel@tonic-gate }
95*0Sstevel@tonic-gate
96*0Sstevel@tonic-gate Str&
operator <<(Str rhs)97*0Sstevel@tonic-gate Str::operator<<(Str rhs)
98*0Sstevel@tonic-gate {
99*0Sstevel@tonic-gate char *tmp = new char[strlen(str_)+strlen(rhs.peak())+1];
100*0Sstevel@tonic-gate strcpy(tmp, str_);
101*0Sstevel@tonic-gate delete[] str_;
102*0Sstevel@tonic-gate str_ = tmp;
103*0Sstevel@tonic-gate strcat(str_, rhs.peak());
104*0Sstevel@tonic-gate return (*this);
105*0Sstevel@tonic-gate }
106*0Sstevel@tonic-gate
107*0Sstevel@tonic-gate Str&
operator <<(long long i)108*0Sstevel@tonic-gate Str::operator<<(long long i)
109*0Sstevel@tonic-gate {
110*0Sstevel@tonic-gate char msg[256];
111*0Sstevel@tonic-gate sprintf(msg, "%lld", i);
112*0Sstevel@tonic-gate return (*this << msg);
113*0Sstevel@tonic-gate }
114*0Sstevel@tonic-gate
115*0Sstevel@tonic-gate Str&
operator <<(long i)116*0Sstevel@tonic-gate Str::operator<<(long i)
117*0Sstevel@tonic-gate {
118*0Sstevel@tonic-gate char msg[256];
119*0Sstevel@tonic-gate sprintf(msg, "%ld", i);
120*0Sstevel@tonic-gate return (*this << msg);
121*0Sstevel@tonic-gate }
122*0Sstevel@tonic-gate
123*0Sstevel@tonic-gate Str&
operator <<(int i)124*0Sstevel@tonic-gate Str::operator<<(int i)
125*0Sstevel@tonic-gate {
126*0Sstevel@tonic-gate char msg[256];
127*0Sstevel@tonic-gate sprintf(msg, "%d", i);
128*0Sstevel@tonic-gate return (*this << msg);
129*0Sstevel@tonic-gate }
130*0Sstevel@tonic-gate
131*0Sstevel@tonic-gate Str&
operator <<(char c)132*0Sstevel@tonic-gate Str::operator<<(char c)
133*0Sstevel@tonic-gate {
134*0Sstevel@tonic-gate char msg[256];
135*0Sstevel@tonic-gate sprintf(msg, "%c", c);
136*0Sstevel@tonic-gate return (*this << msg);
137*0Sstevel@tonic-gate }
138*0Sstevel@tonic-gate
139*0Sstevel@tonic-gate // normal "C" strcmp
140*0Sstevel@tonic-gate int
compare(const Str & rhs) const141*0Sstevel@tonic-gate Str::compare(const Str& rhs) const
142*0Sstevel@tonic-gate {
143*0Sstevel@tonic-gate return (strcmp(str_, rhs.str_));
144*0Sstevel@tonic-gate }
145*0Sstevel@tonic-gate
146*0Sstevel@tonic-gate int
length(void) const147*0Sstevel@tonic-gate Str::length(void) const
148*0Sstevel@tonic-gate {
149*0Sstevel@tonic-gate return (strlen(str_));
150*0Sstevel@tonic-gate }
151*0Sstevel@tonic-gate
152*0Sstevel@tonic-gate char
tokenize(Str & token,const Str & separators,Str & remainder)153*0Sstevel@tonic-gate Str::tokenize(Str& token, const Str& separators, Str& remainder)
154*0Sstevel@tonic-gate {
155*0Sstevel@tonic-gate int i = 0;
156*0Sstevel@tonic-gate int j = 0;
157*0Sstevel@tonic-gate for (i = 0; nextTok_[i] != '\0'; i++) {
158*0Sstevel@tonic-gate for (j = 0; j < separators.length(); j++) {
159*0Sstevel@tonic-gate if (nextTok_[i] == separators[j]) {
160*0Sstevel@tonic-gate Str rc(nextTok_, i);
161*0Sstevel@tonic-gate token = rc;
162*0Sstevel@tonic-gate nextTok_ = &(nextTok_[i+1]);
163*0Sstevel@tonic-gate // Str remain(nextTok_);
164*0Sstevel@tonic-gate remainder = nextTok_;
165*0Sstevel@tonic-gate return (separators[j]);
166*0Sstevel@tonic-gate }
167*0Sstevel@tonic-gate }
168*0Sstevel@tonic-gate }
169*0Sstevel@tonic-gate
170*0Sstevel@tonic-gate token = "";
171*0Sstevel@tonic-gate remainder = nextTok_;
172*0Sstevel@tonic-gate // remainder = *this;
173*0Sstevel@tonic-gate // did not find it!
174*0Sstevel@tonic-gate return (NULL);
175*0Sstevel@tonic-gate }
176*0Sstevel@tonic-gate
177*0Sstevel@tonic-gate void
resetToken(void)178*0Sstevel@tonic-gate Str::resetToken(void)
179*0Sstevel@tonic-gate {
180*0Sstevel@tonic-gate nextTok_ = str_;
181*0Sstevel@tonic-gate }
182*0Sstevel@tonic-gate
183*0Sstevel@tonic-gate const char *
peak(void) const184*0Sstevel@tonic-gate Str::peak(void) const
185*0Sstevel@tonic-gate {
186*0Sstevel@tonic-gate return (str_);
187*0Sstevel@tonic-gate }
188*0Sstevel@tonic-gate
189*0Sstevel@tonic-gate void
replaceAll(char c,char newc)190*0Sstevel@tonic-gate Str::replaceAll(char c, char newc)
191*0Sstevel@tonic-gate {
192*0Sstevel@tonic-gate for (int i = 0; i < strlen(str_); i++) {
193*0Sstevel@tonic-gate if (str_[i] == c) {
194*0Sstevel@tonic-gate str_[i] = newc;
195*0Sstevel@tonic-gate }
196*0Sstevel@tonic-gate }
197*0Sstevel@tonic-gate }
198*0Sstevel@tonic-gate // oh look an extra line!!!
199