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