xref: /illumos-gate/usr/src/lib/libfru/libgenutil/Str.cc (revision 4c28a617e3922d92a58e813a5b955eb526b9c386)
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 #include <string.h>
28 #include <stdio.h>
29 
30 #include "Str.h"
31 
32 Str::Str()
33 	: str_(strcpy(new char[strlen("")+1], "")),
34     nextTok_(str_)
35 {}
36 
37 Str::Str(const char *str)
38 	: str_(strcpy(new char[strlen(str)+1], str)),
39     nextTok_(str_)
40 {}
41 
42 Str::Str(const char *str, int len)
43 	: str_(new char[len+1]),
44     nextTok_(str_)
45 {
46 	strlcpy(str_, str, len+1);
47 }
48 
49 Str::Str(const Str& rhs)
50 	: str_(strcpy(new char[strlen(rhs.str_)+1], rhs.str_)),
51     nextTok_(str_)
52 {}
53 
54 Str::~Str()
55 {
56 	delete[] str_;
57 }
58 
59 void
60 Str::operator = (const Str& rhs)
61 {
62 	delete[] str_;
63 	str_ = strcpy(new char[strlen(rhs.str_)+1], rhs.str_);
64 	// pointer arithmetic very BAD I know...
65 	nextTok_ = str_ + (rhs.nextTok_ - rhs.str_);
66 }
67 
68 void
69 Str::operator = (const char *str)
70 {
71 	delete[] str_;
72 	str_ = strcpy(new char[strlen(str)+1], str);
73 	nextTok_ = str_;
74 }
75 
76 int
77 Str::operator == (const Str& rhs) const
78 {
79 	return (strcmp(str_, rhs.str_) == 0);
80 }
81 
82 int
83 Str::operator != (const Str& rhs) const
84 {
85 	return (strcmp(str_, rhs.str_) != 0);
86 }
87 
88 char&
89 Str::operator[](int index) const
90 {
91 	return (str_[index]);
92 }
93 
94 Str&
95 Str::operator<<(Str rhs)
96 {
97 	char *tmp = new char[strlen(str_)+strlen(rhs.peak())+1];
98 	strcpy(tmp, str_);
99 	delete[] str_;
100 	str_ = tmp;
101 	strcat(str_, rhs.peak());
102 	return (*this);
103 }
104 
105 Str&
106 Str::operator<<(long long i)
107 {
108 	char msg[256];
109 	sprintf(msg, "%lld", i);
110 	return (*this << msg);
111 }
112 
113 Str&
114 Str::operator<<(long i)
115 {
116 	char msg[256];
117 	sprintf(msg, "%ld", i);
118 	return (*this << msg);
119 }
120 
121 Str&
122 Str::operator<<(int i)
123 {
124 	char msg[256];
125 	sprintf(msg, "%d", i);
126 	return (*this << msg);
127 }
128 
129 Str&
130 Str::operator<<(char c)
131 {
132 	char msg[256];
133 	sprintf(msg, "%c", c);
134 	return (*this << msg);
135 }
136 
137 // normal "C" strcmp
138 int
139 Str::compare(const Str& rhs) const
140 {
141 	return (strcmp(str_, rhs.str_));
142 }
143 
144 int
145 Str::length(void) const
146 {
147 	return (strlen(str_));
148 }
149 
150 char
151 Str::tokenize(Str& token, const Str& separators, Str& remainder)
152 {
153 	int i = 0;
154 	int j = 0;
155 	for (i = 0; nextTok_[i] != '\0'; i++) {
156 		for (j = 0; j < separators.length(); j++) {
157 			if (nextTok_[i] == separators[j]) {
158 				Str rc(nextTok_, i);
159 				token = rc;
160 				nextTok_ = &(nextTok_[i+1]);
161 				// Str remain(nextTok_);
162 				remainder = nextTok_;
163 				return (separators[j]);
164 			}
165 		}
166 	}
167 
168 	token = "";
169 	remainder = nextTok_;
170 	// remainder = *this;
171 	// did not find it!
172 	return ('\0');
173 }
174 
175 void
176 Str::resetToken(void)
177 {
178 	nextTok_ = str_;
179 }
180 
181 const char *
182 Str::peak(void) const
183 {
184 	return (str_);
185 }
186 
187 void
188 Str::replaceAll(char c, char newc)
189 {
190 	for (int i = 0; i < strlen(str_); i++) {
191 		if (str_[i] == c) {
192 			str_[i] = newc;
193 		}
194 	}
195 }
196 // oh look an extra line!!!
197