home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Simtel MSDOS 1992 June
/
SIMTEL_0692.cdr
/
msdos
/
microcrn
/
issue_40.arc
/
DAIMS.ARC
/
STRINGS.CPP
< prev
next >
Wrap
Text File
|
1988-02-10
|
4KB
|
236 lines
#include <stdio.h>
#include "strings.hxx"
#undef TEST
/*
-*++ string::string(): un-initialized string object
**
** (*++ history:
** 7 Dec 87 Bruce Eckel Creation date
** ++*)
**
** (*++ detailed:
** ++*)
*/
string::string()
{
p = new srep;
p->s = 0;
p->n = 1;
}
/*
-*++ string::string(): build a string object from a string of characters
**
** (*++ history:
** 7 Dec 87 Bruce Eckel Creation date
** ++*)
**
** (*++ detailed:
** ++*)
*/
string::string(char* s)
{
p = new srep;
p->s = new char[strlen(s)+1];
strcpy(p->s,s);
p->n = 1;
}
/*
-*++ string::string(): build a string object from a string reference
**
** (*++ history:
** 7 Dec 87 Bruce Eckel Creation date
** ++*)
**
** (*++ detailed:
** ++*)
*/
string::string(string& x)
{
x.p->n++;
p = x.p;
}
/*
-*++ string::~string(): destructor for string objects
**
** (*++ history:
** 7 Dec 87 Bruce Eckel Creation date
** ++*)
**
** (*++ detailed:
** ++*)
*/
string::~string()
{
if (--p->n == 0){
delete p->s;
delete p;
}
}
/*
-*++ string::operator=(): string object assignment to character string
**
** (*++ history:
** 7 Dec 87 Bruce Eckel Creation date
** ++*)
**
** (*++ detailed:
** ++*)
*/
string & string::operator=(char * s)
{
if (p->n > 1) {
p->n--;
p = new srep;
}
else if (p->n == 1)
delete p->s;
p->s = new char[strlen(s) + 1 ];
strcpy(p->s,s);
p->n = 1;
return *this;
}
/*
-*++ string::operator=(): string object assignment to string object
**
** (*++ history:
** 7 Dec 87 Bruce Eckel Creation date
** ++*)
**
** (*++ detailed:
** ++*)
*/
string& string::operator=(string& x)
{
x.p->n++;
if (--p->n == 0) {
delete p->s;
delete p;
}
p = x.p;
return *this;
}
/*
-*++ string::operator<<(): stream output of a string
**
** (*++ history:
** 7 Dec 87 Bruce Eckel Creation date
** ++*)
**
** (*++ detailed:
** ++*)
*/
ostream& operator<<(ostream& s, string& x)
{
return s << x.p->s << " [" << x.p->n << "]\n";
}
/*
-*++ string::operator>>(): stream input to a string
**
** (*++ history:
** 7 Dec 87 Bruce Eckel Creation date
** ++*)
**
** (*++ detailed:
** ++*)
*/
istream & operator>>(istream& s, string& x)
{
char buf[256];
s >> buf;
x = buf;
cout << "echo: " << x << "\n";
return s;
}
/*
-*++ string::operator[](): index into a string
**
** (*++ history:
** 7 Dec 87 Bruce Eckel Creation date
** ++*)
**
** (*++ detailed:
** ++*)
*/
char & string::operator[](int i)
{
if (i<0 || strlen(p->s) < i) error("index out of range");
return p->s[i];
}
/*
-*++ string::operator+(): add two string objects
**
** (*++ history:
** 7 Dec 87 Bruce Eckel Creation date
** ++*)
**
** (*++ detailed:
** ++*)
*/
string& operator+(string& x, string& y)
{
char buf[256];
strcpy(buf,x.p->s);
strcat(buf,y.p->s);
string & tmp = *new string(buf);
return tmp;
}
#ifdef TEST
main()
{
string x = "this is string x";
string y = "this is string y";
cout << x << y << "\n";
cout << x + y << "\n";
string z = "this is string z ";
string q = "this is string q ";
cout << z + q << "\n" << q + x << "\n";
/* string x[100];
int n;
cout << "here we go\n";
for (n = 0; cin >> x[n]; n++) {
string y;
if (n == 100) error("too many strings");
cout << (y = x[n]);
if (y == "done") break;
}
cout << "here we go back again\n";
for (int i=n-1; 0 <= i; i--) cout << x[i];
*/
}
#endif TEST /* use test for testing string class, undef otherwise */