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