home *** CD-ROM | disk | FTP | other *** search
/ Monster Media 1994 #1 / monster.zip / monster / PROG_C / SNPD9404.ZIP / STR.H < prev    next >
C/C++ Source or Header  |  1994-04-03  |  7KB  |  323 lines

  1. //
  2. // Simple string class
  3. // Public domain
  4. //
  5. // Written by david nugent
  6. // davidn@csource.pronet.com
  7. // 3:632/348@fidonet
  8. //
  9.  
  10. # if !defined( _str_h )
  11. # define _str_h 1
  12.  
  13. # if defined( __BORLANDC__ ) && ( __BORLANDC__ <= 0x0300 )
  14. # define PLACEMENT_NEW_BUG
  15. # define SIGNED_CHAR_BUG
  16. # endif
  17.  
  18.  
  19. struct refstr
  20. {
  21.     short _size;
  22.     short _length;
  23.     short _refs;
  24.     refstr (short length, short size)
  25.         : _length(length), _size(size), _refs(1)
  26.         {}
  27.     ~refstr (void) {}
  28. # if !defined( PLACEMENT_NEW_BUG )
  29.     void * operator new(unsigned sz, short allocsz);
  30. # endif
  31.     char * ptr (void)
  32.     {
  33.         return ((char *)this) + sizeof(refstr);
  34.     }
  35. };
  36.  
  37.  
  38.  
  39.  
  40. class str
  41. {
  42.  
  43.   public:
  44.  
  45.         // constructors/destructors
  46.  
  47.     str (void)
  48.     {
  49.         _strinit();
  50.     }
  51.  
  52.     str (char const * s, short len =-1)
  53.     {
  54.         _strinit(s, len, -1);
  55.     }
  56.  
  57.     str (unsigned char const * s, short len =-1)
  58.     {
  59.         _strinit((char const *)s, len, -1);
  60.     }
  61. # if !defined( SIGNED_CHAR_BUG )
  62.     str (signed char const * s, short len =-1)
  63.     {
  64.         _strinit((char const *)s, len, -1);
  65.     }
  66. # endif
  67.  
  68.     str (char c)
  69.     {
  70.         _strinit (&c, 1, -1);
  71.     }
  72.  
  73.     str (unsigned char c)
  74.     {
  75.         _strinit ((char const *)&c, 1, -1);
  76.     }
  77. # if !defined( SIGNED_CHAR_BUG )
  78.     str (signed char c)
  79.     {
  80.         _strinit ((char const *)&c, 1, -1);
  81.     }
  82. # endif
  83.     str (str const & s)
  84.       : strdata(s.strdata)
  85.     {
  86.         ++strdata->_refs;
  87.     }
  88.  
  89.     ~str (void)
  90.     {
  91.         if (!--strdata->_refs)
  92.             delete strdata;
  93.     }
  94.  
  95.         // assignment
  96.  
  97.     str & operator= (str const & s);
  98.     str & operator= (char const * s);
  99.     str & operator= (char c);
  100.  
  101.     str & operator= (unsigned char const * s)
  102.     {
  103.         return operator= ((char const *)s);
  104.     }
  105. # if !defined( SIGNED_CHAR_BUG )
  106.     str & operator= (signed char const * s)
  107.     {
  108.         return operator= ((char const *)s);
  109.     }
  110. # endif
  111.  
  112.         // primitive members
  113.  
  114.     short length (void) const
  115.     {
  116.         return strdata->_length;
  117.     }
  118.  
  119.     short size (void) const
  120.     {
  121.         return strdata->_size;
  122.     }
  123.  
  124.         // operators
  125.  
  126.     str & operator<< (char const * s)   // concatenate
  127.     {
  128.         _concat (s);
  129.         return *this;
  130.     }
  131.  
  132.     str & operator<< (unsigned char const * s)
  133.     {
  134.         _concat ((char const *)s);
  135.         return *this;
  136.     }
  137. # if !defined( SIGNED_CHAR_BUG )
  138.     str & operator<< (signed char const * s)
  139.     {
  140.         _concat ((char const *)s);
  141.         return *this;
  142.     }
  143. # endif
  144.     str & operator<< (str const & s)
  145.     {
  146.         _concat (s);
  147.         return *this;
  148.     }
  149.  
  150.     str & operator<< (char c)
  151.     {
  152.         _concat (c);
  153.         return *this;
  154.     }
  155.  
  156.     str & operator<< (unsigned char c)
  157.     {
  158.         _concat (c);
  159.         return *this;
  160.     }
  161.  
  162. # if !defined( SIGNED_CHAR_BUG )
  163.     str & operator<< (signed char c)
  164.     {
  165.         _concat (c);
  166.         return *this;
  167.     }
  168. # endif
  169.  
  170.     char & operator[] (short pos)
  171.     {
  172.         if (pos < 0 || pos >= strdata->_length)
  173.         {
  174.             char * buf = c_ptr() + length();
  175.             *buf = 0;
  176.             return *buf;
  177.         }
  178.         return c_ptr()[pos];
  179.     }
  180.  
  181.     char * c_ptr (void) const   // not necessarily NUL terminated!
  182.     {                           // Use with caution...
  183.         return strdata->ptr();
  184.     }
  185.  
  186.     char const * c_str (void) const    // return char*
  187.     {
  188.         char * buf = c_ptr();
  189.         buf[strdata->_length] = 0;
  190.         return buf;
  191.     }
  192.  
  193.     unsigned char const * u_str (void) const
  194.     {
  195.         return (unsigned char const *)c_str();
  196.     }
  197.  
  198.         // manipulators
  199.  
  200.     short insert (short pos, char const * s, short len =-1);
  201.  
  202.     short insert (short pos, str const & s)
  203.     {
  204.         return insert (pos, s.c_ptr(), s.length());
  205.     }
  206.  
  207.     short insert (short pos, unsigned char const * s,
  208.                     short len =-1)
  209.     {
  210.         return insert (pos, (char const *)s, len);
  211.     }
  212. # if !defined( SIGNED_CHAR_BUG )
  213.     short insert (short pos, signed char const * s,
  214.                     short len =-1)
  215.     {
  216.         return insert (pos, (char const *)s, len);
  217.     }
  218. # endif
  219.     short insert (short pos, char c)
  220.     {
  221.         return insert (pos, &c, 1);
  222.     }
  223.  
  224.     short insert (short pos, unsigned char c)
  225.     {
  226.         return insert (pos, (char const *)&c, 1);
  227.     }
  228. # if !defined( SIGNED_CHAR_BUG )
  229.     short insert (short pos, signed char c)
  230.     {
  231.         return insert (pos, (char const *)&c, 1);
  232.     }
  233. # endif
  234.  
  235.     short remove (short pos =0, short len =-1);
  236.  
  237.     short replace (short pos, char const * s, short clen =-1,
  238.                     short len =-1);
  239.  
  240.     short replace (short pos, str & s, short clen =-1)
  241.     {
  242.         return replace (pos, s.c_ptr(), clen, s.length());
  243.     }
  244.  
  245.     short replace (short pos, unsigned char const * s,
  246.                     short clen =-1, short len =-1)
  247.     {
  248.         return replace (pos, (char const *)s, clen, len);
  249.     }
  250. # if !defined( SIGNED_CHAR_BUG )
  251.     short replace (short pos, signed char const * s,
  252.                     short clen =-1, short len =-1)
  253.     {
  254.         return replace (pos, (char const *)s, clen, len);
  255.     }
  256. # endif
  257.     short replace (short pos, char c, short clen =-1)
  258.     {
  259.         return replace (pos, &c, clen, 1);
  260.     }
  261.  
  262.     short replace (short pos, unsigned char c, short clen =-1)
  263.     {
  264.         return replace (pos, (char const *)&c, clen, 1);
  265.     }
  266. # if !defined( SIGNED_CHAR_BUG )
  267.     short replace (short pos, signed char c, short clen =-1)
  268.     {
  269.         return replace (pos, (char const *)&c, clen, 1);
  270.     }
  271. # endif
  272.  
  273.     str & left (short len, char padch =' ');
  274.     str & right (short len, char padch =' ');
  275.     str & mid (short pos, short len, char padch =' ');
  276.  
  277.     short removech (char const * clist ="\r\n");
  278.     short countch (char const * clist);
  279.  
  280.   protected:
  281.  
  282.     refstr * strdata;
  283.  
  284.     // Check to see if big enough for size
  285.     int _chksize (short sz =0);
  286.  
  287.     int _concat (char const * s, short len =-1);
  288.  
  289.     int _concat (str const & s)
  290.     {
  291.         return _concat (s.c_ptr(), s.length());
  292.     }
  293.  
  294.     int _concat (char ch)
  295.     {
  296.         return _concat (&ch, 1);
  297.     }
  298.  
  299.     int _concat (unsigned char const * s, short len =-1)
  300.     {
  301.         return _concat ((char const *)s, len);
  302.     }
  303. # if !defined( SIGNED_CHAR_BUG )
  304.     int _concat (signed char const * s, short len =-1)
  305.     {
  306.         return _concat ((char const *)s, len);
  307.     }
  308. # endif
  309.  
  310.   private:
  311.         // Common constructor
  312.     void _strinit (char const * s =0, short slen =0,
  313.                     short siz =-1);
  314.  
  315. };
  316.  
  317.  
  318. str left (str const & s, short len, char padch =' ');
  319. str right (str const & s, short len, char padch =' ');
  320. str mid (str const & s, short pos, short len, char padch =' ');
  321.  
  322. # endif
  323.