home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / emxtutor.zip / emxsrcd1.zip / emx / src / pmgdb / string.h < prev    next >
C/C++ Source or Header  |  1996-08-04  |  4KB  |  129 lines

  1. /* string.h -*- C++ -*-
  2.    Copyright (c) 1996 Eberhard Mattes
  3.  
  4. This file is part of pmgdb.
  5.  
  6. pmgdb is free software; you can redistribute it and/or modify
  7. it under the terms of the GNU General Public License as published by
  8. the Free Software Foundation; either version 2, or (at your option)
  9. any later version.
  10.  
  11. pmgdb is distributed in the hope that it will be useful,
  12. but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  14. GNU General Public License for more details.
  15.  
  16. You should have received a copy of the GNU General Public License
  17. along with pmgdb; see the file COPYING.  If not, write to
  18. the Free Software Foundation, 59 Temple Place - Suite 330,
  19. Boston, MA 02111-1307, USA.  */
  20.  
  21.  
  22. class bstring
  23. {
  24. public:
  25.   bstring () { str = NULL; }
  26.   virtual ~bstring () { delete[] str; }
  27.   char * modify () { return str; }
  28.   virtual void set (const char *s, int len) = 0;
  29.   virtual void set (int len) = 0;
  30.   virtual void set_null () = 0;
  31.   bool is_null () const { return str == NULL; }
  32.   int length () const { return strlen (str); }
  33.   void set (const char *s) { set (s, strlen (s)); }
  34.   operator const char * () const { return str; }
  35.   operator const unsigned char * () const { return (unsigned char *)str; }
  36.   const char operator [] (int index) const { return str[index]; }
  37.   bool operator == (const bstring &b) const;
  38.   bool operator != (const bstring &b) const { return !operator== (b); }
  39.  
  40. protected:
  41.   char *str;
  42. };
  43.  
  44. // A vstring is expected to be modified quite often
  45.  
  46. class vstring : public bstring
  47. {
  48. public:
  49.   vstring () { alloc = 0; }
  50.   ~vstring () {}
  51.   void set (const char *s) { bstring::set (s); }
  52.   void set (const char *s, int len);
  53.   void set (int len);
  54.   void set_null ();
  55.   vstring &operator = (const char *s) { set (s); return *this; }
  56.   vstring &operator = (const vstring &src);
  57.  
  58. protected:
  59.   int alloc;
  60. };
  61.  
  62. // An fstring is expected to be set only once
  63.  
  64. class fstring : public bstring
  65. {
  66. public:
  67.   fstring () {}
  68.   ~fstring () {}
  69.   void set (const char *s) { bstring::set (s); }
  70.   void set (const char *s, int len);
  71.   void set (int len);
  72.   void set_null ();
  73.   fstring &operator = (const char *s) { set (s); return *this; }
  74.   fstring &operator = (const fstring &src);
  75. };
  76.  
  77.  
  78. // An astring is expected to be appended to
  79.  
  80. class astring : public vstring
  81. {
  82. private:
  83.   typedef vstring parent;
  84. public:
  85.   astring () { cur_len = 0; }
  86.   ~astring () {}
  87.   void set (const char *s) { bstring::set (s); }
  88.   void set (const char *s, int len) { parent::set (s, len); cur_len = len; }
  89.   void set (int len) { parent::set (len); if (len < cur_len) cur_len = len; }
  90.   void set_null () { parent::set_null (); cur_len = 0; }
  91.   void append (const char *s, int len);
  92.   void append (const char *s) { append (s, strlen (s)); }
  93.   vstring &operator = (const char *s) { set (s); return *this; }
  94.   astring &operator = (const astring &src);
  95.   int length () const { return cur_len; }
  96.   void remove_trailing (char c);
  97.  
  98. private:
  99.   int cur_len;
  100. };
  101.  
  102.  
  103. // String consisting of lines
  104.  
  105. class lstring
  106. {
  107. public:
  108.   lstring () { str = NULL; vec = NULL; n = 0; }
  109.   lstring (const char *s);
  110.   lstring (const bstring &s);
  111.   lstring (const lstring &s);
  112.   ~lstring ();
  113.   lstring &operator = (const lstring &s);
  114.   void set (const char *s, size_t len);
  115.   void set (const char *s) { set (s, strlen (s)); }
  116.   int get_lines () const { return n; }
  117.   const char *operator [] (int index) const
  118.     { return (index >= 0 && index < n) ? vec[index] : NULL; }
  119.   void sort (int (*compare) (const char *, const char *));
  120.   void sort ();
  121.  
  122. private:
  123.   void assign (const lstring &s);
  124.   char *str;
  125.   const char **vec;
  126.   int n;
  127.   size_t str_len;
  128. };
  129.