ADMB Documentation  11.1.2394
 All Classes Files Functions Variables Typedefs Friends Defines
string.cpp
Go to the documentation of this file.
00001 /*
00002  * $Id: string.cpp 2325 2014-09-11 22:27:41Z johnoel $
00003  *
00004  * Author: David Fournier
00005  * Copyright (c) 2008-2012 Regents of the University of California
00006  */
00007 #include <fvar.hpp>
00008 #include <string.h>
00009 #include <stdlib.h>
00010 
00011 #ifndef OPT_LIB
00012   #include <cassert>
00013   #include <climits>
00014 #endif
00015 
00016 void adstring::allocate(int sz)
00017 {
00018   shape = new adstring_shape(sz);
00019   s =  new unsigned char[sz+1];
00020   if (!s) {
00021     cerr << "Error allocating memory for adstring" << endl;
00022     exit(1);
00023   }
00024   s--;
00025 }
00026 
00027 void adstring::deallocate()
00028 {
00029   if (shape)
00030   {
00031     delete shape;
00032   }
00033   shape=0;
00034   if (s)
00035   {
00036     s++;
00037     delete [] s;
00038     s=0;
00039   }
00040 }
00041 adstring::operator unsigned char * ()
00042 {
00043   return (unsigned char*)s + 1;
00044 }
00045 
00046 adstring::operator char * ()
00047 {
00048   return (char*)(s + 1);
00049 }
00050 
00051 adstring::operator const unsigned char * () const
00052 {
00053   return (const unsigned char*)s + 1;
00054 }
00055 
00056 adstring::operator const char * () const
00057 {
00058   return (const char*)(s + 1);
00059 }
00060 
00061 unsigned int adstring::size(void) const
00062 {
00063   if (!s) return 0;
00064 
00065 #ifdef OPT_LIB
00066   return (unsigned int)strlen((char*)(s+1));
00067 #else
00068   size_t len = strlen((char*)(s+1));
00069   assert(len <= (size_t)UINT_MAX);
00070   return (unsigned int)len;
00071 #endif
00072 }
00073 
00074 unsigned int adstring::buff_size(void)
00075 {
00076   return shape->size();
00077 }
00078 
00079 ostream& operator<<(ostream& c, const adstring& t)
00080 {
00081   for (unsigned int i = 1; i <= t.size(); i++)
00082   {
00083   #if (defined __ZTC__) || (defined __NDPX__)
00084     c << (char)(t.s)[i];
00085   #else
00086     c << (t.s)[i];
00087   #endif
00088   }
00089   return (c);
00090 }
00091 
00092 adstring& adstring::operator=(const char t)
00093 {
00094   return (*this = adstring(t));
00095 }