ADMB Documentation  11.1.2256
 All Classes Files Functions Variables Typedefs Friends Defines
string.cpp
Go to the documentation of this file.
00001 /*
00002  * $Id: string.cpp 1919 2014-04-22 22:02:01Z 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 void adstring::allocate(int sz)
00012 {
00013   shape = new adstring_shape(sz);
00014   s =  new unsigned char[sz+1];
00015   if (!s) {
00016     cerr << "Error allocating memory for adstring" << endl;
00017     exit(1);
00018   }
00019   s--;
00020 }
00021 
00022 void adstring::deallocate()
00023 {
00024   if (shape)
00025   {
00026     delete shape;
00027   }
00028   shape=0;
00029   if (s)
00030   {
00031     s++;
00032     delete [] s;
00033     s=0;
00034   }
00035 }
00036 adstring::operator unsigned char * ()
00037 {
00038   return (unsigned char*)s + 1;
00039 }
00040 
00041 adstring::operator char * ()
00042 {
00043   return (char*)(s + 1);
00044 }
00045 
00046 adstring::operator const unsigned char * () const
00047 {
00048   return (const unsigned char*)s + 1;
00049 }
00050 
00051 adstring::operator const char * () const
00052 {
00053   return (const char*)(s + 1);
00054 }
00055 
00056   unsigned int adstring::size(void) const
00057   {
00058     if (!s) return 0;
00059     return (strlen((char*)(s+1)));
00060   }
00061 
00062   unsigned int adstring::buff_size(void)
00063   {
00064     return (shape->size());
00065   }
00066 
00067 ostream& operator<<(ostream& c, const adstring& t)
00068 {
00069   for (unsigned int i = 1; i <= t.size(); i++)
00070   {
00071   #if (defined __ZTC__) || (defined __NDPX__)
00072     c << (char)(t.s)[i];
00073   #else
00074     c << (t.s)[i];
00075   #endif
00076   }
00077   return (c);
00078 }
00079 
00080 adstring& adstring::operator=(const char t)
00081 {
00082   return (*this = adstring(t));
00083 }