Main Page   Class Hierarchy   Compound List   File List   Compound Members   File Members  

/magnus/back_end/general/include/Chars.h

Go to the documentation of this file.
00001 /*
00002  *   $Id: Chars.h,v 1.7 2000/03/03 04:11:09 bormotov Exp $
00003  */
00004  
00005 //   Copyright (C) 1994 The New York Group Theory Cooperative
00006 //   See magnus/doc/COPYRIGHT for the full notice.
00007  
00008 // Contents: Definition of Chars and CharsRep classes
00009 //
00010 //   Chars is a char* which knows its length, wrapped up as a user class.
00011 //
00012 // Principal Author: Roger Needham
00013 //
00014 // Status: Useable.
00015 //
00016 // Revision History:
00017 //
00018 // * 6/23/95 Roger flushed the old version, which used the libg++
00019 //           String class, when he discovered the hard way that a
00020 //           String cannot be longer than 2^15.
00021 //
00022 // * 01/96 Dmitry B. implemented IPC tools.
00023 //
00024 // * 2/97 EP added: 
00025 //           Chars(int num);  // constructor
00026 //           Chars ordinalPostfix(int num); // global
00027 //
00028 // * 03/97 dp made destructor ~CharsRep() virtual.
00029 //
00030 // Next Implementation Steps:
00031 //
00032 // * Make more versions of Chars::op == and Chars::op !=
00033 //   which do not require conversion of the arguments.
00034 //
00035  
00036 #ifndef _CHARS_H_
00037 #define _CHARS_H_
00038  
00039 
00040 #include <string.h>
00041 #include "RefCounter.h"
00042 #include "ObjectOf.h"
00043 
00044 
00045 //------------------------------------------------------------------------//
00046 //---------------------------- CharsRep ----------------------------------//
00047 //------------------------------------------------------------------------//
00048 
00049 //! Representation class for Chars
00050 class CharsRep : public RefCounter
00051 {
00052 public:
00053 
00054   /////////////////////////////////////////////////////////////////////////
00055   //                                                                     //
00056   // Constructors:                                                       //
00057   //                                                                     //
00058   /////////////////////////////////////////////////////////////////////////
00059 
00060   CharsRep( );  //!< Gives empty string
00061 
00062   CharsRep( const char* s );  //!< Creates a string from 's'
00063 
00064   CharsRep( char c ); //!< Creates a string from 'c'
00065 
00066   CharsRep( const CharsRep& CR ); //!< Copy constructor
00067 
00068   CharsRep( int num ); //!< Creates an empty string of length 'num'
00069 
00070   virtual ~CharsRep( );
00071 
00072   /////////////////////////////////////////////////////////////////////////
00073   //                                                                     //
00074   // Accessors:                                                          //
00075   //                                                                     //
00076   /////////////////////////////////////////////////////////////////////////
00077 
00078   int len( ) const { return length; } //!< Returns length of the string
00079 
00080   const char* getChars( ) const { return theChars; } //!< Returns string as a pointer to char
00081   
00082    //! for reference access
00083   char& ref(int i);
00084   
00085   //! for value access
00086   char val(int i) const;
00087 
00088   /////////////////////////////////////////////////////////////////////////
00089   //                                                                     //
00090   // Misc:                                                               //
00091   //                                                                     //
00092   /////////////////////////////////////////////////////////////////////////
00093   
00094   CharsRep* clone( ) const { return new CharsRep( *this ); }
00095 
00096   void concat( const char*, int len );
00097   //!< Concatenate string of length 'len'
00098 
00099   void readFrom(istream& istr);
00100   //!< Read string from a stream
00101 
00102   /////////////////////////////////////////////////////////////////////////
00103   //                                                                     //
00104   // IPC tools:                                                          //
00105   //                                                                     //
00106   /////////////////////////////////////////////////////////////////////////
00107 
00108   virtual void write( ostream& ostr ) const;
00109   //!< IPC tools:                                                          
00110  
00111   virtual void read( istream& istr );
00112   //!< IPC tools:                                                          
00113  
00114 
00115 private:
00116 
00117   CharsRep operator = ( const CharsRep& );  // Hide this
00118   //!< Prohibited
00119 
00120   /////////////////////////////////////////////////////////////////////////
00121   //                                                                     //
00122   // Data Members:                                                       //
00123   //                                                                     //
00124   /////////////////////////////////////////////////////////////////////////
00125 
00126   int length;
00127 
00128   char* theChars;
00129 
00130   char special[2];
00131   // To not use the heap even more in the special cases of the
00132   // empty string, and the one char string.
00133 
00134 };
00135 
00136 
00137 //------------------------------------------------------------------------//
00138 //------------------------------ Chars -----------------------------------//
00139 //------------------------------------------------------------------------//
00140 
00141 
00142 //!   Chars is a char* which knows its length, wrapped up as a user class.
00143 class Chars : public ObjectOf<CharsRep>
00144 {
00145 public:
00146   
00147   /////////////////////////////////////////////////////////////////////////
00148   //                                                                     //
00149   // Constructors:                                                       //
00150   //                                                                     //
00151   /////////////////////////////////////////////////////////////////////////
00152   
00153   // copy constructor supplied by compiler.
00154 
00155   Chars( ) : ObjectOf<CharsRep>( new CharsRep() ) { }  //!< Gives empty string
00156 
00157   
00158   Chars( const char* s ) : ObjectOf<CharsRep>( new CharsRep(s) ) { }
00159   //!< Creates a string from 's'
00160 
00161   Chars( const char c ) : ObjectOf<CharsRep>( new CharsRep(c) ) { }
00162   //!< Creates a string from 'c'
00163     
00164   Chars ( int num ) : ObjectOf<CharsRep>( new CharsRep(num) ) { }
00165 //!< Creates an empty string of length 'num'
00166   
00167   // destructor supplied by compiler.
00168 
00169   /////////////////////////////////////////////////////////////////////////
00170   //                                                                     //
00171   // Accessors:                                                          //
00172   //                                                                     //
00173   /////////////////////////////////////////////////////////////////////////
00174   
00175   int length( ) const { return look()->len(); }
00176 //!< Returns length of the string
00177   
00178   /////////////////////////////////////////////////////////////////////////
00179   //                                                                     //
00180   // Operators:                                                          //
00181   //                                                                     //
00182   /////////////////////////////////////////////////////////////////////////
00183   
00184   // assignment operator supplied by compiler.
00185 
00186   inline friend int operator == ( const Chars& l, const Chars& r ) {
00187     return ( ! strcmp(l.look()->getChars(), r.look()->getChars()) );
00188   }
00189   //!< Returns true if two strings are equal, false otherwise
00190 
00191   bool operator == ( const char * s) const {
00192     return ! strcmp( look()->getChars(), s );
00193   }
00194   //!< Returns true if the string is equal to 's', false otherwise
00195  
00196   inline friend int operator != ( const Chars& l, const Chars& r ) {
00197     return ( !(l == r) );
00198   }
00199   //!< Returns false if two strings are equal
00200 
00201   inline friend int operator != ( const Chars& l, const char* r ) {
00202     return ( !(l == r) );
00203   }
00204   //!< Returns false if two strings are equal
00205   // op == and op != global to permit symmetric promotion of arguments.
00206   
00207   Chars operator + (const Chars&) const;
00208    //!< Returns a concatenation of *this with  a string
00209  
00210   Chars operator + (const char*) const;
00211    //!< Returns a  concatenation of *this with  a string
00212  
00213   Chars operator + (char) const;
00214     //!< Returns concatenation of *this with  a character
00215   
00216   Chars& operator += (const Chars&);
00217   //!< Concatenate a string
00218 
00219   Chars& operator += (const char*);
00220   //!< Concatenate a string
00221 
00222   Chars& operator += (char);
00223    //!< Concatenate a character
00224  
00225   operator const char*( ) const { return look()->getChars(); }
00226     //!< Convertion operator
00227  
00228   char operator [] ( int i ) const { return look()->val(i); }
00229   //!< Returns the i'th character in the string
00230 
00231   char& operator [] ( int i ) { return change()->ref(i); }
00232   //!< Returns a reference to the  i'th character in the string
00233 
00234   friend ostream& operator << ( ostream& ostr, const Chars& C ) {
00235     ostr << C.look()->getChars();
00236     return ostr;
00237   }
00238   //!< Output string 
00239   
00240   friend istream& operator >> ( istream& istr, Chars& C ) {
00241          C.change()->readFrom(istr);
00242          return istr;
00243   }
00244   //!< This reads a whitespace-delimited string which cannot be longer than 2^15.
00245 
00246   
00247   /////////////////////////////////////////////////////////////////////////
00248   //                                                                     //
00249   // IPC tools:                                                          //
00250   //                                                                     //
00251   /////////////////////////////////////////////////////////////////////////
00252 
00253   friend ostream& operator < ( ostream& ostr, const Chars& c )
00254   {
00255     c.look()->write(ostr);
00256     return ostr;
00257   }
00258    //!< IPC tools:                                                          
00259  
00260   friend istream& operator > ( istream& istr, Chars& c )
00261   {
00262     c.change()->read(istr);
00263     return istr;
00264   }
00265   //!< IPC tools:                                                          
00266 
00267 };
00268 
00269 //========================================================================
00270 //                    Global scope functions
00271 //========================================================================
00272 
00273 Chars ordinalPostfix(int num);
00274 //!< returns ordinal postfix for given number, i.e. "st", "nd", "rd" or "th"
00275 
00276 Chars encodeSpaces( const Chars& s );
00277 //!< Encodes all spaces with escape char. 
00278 /*!< The original escape char is encoded  with double escape. */
00279 
00280 Chars decodeSpaces( const Chars& s );
00281 //!< Decodes string restoring spaces.
00282 
00283 #endif

Generated at Tue Jun 19 09:49:35 2001 for Magnus Classes by doxygen1.2.6 written by Dimitri van Heesch, © 1997-2001