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