home *** CD-ROM | disk | FTP | other *** search
/ PC World Komputer 1998 May / Pcwk5b98.iso / Borland / Cplus45 / BC45 / INC.PAK / CSTRING.H < prev    next >
C/C++ Source or Header  |  1995-08-29  |  37KB  |  1,165 lines

  1. /*------------------------------------------------------------------------*/
  2. /*                                                                        */
  3. /*  CSTRING.H                                                             */
  4. /*                                                                        */
  5. /*                                                                        */
  6. /*------------------------------------------------------------------------*/
  7.  
  8. /*
  9.  *      C/C++ Run Time Library - Version 6.5
  10.  *
  11.  *      Copyright (c) 1993, 1994 by Borland International
  12.  *      All Rights Reserved.
  13.  *
  14.  */
  15.  
  16. #ifndef __cplusplus
  17. #error Must use C++ for CSTRING.H
  18. #endif
  19.  
  20. #ifndef __CSTRING_H
  21. #define __CSTRING_H
  22.  
  23. /*------------------------------------------------------------------------*/
  24. /*                                                                        */
  25. /*  This class uses a technique called "copy-on-write".                   */
  26. /*  Multiple instances of a string can refer to the same piece of data    */
  27. /*  so long as it is in a "readonly" situation.  If a string writes to    */
  28. /*  the data, then a copy is automatically made if more than one string   */
  29. /*  is referring to it.                                                   */
  30. /*                                                                        */
  31. /*------------------------------------------------------------------------*/
  32.  
  33. #define STRICT
  34.  
  35. #if !defined(__STRING_H)
  36. #include <string.h>
  37. #endif
  38.  
  39. #if !defined(__REF_H)
  40. #include <ref.h>
  41. #endif
  42.  
  43. #if !defined(__CTYPE_H)
  44. #include <ctype.h>
  45. #endif
  46.  
  47. #if !defined(__STDDEF_H)
  48. #include <stddef.h>
  49. #endif
  50.  
  51. #if !defined(__EXCEPT_H)
  52. #include <except.h>
  53. #endif
  54.  
  55. #if defined(_Windows) && !defined(__WINDOWS_H)    
  56. #include <windows.h>
  57. #endif
  58.  
  59.  
  60. #if !defined(RC_INVOKED)
  61.  
  62. #pragma option -a-      // byte packing
  63.  
  64. #if defined(__BCOPT__)
  65. #if !defined(_RTL_ALLOW_po) && !defined(__FLAT__)
  66. #pragma option -po-     // disable Object data calling convention
  67. #endif
  68. #endif
  69.  
  70. #if !defined(__TINY__)
  71. #pragma option -RT
  72. #endif
  73.  
  74. #pragma option -Vo-     // set standard C++ options
  75.  
  76. #if defined(__STDC__)
  77. #pragma warn -nak
  78. #endif
  79.  
  80. #endif  /* !RC_INVOKED */
  81.  
  82.  
  83. class _EXPCLASS string;
  84. class _EXPCLASS TRegexp;
  85. class _EXPCLASS TSubString;
  86. class _EXPCLASS TStringRef;
  87. class _EXPCLASS istream;
  88. class _EXPCLASS ostream;
  89.  
  90. /*------------------------------------------------------------------------*/
  91. /*                                                                        */
  92. /*  Since inline functions that use throw specifiers                      */
  93. /*  currently end up rather large, we don't use throw                     */
  94. /*  specifiers on them unless you #define USE_THROW_SPECIFIERS.           */
  95. /*                                                                        */
  96. /*------------------------------------------------------------------------*/
  97.  
  98. #if defined( USE_THROW_SPECIFIERS )
  99. #define THROW_NONE                  throw()
  100. #define THROW_XALLOC                throw(xalloc)
  101. #define THROW_OUTOFRANGE            throw( string::outofrange )
  102. #define THROW_XALLOC_LENGTHERROR    throw( xalloc, string::lengtherror )
  103. #define THROW_XALLOC_OUTOFRANGE     throw( xalloc, string::outofrange )
  104. #define THROW_XALLOC_RANGE_LENGTH   \
  105.         throw( xalloc, string::lengtherror, string::outofrange )
  106. #else
  107. #define THROW_NONE
  108. #define THROW_XALLOC
  109. #define THROW_OUTOFRANGE
  110. #define THROW_XALLOC_LENGTHERROR
  111. #define THROW_XALLOC_OUTOFRANGE
  112. #define THROW_XALLOC_RANGE_LENGTH
  113. #endif
  114.  
  115.  
  116. /*------------------------------------------------------------------------*/
  117. /*                                                                        */
  118. /*  string                                                                */
  119. /*                                                                        */
  120. /*------------------------------------------------------------------------*/
  121.  
  122. const size_t NPOS = size_t(-1);
  123.  
  124. class _EXPCLASS string
  125. {
  126.  
  127. public:
  128.  
  129.     //
  130.     // Exceptions
  131.     //
  132.     class outofrange : public xmsg
  133.     {
  134.     public:
  135.         _RTLENTRY outofrange();
  136.     };
  137.  
  138.     class lengtherror : public xmsg
  139.     {
  140.     public:
  141.         _RTLENTRY lengtherror();
  142.     };
  143.  
  144.     //
  145.     // Constructors
  146.     //
  147.  
  148.     _RTLENTRY string() throw( xalloc );
  149.  
  150.     _RTLENTRY string( const string _FAR &s ) throw( xalloc );
  151.     _RTLENTRY string( const string _FAR &s, size_t orig, size_t n = NPOS )
  152.         throw( xalloc );
  153.  
  154.     _RTLENTRY string( const char _FAR *cp ) throw( xalloc, lengtherror );
  155.     _RTLENTRY string( const char _FAR *cp, size_t orig, size_t n = NPOS )
  156.         throw( xalloc, lengtherror );
  157.  
  158.     _RTLENTRY string( char c ) THROW_XALLOC_LENGTHERROR;
  159.     _RTLENTRY string( char c, size_t n ) THROW_XALLOC_LENGTHERROR;
  160.  
  161.     _RTLENTRY string( signed char c ) THROW_XALLOC_LENGTHERROR;
  162.     _RTLENTRY string( signed char c, size_t n ) THROW_XALLOC_LENGTHERROR;
  163.  
  164.     _RTLENTRY string( unsigned char c ) THROW_XALLOC_LENGTHERROR;
  165.     _RTLENTRY string( unsigned char c, size_t n ) THROW_XALLOC_LENGTHERROR;
  166.  
  167.     // non-standard constructors
  168.     _RTLENTRY string( const TSubString _FAR &ss ) throw( xalloc );
  169.  
  170.     // Special far string ctors for small & medium model
  171.     #if (defined( __TINY__ ) || defined( __SMALL__ ) || defined( __MEDIUM__ )) && !defined( __DLL__ )
  172.     _RTLENTRY string( const char __far *cp ) THROW_XALLOC_LENGTHERROR;
  173.     _RTLENTRY string( const char __far *cp, size_t orig, size_t n = NPOS )
  174.         throw( xalloc, lengtherror );
  175.     #endif
  176.  
  177.     // Ctor to make a string from a resource
  178.     #if defined( _Windows )
  179.     _RTLENTRY string( HINSTANCE instance, UINT id, int len = 255 )
  180.         throw( xalloc, lengtherror );
  181.     #endif
  182.  
  183.     //
  184.     // Destructor
  185.     //
  186.     _RTLENTRY ~string() throw();
  187.  
  188.     //
  189.     // Assignment
  190.     //
  191.     string _FAR & _RTLENTRY operator = ( const string _FAR &s ) THROW_XALLOC;
  192.     string _FAR & _RTLENTRY assign( const string _FAR &s ) THROW_XALLOC;
  193.     string _FAR & _RTLENTRY assign( const string _FAR &s,
  194.                                     size_t orig,
  195.                                     size_t n = NPOS ) throw( xalloc );
  196.  
  197.     //
  198.     // Concatenation
  199.     //
  200.     string _FAR & _RTLENTRY operator += ( const string _FAR &s )
  201.         THROW_XALLOC_LENGTHERROR;
  202.     string _FAR & _RTLENTRY append( const string _FAR &s )
  203.         THROW_XALLOC_LENGTHERROR;
  204.     string _FAR & _RTLENTRY append( const string _FAR &s,
  205.                                     size_t orig,
  206.                                     size_t n = NPOS ) throw( xalloc, lengtherror );
  207.  
  208.     string _FAR & _RTLENTRY operator += ( const char _FAR *cp )
  209.         THROW_XALLOC_LENGTHERROR;
  210.     friend string _RTLENTRY _FARFUNC operator + ( const string _FAR &s,
  211.                                                const char _FAR *cp )
  212.         THROW_XALLOC_LENGTHERROR;
  213.     string _FAR & _RTLENTRY append( const char _FAR *cp )
  214.         throw( xalloc, lengtherror );
  215.     string _FAR & _RTLENTRY append( const char _FAR *cp,
  216.                                     size_t orig,
  217.                                     size_t n = NPOS ) throw( xalloc, lengtherror );
  218.  
  219.     string _FAR & _RTLENTRY prepend( const string _FAR &s )
  220.         THROW_XALLOC_LENGTHERROR;
  221.     string _FAR & _RTLENTRY prepend( const string _FAR &s,
  222.                                      size_t orig,
  223.                                      size_t n = NPOS ) THROW_XALLOC_LENGTHERROR;
  224.     string _FAR & _RTLENTRY prepend( const char _FAR *cp )
  225.         THROW_XALLOC_LENGTHERROR;
  226.     string _FAR & _RTLENTRY prepend( const char _FAR *cp,
  227.                                      size_t orig,
  228.                                      size_t n = NPOS ) throw( xalloc, lengtherror );
  229.  
  230.     //
  231.     // Comparison
  232.     //
  233.     int _RTLENTRY compare(const string _FAR &s) const throw();
  234.     int _RTLENTRY compare(const string _FAR &s,
  235.                           size_t orig,
  236.                           size_t n = NPOS ) const throw();
  237.  
  238.     friend int _RTLENTRY operator == ( const string _FAR &s1, const string _FAR &s2 )
  239.         THROW_NONE;
  240.  
  241.     friend int _RTLENTRY operator != ( const string _FAR &s1, const string _FAR &s2 )
  242.         THROW_NONE;
  243.  
  244.     friend int _RTLENTRY operator == ( const string _FAR &s, const char _FAR *cp )
  245.         THROW_NONE;
  246.     friend int _RTLENTRY operator == ( const char _FAR *cp, const string _FAR &s )
  247.         THROW_NONE;
  248.  
  249.     friend int _RTLENTRY operator != ( const string _FAR &s, const char _FAR *cp )
  250.         THROW_NONE;
  251.     friend int _RTLENTRY operator != ( const char _FAR *cp, const string _FAR &s )
  252.         THROW_NONE;
  253.  
  254.     friend int _RTLENTRY operator <  ( const string _FAR &s1, const string _FAR &s2 )
  255.         THROW_NONE;
  256.     friend int _RTLENTRY operator <  ( const string _FAR &s, const char _FAR *cp )
  257.         THROW_NONE;
  258.     friend int _RTLENTRY operator <  ( const char _FAR *cp, const string _FAR &s )
  259.         THROW_NONE;
  260.  
  261.     friend int _RTLENTRY operator <= ( const string _FAR &s1, const string _FAR &s2 )
  262.         THROW_NONE;
  263.     friend int _RTLENTRY operator <= ( const string _FAR &s, const char _FAR *cp )
  264.         THROW_NONE;
  265.     friend int _RTLENTRY operator <= ( const char _FAR *cp, const string _FAR &s )
  266.         THROW_NONE;
  267.  
  268.     friend int _RTLENTRY operator >  ( const string _FAR &s1, const string _FAR &s2 )
  269.         THROW_NONE;
  270.     friend int _RTLENTRY operator >  ( const string _FAR &s, const char _FAR *cp )
  271.         THROW_NONE;
  272.     friend int _RTLENTRY operator >  ( const char _FAR *cp, const string _FAR &s )
  273.         THROW_NONE;
  274.  
  275.     friend int _RTLENTRY operator >= ( const string _FAR &s1, const string _FAR &s2 )
  276.         THROW_NONE;
  277.     friend int _RTLENTRY operator >= ( const string _FAR &s, const char _FAR *cp )
  278.         THROW_NONE;
  279.     friend int _RTLENTRY operator >= ( const char _FAR *cp, const string _FAR &s )
  280.         THROW_NONE;
  281.  
  282.     //
  283.     // Insertion at some position
  284.     //
  285.     string _FAR & _RTLENTRY insert( size_t pos, const string _FAR &s )
  286.         throw( xalloc, outofrange, lengtherror );
  287.     string _FAR & _RTLENTRY insert( size_t pos,
  288.                                     const string _FAR &s,
  289.                                     size_t orig,
  290.                                     size_t n = NPOS ) throw( xalloc, outofrange, lengtherror );
  291.  
  292.     //
  293.     // Removal
  294.     //
  295.     string _FAR & _RTLENTRY remove( size_t pos ) THROW_XALLOC_OUTOFRANGE;
  296.     string _FAR & _RTLENTRY remove( size_t pos, size_t n )
  297.         throw( xalloc, outofrange );
  298.  
  299.     //
  300.     // Replacement at some position
  301.     //
  302.     string _FAR & _RTLENTRY replace( size_t pos, size_t n, const string _FAR &s )
  303.         THROW_XALLOC_RANGE_LENGTH;
  304.     string _FAR & _RTLENTRY replace( size_t pos,
  305.                                      size_t n1,
  306.                                      const string _FAR &s,
  307.                                      size_t orig,
  308.                                      size_t n2 = NPOS ) throw( xalloc, outofrange, lengtherror );
  309.  
  310.     //
  311.     // Subscripting
  312.     //
  313.     char _RTLENTRY get_at( size_t pos ) const THROW_OUTOFRANGE;
  314.     void _RTLENTRY put_at( size_t pos, char c ) THROW_OUTOFRANGE;
  315.  
  316.     char _FAR & _RTLENTRY operator[]( size_t pos ) THROW_OUTOFRANGE;
  317.     char _FAR & _RTLENTRY operator()( size_t pos ) throw( outofrange );
  318.     TSubString _RTLENTRY operator()( size_t start, size_t len ) THROW_NONE;
  319.     TSubString _RTLENTRY operator()( const TRegexp _FAR &re ) THROW_NONE;
  320.     TSubString _RTLENTRY operator()( const TRegexp _FAR &re, size_t start ) throw();
  321.  
  322.     char _RTLENTRY operator[]( size_t pos ) const THROW_OUTOFRANGE;
  323.     char _RTLENTRY operator()( size_t pos ) const THROW_OUTOFRANGE;
  324.     const TSubString _RTLENTRY operator()( size_t start, size_t len ) const throw();
  325.     const TSubString _RTLENTRY operator()( const TRegexp _FAR &pat ) const THROW_NONE;
  326.     const TSubString _RTLENTRY operator()( const TRegexp _FAR &pat, size_t start )
  327.         const throw();
  328.  
  329.     //
  330.     // Searching
  331.     //
  332.     size_t _RTLENTRY find( const string _FAR &s ) const THROW_NONE;
  333.     size_t _RTLENTRY find( const string _FAR &s, size_t pos ) const throw();
  334.     size_t _RTLENTRY rfind( const string _FAR &s ) const THROW_NONE;
  335.     size_t _RTLENTRY rfind( const string _FAR &s, size_t pos ) const throw();
  336.  
  337.     int _RTLENTRY contains( const char _FAR *pat ) const throw();
  338.     int _RTLENTRY contains(const string _FAR &s) const THROW_NONE;
  339.     size_t _RTLENTRY find( const TRegexp _FAR &pat, size_t i = 0 ) const throw();
  340.     size_t _RTLENTRY find( const TRegexp _FAR &pat, size_t _FAR *ext, size_t i = 0 )
  341.         const throw();
  342.  
  343.     //
  344.     // Substring
  345.     //
  346.     string _RTLENTRY substr( size_t pos ) const
  347.         throw( xalloc, outofrange );
  348.     string _RTLENTRY substr( size_t pos, size_t n ) const
  349.         throw( xalloc, outofrange );
  350.  
  351.     TSubString _RTLENTRY substring( const char _FAR *cp ) THROW_NONE;
  352.     const TSubString _RTLENTRY substring( const char _FAR *cp )
  353.         const THROW_NONE;
  354.     TSubString _RTLENTRY substring( const char _FAR *cp, size_t start ) throw();
  355.     const TSubString _RTLENTRY substring( const char _FAR *cp, size_t start )
  356.         const throw();
  357.  
  358.     //
  359.     // Character set searching
  360.     //
  361.     size_t _RTLENTRY find_first_of( const string _FAR &s ) const THROW_NONE;
  362.     size_t _RTLENTRY find_first_of( const string _FAR &s, size_t pos ) const throw();
  363.     size_t _RTLENTRY find_first_not_of( const string _FAR &s ) const THROW_NONE;
  364.     size_t _RTLENTRY find_first_not_of( const string _FAR &s, size_t pos )
  365.         const throw();
  366.     size_t _RTLENTRY find_last_of( const string _FAR &s ) const THROW_NONE;
  367.     size_t _RTLENTRY find_last_of( const string _FAR &s, size_t pos ) const throw();
  368.     size_t _RTLENTRY find_last_not_of( const string _FAR &s ) const THROW_NONE;
  369.     size_t _RTLENTRY find_last_not_of( const string _FAR &s, size_t pos )
  370.         const throw();
  371.  
  372.     //
  373.     // Miscellaneous
  374.     //
  375.     size_t _RTLENTRY length() const THROW_NONE;
  376.     size_t _RTLENTRY copy( char _FAR *cb, size_t n ) throw( outofrange );
  377.     size_t _RTLENTRY copy( char _FAR *cb, size_t n, size_t pos ) throw( outofrange );
  378.     const char _FAR * _RTLENTRY c_str() const THROW_XALLOC;
  379.     size_t _RTLENTRY reserve() const THROW_NONE;
  380.     void _RTLENTRY reserve( size_t ic ) throw( xalloc, outofrange );
  381.  
  382.     string _RTLENTRY copy() const throw( xalloc );    // Distinct copy of self.
  383.  
  384.  
  385.     // Static member functions:
  386.     static int _RTLENTRY set_case_sensitive( int tf = 1 );
  387.     static int _RTLENTRY set_paranoid_check( int ck = 1 );
  388.     static int _RTLENTRY skip_whitespace( int sk = 1 );
  389.     static size_t _RTLENTRY initial_capacity( size_t ic = 63 );
  390.     static size_t _RTLENTRY resize_increment( size_t ri = 64 );
  391.     static size_t _RTLENTRY max_waste( size_t mw = 63 );
  392.  
  393.     static int _RTLENTRY get_case_sensitive_flag();
  394.     static int _RTLENTRY get_paranoid_check_flag();
  395.     static int _RTLENTRY get_skip_whitespace_flag();
  396.     static size_t _RTLENTRY get_initial_capacity();
  397.     static size_t _RTLENTRY get_resize_increment();
  398.     static size_t _RTLENTRY get_max_waste();
  399.  
  400.     enum StripType { Leading, Trailing, Both };
  401.  
  402.     // Non-static member functions:
  403.     unsigned _RTLENTRY hash() const;
  404.     int      _RTLENTRY is_null() const;
  405.     istream _FAR & _RTLENTRY read_file( istream _FAR &is );
  406.     istream _FAR & _RTLENTRY read_string( istream _FAR &is );
  407.     istream _FAR & _RTLENTRY read_line( istream _FAR &is );
  408.     istream _FAR & _RTLENTRY read_to_delim( istream _FAR &is, char delim = '\n' );
  409.     istream _FAR & _RTLENTRY read_token( istream _FAR &is );
  410.     void       _RTLENTRY resize( size_t m );
  411.     TSubString _RTLENTRY strip( StripType s = Trailing, char c = ' ' );
  412.     void       _RTLENTRY to_lower();
  413.     void       _RTLENTRY to_upper();
  414.  
  415.     #if defined( _Windows )
  416.     void _RTLENTRY ansi_to_oem() THROW_NONE;
  417.     void _RTLENTRY oem_to_ansi() THROW_NONE;
  418.     #endif
  419.  
  420. protected:
  421.  
  422.     int  _RTLENTRY valid_element( size_t pos ) const THROW_NONE;
  423.     int  _RTLENTRY valid_index( size_t pos ) const THROW_NONE;
  424.  
  425.     void _RTLENTRY assert_element( size_t pos ) const throw( outofrange );
  426.     void _RTLENTRY assert_index( size_t pos ) const throw( outofrange );
  427.  
  428.     _RTLENTRY string( const string _FAR &s, const char _FAR *cb );
  429.     void _RTLENTRY cow();
  430.  
  431. private:
  432.  
  433.     TStringRef _FAR *p;
  434.  
  435.     static int case_sensitive;
  436.     static int paranoid_check;
  437.     static int skip_white;
  438.     static size_t initial_capac;
  439.     static size_t resize_inc;
  440.     static size_t freeboard;
  441.  
  442. private:
  443.  
  444.     friend class _EXPCLASS TSubString;
  445.     friend class _EXPCLASS TStringRef;
  446.  
  447.     void _RTLENTRY clone();
  448.     size_t _RTLENTRY find_case_index( const char _FAR *cb,
  449.                             size_t start,
  450.                             size_t _FAR &patl) const;
  451.     size_t _RTLENTRY rfind_case_index( const char _FAR *cb,
  452.                              size_t start,
  453.                              size_t _FAR &patl) const;
  454.     size_t _RTLENTRY find_index(const char _FAR *,
  455.                       size_t start,
  456.                       size_t _FAR & patl) const;
  457.     size_t _RTLENTRY rfind_index(const char _FAR *,
  458.                        size_t start,
  459.                        size_t _FAR & patl) const;
  460.     unsigned _RTLENTRY hash_case() const;
  461.  
  462. };
  463.  
  464. #if defined( BI_OLDNAMES )
  465. #define BI_String string
  466. #endif
  467.  
  468. /*------------------------------------------------------------------------*/
  469. /*                                                                        */
  470. /*  Related global functions                                              */
  471. /*                                                                        */
  472. /*------------------------------------------------------------------------*/
  473.  
  474. istream _FAR &
  475. _RTLENTRY _FARFUNC operator >> ( istream _FAR &is, string _FAR &s );
  476.  
  477. ostream _FAR &
  478. _RTLENTRY _FARFUNC operator << ( ostream _FAR &os, const string _FAR &s );
  479.  
  480. istream _FAR &
  481. _RTLENTRY _FARFUNC getline( istream _FAR &is, string _FAR &s );
  482.  
  483. istream _FAR &
  484. _RTLENTRY _FARFUNC getline( istream _FAR &is, string _FAR &s, char c );
  485.  
  486. string _RTLENTRY _FARFUNC to_lower( const string _FAR &s ) throw();
  487. string _RTLENTRY _FARFUNC to_upper( const string _FAR &s ) throw();
  488. string _RTLENTRY _FARFUNC operator + ( const char _FAR *cp,
  489.                                     const string _FAR & s)
  490.                                     throw( xalloc, string::lengtherror );
  491. string _RTLENTRY _FARFUNC operator + ( const string _FAR &s1,
  492.                                     const string _FAR &s2 )
  493.                                     THROW_XALLOC_LENGTHERROR;
  494.  
  495. /*------------------------------------------------------------------------*/
  496. /*                                                                        */
  497. /*  TStringRef                                                            */
  498. /*                                                                        */
  499. /*  This is the dynamically allocated part of a string.                   */
  500. /*  It maintains a reference count.                                       */
  501. /*  There are no public member functions.                                 */
  502. /*                                                                        */
  503. /*------------------------------------------------------------------------*/
  504.  
  505. class _EXPCLASS TStringRef : public TReference
  506. {
  507.  
  508.     friend class _EXPCLASS string;
  509.     friend class _EXPCLASS TSubString;
  510.  
  511.     //
  512.     // Data
  513.     //
  514.     char _FAR *array;
  515.     size_t nchars;
  516.     size_t capacity;
  517.  
  518.     //
  519.     // State flags
  520.     //
  521.     enum {
  522.         MemReserved = 1     // indicates that reserve() has been
  523.                             // called on this string
  524.         };
  525.     unsigned flags;
  526.  
  527.     //
  528.     // Constructors
  529.     //
  530.     _RTLENTRY TStringRef( char c, size_t n );
  531.     _RTLENTRY TStringRef( const char _FAR *str1, size_t count1,
  532.                 const char _FAR *str2, size_t count2,
  533.                 size_t extra );
  534.  
  535.     // Special far TStringRef ctor for small data models
  536.     #if (defined( __TINY__ ) || defined( __SMALL__ ) || defined( __MEDIUM__ )) && !defined( __DLL__ )
  537.     _RTLENTRY TStringRef( const char __far*, size_t n = NPOS );
  538.     #endif
  539.  
  540.     //
  541.     // Ctor to make a TStringRef from a resource
  542.     //
  543.     #if defined( _Windows )
  544.     _RTLENTRY TStringRef( HINSTANCE instance, UINT id, int len = 255 )
  545.          throw( xalloc, string::lengtherror );
  546.     #endif
  547.  
  548.     //
  549.     // Destructor
  550.     //
  551.     _RTLENTRY ~TStringRef() throw();
  552.  
  553.     //
  554.     // Miscellaneous
  555.     //
  556.     void _RTLENTRY reserve( size_t ic ) throw( xalloc, string::outofrange );
  557.     void _RTLENTRY check_freeboard() throw();
  558.     void _RTLENTRY grow_to( size_t n ) throw( xalloc, string::lengtherror );
  559.     void _RTLENTRY read_to_delim( istream _FAR &is, char delim ) throw( xalloc );
  560.     void _RTLENTRY read_token( istream _FAR &is ) throw( xalloc );
  561.     static size_t _RTLENTRY round_capacity( size_t cap ) throw();
  562.     void _RTLENTRY splice( size_t start, size_t extent,
  563.                  const char _FAR *cp, size_t n )
  564.         throw( xalloc, string::lengtherror );
  565.  
  566. };
  567.  
  568. #if defined( BI_OLDNAMES )
  569. #define BI_StringRef TStringRef
  570. #endif
  571.  
  572. /*------------------------------------------------------------------------*/
  573. /*                                                                        */
  574. /*  TSubString                                                            */
  575. /*                                                                        */
  576. /*  The TSubString class allows selected elements to be addressed.        */
  577. /*  There are no public constructors.                                     */
  578. /*                                                                        */
  579. /*------------------------------------------------------------------------*/
  580.  
  581. class _EXPCLASS TSubString
  582. {
  583.  
  584. public:
  585.  
  586.     //
  587.     // Assignment
  588.     //
  589.     TSubString _FAR & _RTLENTRY operator = ( const string _FAR &s ) throw();
  590.  
  591.     //
  592.     // Comparison
  593.     //
  594.     int _RTLENTRY operator == ( const char _FAR *cp ) const throw();
  595.     int _RTLENTRY operator == ( const string _FAR &s ) const THROW_NONE;
  596.     int _RTLENTRY operator != ( const char _FAR *cp ) const THROW_NONE;
  597.     int _RTLENTRY operator != ( const string _FAR & str ) const THROW_NONE;
  598.  
  599.     //
  600.     // Subscripting
  601.     //
  602.     char _RTLENTRY get_at( size_t pos ) const THROW_OUTOFRANGE;
  603.     void _RTLENTRY put_at( size_t pos, char c ) THROW_OUTOFRANGE;
  604.  
  605.     char _FAR & _RTLENTRY operator[]( size_t pos ) THROW_OUTOFRANGE;
  606.     char _FAR & _RTLENTRY operator()( size_t pos ) throw( string::outofrange );
  607.     char _RTLENTRY operator[]( size_t pos ) const THROW_OUTOFRANGE;
  608.     char _RTLENTRY operator()( size_t pos ) const THROW_OUTOFRANGE;
  609.     size_t _RTLENTRY length() const THROW_NONE;
  610.     int _RTLENTRY start() const THROW_NONE;
  611.     void _RTLENTRY to_lower() throw();
  612.     void _RTLENTRY to_upper() throw();
  613.  
  614.     //
  615.     // Detecting empty strings
  616.     //
  617.     int _RTLENTRY is_null() const THROW_NONE;
  618.     int _RTLENTRY operator!() const THROW_NONE;
  619.  
  620. protected:
  621.  
  622.     void _RTLENTRY assert_element( size_t pos ) const throw( string::outofrange );
  623.     int _RTLENTRY valid_element( size_t pos ) const;
  624.  
  625. private:
  626.  
  627.     friend class _EXPCLASS string;
  628.  
  629.     //
  630.     // Data
  631.     //
  632.     string _FAR *s;
  633.     size_t begin;
  634.     size_t extent;
  635.  
  636.     //
  637.     // Constructor
  638.     //
  639.     _RTLENTRY TSubString( const string _FAR *cp, size_t start, size_t len );
  640.  
  641. };
  642.  
  643. #if defined( BI_OLDNAMES )
  644. #define BI_SubString TSubString
  645. #endif
  646.  
  647. /*------------------------------------------------------------------------*/
  648. /*                                                                        */
  649. /*  string inlines                                                        */
  650. /*                                                                        */
  651. /*------------------------------------------------------------------------*/
  652.  
  653. inline _RTLENTRY string::outofrange::outofrange() :
  654.     xmsg( "String reference out of range" )
  655. {
  656. }
  657.  
  658. inline _RTLENTRY string::lengtherror::lengtherror() :
  659.     xmsg( "String length error" )
  660. {
  661. }
  662.  
  663. inline _RTLENTRY string::string( char c ) THROW_XALLOC_LENGTHERROR
  664. {
  665.     p = new TStringRef(c,1);
  666. }
  667.  
  668. inline _RTLENTRY string::string( char c, size_t n ) THROW_XALLOC_LENGTHERROR
  669. {
  670.     p = new TStringRef(c,n);
  671. }
  672.  
  673. inline _RTLENTRY string::string( signed char c ) THROW_XALLOC_LENGTHERROR
  674. {
  675.     p = new TStringRef(c,1);
  676. }
  677.  
  678. inline _RTLENTRY string::string( signed char c, size_t n ) THROW_XALLOC_LENGTHERROR
  679. {
  680.     p = new TStringRef(c,n);
  681. }
  682.  
  683. inline _RTLENTRY string::string( unsigned char c ) THROW_XALLOC_LENGTHERROR
  684. {
  685.     p = new TStringRef(c,1);
  686. }
  687.  
  688. inline _RTLENTRY string::string( unsigned char c, size_t n ) THROW_XALLOC_LENGTHERROR
  689. {
  690.     p = new TStringRef(c,n);
  691. }
  692.  
  693. #if (defined(__TINY__)|| defined(__SMALL__)|| defined(__MEDIUM__)) && !defined(__DLL__)
  694. // Far string ctors make a near string from a far string
  695.  
  696. inline _RTLENTRY string::string( const char __far *cp ) THROW_XALLOC_LENGTHERROR
  697. {
  698.     p = new TStringRef(cp);
  699. }
  700.  
  701. #endif
  702.  
  703. inline string _FAR & _RTLENTRY string::operator = ( const string _FAR &s )
  704.     THROW_NONE
  705. {
  706.     return assign( s, 0, NPOS );
  707. }
  708.  
  709. inline string _FAR & _RTLENTRY string::assign( const string _FAR &s )
  710.     THROW_NONE
  711. {
  712.     return assign( s, 0, NPOS );
  713. }
  714.  
  715. inline string _FAR & _RTLENTRY string::operator += ( const string _FAR &s )
  716.     THROW_XALLOC_LENGTHERROR
  717. {
  718.     return append( s, 0, NPOS );
  719. }
  720.  
  721. inline string _FAR & _RTLENTRY string::append( const string _FAR &s )
  722.     THROW_XALLOC_LENGTHERROR
  723. {
  724.     return append(s, 0, NPOS);
  725. }
  726.  
  727. inline string _FAR & _RTLENTRY string::prepend( const char _FAR *cp )
  728.     THROW_XALLOC_LENGTHERROR
  729. {
  730.     return prepend( cp, 0, strlen(cp) );
  731. }
  732.  
  733. inline int _RTLENTRY operator == ( const string _FAR &s1, const string _FAR &s2 )
  734.     THROW_NONE
  735. {
  736.     return s1.compare( s2 ) == 0;
  737. }
  738.  
  739. inline int _RTLENTRY operator != ( const string _FAR &s1, const string _FAR &s2 )
  740.     THROW_NONE
  741. {
  742.     return !(s1==s2);
  743. }
  744.  
  745. inline string _FAR & _RTLENTRY string::remove( size_t pos )
  746.     THROW_XALLOC_OUTOFRANGE
  747. {
  748.     return remove( pos, length() );
  749. }
  750.  
  751. inline string _FAR & _RTLENTRY string::replace( size_t pos,
  752.                                      size_t n,
  753.                                      const string _FAR &s )
  754.     THROW_XALLOC_RANGE_LENGTH
  755. {
  756.     return replace( pos, n, s, 0, NPOS );
  757. }
  758.  
  759. inline char _RTLENTRY string::get_at( size_t pos ) const THROW_OUTOFRANGE
  760. {
  761.     return (*this)[pos];
  762. }
  763.  
  764. inline void _RTLENTRY string::put_at( size_t pos, char c ) THROW_OUTOFRANGE
  765. {
  766.     (*this)[pos] = c;
  767. }
  768.  
  769. inline char _FAR & _RTLENTRY string::operator[]( size_t pos ) THROW_OUTOFRANGE
  770. {
  771.     return (*this)(pos);    // use operator()
  772. }
  773.  
  774. inline TSubString _RTLENTRY string::operator()( size_t start, size_t len ) THROW_NONE
  775. {
  776.     return TSubString( this, start, len );
  777. }
  778.  
  779. inline size_t _RTLENTRY string::find( const string _FAR &s ) const THROW_NONE
  780. {
  781.     return find( s, 0 );
  782. }
  783.  
  784. inline size_t _RTLENTRY string::rfind( const string _FAR &s ) const THROW_NONE
  785. {
  786.     return rfind( s, length() );
  787. }
  788.  
  789. inline size_t _RTLENTRY string::length() const THROW_NONE
  790. {
  791.     return p->nchars;
  792. }
  793.  
  794. inline const char _FAR * _RTLENTRY string::c_str() const THROW_NONE
  795. {
  796.     return p->array;
  797. }
  798.  
  799. inline size_t _RTLENTRY string::reserve() const THROW_NONE
  800. {
  801.     return p->capacity;
  802. }
  803.  
  804. inline void _RTLENTRY string::cow()
  805. {
  806.     if( p->References() > 1 )
  807.         clone();
  808. }
  809.  
  810. inline string _FAR & _RTLENTRY string::operator += ( const char _FAR *cp )
  811.     THROW_XALLOC_LENGTHERROR
  812. {
  813.     return append( cp, 0, strlen(cp) );
  814. }
  815.  
  816. inline string _FAR & _RTLENTRY string::prepend( const string _FAR &s )
  817.     THROW_XALLOC_LENGTHERROR
  818. {
  819.     return prepend( s.c_str() );
  820. }
  821.  
  822. inline string _FAR & _RTLENTRY string::prepend( const string _FAR &s,
  823.                                                 size_t orig,
  824.                                                 size_t n ) THROW_XALLOC_LENGTHERROR
  825. {
  826.     return prepend( s.c_str(), orig, n );
  827. }
  828.  
  829. inline int _RTLENTRY operator == ( const string _FAR &s1, const char _FAR *s2 ) THROW_NONE
  830. {
  831.     return s1.compare(s2) == 0;
  832. }
  833.  
  834. inline int _RTLENTRY operator == ( const char _FAR *cp, const string _FAR &s ) THROW_NONE
  835. {
  836.     return string(cp).compare(s) == 0;
  837. }
  838.  
  839. inline int _RTLENTRY operator != ( const string _FAR &s, const char _FAR *cp ) THROW_NONE
  840. {
  841.     return !(s==cp);
  842. }
  843.  
  844. inline int _RTLENTRY operator != ( const char _FAR *cp, const string _FAR &s ) THROW_NONE
  845. {
  846.     return !(cp==s);
  847. }
  848.  
  849. inline int _RTLENTRY operator <  ( const string _FAR &s1, const string _FAR &s2 )
  850.     THROW_NONE
  851. {
  852.     return s1.compare(s2) < 0;
  853. }
  854.  
  855. inline int _RTLENTRY operator <  ( const string _FAR &s1, const char _FAR *s2 ) THROW_NONE
  856. {
  857.     return s1.compare(s2) < 0;
  858. }
  859.  
  860. inline int _RTLENTRY operator <  ( const char _FAR *cp, const string _FAR &s ) THROW_NONE
  861. {
  862.     return string(cp).compare(s) < 0;
  863. }
  864.  
  865. inline int _RTLENTRY operator <= ( const string _FAR &s1, const string _FAR &s2 )
  866.     THROW_NONE
  867. {
  868.     return s1.compare(s2) <= 0;
  869. }
  870.  
  871. inline int _RTLENTRY operator <= ( const string _FAR &s, const char _FAR *cp ) THROW_NONE
  872. {
  873.     return s.compare(string(cp)) <= 0;
  874. }
  875.  
  876. inline int _RTLENTRY operator <= ( const char _FAR *cp, const string _FAR &s ) THROW_NONE
  877. {
  878.     return string(cp).compare(s) <= 0;
  879. }
  880.  
  881. inline int _RTLENTRY operator >  ( const string _FAR &s1, const string _FAR &s2 )
  882.     THROW_NONE
  883. {
  884.     return s1.compare(s2) > 0;
  885. }
  886.  
  887. inline int _RTLENTRY operator >  ( const string _FAR &s, const char _FAR *cp ) THROW_NONE
  888. {
  889.     return s.compare(cp) > 0;
  890. }
  891.  
  892. inline int _RTLENTRY operator >  ( const char _FAR *cp, const string _FAR &s ) THROW_NONE
  893. {
  894.     return string(cp).compare(s) > 0;
  895. }
  896.  
  897. inline int _RTLENTRY operator >= ( const string _FAR &s1, const string _FAR &s2 )
  898.     THROW_NONE
  899. {
  900.     return s1.compare(s2) >= 0;
  901. }
  902.  
  903. inline int _RTLENTRY operator >= ( const string _FAR &s, const char _FAR *cp ) THROW_NONE
  904. {
  905.     return s.compare(cp) >= 0;
  906. }
  907.  
  908. inline int _RTLENTRY operator >= ( const char _FAR *cp, const string _FAR &s ) THROW_NONE
  909. {
  910.     return string(cp).compare(s) >= 0;
  911. }
  912.  
  913. inline char _RTLENTRY string::operator[]( size_t pos ) const THROW_OUTOFRANGE
  914. {
  915.     assert_element(pos);
  916.     return p->array[pos];
  917. }
  918.  
  919. inline char _RTLENTRY string::operator()( size_t pos ) const THROW_OUTOFRANGE
  920. {
  921. #if defined( BOUNDS_CHECK )
  922.     assert_element(pos);
  923. #endif
  924.     return p->array[pos];
  925. }
  926.  
  927. inline int _RTLENTRY string::contains( const string _FAR &s ) const THROW_NONE
  928. {
  929.     return contains( s.c_str() );
  930. }
  931.  
  932. inline TSubString _RTLENTRY string::substring( const char _FAR *cp ) THROW_NONE
  933. {
  934.     return substring( cp, 0 );
  935. }
  936.  
  937. inline const TSubString _RTLENTRY string::substring( const char _FAR *cp ) const
  938.     THROW_NONE
  939. {
  940.     return substring( cp, 0 );
  941. }
  942.  
  943. inline size_t _RTLENTRY string::find_first_of( const string _FAR &s ) const THROW_NONE
  944. {
  945.     return find_first_of( s, 0 );
  946. }
  947.  
  948. inline size_t _RTLENTRY string::find_first_not_of( const string _FAR &s ) const THROW_NONE
  949. {
  950.     return find_first_not_of( s, 0 );
  951. }
  952.  
  953. inline size_t _RTLENTRY string::find_last_of( const string _FAR &s ) const THROW_NONE
  954. {
  955.     return find_last_of( s, NPOS );
  956. }
  957.  
  958. inline size_t _RTLENTRY string::find_last_not_of( const string _FAR &s ) const THROW_NONE
  959. {
  960.     return find_last_not_of( s, NPOS );
  961. }
  962.  
  963. inline int _RTLENTRY string::get_case_sensitive_flag()
  964. {
  965.     return case_sensitive;
  966. }
  967.  
  968. inline int _RTLENTRY string::get_paranoid_check_flag()
  969. {
  970.     return paranoid_check;
  971. }
  972.  
  973. inline int _RTLENTRY string::get_skip_whitespace_flag()
  974. {
  975.     return skip_white;
  976. }
  977.  
  978. inline size_t _RTLENTRY string::get_initial_capacity()
  979. {
  980.     return initial_capac;
  981. }
  982.  
  983. inline size_t _RTLENTRY string::get_resize_increment()
  984. {
  985.     return resize_inc;
  986. }
  987.  
  988. inline size_t _RTLENTRY string::get_max_waste()
  989. {
  990.     return freeboard;
  991. }
  992.  
  993. inline int _RTLENTRY string::is_null() const
  994. {
  995.     return *p->array==0;
  996. }
  997.  
  998. #if defined( _Windows )
  999. inline void _RTLENTRY string::ansi_to_oem() THROW_NONE
  1000. {
  1001.     ::AnsiToOem( p->array, p->array );
  1002. }
  1003.  
  1004. inline void _RTLENTRY string::oem_to_ansi() THROW_NONE
  1005. {
  1006.     ::OemToAnsi( p->array, p->array );
  1007. }
  1008. #endif
  1009.  
  1010. // Check to make sure a string index refers to a valid element
  1011. inline int _RTLENTRY string::valid_element( size_t n ) const THROW_NONE
  1012. {
  1013.     return n < length();
  1014. }
  1015.  
  1016. // Check to make sure a string index is in range
  1017. inline int _RTLENTRY string::valid_index( size_t n ) const THROW_NONE
  1018. {
  1019.     return n <= length();
  1020. }
  1021.  
  1022. // Constructor for internal use only
  1023. inline _RTLENTRY string::string( const string _FAR &s, const char _FAR *cp )
  1024. {
  1025.     p = new TStringRef( s.c_str(), s.length(), cp, cp?strlen(cp):0, 0 );
  1026. }
  1027.  
  1028. inline string _RTLENTRY operator + ( const string _FAR &s,
  1029.                                   const char _FAR *cp )
  1030.     THROW_XALLOC_LENGTHERROR
  1031. {
  1032.     return string(s,cp);
  1033. }
  1034.  
  1035. inline string _RTLENTRY operator + ( const string _FAR &s1,
  1036.                                   const string _FAR &s2 )
  1037.     THROW_XALLOC_LENGTHERROR
  1038. {
  1039.     return s1 + s2.c_str();
  1040. }
  1041.  
  1042. /*------------------------------------------------------------------------*/
  1043. /*                                                                        */
  1044. /*  TSubString inlines                                                    */
  1045. /*                                                                        */
  1046. /*------------------------------------------------------------------------*/
  1047.  
  1048. inline int _RTLENTRY TSubString::operator == ( const string _FAR &s ) const THROW_NONE
  1049. {
  1050.     return operator==(s.c_str());
  1051. }
  1052.  
  1053. inline int _RTLENTRY TSubString::operator != ( const char _FAR *cp ) const THROW_NONE
  1054. {
  1055.     return !operator==(cp);
  1056. }
  1057.  
  1058. inline int _RTLENTRY TSubString::operator != ( const string _FAR &s ) const THROW_NONE
  1059. {
  1060.     return !operator==(s.c_str());
  1061. }
  1062.  
  1063. inline char _RTLENTRY TSubString::get_at( size_t pos ) const THROW_OUTOFRANGE
  1064. {
  1065.     return (*this)[pos];
  1066. }
  1067.  
  1068. inline void _RTLENTRY TSubString::put_at( size_t pos, char c ) THROW_OUTOFRANGE
  1069. {
  1070.     (*this)[pos] = c;
  1071. }
  1072.  
  1073. inline char _FAR & _RTLENTRY TSubString::operator[]( size_t pos ) THROW_OUTOFRANGE
  1074. {
  1075.     return (*this)(pos);    // use operator()
  1076. }
  1077.  
  1078. inline char _RTLENTRY TSubString::operator[]( size_t pos ) const
  1079.     THROW_OUTOFRANGE
  1080. {
  1081.     assert_element(pos);
  1082.     return s->p->array[begin+pos];
  1083. }
  1084.  
  1085. inline char _RTLENTRY TSubString::operator()( size_t pos ) const
  1086.     THROW_OUTOFRANGE
  1087. {
  1088. #if defined( BOUNDS_CHECK )
  1089.     assert_element(pos);
  1090. #endif
  1091.     return s->p->array[begin+pos];
  1092. }
  1093.  
  1094. inline size_t _RTLENTRY TSubString::length() const THROW_NONE
  1095. {
  1096.     return extent;
  1097. }
  1098.  
  1099. inline int _RTLENTRY TSubString::start() const THROW_NONE
  1100. {
  1101.     return begin;
  1102. }
  1103.  
  1104. inline int _RTLENTRY TSubString::is_null() const THROW_NONE
  1105. {
  1106.     return begin == NPOS;
  1107. }
  1108.  
  1109. inline int _RTLENTRY TSubString::operator!() const THROW_NONE
  1110. {
  1111.     return begin == NPOS;
  1112. }
  1113.  
  1114. inline int _RTLENTRY TSubString::valid_element( size_t n ) const THROW_NONE
  1115. {
  1116.     return n < length();
  1117. }
  1118.  
  1119. // Private constructor
  1120. inline _RTLENTRY TSubString::TSubString(const string _FAR *sp,
  1121.                               size_t start,
  1122.                               size_t len ) :
  1123.     begin(start),
  1124.     extent(len),
  1125.     s((string _FAR *)sp)
  1126. {
  1127. }
  1128.  
  1129. inline TSubString _RTLENTRY string::operator()( const TRegexp _FAR & r ) THROW_NONE
  1130. {
  1131.     return (*this)(r,0);
  1132. }
  1133.  
  1134. inline const TSubString _RTLENTRY string::operator()( const TRegexp _FAR &r ) const THROW_NONE
  1135. {
  1136.     return (*this)(r,0);
  1137. }
  1138.  
  1139.  
  1140. #if !defined(RC_INVOKED)
  1141.  
  1142. #if defined(__STDC__)
  1143. #pragma warn .nak
  1144. #endif
  1145.  
  1146. #pragma option -Vo.     // restore user C++ options
  1147.  
  1148. #if !defined(__TINY__)
  1149. #pragma option -RT.
  1150. #endif
  1151.  
  1152. #if defined(__BCOPT__)
  1153. #if !defined(_RTL_ALLOW_po) && !defined(__FLAT__)
  1154. #pragma option -po.     // restore Object data calling convention
  1155. #endif
  1156. #endif
  1157.  
  1158. #pragma option -a.      // restore default packing
  1159.  
  1160. #endif  /* !RC_INVOKED */
  1161.  
  1162.  
  1163. #endif  // __CSTRING_H
  1164.  
  1165.