home *** CD-ROM | disk | FTP | other *** search
/ ftptest.leeds.ac.uk / 2015.02.ftptest.leeds.ac.uk.tar / ftptest.leeds.ac.uk / bionet / CAE-GROUP / SCL-WIN3x / SCL.EXE / SCL_STRI.CPP < prev    next >
C/C++ Source or Header  |  1994-10-03  |  4KB  |  199 lines

  1.  
  2. /*
  3. * NIST Utils Class Library
  4. * clutils/scl_string.cc
  5. * February, 1994
  6. * K. C. Morris
  7. * David Sauder
  8.  
  9. * Development of this software was funded by the United States Government,
  10. * and is not subject to copyright.
  11. */
  12.  
  13. /* $Id: scl_string.cc,v 2.0.1.2 1994/05/10 20:56:23 kc Exp $ */
  14.  
  15. #include <scl_string.h>
  16. #include <stdio.h>
  17. /*******************************************************************/
  18.  
  19. int SCLstring::newBufSize (int len) const
  20. {
  21.   int rval =  (((len + 1)/32) +1) * 32;
  22.  
  23.   // if there\'s a max and rval > max return max +1
  24.   return (MaxLength () && (rval > MaxLength ())) ?  MaxLength ()+1 : rval;
  25. }
  26.  
  27. //constructor
  28. SCLstring::SCLstring (const char * str, int lim)
  29. :  _max_len (lim)
  30. {
  31.  
  32.   if (!str) { _strBufSize = 0; _strBuf = 0;  return;  }
  33.  
  34.   // check the length
  35.   int len;
  36.   if (lim && (strlen (str) > lim)) len = lim;
  37.   else len = strlen (str);
  38.  
  39.   _strBufSize = newBufSize (len);
  40.   _strBuf = new char[_strBufSize];
  41.   strncpy (_strBuf, str, len);
  42.   _strBuf [len] = '\0';
  43.  
  44. }    
  45.  
  46. SCLstring::SCLstring (const SCLstring& s)  
  47. :  _max_len (s.MaxLength ())
  48. {
  49.   if (s.is_null ()) { _strBufSize = 0; _strBuf = 0;  return;  }
  50.   int len = newBufSize (s.Length ());
  51.   _strBuf = new char [len];
  52.   strncpy (_strBuf, s, len);
  53.   _strBuf [len] = '\0';
  54. }  
  55.   
  56.  
  57. //destructor
  58. SCLstring::~SCLstring()
  59. {
  60.     if(_strBufSize)
  61.     delete [] _strBuf;
  62. }
  63.  
  64. //operators
  65.  
  66. SCLstring& SCLstring::operator= (const char* str) 
  67. {
  68.   if (!str) { 
  69.     _strBufSize = 0; delete [] _strBuf; _strBuf =0;  
  70.   }
  71.   else {
  72.     int slen = strlen (str);
  73.     if(!_strBuf || (StrBufSize () < newBufSize (slen)) )  {
  74.       // make more room
  75.       // first clean up the space
  76.       if (_strBuf)  delete [] _strBuf;
  77.       _strBufSize = newBufSize (slen+1);
  78.       _strBuf = new char[_strBufSize];
  79.       }
  80.     strncpy (_strBuf, str, _strBufSize);
  81.     _strBuf [_strBufSize -1] = '\0';
  82.   }
  83.     return *this;
  84. }
  85.  
  86. SCLstring::operator const char * ()  const
  87. {
  88.     if (_strBuf) return _strBuf;
  89.     else return "";
  90. }
  91.  
  92. SCLstring& 
  93. SCLstring::Append (const char * s)  
  94. {
  95.   if (!s) return *this;
  96.   int olen = Length (), slen = strlen (s);
  97.   int len = olen + slen +1;
  98.   if (_strBuf && (len < _strBufSize))  strncat (_strBuf, s, slen+1);
  99.   else {  // make more space
  100.     int sz = newBufSize (len);
  101.     char * tmp = new char [sz];  tmp [0] ='\0';
  102.     _strBufSize = sz;
  103.     if (_strBuf) strcpy (tmp, _strBuf );
  104.     len = ((slen > sz-olen) ? sz-olen : slen);
  105.     strncat (tmp, s, len);
  106.     *(tmp+olen+len) = '\0';  
  107.     delete [] _strBuf;
  108.     _strBuf = tmp;
  109.     }
  110.   return *this;
  111. }
  112.  
  113. SCLstring& 
  114. SCLstring::Append (const char c)  
  115. {
  116.   char tmp [2];
  117.   tmp [0] = c;
  118.   tmp [1] = '\0';
  119.   return Append (tmp);
  120. }
  121.    
  122. SCLstring& 
  123. SCLstring::Append (const long int i)
  124. {
  125.   char tmp [BUFSIZ];
  126.   sprintf (tmp, "%ld", i);
  127.   return Append (tmp);
  128. }
  129. SCLstring& 
  130. SCLstring::Append (const double i, const int prec)
  131. {
  132.   char tmp [BUFSIZ];
  133.   sprintf (tmp, "%.*g", prec, i);
  134.   return Append (tmp);
  135.  
  136. SCLstring& 
  137. SCLstring::Prepend (const char * s)  
  138. {
  139.   if (!_strBuf)  {  // no string 
  140.     int sz = newBufSize (strlen (s));
  141.     _strBuf = new char [sz];  
  142.     sz = sz-1;
  143.     strncpy (_strBuf, s, sz);
  144.     _strBuf [sz] = '\0';
  145.     return *this;
  146.   }
  147.   //  otherwise make some space
  148.   int slen = strlen (s);
  149.   int sz = newBufSize (slen + Length () );
  150.   char * nstr = new char [sz];  
  151.   strncpy (nstr, s, sz-1);
  152.   if (slen < sz)  // copy the rest
  153.     strncat (nstr, _strBuf, sz-slen);
  154.   nstr [sz -1] = '\0';  // make sure it\'s null terminated
  155.   delete [] _strBuf;
  156.   _strBuf = nstr;
  157.   return *this;
  158. }
  159.    
  160.  
  161. int 
  162. SCLstring::operator== (const char* s) const
  163. {
  164.   return !strcmp (_strBuf, s);  
  165. }
  166.  
  167. int 
  168. SCLstring::is_null() const
  169. {
  170.      if (_strBuf) return _strBuf[0] == '\0';
  171.      else return 1;
  172. }
  173.  
  174. int 
  175. SCLstring::set_null() 
  176. {
  177.   for (register int i =0; i < _strBufSize; ++i)
  178.     *(_strBuf +i) = '\0';
  179.   return 1;
  180. /*
  181.   delete [] _strBuf;
  182.   _strBuf =0;
  183.   _strBufSize = 0;
  184.   return 1;
  185.   */
  186. }
  187.  
  188. int
  189. SCLstring::StrBufSize() const
  190. { return _strBufSize; }
  191.  
  192. int
  193. SCLstring::Length() const
  194.   if (_strBuf) return strlen (_strBuf); 
  195.   else return 0;
  196. }
  197.