home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / snip9707.zip / STR.H < prev    next >
C/C++ Source or Header  |  1997-07-05  |  12KB  |  505 lines

  1. /* +++Date last modified: 05-Jul-1997 */
  2.  
  3. //
  4. // Simple string class
  5. // Public domain
  6. //
  7. // Written by david nugent
  8. // davidn@csource.pronet.com
  9. // 3:632/348@fidonet
  10. //
  11.  
  12. # if !defined( _str_h )
  13. # define _str_h 1
  14. # if !defined( _Bool_defined )
  15. enum Bool { False, True };
  16. # endif
  17.  
  18. # if defined( __BORLANDC__ ) && ( __BORLANDC__ <= 0x3000 )
  19. # define PLACEMENT_NEW_BUG
  20. # define SIGNED_CHAR_BUG
  21. # endif
  22.  
  23. // Define to 0 for much smaller class
  24. #define VIRTUAL_DESTRUCTOR  0
  25. #if VIRTUAL_DESTRUCTOR
  26. #define __VIRTUAL   virtual
  27. #else
  28. #define __VIRTUAL
  29. #endif
  30.  
  31. struct refstr
  32. {
  33.     short _size;
  34.     short _length;
  35.     short _refs;
  36.     unsigned short _flags;
  37.  
  38.     enum rsflags { ICASE=1 };
  39.  
  40.     refstr (short length, short size, unsigned short flgs =0)
  41.         : _length(length), _size(size), _refs(1), _flags(flgs)
  42.         {}
  43.     ~refstr (void) {}
  44.     unsigned short flags() const
  45.     {
  46.         return _flags;
  47.     }
  48.     void setf(unsigned short f)
  49.     {
  50.         _flags = (unsigned short)(_flags | f);
  51.     }
  52.     void resetf(unsigned short f)
  53.     {
  54.         _flags = (unsigned short)(_flags & ~f);
  55.     }
  56. # if !defined( PLACEMENT_NEW_BUG )
  57.     void * operator new(unsigned sz, short allocsz);
  58. # endif
  59.     char * ptr (void)
  60.     {
  61.         return ((char *)this) + sizeof(refstr);
  62.     }
  63. };
  64.  
  65.  
  66.  
  67.  
  68. class str
  69. {
  70.  
  71.   public:
  72.  
  73.         // constructors/destructors
  74.  
  75.     str (void);
  76.     str (char const * s, short len =-1);
  77.     str (unsigned char const * s, short len =-1);
  78. # if !defined( SIGNED_CHAR_BUG )
  79.     str (signed char const * s, short len =-1);
  80. # endif
  81.     str (int val, int radix =10);
  82.     str (unsigned int val, int radix =10);
  83.     str (short val, int radix =10);
  84.     str (unsigned short val, int radix =10);
  85.     str (long val, int radix =10);
  86.     str (unsigned long val, int radix =10);
  87.     str (char c);
  88.     str (unsigned char c);
  89. # if !defined( SIGNED_CHAR_BUG )
  90.     str (signed char c);
  91. # endif
  92.     str (str const & s);
  93.     __VIRTUAL ~str (void);
  94.  
  95.     str & clear(void);
  96.  
  97.         // assignment
  98.  
  99.     str & operator= (str const & s);
  100.     str & operator= (char const * s);
  101.     str & operator= (char c);
  102.  
  103.     str & operator= (unsigned char const * s)
  104.     {
  105.         return operator= ((char const *)s);
  106.     }
  107. # if !defined( SIGNED_CHAR_BUG )
  108.     str & operator= (signed char const * s)
  109.     {
  110.         return operator= ((char const *)s);
  111.     }
  112. # endif
  113.  
  114.         // primitive members
  115.  
  116.     short length (void) const
  117.     {
  118.         return strdata->_length;
  119.     }
  120.  
  121.     short size (void) const
  122.     {
  123.         return strdata->_size;
  124.     }
  125.  
  126.         // operators
  127.  
  128.     str & operator<< (char const * s);              // concatenate
  129.     str & operator<< (unsigned char const * s);
  130. # if !defined( SIGNED_CHAR_BUG )
  131.     str & operator<< (signed char const * s);
  132. # endif
  133.     str & operator<< (str const & s);
  134.     str & operator<< (int val);
  135.     str & operator<< (unsigned int val);
  136.     str & operator<< (short val);
  137.     str & operator<< (unsigned short val);
  138.     str & operator<< (long val);
  139.     str & operator<< (unsigned long val);
  140.     str & operator<< (char c);
  141.     str & operator<< (unsigned char c);
  142. # if !defined( SIGNED_CHAR_BUG )
  143.     str & operator<< (signed char c);
  144. # endif
  145.  
  146.     char const & operator[] (short pos) const;
  147.     char & operator[] (short pos);
  148.  
  149.     char * c_ptr (void) const            // not necessarily NUL terminated!
  150.     {                                                // Use with caution...
  151.         return strdata->ptr();
  152.     }
  153.     char const * c_str (void) const    // return char*
  154.     {
  155.         char * buf = c_ptr();
  156.         buf[strdata->_length] = 0;
  157.         return buf;
  158.     }
  159.     unsigned char const * u_str (void) const
  160.     {
  161.         return (unsigned char const *)c_str();
  162.     }
  163. # if !defined( SIGNED_CHAR_BUG )
  164.     signed char const * s_str (void) const
  165.     {
  166.         return (signed char const *)c_str();
  167.     }
  168. # endif
  169.  
  170.     int copy(char * dest, short maxlen =-1) const;
  171.  
  172.         // manipulators
  173.  
  174.     short insert (short pos, char const * s, short len =-1);
  175.     short insert (short pos, str const & s)
  176.     {
  177.         return insert (pos, s.c_ptr(), s.length());
  178.     }
  179.     short insert (short pos, unsigned char const * s, short len =-1)
  180.     {
  181.         return insert (pos, (char const *)s, len);
  182.     }
  183. # if !defined( SIGNED_CHAR_BUG )
  184.     short insert (short pos, signed char const * s,
  185.                     short len =-1)
  186.     {
  187.         return insert (pos, (char const *)s, len);
  188.     }
  189. # endif
  190.     short insert (short pos, char c)
  191.     {
  192.         return insert (pos, &c, 1);
  193.     }
  194.     short insert (short pos, unsigned char c)
  195.     {
  196.         return insert (pos, (char const *)&c, 1);
  197.     }
  198. # if !defined( SIGNED_CHAR_BUG )
  199.     short insert (short pos, signed char c)
  200.     {
  201.         return insert (pos, (char const *)&c, 1);
  202.     }
  203. # endif
  204.  
  205.     short remove (short pos =0, short len =-1);
  206.     short replace (short pos, char const * s, short clen =-1, short len =-1);
  207.     short replace (short pos, str & s, short clen =-1)
  208.     {
  209.         return replace (pos, s.c_ptr(), clen, s.length());
  210.     }
  211.     short replace (short pos, unsigned char const * s, short clen =-1, short len =-1)
  212.     {
  213.         return replace (pos, (char const *)s, clen, len);
  214.     }
  215. # if !defined( SIGNED_CHAR_BUG )
  216.     short replace (short pos, signed char const * s, short clen =-1, short len =-1)
  217.     {
  218.         return replace (pos, (char const *)s, clen, len);
  219.     }
  220. # endif
  221.     short replace (short pos, char c, short clen =-1)
  222.     {
  223.         return replace (pos, &c, clen, 1);
  224.     }
  225.     short replace (short pos, unsigned char c, short clen =-1)
  226.     {
  227.         return replace (pos, (char const *)&c, clen, 1);
  228.     }
  229. # if !defined( SIGNED_CHAR_BUG )
  230.     short replace (short pos, signed char c, short clen =-1)
  231.     {
  232.         return replace (pos, (char const *)&c, clen, 1);
  233.     }
  234. # endif
  235.  
  236.     str & left (short len, char padch =' ');
  237.     str & right (short len, char padch =' ');
  238.     str & mid (short pos, short len, char padch =' ');
  239.  
  240.     str substr(short start, short len =-1) const;
  241.  
  242.     short removech (char const * clist ="\r\n");
  243.     short countch (char const * clist);
  244.  
  245.     Bool operator== (str const & s) const
  246.     {
  247.         return (_compare(s) == 0) ? True : False;
  248.     }
  249.     Bool operator== (char const * s) const
  250.     {
  251.         return (_compare(s) == 0) ? True : False;
  252.     }
  253.     Bool operator== (unsigned char const * s) const
  254.     {
  255.         return (_compare(s) == 0) ? True : False;
  256.     }
  257. # if !defined( SIGNED_CHAR_BUG )
  258.     Bool operator== (signed char const * s) const
  259.     {
  260.         return (_compare(s) == 0) ? True : False;
  261.     }
  262. #endif
  263.  
  264.     Bool operator!= (str const & s) const
  265.     {
  266.         return (_compare(s) != 0) ? True : False;
  267.     }
  268.     Bool operator!= (char const * s) const
  269.     {
  270.         return (_compare(s) != 0) ? True : False;
  271.     }
  272.     Bool operator!= (unsigned char const * s) const
  273.     {
  274.         return (_compare(s) != 0) ? True : False;
  275.     }
  276. # if !defined( SIGNED_CHAR_BUG )
  277.     Bool operator!= (signed char const * s) const
  278.     {
  279.         return (_compare(s) != 0) ? True : False;
  280.     }
  281. #endif
  282.  
  283.     Bool operator< (str const & s) const
  284.     {
  285.         return (_compare(s) < 0) ? True : False;
  286.     }
  287.     Bool operator< (char const * s) const
  288.     {
  289.         return (_compare(s) < 0) ? True : False;
  290.     }
  291.     Bool operator< (unsigned char const * s) const
  292.     {
  293.         return (_compare(s) < 0) ? True : False;
  294.     }
  295. # if !defined( SIGNED_CHAR_BUG )
  296.     Bool operator< (signed char const * s) const
  297.     {
  298.         return (_compare(s) < 0) ? True : False;
  299.     }
  300. #endif
  301.  
  302.     Bool operator<= (str const & s) const
  303.     {
  304.         return (_compare(s) <= 0) ? True : False;
  305.     }
  306.     Bool operator<= (char const * s) const
  307.     {
  308.         return (_compare(s) <= 0) ? True : False;
  309.     }
  310.     Bool operator<= (unsigned char const * s) const
  311.     {
  312.         return (_compare(s) <= 0) ? True : False;
  313.     }
  314. # if !defined( SIGNED_CHAR_BUG )
  315.     Bool operator<= (signed char const * s) const
  316.     {
  317.         return (_compare(s) <= 0) ? True : False;
  318.     }
  319. #endif
  320.  
  321.     Bool operator> (str const & s) const
  322.     {
  323.         return (_compare(s) > 0) ? True : False;
  324.     }
  325.     Bool operator> (char const * s) const
  326.     {
  327.         return (_compare(s) > 0) ? True : False;
  328.     }
  329.     Bool operator> (unsigned char const * s) const
  330.     {
  331.         return (_compare(s) > 0) ? True : False;
  332.     }
  333. # if !defined( SIGNED_CHAR_BUG )
  334.     Bool operator> (signed char const * s) const
  335.     {
  336.         return (_compare(s) > 0) ? True : False;
  337.     }
  338. #endif
  339.  
  340.     Bool operator>= (str const & s) const
  341.     {
  342.         return (_compare(s) >= 0) ? True : False;
  343.     }
  344.     Bool operator>= (char const * s) const
  345.     {
  346.         return (_compare(s) >= 0) ? True : False;
  347.     }
  348.     Bool operator>= (unsigned char const * s) const
  349.     {
  350.         return (_compare(s) >= 0) ? True : False;
  351.     }
  352. # if !defined( SIGNED_CHAR_BUG )
  353.     Bool operator>= (signed char const * s) const
  354.     {
  355.         return (_compare(s) >= 0) ? True : False;
  356.     }
  357. #endif
  358.  
  359.     int compare (str const & s) const
  360.     {
  361.         return _compare(s);
  362.     }
  363.     int compare (char const * s) const
  364.     {
  365.         return _compare(str(s));
  366.     }
  367.     int compare (unsigned char const * s) const
  368.     {
  369.         return _compare(str(s));
  370.     }
  371. # if !defined( SIGNED_CHAR_BUG )
  372.     int compare (signed char const * s) const
  373.     {
  374.         return _compare(str(s));
  375.     }
  376. #endif
  377.  
  378.     short strstr (str const & s) const
  379.     {
  380.         return _strstr(s);
  381.     }
  382.     short strstr (char const * s) const
  383.     {
  384.         return _strstr(str(s));
  385.     }
  386.     short strstr (unsigned char const * s) const
  387.     {
  388.         return _strstr(str(s));
  389.     }
  390. # if !defined( SIGNED_CHAR_BUG )
  391.     short strstr (signed char const * s) const
  392.     {
  393.         return _strstr(str(s));
  394.     }
  395. #endif
  396.  
  397.     void setflags (unsigned short flags);
  398.     void resetflags (unsigned short flags);
  399.  
  400.     void setcase (Bool s =True)
  401.     {
  402.         if (s)
  403.             setflags(refstr::ICASE);
  404.         else
  405.             resetflags(refstr::ICASE);
  406.     }
  407.  
  408.     static void setdefaultcase (Bool s = True)
  409.     {
  410.         default_flags = (s == False)
  411.                         ? (unsigned short)(default_flags | refstr::ICASE)
  412.                         : (unsigned short)(default_flags & ~refstr::ICASE);
  413.     }
  414.  
  415.   protected:
  416.  
  417.     static unsigned short default_flags;
  418.     refstr * strdata;
  419.  
  420.     // Check to see if big enough for size
  421.     int _chksize (short sz =0);
  422.     int _concat (char const * s, short len =-1);
  423.     int _concat (str const & s)
  424.     {
  425.         return _concat (s.c_ptr(), s.length());
  426.     }
  427.     int _concat (char ch)
  428.     {
  429.         return _concat (&ch, 1);
  430.     }
  431.     int _concat (unsigned char const * s, short len =-1)
  432.     {
  433.         return _concat ((char const *)s, len);
  434.     }
  435. # if !defined( SIGNED_CHAR_BUG )
  436.     int _concat (signed char const * s, short len =-1)
  437.     {
  438.         return _concat ((char const *)s, len);
  439.     }
  440. # endif
  441.  
  442.     int _compare (str const s) const;
  443.     short _strstr (str const s) const;
  444.  
  445.         // Common constructor
  446.  
  447.     void _strinit (char const * s =0, short slen =0,
  448.                     short siz =-1, unsigned short flgs =default_flags);
  449.     void _strinit (unsigned long val, Bool positive, int radix);
  450.  
  451. };
  452.  
  453.  
  454. inline str
  455. left (str s, short len, char padch =' ')
  456. {
  457.     return s.left(len, padch);
  458. }
  459.  
  460. inline str
  461. right (str s, short len, char padch =' ')
  462. {
  463.     return s.right(len, padch);
  464. }
  465.  
  466. inline str
  467. mid (str s, short pos, short len, char padch =' ')
  468. {
  469.     return s.mid(pos, len, padch);
  470. }
  471.  
  472. inline int
  473. compare(str s, str b)
  474. {
  475.     return s.compare(b);
  476. }
  477.  
  478. inline int
  479. compare(str s, char const * b)
  480. {
  481.     return s.compare(b);
  482. }
  483.  
  484. inline int
  485. compare(str s, unsigned char const * b)
  486. {
  487.     return s.compare(b);
  488. }
  489.  
  490. # if !defined( SIGNED_CHAR_BUG )
  491. inline int
  492. compare(str s, signed char const * b)
  493. {
  494.     return s.compare(b);
  495. }
  496. # endif
  497.  
  498. class ostream;
  499. class istream;
  500.  
  501. ostream & operator<< (ostream & os, str const & s);
  502. istream & operator>> (istream & is, str & s);
  503.  
  504. # endif
  505.