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

String.cpp

00001 
00002 //  -- stdnet headers --
00003 #include <stdnet/system/String>
00004 
00005 //  -- C Headers --
00006 #include <string.h>
00007 
00008 namespace stdbase {
00009     namespace system {
00010 
00011 
00012 // -- Factory Functions --
00013 //
00014 reference<String> String::
00015     create( const char val[])
00016 {
00017     return reference<String>( new String(val) );
00018 }
00019 
00020 reference<String> String::
00021     create( const char val[], const int len)
00022 {
00023     return reference<String>( new String(val, len) );
00024 }
00025 
00026 reference<String> String::
00027     create( char * const val, _TakeOwnership)
00028 {
00029     return reference<String>( new String(val, _TakeOwnership() ) );
00030 }
00031 
00032 reference<String> String::
00033     create(
00034         char * const val,
00035         const int len,
00036         _TakeOwnership
00037     )
00038 {
00039     return reference<String>( new String(val, len, _TakeOwnership() ) );
00040 }
00041 
00042 //  --  Class Methods --
00043 //
00044 String::
00045     String( const char s[])
00046         :   Object(),
00047             string_val( 0 ),
00048             string_len( strlen(s) )
00049 {
00050     string_val = new char[ string_len ];
00051     strncpy( string_val, s, string_len );
00052 }
00053 
00054 String::
00055     String( const char s[], const int len)
00056         :   Object(),
00057             string_val( new char[ len ] ),
00058             string_len( len )
00059 {   strncpy( string_val, s, len ); }
00060 
00061 String::
00062     String( char * const s, _TakeOwnership)
00063         :   Object(),
00064             string_val( s ),
00065             string_len( strlen(s) )
00066 {};
00067 
00068 String::
00069     String( char * const s, const int len, _TakeOwnership o)
00070         :   Object(),
00071             string_val( s ),
00072             string_len( len )
00073 {};
00074 
00075 String::
00076     ~String()
00077 {
00078     delete [] string_val;
00079     string_val = 0;
00080     string_len = 0;
00081 }
00082 
00083 bool String::
00084     equals( ObjectRef obj) const
00085 {
00086     return const_cast< const Object *>( obj.get() ) == this;
00087 }
00088 
00089 int String::
00090     length() const
00091 {   return string_len; }
00092 
00093 void String::
00094     getChars(
00095         int     srcBegin,
00096         int     srcEnd,
00097         char    dst[],
00098         int     dstBegin
00099     ) const
00100 {
00101     int srcLen = srcEnd - srcBegin;
00102 
00103     strncpy(
00104         & dst[ dstBegin ],
00105         & string_val[ srcBegin ],
00106         srcLen
00107     );
00108 }
00109 
00110 
00111 StringRef   StringRef::
00112 operator + (
00113         // this ptr is implicit 'a'
00114         const StringRef & b
00115     ) const
00116 {
00117     const StringRef & a = *this;
00118     int a_len = a->length();
00119     int b_len = b->length();
00120     const int len = a_len + b_len;  // + 1;
00121 
00122     char * store = new char[ len ];
00123 
00124     a->getChars( 0, a_len, store, 0);
00125     b->getChars( 0, b_len, store, a_len);
00126     store[len] = 0;
00127 
00128     StringRef str = String::
00129                 create(
00130                     (char * const) store,
00131                     len,
00132                     _TakeOwnership()
00133                 );
00134 
00135     //  -- PLEASE NOTE --
00136     //  I have not deleted the store variable deliberately!
00137     //  The _TakeOwnership argument flags to the String object
00138     //  that the store pointer must be managed by it.
00139     //
00140     return str;
00141 }
00142 
00143 StringRef StringRef::
00144 operator + (
00145         //  this is implicit a
00146         const char        b[]
00147     ) const
00148 {
00149     const StringRef & a = *this;
00150     int a_len = a->length();
00151     int b_len = strlen( b );
00152     const int len = a_len + b_len; // + 1;
00153 
00154     char * store = new char[ len + 1];
00155 
00156     a->getChars( 0, a_len, store, 0);
00157     strncpy( & store[ a_len ], b, b_len );
00158     store[len] = 0;
00159 
00160     StringRef str = String::
00161                 create(
00162                     (char * const) store,
00163                     len,
00164                     _TakeOwnership()
00165                 );
00166 
00167     //  -- PLEASE NOTE --
00168     //  I have not deleted the store variable deliberately!
00169     //  The _TakeOwnership argument flags to the String object
00170     //  that the store pointer must be managed by it.
00171     //
00172     return str;
00173 }
00174 
00175 StringRef StringRef::
00176 operator + (
00177         //  this is operand a
00178         const int         b_int
00179     ) const
00180 {
00181     const StringRef & a = *this;
00182     int a_len = a->length();
00183 
00184     //  max length of a 64-bit int
00185     //  we'll use this as a speculative length for b
00186     //  so we can do the string operation once only.
00187     //
00188     const int b_max = 22;
00189     int b_len = b_max;
00190     
00191     int len = a_len + b_max + 1;
00192 
00193     char * store = new char[ len ];
00194 
00195     a->getChars( 0, a_len, store, 0);
00196     b_len = snprintf( & store[ a_len ], b_max, "%d", b_int );
00197     
00198     len = a_len + b_len + 1;
00199     store[len] = 0;
00200 
00201     StringRef str = String::
00202                 create(
00203                     (char * const) store,
00204                     len,
00205                     _TakeOwnership()
00206                 );
00207 
00208     //  -- PLEASE NOTE --
00209     //  I have not deleted the store variable deliberately!
00210     //  The _TakeOwnership argument flags to the String object
00211     //  that the store pointer must be managed by it.
00212     //
00213     return str;
00214 }
00215 
00216 
00217     } // -- end namespace system --
00218 } // ------ end namespace stdbase --
00219 
00220 //  -- Globally accessible operators --
00221 //
00222 

Generated at Tue Aug 13 14:19:39 2002 for stdnet2 by doxygen1.2.9.1 written by Dimitri van Heesch, © 1997-2001