Category : C++ Source Code
Archive   : STRX.ZIP
Filename : STR.H

 
Output of file : STR.H contained in archive : STRX.ZIP
//
// str.h : str class interface
// Author : Roy S. Woll
//
// Copyright (c) 1993 by Roy S. Woll
// You may distribute this source freely as long as you leave all files
// in their original form, including the copyright notice as is.
//
// Version 2.2 05/16/93 Add member function read, lowercase,
// uppercase, and variations of pad and strip.
// Version 2.01 03/01/93 Support Turbo C++ (no nested class)
// Version 2.00 12/01/92
//
#ifndef _STR_H
#define _STR_H

//#define DEBUG_STR

class ostream;
class istream;
class dynstream;
class regX;

#ifdef __TURBOC__
#ifndef __BORLANDC__
#define _SUBSTR substr
#else
#define _SUBSTR str::substr
class str {
#endif
#else
#define _SUBSTR str::substr
class str {
#endif

class substr{
friend class str;

int posReplace;
int numReplace;
str * mystr;

substr(const str * data, int AposReplace, int AnumReplace);

int compare(const char *) const;
int compare(const substr&) const;
int length() const;

public:
str & operator = (const substr& s);
str & operator = (const char * s);

str operator+(const char *) const;
str operator+(const substr&) const;
int operator==(const char *) const;
int operator<=(const char *) const;
int operator>=(const char *) const;
int operator!=(const char *) const;
int operator< (const char *) const;
int operator> (const char *) const;
int operator==(const substr&) const;
int operator<=(const substr&) const;
int operator>=(const substr&) const;
int operator!=(const substr&) const;
int operator< (const substr&) const;
int operator> (const substr&) const;

operator str() const;
};

#ifdef __TURBOC__
#ifndef __BORLANDC__
class str {
#endif
#endif

friend class dynstreambuf;

protected:
enum {STR_DEFAULT_MEMINCR=256};
enum {ICASE=1};


#ifdef DEBUG_STR
public:
static int dynstreamCount;
static int ObjectCount;
static int AllocationCount;
static int TotalObjectCount;
static int TotalAllocationCount;
#endif

friend class substr;


struct strdata{
enum {STR_DEBUG_BUFSIZE=80};
int cursize; // cursize should be defined before buf!
int curlength; // current length of buffer
int refcount;

dynstream * mystream;// pointer to str's stream
char strChange; // Internal flag indicating when the length has been
// changed by the str class.
// If true, stream is notified.
char buf[STR_DEBUG_BUFSIZE];
// buf[1] is all that is required, but we allow
// it to be buf[80] so that the debugger can
// treat it as a char *.
};


static int defaultFlags;

int memsize_init; // Initial amount of memory to allocate for buffer
int memsize_incr; // Memory expansion increment
int flags; // Default flags for case sensitivity, etc.

str& _assign (const char * source, int len);
str& _concat (const char * source, int len);
int _checkMemAllocation(int requiredLen=0);

static void init(strdata *&, int, int);

protected:

strdata * data;
char * getNewBuffer( int len, int newbufsize);
char * getNewBuffer(int newbufsize);

void setNewBuffer(strdata * newdata, int newbufsize, int len);

void setlength(int len) const; // update the current length

int strncmp(const char * s1, const char * s2, int n) const;
int strcmp(const char * s1, const char * s2) const;

//
// Special constructors used by implementation
//
str(const char *, const char *);
str(const char *, const _SUBSTR&);
str(const substr&, const char *);
str(const substr&, const substr&);


public:

//
// Constructors
//
str(void);
str(int p_bufsize, int = STR_DEFAULT_MEMINCR);
str(const char * s, int bufsize=0, int = STR_DEFAULT_MEMINCR);
str(const str&, int bufsize=0, int = STR_DEFAULT_MEMINCR);

virtual ~str();

//
// Access Operators
//
int size(void) const; // return current memory allocated for buffer
int length(void) const; // return current string length of buffer

operator const char * () const; // for implicit casting
const char * operator()() const; // for explicit casting
const char * operator()(int index) const; //

//
// String substitution using substr "() syntax"
//
substr operator()(int pos, int numreplace); // create substr from str
const substr operator()(int pos, int numreplace) const;

//
// String search/replace member functions
//
int search (const char *, int * startPtr) const;
int search (const char *, int start=0) const;
int search (const regX&, int * startPtr) const;
int search (const regX&, int start) const;
int search (const regX&, str * matchPtr=0, int start=0) const;
int search (const regX&, str * matchPtr, int * startPtr) const;

int index (const char *, int start=0) const;
int index (const regX&, int start=0) const;
int index (const regX&, int * matchLen, int start=0) const;


int replace (const regX&, const char * replaceString,
int* startPtr, int numReplacements=1);
int replace (const char * pattern, const char * replaceString,
int* startPtr, int numReplacements=1);
int replace (const regX&, const char * replaceString,
int start=0, int numReplacements=1);
int replace (const char * pattern, const char * replaceString,
int start=0, int numReplacements=1);
int replaceAll (const char *, const char * replaceString, int start=0);
int replaceAll (const regX&, const char * replaceString, int start=0);

char& operator[](int position); // array indexing
char operator[](int position) const; // array indexing for const

ostream& stream(void); // return stream for this str
ostream& stream(int); // return stream and move put pointer

//
// Assignment & Concatenation Operators
//
str & operator = (const str & s); // s = str;
str & operator = (const substr & s); // s = substr;
str & operator = (const char * s); // s = charptr
str & operator = (const char s); // s = character
str & assign (const char * source, int len=0);

str & operator += (const str & s); // s += str
str & operator += (const substr & s); // s += substr
str & operator += (const char * s); // s += charptr
str & operator += (const char s); // s += char

str & operator << (const str& s); // s << str
str & operator << (const substr& s); // s << substr
str & operator << (const char * s); // s << charptr
str & operator << (const int s); // s << int
str & operator << (const char s); // s << char

// Manipulators
enum PadStripT {left, right, both, leading=0, trailing};

str& strip(PadStripT t=trailing, const char * stripchar=" \t");
str& strip(PadStripT t, char stripchar);
str& stripTrailing(const char * stripchar=" \t");
str& stripLeading(const char * stripchar=" \t");
str& stripBoth(const char * stripchar=" \t");

str& pad(int padsize, PadStripT t=right, char padchar = ' ');
str& padRight(int padsize, char padchar = ' ');
str& padLeft(int padsize, char padchar = ' ');
str& padBoth(int padsize, char padchar = ' ');

str& lowercase(); // convert this string to lowercase
str& uppercase(); // convert this string to uppercase


// Mutators
int insert(int pos, char ch); // insert ch, return 0 if fail
int insert(int pos, const char * insertStr);
// insert char *, return 0 if fail
void remove(int pos, int numdel=1); // remove numdel characters starting at
// position pos

str operator+(const str &) const;
str operator+(const substr &) const;
str operator+(const char * b) const;
str operator+(const char b) const;

int operator==(const char *) const; // made member functions so that
int operator<=(const char *) const; // derived classes inherit functions
int operator>=(const char *) const;
int operator!=(const char *) const;
int operator< (const char *) const;
int operator> (const char *) const;

int operator==(const str &) const;
int operator<=(const str &) const;
int operator>=(const str &) const;
int operator!=(const str &) const;
int operator< (const str &) const;
int operator> (const str &) const;

// Friend Stream Operators
istream& read(istream&, int);
friend istream& operator >> (istream&, str &);
friend ostream& operator << (ostream&, const str &);

friend int compare(const str&, const char *);
friend int compare(const str&, const str &);
friend int compare(const char *, const str &);

friend str operator+(const char *, const str &);
friend str operator+(const char *, const _SUBSTR &);

friend str strip(str, PadStripT t=trailing, const char * stripchar=" \t");
friend str strip(str, PadStripT t, char stripchar);
friend str pad(str s, int padsize, PadStripT t=right, char padchar= ' ');
friend str lowercase(const char *); // return lower case of string
friend str uppercase(const char *); // return upper case of string


//
// Case sensitivity functions
//
static void setdefaultCaseSensitive(int val);
void setCaseSensitive(int val);
int caseSensitive(void) const;
};

//
// Define inline functions
//
inline const char * str::operator()() const // for explicit casting
{return (const char *)*this;}

inline char str::operator[](int position) const{ // array indexing
return data->buf[position];
}

inline void str::setlength(int len) const{ // update the current length
data->buf[len]=0; // null terminated
data->curlength=len;
data->strChange=1;
}

//
// Global member function operators
//
int operator==(const char *, const str &);
int operator<=(const char *, const str &);
int operator>=(const char *, const str &);
int operator!=(const char *, const str &);
int operator< (const char *, const str &);
int operator> (const char *, const str &);

int operator==(const char *, const _SUBSTR &);
int operator<=(const char *, const _SUBSTR &);
int operator>=(const char *, const _SUBSTR &);
int operator!=(const char *, const _SUBSTR &);
int operator< (const char *, const _SUBSTR &);
int operator> (const char *, const _SUBSTR &);


#endif


  3 Responses to “Category : C++ Source Code
Archive   : STRX.ZIP
Filename : STR.H

  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/