home *** CD-ROM | disk | FTP | other *** search
/ C!T ROM 2 / ctrom_ii_b.zip / ctrom_ii_b / PROGRAM / C / STRPP11 / STR.H < prev    next >
C/C++ Source or Header  |  1992-03-23  |  4KB  |  104 lines

  1. /* -------------------------------------------------------------------- */
  2. /* str.h                                */
  3. /*                                    */
  4. /* String++ Version 1.1                Last revised 03/23/92    */
  5. /*                                    */
  6. /* Enhanced string class for Turbo C++/Borland C++.            */
  7. /* Copyright 1991, 1992 by Carl W. Moreland                */
  8. /* This source code may be freely distributed as long as the copyright    */
  9. /* notice remains intact.                        */
  10. /* -------------------------------------------------------------------- */
  11.  
  12. #ifndef STRdotH
  13. #define STRdotH
  14.  
  15. #include <string.h>
  16.  
  17. #define LEFT_JUSTIFY    0
  18. #define CENTER_JUSTIFY    1
  19. #define RIGHT_JUSTIFY    2
  20. #define NOCLIP        0
  21. #define CLIP        1
  22.  
  23. class string
  24. {
  25. private:
  26.   char *_ptr;
  27.  
  28. public:
  29.   string();                // default constructor;
  30.   string(const char c,            // initialize with a character,
  31.          unsigned int n = 1);        //   optional # of characters
  32.   string(const char *s,            // initialize with a char *,
  33.          unsigned int n = 1);        //   optional # of copies
  34.   string(const string& s,        // initialize with another string,
  35.          unsigned int n = 1);        //   optional # of copies
  36.  ~string(void);
  37.  
  38.   const char* ptr(void) const         { return _ptr; }
  39.   const char* operator()() const      { return _ptr; }
  40.   const char* operator()(int n) const { return _ptr + n; }
  41.  
  42.   int    length(void) const { return strlen(_ptr); }
  43.   int    len(void)    const { return strlen(_ptr); }
  44.   string right(int len) const;        // right len chars
  45.   string left(int len) const;        // left  len chars
  46.   string mid(int pos, int len) const;    // middle len chars from pos
  47.   string justify(int mode, int len, int clip=0) const;
  48.  
  49.   string& toupper(void);        // convert the string to uppercase
  50.   string& tolower(void);        // convert the string to lowercase
  51.  
  52.   string& operator=(const char);    // str1 = char
  53.   string& operator=(const char*);    // str1 = char*
  54.   string& operator=(const string&);    // str1 = str
  55.   string& operator+=(const char);    // str1 += char
  56.   string& operator+=(const char*);    // str1 += char*
  57.   string& operator+=(const string&);    // str1 += str
  58.   string& operator*=(int n);        // str1 *= n
  59.   const char operator[](int) const;    // ch = str[i]
  60.  
  61.   friend string operator +  (const string&, const string&);
  62.   friend string operator +  (const string&, const char*);
  63.   friend string operator +  (const char*,   const string&);
  64.   friend string operator *  (const string&, int n);
  65.   friend int    operator == (const string&, const string&);
  66.   friend int    operator == (const string&, const char*);
  67.   friend int    operator == (const char*,   const string&);
  68.   friend int    operator != (const string&, const string&);
  69.   friend int    operator != (const string&, const char*);
  70.   friend int    operator != (const char*,   const string&);
  71.   friend int    operator <  (const string&, const string&);
  72.   friend int    operator <  (const string&, const char*);
  73.   friend int    operator <  (const char*,   const string&);
  74.   friend int    operator >  (const string&, const string&);
  75.   friend int    operator >  (const string&, const char*);
  76.   friend int    operator >  (const char*,   const string&);
  77.   friend int    operator <= (const string&, const string&);
  78.   friend int    operator <= (const string&, const char*);
  79.   friend int    operator <= (const char*,   const string&);
  80.   friend int    operator >= (const string&, const string&);
  81.   friend int    operator >= (const string&, const char*);
  82.   friend int    operator >= (const char*,   const string&);
  83.  
  84.   /* --- Awk-style functions ------------------------------------------ */
  85.  
  86.   friend int    length(const string& s) { return s.len(); }
  87.   friend int    index(const string& s, const string& t);
  88.   friend string substr(const string& s, unsigned int p, unsigned int n=10000);
  89.   friend int    split(const string& s, string **a, const string& fs);
  90.   friend int    sub(const string& from, const string& to, string& str);
  91.   friend int    gsub(const string& from, const string& to, string& str, int count=10000);
  92.  
  93.   /* --- Other C-style functions -------------------------------------- */
  94.  
  95.   friend string toupper(const string& s);
  96.   friend string tolower(const string& s);
  97.   friend string right(const string& s, int n);
  98.   friend string left(const string& s, int n);
  99.   friend string mid(const string& s,int p,int n);
  100.   friend string justify(const string& s, int mode, int len, int clip=1);
  101. };
  102.  
  103. #endif
  104.