Category : Files from Magazines
Archive   : DFPP01.ZIP
Filename : STRINGS.CPP

 
Output of file : STRINGS.CPP contained in archive : DFPP01.ZIP
// -------- strings.cpp

#include "strings.h"

// ------- put a string into a String
void String::putstr(char *s)
{
delete sptr;
length = strlen(s);
sptr = new char[length+1];
strcpy(sptr, s);
}

// --- convert from char *
String::String(char *s)
{
sptr = NULL;
putstr(s);
}

// ------- copy constructor
String::String(String& s)
{
sptr = NULL;
putstr(s.sptr);
}

// -------- construct with a size and fill character
String::String(int len, char fill)
{
length = len;
sptr = new char[length+1];
memset(sptr, fill, length);
*(sptr+len) = '\0';
}

// -------- assignment
String& String::operator=(String &s)
{
if (this != &s)
putstr(s.sptr);
return *this;
}

// ------- concatenation operator (str1 + str2)
String String::operator+(String &s)
{
int ln = 0;
if (sptr)
ln += strlen(sptr);
if (s.sptr)
ln += strlen(s.sptr);
String temp(ln);
if (sptr)
strcpy(temp.sptr, sptr);
if (s.sptr)
strcat(temp.sptr, s.sptr);
return temp;
}

// ------ substring: right len chars
String String::right(int len)
{
String tmp;
if (sptr && len) {
int sln = strlen(sptr);
if (len > sln)
len = sln;
if (len)
tmp = String(sptr + sln - len);
}
return tmp;
}

// ------ substring: left len chars
String String::left(int len)
{
String tmp(len);
if (sptr && len) {
int sln = strlen(sptr);
if (len > sln)
len = sln;
if (len)
strncpy(tmp.sptr, sptr, len);
}
return tmp;
}

// ------ substring: middle len chars starting from where
String String::mid(int len, int where)
{
String tmp(len);
if (len && sptr && where < (int) strlen(sptr))
strncpy(tmp.sptr,sptr+where,len);
return tmp;
}

// ---- find offset to first instance of specified char
int String::FindChar(unsigned char ch)
{
if (sptr) {
char *cp = strchr(sptr, ch);
if (cp)
return (int) (cp - sptr);
}
return -1;
}

// ------ change the buffer length of a string
void String::ChangeLength(unsigned int newlen)
{
if (length == 0)
*this = String(newlen);
else {
char *cp = new char[newlen+1];
memset(cp, '\0', newlen+1);
if (sptr != NULL) {
strncpy(cp, sptr, newlen);
delete sptr;
}
sptr = cp;
length = newlen;
}
}





  3 Responses to “Category : Files from Magazines
Archive   : DFPP01.ZIP
Filename : STRINGS.CPP

  1. Very nice! Thank you for this wonderful archive. I wonder why I found it only now. Long live the BBS file archives!

  2. This is so awesome! 😀 I’d be cool if you could download an entire archive of this at once, though.

  3. But one thing that puzzles me is the “mtswslnkmcjklsdlsbdmMICROSOFT” string. There is an article about it here. It is definitely worth a read: http://www.os2museum.com/wp/mtswslnk/