home *** CD-ROM | disk | FTP | other *** search
/ DOS/V Power Report 1997 May / VPR9705A.ISO / VPR_DATA / PROGRAM / CBTRIAL / SETUP / DATA.Z / CSTRING.H < prev    next >
C/C++ Source or Header  |  1997-02-14  |  36KB  |  1,127 lines

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