ADMB Documentation  11.1x.2735
 All Classes Files Functions Variables Typedefs Friends Defines
string.cpp
Go to the documentation of this file.
00001 /*
00002  * $Id: string.cpp 2421 2014-09-28 03:59:00Z 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(const size_t 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     shape=0;
00028   }
00029   if (s)
00030   {
00031     s++;
00032 
00033     delete [] s;
00034     s=0;
00035   }
00036 }
00037 adstring::operator unsigned char*()
00038 {
00039   return (unsigned char*)s + 1;
00040 }
00041 
00042 adstring::operator char*()
00043 {
00044   return (char*)(s + 1);
00045 }
00046 
00047 adstring::operator const unsigned char*() const
00048 {
00049   return (const unsigned char*)s + 1;
00050 }
00051 
00052 adstring::operator const char*() const
00053 {
00054   return (const char*)(s + 1);
00055 }
00056 
00057 size_t adstring::size() const
00058 {
00059   return s ? strlen((char*)(s+1)) : 0;
00060 }
00061 
00062 size_t adstring::buff_size() const
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   *this = adstring(t);
00083   return *this;
00084 }