dmake: do not set MAKEFLAGS=k
[unleashed/tickless.git] / usr / src / lib / libfru / libgenutil / Str.cc
blobbcccdf4fcd6570645d7d0b82d23d9355d5683758
1 /*
2 * CDDL HEADER START
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.
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.
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]
20 * CDDL HEADER END
23 * Copyright (c) 2000 by Sun Microsystems, Inc.
24 * All rights reserved.
27 #include <string.h>
28 #include <stdio.h>
30 #include "Str.h"
32 Str::Str()
33 : str_(strcpy(new char[strlen("")+1], "")),
34 nextTok_(str_)
37 Str::Str(const char *str)
38 : str_(strcpy(new char[strlen(str)+1], str)),
39 nextTok_(str_)
42 Str::Str(const char *str, int len)
43 : str_(new char[len+1]),
44 nextTok_(str_)
46 strlcpy(str_, str, len+1);
49 Str::Str(const Str& rhs)
50 : str_(strcpy(new char[strlen(rhs.str_)+1], rhs.str_)),
51 nextTok_(str_)
54 Str::~Str()
56 delete[] str_;
59 void
60 Str::operator = (const Str& rhs)
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_);
68 void
69 Str::operator = (const char *str)
71 delete[] str_;
72 str_ = strcpy(new char[strlen(str)+1], str);
73 nextTok_ = str_;
76 int
77 Str::operator == (const Str& rhs) const
79 return (strcmp(str_, rhs.str_) == 0);
82 int
83 Str::operator != (const Str& rhs) const
85 return (strcmp(str_, rhs.str_) != 0);
88 char&
89 Str::operator[](int index) const
91 return (str_[index]);
94 Str&
95 Str::operator<<(Str rhs)
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);
105 Str&
106 Str::operator<<(long long i)
108 char msg[256];
109 sprintf(msg, "%lld", i);
110 return (*this << msg);
113 Str&
114 Str::operator<<(long i)
116 char msg[256];
117 sprintf(msg, "%ld", i);
118 return (*this << msg);
121 Str&
122 Str::operator<<(int i)
124 char msg[256];
125 sprintf(msg, "%d", i);
126 return (*this << msg);
129 Str&
130 Str::operator<<(char c)
132 char msg[256];
133 sprintf(msg, "%c", c);
134 return (*this << msg);
137 // normal "C" strcmp
139 Str::compare(const Str& rhs) const
141 return (strcmp(str_, rhs.str_));
145 Str::length(void) const
147 return (strlen(str_));
150 char
151 Str::tokenize(Str& token, const Str& separators, Str& remainder)
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]);
168 token = "";
169 remainder = nextTok_;
170 // remainder = *this;
171 // did not find it!
172 return ('\0');
175 void
176 Str::resetToken(void)
178 nextTok_ = str_;
181 const char *
182 Str::peak(void) const
184 return (str_);
187 void
188 Str::replaceAll(char c, char newc)
190 for (int i = 0; i < strlen(str_); i++) {
191 if (str_[i] == c) {
192 str_[i] = newc;
196 // oh look an extra line!!!