2N/A/*
2N/A * CDDL HEADER START
2N/A *
2N/A * The contents of this file are subject to the terms of the
2N/A * Common Development and Distribution License, Version 1.0 only
2N/A * (the "License"). You may not use this file except in compliance
2N/A * with the License.
2N/A *
2N/A * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
2N/A * or http://www.opensolaris.org/os/licensing.
2N/A * See the License for the specific language governing permissions
2N/A * and limitations under the License.
2N/A *
2N/A * When distributing Covered Code, include this CDDL HEADER in each
2N/A * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
2N/A * If applicable, add the following below this CDDL HEADER, with the
2N/A * fields enclosed by brackets "[]" replaced with your own identifying
2N/A * information: Portions Copyright [yyyy] [name of copyright owner]
2N/A *
2N/A * CDDL HEADER END
2N/A */
2N/A/*
2N/A * Copyright (c) 2000 by Sun Microsystems, Inc.
2N/A * All rights reserved.
2N/A */
2N/A
2N/A#pragma ident "%Z%%M% %I% %E% SMI"
2N/A
2N/A#include <string.h>
2N/A#include <stdio.h>
2N/A
2N/A#include "Str.h"
2N/A
2N/AStr::Str()
2N/A : str_(strcpy(new char[strlen("")+1], "")),
2N/A nextTok_(str_)
2N/A{}
2N/A
2N/AStr::Str(const char *str)
2N/A : str_(strcpy(new char[strlen(str)+1], str)),
2N/A nextTok_(str_)
2N/A{}
2N/A
2N/AStr::Str(const char *str, int len)
2N/A : str_(new char[len+1]),
2N/A nextTok_(str_)
2N/A{
2N/A strlcpy(str_, str, len+1);
2N/A}
2N/A
2N/AStr::Str(const Str& rhs)
2N/A : str_(strcpy(new char[strlen(rhs.str_)+1], rhs.str_)),
2N/A nextTok_(str_)
2N/A{}
2N/A
2N/AStr::~Str()
2N/A{
2N/A delete[] str_;
2N/A}
2N/A
2N/Avoid
2N/AStr::operator = (const Str& rhs)
2N/A{
2N/A delete[] str_;
2N/A str_ = strcpy(new char[strlen(rhs.str_)+1], rhs.str_);
2N/A // pointer arithmetic very BAD I know...
2N/A nextTok_ = str_ + (rhs.nextTok_ - rhs.str_);
2N/A}
2N/A
2N/Avoid
2N/AStr::operator = (const char *str)
2N/A{
2N/A delete[] str_;
2N/A str_ = strcpy(new char[strlen(str)+1], str);
2N/A nextTok_ = str_;
2N/A}
2N/A
2N/Aint
2N/AStr::operator == (const Str& rhs) const
2N/A{
2N/A return (strcmp(str_, rhs.str_) == 0);
2N/A}
2N/A
2N/Aint
2N/AStr::operator != (const Str& rhs) const
2N/A{
2N/A return (strcmp(str_, rhs.str_) != 0);
2N/A}
2N/A
2N/Achar&
2N/AStr::operator[](int index) const
2N/A{
2N/A return (str_[index]);
2N/A}
2N/A
2N/AStr&
2N/AStr::operator<<(Str rhs)
2N/A{
2N/A char *tmp = new char[strlen(str_)+strlen(rhs.peak())+1];
2N/A strcpy(tmp, str_);
2N/A delete[] str_;
2N/A str_ = tmp;
2N/A strcat(str_, rhs.peak());
2N/A return (*this);
2N/A}
2N/A
2N/AStr&
2N/AStr::operator<<(long long i)
2N/A{
2N/A char msg[256];
2N/A sprintf(msg, "%lld", i);
2N/A return (*this << msg);
2N/A}
2N/A
2N/AStr&
2N/AStr::operator<<(long i)
2N/A{
2N/A char msg[256];
2N/A sprintf(msg, "%ld", i);
2N/A return (*this << msg);
2N/A}
2N/A
2N/AStr&
2N/AStr::operator<<(int i)
2N/A{
2N/A char msg[256];
2N/A sprintf(msg, "%d", i);
2N/A return (*this << msg);
2N/A}
2N/A
2N/AStr&
2N/AStr::operator<<(char c)
2N/A{
2N/A char msg[256];
2N/A sprintf(msg, "%c", c);
2N/A return (*this << msg);
2N/A}
2N/A
2N/A// normal "C" strcmp
2N/Aint
2N/AStr::compare(const Str& rhs) const
2N/A{
2N/A return (strcmp(str_, rhs.str_));
2N/A}
2N/A
2N/Aint
2N/AStr::length(void) const
2N/A{
2N/A return (strlen(str_));
2N/A}
2N/A
2N/Achar
2N/AStr::tokenize(Str& token, const Str& separators, Str& remainder)
2N/A{
2N/A int i = 0;
2N/A int j = 0;
2N/A for (i = 0; nextTok_[i] != '\0'; i++) {
2N/A for (j = 0; j < separators.length(); j++) {
2N/A if (nextTok_[i] == separators[j]) {
2N/A Str rc(nextTok_, i);
2N/A token = rc;
2N/A nextTok_ = &(nextTok_[i+1]);
2N/A // Str remain(nextTok_);
2N/A remainder = nextTok_;
2N/A return (separators[j]);
2N/A }
2N/A }
2N/A }
2N/A
2N/A token = "";
2N/A remainder = nextTok_;
2N/A // remainder = *this;
2N/A // did not find it!
2N/A return (NULL);
2N/A}
2N/A
2N/Avoid
2N/AStr::resetToken(void)
2N/A{
2N/A nextTok_ = str_;
2N/A}
2N/A
2N/Aconst char *
2N/AStr::peak(void) const
2N/A{
2N/A return (str_);
2N/A}
2N/A
2N/Avoid
2N/AStr::replaceAll(char c, char newc)
2N/A{
2N/A for (int i = 0; i < strlen(str_); i++) {
2N/A if (str_[i] == c) {
2N/A str_[i] = newc;
2N/A }
2N/A }
2N/A}
2N/A// oh look an extra line!!!