Main Page   Class Hierarchy   Alphabetical List   Compound List   File List   Compound Members   File Members   Related Pages  

String_Base.cpp

Go to the documentation of this file.
00001 // $Id: String_Base.cpp,v 1.1.1.1 2003/02/21 18:36:32 chad Exp $
00002 
00003 #include "ace/Malloc.h"
00004 #include "ace/String_Base.h"
00005 #include "ace/Auto_Ptr.h"
00006 
00007 #if !defined (ACE_LACKS_IOSTREAM_TOTALLY)
00008 # include "ace/streams.h"
00009 #endif /* ! ACE_LACKS_IOSTREAM_TOTALLY */
00010 
00011 #if !defined (__ACE_INLINE__)
00012 #include "ace/String_Base.i"
00013 #endif /* __ACE_INLINE__ */
00014 
00015 ACE_RCSID(ace, String_Base, "String_Base.cpp,v 4.61 2001/03/04 00:55:30 brunsch Exp")
00016 
00017 ACE_ALLOC_HOOK_DEFINE(ACE_String_Base)
00018 
00019 template <class CHAR>
00020 CHAR ACE_String_Base<CHAR>::NULL_String_ = '\0';
00021 
00022 // this method might benefit from a little restructuring.
00023 template <class CHAR> void
00024 ACE_String_Base<CHAR>::set (const CHAR *s,
00025                             size_t len,
00026                             int release)
00027 {
00028   // Case 1. Going from memory to more memory
00029   size_t new_buf_len = len + 1;
00030   if (s != 0 && len != 0 && release && this->buf_len_ < new_buf_len)
00031     {
00032       CHAR *temp;
00033       ACE_ALLOCATOR (temp,
00034                      (CHAR *) this->allocator_->malloc (new_buf_len * sizeof (CHAR)));
00035 
00036       if (this->release_)
00037         this->allocator_->free (this->rep_);
00038 
00039       this->rep_ = temp;
00040       this->buf_len_ = new_buf_len;
00041       this->release_ = 1;
00042       this->len_ = len;
00043       ACE_OS::memcpy (this->rep_, s, len * sizeof (CHAR));
00044       // NUL terminate.
00045       this->rep_[len] = '\0';
00046     }
00047 
00048   // Case 2. No memory allocation is necessary.
00049   else
00050     {
00051       // Free memory if necessary and figure out future ownership
00052       if (!release || s == 0 || len == 0)
00053         {
00054           if (this->release_)
00055             {
00056               this->allocator_->free (this->rep_);
00057               this->release_ = 0;
00058             }
00059         }
00060       // else - stay with whatever value for release_ we have.
00061 
00062       // Populate data.
00063       if (s == 0 || len == 0)
00064         {
00065           this->buf_len_ = 0;
00066           this->len_ = 0;
00067           this->rep_ = &ACE_String_Base<CHAR>::NULL_String_;
00068         }
00069       else if (!release)
00070         {
00071           this->buf_len_ = len;
00072           this->len_ = len;
00073           this->rep_ = (CHAR *) s;
00074         }
00075       else
00076         {
00077           ACE_OS::memcpy (this->rep_, s, len * sizeof (CHAR));
00078           // NUL terminate.
00079           this->rep_[len] = 0;
00080           this->len_ = len;
00081         }
00082     }
00083 }
00084 
00085 // Return substring.
00086 template <class CHAR> ACE_String_Base<CHAR>
00087 ACE_String_Base<CHAR>::substring (size_t offset,
00088                                   ssize_t length) const
00089 {
00090   ACE_String_Base<CHAR> nill;
00091   size_t count = length;
00092 
00093   // case 1. empty string
00094   if (this->len_ == 0)
00095     return nill;
00096   
00097   // case 2. start pos past our end
00098   if (offset >= this->len_)
00099     return nill;
00100   // No length == empty string.
00101   else if (length == 0)
00102     return nill;
00103   // Get all remaining bytes.
00104   else if (length == -1 || count > (this->len_ - offset))
00105     count = this->len_ - offset;
00106 
00107   return ACE_String_Base<CHAR> (&this->rep_[offset],
00108                                 count,
00109                                 this->allocator_);
00110 }
00111 
00112 // Concat operator (does copy memory).
00113 
00114 template <class CHAR> ACE_String_Base<CHAR> &
00115 ACE_String_Base<CHAR>::operator+= (const ACE_String_Base<CHAR> &s)
00116 {
00117   ACE_TRACE ("ACE_String_Base<CHAR>::operator+=");
00118 
00119   if (s.len_ > 0)
00120     {
00121       size_t new_buf_len = this->len_ + s.len_ + 1;
00122 
00123       // case 1. No memory allocation needed.
00124       if (this->buf_len_ >= new_buf_len)
00125         // Copy in data from new string.
00126         ACE_OS::memcpy (this->rep_ + this->len_,
00127                         s.rep_,
00128                         s.len_ * sizeof (CHAR));
00129       // case 2. Memory reallocation is needed
00130       else
00131         {
00132           CHAR *t = 0;
00133 
00134           ACE_ALLOCATOR_RETURN (t,
00135                                 (CHAR *) this->allocator_->malloc (new_buf_len *
00136                                                                    sizeof (CHAR)),
00137                                 *this);
00138 
00139           // Copy memory from old string into new string.
00140           ACE_OS::memcpy (t,
00141                           this->rep_,
00142                           this->len_ * sizeof (CHAR));
00143 
00144           ACE_OS::memcpy (t + this->len_,
00145                           s.rep_,
00146                           s.len_ * sizeof (CHAR));
00147 
00148           if (this->release_)
00149             this->allocator_->free (this->rep_);
00150 
00151           this->release_ = 1;
00152           this->rep_ = t;
00153           this->buf_len_ = new_buf_len;
00154         }
00155 
00156       this->len_ += s.len_;
00157       this->rep_[this->len_] = '\0';
00158     }
00159 
00160   return *this;
00161 }
00162 
00163 template <class CHAR> void
00164 ACE_String_Base<CHAR>::resize (size_t len, CHAR c)
00165 {
00166   ACE_TRACE ("ACE_String_Base<CHAR>::resize");
00167 
00168   // Only reallocate if we don't have enough space...
00169   if (this->buf_len_ <= len)
00170     {
00171       if (this->buf_len_ != 0)
00172         this->allocator_->free (this->rep_);
00173 
00174       this->rep_ = (CHAR *)
00175         this->allocator_->malloc ((len + 1) * sizeof (CHAR));
00176       this->buf_len_ = len + 1;
00177     }
00178 
00179   this->len_ = 0;
00180   ACE_OS::memset (this->rep_,
00181                   c,
00182                   this->buf_len_ * sizeof (CHAR));
00183 }

Generated on Mon Jun 16 11:21:31 2003 for ACE by doxygen1.2.14 written by Dimitri van Heesch, © 1997-2002