ADMB Documentation  11.1.2372
 All Classes Files Functions Variables Typedefs Friends Defines
string2.cpp
Go to the documentation of this file.
00001 /*
00002  * $Id: string2.cpp 2365 2014-09-17 19:19:02Z 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 #endif
00014 
00015 adstring::adstring(int lb, int ub) : clist()
00016 {
00017   if (lb != 1)
00018   {
00019     cerr << " Error in adstring::adstring(int lb,int ub) : clist()\n"
00020     " At present minimum index for adstring must equal 1\n";
00021     exit(1);
00022   }
00023   unsigned int sz = ub;
00024   allocate(sz);
00025   for (unsigned int i = 1; i <= sz; i++)
00026   {
00027     s[i] = '\0'; //' ';
00028   }
00029   s[sz + 1] = '\0';
00030 }
00031 
00032 adstring::adstring(const char ub) : clist()
00033 {
00034   unsigned int sz = 1;
00035   allocate(sz);
00036 
00037 #ifndef OPT_LIB
00038   assert(ub >= 0);
00039 #endif
00040 
00041   s[1] = (unsigned char)ub;
00042   s[2] = '\0';
00043 }
00044 
00045 adstring::adstring(const adstring & v) : clist(v)
00046 {
00047   shape = v.shape;
00048   s = v.s;
00049 }
00050 
00051 adstring operator+(const adstring& u, const adstring& v)
00052 {
00053   int us = u.size ();
00054   int vs = v.size ();
00055   adstring tmp(1, us + vs);
00056   int i;
00057   for (i = 1; i <= us; i++)
00058   {
00059     tmp(i) = u(i);
00060   }
00061   for (i = 1; i <= vs; i++)
00062   {
00063     tmp(i + us) = v(i);
00064   }
00065   return (tmp);
00066 }
00067 
00068 adstring itoa(int n,int r)
00069 {
00070   char buf[50];
00071   int ii=0;
00072   do
00073   {
00074     buf[ii++]=n%r;
00075   } while (n/=r);
00076 
00077   adstring s(1,ii);
00078   for (int i=0;i<ii;i++)
00079   {
00080     s[ii-i]=buf[i]+48;
00081   }
00082   return s;
00083 }