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 >
Text File  |  1993-12-07  |  9KB  |  269 lines

  1. /* -------------------------------------------------------------------- */
  2. /* String++ Version 2.12                                       02/16/93 */
  3. /*                                                                      */
  4. /* Enhanced string class for Turbo C++/Borland C++.                     */
  5. /* Copyright 1991-1993 by Carl W. Moreland                              */
  6. /*                                                                      */
  7. /* str.h                                                                */
  8. /* -------------------------------------------------------------------- */
  9.  
  10. #ifndef _STR_H
  11. #define _STR_H
  12.  
  13.  
  14. #include "strstream.h"
  15.  
  16. extern "C" 
  17.   {
  18.   #include <string.h>
  19.   }
  20.  
  21. #if defined (__ZTC__)
  22. #include "stream.hpp"            // Zortech
  23. #else
  24. #include "iostream.h"            // Borland and UNIX
  25. #endif
  26.  
  27.  
  28.  
  29. #undef toupper
  30. #undef tolower
  31.  
  32. #define LEFT_JUSTIFY    0        // obsolete - use LEFT
  33. #define CENTER_JUSTIFY    1        // obsolete - use CENTER
  34. #define RIGHT_JUSTIFY    2        // obsolete - use RIGHT
  35. #define LEFT        0
  36. #define CENTER          1
  37. #define RIGHT            2
  38. #define NOCLIP        0
  39. #define CLIP        1
  40. #define WHITESPACE    0
  41.  
  42. class string
  43. {
  44. private:
  45.   char *_ptr;
  46.   void ltos(long n);
  47.  
  48. public:
  49.   string();                // default constructor;
  50.   string(const char c,            // initialize with a character,
  51.          unsigned n = 1);        //   optional # of characters
  52.   string(const char *s,            // initialize with a char *,
  53.          unsigned pos = 0,        //   optional starting char
  54.          unsigned len = 10000);        //   optional # of chars
  55.   string(const string& s,        // initialize with another string,
  56.          unsigned pos = 0,        //   optional starting char
  57.          unsigned len = 10000);        //   optional # of chars
  58.   string(int n)  { ltos((long)n); }    // initialize with an integer
  59.   string(long n) { ltos(n); }        // initialize with a long int
  60.  
  61.  ~string(void);
  62.  
  63.   operator char*() const                     { return _ptr; }
  64.   char* ptr(void) const                      { return _ptr; }
  65.   const char* operator()() const             { return _ptr; }
  66.   const char* operator()(unsigned pos) const { return _ptr + pos; }
  67.   string      operator()(unsigned pos, unsigned len) const;
  68.  
  69.   int     Length(void) const { return strlen(_ptr); }
  70.   int     Len(void)    const { return strlen(_ptr); }
  71.   string& toUpper(void);        // convert str to uppercase
  72.   string& toLower(void);        // convert str to lowercase
  73.   int&    Value(int& n);        // convert str to an integer
  74.   long&   Value(long& n);        // convert str to a long int
  75.  
  76.   string& Left(unsigned len);        // left   len chars
  77.   string& Right(unsigned len);        // right  len chars
  78.   string& Mid(unsigned pos,        // middle len chars from pos
  79.               unsigned len);
  80.   string& Justify(int mode,        // justify str according to mode
  81.                   unsigned len,
  82.           int clip=0);
  83.   string& Trim(int mode = CENTER,    // Delete leading/trailing whitespace
  84.                char ch = WHITESPACE);
  85.  
  86.   string& Insert(unsigned pos,        // insert substring
  87.                  const string& s);
  88.   string& Delete(unsigned pos,        // delete substring
  89.                  unsigned len = 10000);
  90.   char*   Copy(char*&);            // copy str to char* (non-const)
  91.  
  92.   int     Index(const string& t);    // position of t in str
  93.   string  SubStr(unsigned p,        // substring of str at position p
  94.                  unsigned n=10000);
  95.   int     Split(string*& a,        // split str into an array a on
  96.                 const string& fs);    //   field separator fs
  97.   int     Sub(const string& from,    // substitute from with to in str
  98.               const string& to,
  99.           unsigned count=10000);
  100.  
  101.   string& operator=(const char);    // str1 = char
  102.   string& operator=(const char*);    // str1 = char*
  103.   string& operator=(const string&);    // str1 = str
  104.   string& operator=(const int);        // str1 = n
  105.   string& operator=(const long);    // str1 = n
  106.   string& operator=(const float);    // str1 = x
  107.   string& operator=(const double);    // str1 = x
  108.   string& operator+=(const char);    // str1 += char
  109.   string& operator+=(const char*);    // str1 += char*
  110.   string& operator+=(const string&);    // str1 += str
  111.   string& operator*=(unsigned n);    // str1 *= n
  112.   char&   operator[](unsigned i) const;    // ch = str[i] or str[i] = ch
  113.  
  114.   friend string operator +  (const string&, const string&);
  115.   friend string operator +  (const string&, const char*);
  116.   friend string operator +  (const char*,   const string&);
  117.   friend string operator *  (const string&, int n);
  118.   friend int    operator == (const string&, const string&);
  119.   friend int    operator == (const string&, const char*);
  120.   friend int    operator == (const char*,   const string&);
  121.   friend int    operator != (const string&, const string&);
  122.   friend int    operator != (const string&, const char*);
  123.   friend int    operator != (const char*,   const string&);
  124.   friend int    operator <  (const string&, const string&);
  125.   friend int    operator <  (const string&, const char*);
  126.   friend int    operator <  (const char*,   const string&);
  127.   friend int    operator >  (const string&, const string&);
  128.   friend int    operator >  (const string&, const char*);
  129.   friend int    operator >  (const char*,   const string&);
  130.   friend int    operator <= (const string&, const string&);
  131.   friend int    operator <= (const string&, const char*);
  132.   friend int    operator <= (const char*,   const string&);
  133.   friend int    operator >= (const string&, const string&);
  134.   friend int    operator >= (const string&, const char*);
  135.   friend int    operator >= (const char*,   const string&);
  136.  
  137.   /* --- Awk-style functions ------------------------------------------ */
  138.  
  139.   friend int    length(const string& s) { return s.Len(); }
  140.   friend int    index(const string& s, const string& t);
  141.   friend string substr(const string& s, unsigned p, unsigned n=10000);
  142.   friend int    split(const string& s, string*& a, const string& fs);
  143.   friend int    gsub(const string& from, const string& to, string& str, unsigned count=10000);
  144.   friend int    sub(const string& from, const string& to, string& str);
  145.   friend int    match(const string& s, const string& r);
  146.  
  147.   /* --- Other C-style functions -------------------------------------- */
  148.  
  149.   friend string toupper(const string& s);
  150.   friend string tolower(const string& s);
  151.   friend string left(const string& s, unsigned n);
  152.   friend string right(const string& s, unsigned n);
  153.   friend string mid(const string& s, unsigned p, unsigned n);
  154.   friend string justify(const string& s, int mode, unsigned len, int clip=1);
  155.   friend string trim(const string& s, int mode=CENTER);
  156.  
  157.   /* --- Obsolete naming conventions ---------------------------------- */
  158.  
  159.   int     length(void) const { return strlen(_ptr); }
  160.   int     len(void)    const { return strlen(_ptr); }
  161.  
  162.   string  left(unsigned len) const { string tmp; return tmp.Left(len); }
  163.   string  right(unsigned len) const { string tmp; return tmp.Right(len); }
  164.   string  mid(unsigned pos, unsigned len) const { string tmp; return tmp.Mid(pos, len); }
  165.   string  justify(int mode, unsigned len, int clip=0) const { string tmp; return tmp.Justify(mode, len, clip); }
  166.  
  167.   string& toupper(void) { return toUpper(); }
  168.   string& tolower(void) { return toLower(); }
  169. };
  170.  
  171. typedef string String;
  172.  
  173. ostream& operator<<(ostream&, const string&);
  174. istream& operator>>(istream&, string&);
  175.  
  176. inline int operator==(const string& s1, const char* s2) {
  177.   return strcmp(s1._ptr,s2)==0;
  178. }
  179.  
  180. inline int operator==(const char* s1, const string& s2) {
  181.   return strcmp(s1,s2._ptr)==0;
  182. }
  183.  
  184. inline int operator==(const string& s1, const string& s2) {
  185.   return strcmp(s1._ptr,s2._ptr)==0;
  186. }
  187.  
  188. inline int operator!=(const string& s1, const char *s2) {
  189.   return strcmp(s1._ptr,s2)!=0;
  190. }
  191.  
  192. inline int operator!=(const char *s1, const string& s2) {
  193.   return strcmp(s1,s2._ptr)!=0;
  194. }
  195.  
  196. inline int operator!=(const string& s1, const string& s2) {
  197.   return strcmp(s1._ptr,s2._ptr)!=0;
  198. }
  199.  
  200. inline int operator<(const string& s1, const char *s2) {
  201.   return strcmp(s1._ptr,s2)< 0;
  202. }
  203.  
  204. inline int operator<(const char *s1, const string& s2) {
  205.   return strcmp(s1,s2._ptr)< 0;
  206. }
  207.  
  208. inline int operator<(const string& s1, const string& s2) {
  209.   return strcmp(s1._ptr,s2._ptr)< 0;
  210. }
  211.  
  212. inline int operator>(const string& s1, const char *s2) {
  213.   return strcmp(s1._ptr,s2)> 0;
  214. }
  215.  
  216. inline int operator>(const char *s1, const string& s2) {
  217.   return strcmp(s1,s2._ptr)> 0;
  218. }
  219.  
  220. inline int operator>(const string& s1, const string& s2) {
  221.   return strcmp(s1._ptr,s2._ptr)> 0;
  222. }
  223.  
  224. inline int operator<=(const string& s1, const char *s2) {
  225.   return strcmp(s1._ptr,s2)<=0;
  226. }
  227.  
  228. inline int operator<=(const char *s1, const string& s2) {
  229.   return strcmp(s1,s2._ptr)<=0;
  230. }
  231.  
  232. inline int operator<=(const string& s1, const string& s2) {
  233.   return strcmp(s1._ptr,s2._ptr)<=0;
  234. }
  235.  
  236. inline int operator>=(const string& s1, const char *s2) {
  237.   return strcmp(s1._ptr,s2)>=0;
  238. }
  239.  
  240. inline int operator>=(const char *s1, const string& s2) {
  241.   return strcmp(s1,s2._ptr)>=0;
  242. }
  243.  
  244. inline int operator>=(const string& s1, const string& s2) {
  245.   return strcmp(s1._ptr,s2._ptr)>=0;
  246. }
  247.  
  248. inline int string::Index(const string& t) {
  249.   return index(*this, t);
  250. }
  251.  
  252. inline string string::SubStr(unsigned p, unsigned n) {
  253.   return substr(*this, p, n);
  254. }
  255.  
  256. inline int string::Split(string*& a, const string& fs) {
  257.   return split(*this, a, fs);
  258. }
  259.  
  260. inline int string::Sub(const string& from, const string& to, unsigned count) {
  261.   return gsub(from, to, *this, count);
  262. }
  263.  
  264. inline int sub(const string& from, const string& to, string& str) {
  265.   return gsub(from, to, str, 1);
  266. }
  267.  
  268. #endif
  269.