home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
ftp.uni-stuttgart.de/pub/systems/acorn/
/
Acorn.tar
/
Acorn
/
acornet
/
dev
/
c
/
gcc
/
g++lib.spk
/
h
/
str
< prev
next >
Wrap
Text File
|
1993-12-07
|
9KB
|
269 lines
/* -------------------------------------------------------------------- */
/* String++ Version 2.12 02/16/93 */
/* */
/* Enhanced string class for Turbo C++/Borland C++. */
/* Copyright 1991-1993 by Carl W. Moreland */
/* */
/* str.h */
/* -------------------------------------------------------------------- */
#ifndef _STR_H
#define _STR_H
#include "strstream.h"
extern "C"
{
#include <string.h>
}
#if defined (__ZTC__)
#include "stream.hpp" // Zortech
#else
#include "iostream.h" // Borland and UNIX
#endif
#undef toupper
#undef tolower
#define LEFT_JUSTIFY 0 // obsolete - use LEFT
#define CENTER_JUSTIFY 1 // obsolete - use CENTER
#define RIGHT_JUSTIFY 2 // obsolete - use RIGHT
#define LEFT 0
#define CENTER 1
#define RIGHT 2
#define NOCLIP 0
#define CLIP 1
#define WHITESPACE 0
class string
{
private:
char *_ptr;
void ltos(long n);
public:
string(); // default constructor;
string(const char c, // initialize with a character,
unsigned n = 1); // optional # of characters
string(const char *s, // initialize with a char *,
unsigned pos = 0, // optional starting char
unsigned len = 10000); // optional # of chars
string(const string& s, // initialize with another string,
unsigned pos = 0, // optional starting char
unsigned len = 10000); // optional # of chars
string(int n) { ltos((long)n); } // initialize with an integer
string(long n) { ltos(n); } // initialize with a long int
~string(void);
operator char*() const { return _ptr; }
char* ptr(void) const { return _ptr; }
const char* operator()() const { return _ptr; }
const char* operator()(unsigned pos) const { return _ptr + pos; }
string operator()(unsigned pos, unsigned len) const;
int Length(void) const { return strlen(_ptr); }
int Len(void) const { return strlen(_ptr); }
string& toUpper(void); // convert str to uppercase
string& toLower(void); // convert str to lowercase
int& Value(int& n); // convert str to an integer
long& Value(long& n); // convert str to a long int
string& Left(unsigned len); // left len chars
string& Right(unsigned len); // right len chars
string& Mid(unsigned pos, // middle len chars from pos
unsigned len);
string& Justify(int mode, // justify str according to mode
unsigned len,
int clip=0);
string& Trim(int mode = CENTER, // Delete leading/trailing whitespace
char ch = WHITESPACE);
string& Insert(unsigned pos, // insert substring
const string& s);
string& Delete(unsigned pos, // delete substring
unsigned len = 10000);
char* Copy(char*&); // copy str to char* (non-const)
int Index(const string& t); // position of t in str
string SubStr(unsigned p, // substring of str at position p
unsigned n=10000);
int Split(string*& a, // split str into an array a on
const string& fs); // field separator fs
int Sub(const string& from, // substitute from with to in str
const string& to,
unsigned count=10000);
string& operator=(const char); // str1 = char
string& operator=(const char*); // str1 = char*
string& operator=(const string&); // str1 = str
string& operator=(const int); // str1 = n
string& operator=(const long); // str1 = n
string& operator=(const float); // str1 = x
string& operator=(const double); // str1 = x
string& operator+=(const char); // str1 += char
string& operator+=(const char*); // str1 += char*
string& operator+=(const string&); // str1 += str
string& operator*=(unsigned n); // str1 *= n
char& operator[](unsigned i) const; // ch = str[i] or str[i] = ch
friend string operator + (const string&, const string&);
friend string operator + (const string&, const char*);
friend string operator + (const char*, const string&);
friend string operator * (const string&, int n);
friend int operator == (const string&, const string&);
friend int operator == (const string&, const char*);
friend int operator == (const char*, const string&);
friend int operator != (const string&, const string&);
friend int operator != (const string&, const char*);
friend int operator != (const char*, const string&);
friend int operator < (const string&, const string&);
friend int operator < (const string&, const char*);
friend int operator < (const char*, const string&);
friend int operator > (const string&, const string&);
friend int operator > (const string&, const char*);
friend int operator > (const char*, const string&);
friend int operator <= (const string&, const string&);
friend int operator <= (const string&, const char*);
friend int operator <= (const char*, const string&);
friend int operator >= (const string&, const string&);
friend int operator >= (const string&, const char*);
friend int operator >= (const char*, const string&);
/* --- Awk-style functions ------------------------------------------ */
friend int length(const string& s) { return s.Len(); }
friend int index(const string& s, const string& t);
friend string substr(const string& s, unsigned p, unsigned n=10000);
friend int split(const string& s, string*& a, const string& fs);
friend int gsub(const string& from, const string& to, string& str, unsigned count=10000);
friend int sub(const string& from, const string& to, string& str);
friend int match(const string& s, const string& r);
/* --- Other C-style functions -------------------------------------- */
friend string toupper(const string& s);
friend string tolower(const string& s);
friend string left(const string& s, unsigned n);
friend string right(const string& s, unsigned n);
friend string mid(const string& s, unsigned p, unsigned n);
friend string justify(const string& s, int mode, unsigned len, int clip=1);
friend string trim(const string& s, int mode=CENTER);
/* --- Obsolete naming conventions ---------------------------------- */
int length(void) const { return strlen(_ptr); }
int len(void) const { return strlen(_ptr); }
string left(unsigned len) const { string tmp; return tmp.Left(len); }
string right(unsigned len) const { string tmp; return tmp.Right(len); }
string mid(unsigned pos, unsigned len) const { string tmp; return tmp.Mid(pos, len); }
string justify(int mode, unsigned len, int clip=0) const { string tmp; return tmp.Justify(mode, len, clip); }
string& toupper(void) { return toUpper(); }
string& tolower(void) { return toLower(); }
};
typedef string String;
ostream& operator<<(ostream&, const string&);
istream& operator>>(istream&, string&);
inline int operator==(const string& s1, const char* s2) {
return strcmp(s1._ptr,s2)==0;
}
inline int operator==(const char* s1, const string& s2) {
return strcmp(s1,s2._ptr)==0;
}
inline int operator==(const string& s1, const string& s2) {
return strcmp(s1._ptr,s2._ptr)==0;
}
inline int operator!=(const string& s1, const char *s2) {
return strcmp(s1._ptr,s2)!=0;
}
inline int operator!=(const char *s1, const string& s2) {
return strcmp(s1,s2._ptr)!=0;
}
inline int operator!=(const string& s1, const string& s2) {
return strcmp(s1._ptr,s2._ptr)!=0;
}
inline int operator<(const string& s1, const char *s2) {
return strcmp(s1._ptr,s2)< 0;
}
inline int operator<(const char *s1, const string& s2) {
return strcmp(s1,s2._ptr)< 0;
}
inline int operator<(const string& s1, const string& s2) {
return strcmp(s1._ptr,s2._ptr)< 0;
}
inline int operator>(const string& s1, const char *s2) {
return strcmp(s1._ptr,s2)> 0;
}
inline int operator>(const char *s1, const string& s2) {
return strcmp(s1,s2._ptr)> 0;
}
inline int operator>(const string& s1, const string& s2) {
return strcmp(s1._ptr,s2._ptr)> 0;
}
inline int operator<=(const string& s1, const char *s2) {
return strcmp(s1._ptr,s2)<=0;
}
inline int operator<=(const char *s1, const string& s2) {
return strcmp(s1,s2._ptr)<=0;
}
inline int operator<=(const string& s1, const string& s2) {
return strcmp(s1._ptr,s2._ptr)<=0;
}
inline int operator>=(const string& s1, const char *s2) {
return strcmp(s1._ptr,s2)>=0;
}
inline int operator>=(const char *s1, const string& s2) {
return strcmp(s1,s2._ptr)>=0;
}
inline int operator>=(const string& s1, const string& s2) {
return strcmp(s1._ptr,s2._ptr)>=0;
}
inline int string::Index(const string& t) {
return index(*this, t);
}
inline string string::SubStr(unsigned p, unsigned n) {
return substr(*this, p, n);
}
inline int string::Split(string*& a, const string& fs) {
return split(*this, a, fs);
}
inline int string::Sub(const string& from, const string& to, unsigned count) {
return gsub(from, to, *this, count);
}
inline int sub(const string& from, const string& to, string& str) {
return gsub(from, to, str, 1);
}
#endif