ADMB Documentation  11.1.2490
 All Classes Files Functions Variables Typedefs Friends Defines
string2.cpp
Go to the documentation of this file.
00001 /*
00002  * $Id: string2.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 #ifndef OPT_LIB
00012   #include <cassert>
00013 #endif
00014 
00015 adstring::adstring(const size_t lb, const size_t 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   size_t sz = ub;
00024   allocate(sz);
00025   for (size_t 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   size_t 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   size_t us = u.size();
00054   size_t vs = v.size();
00055   adstring tmp(1, us + vs);
00056   for (size_t i = 1; i <= us; i++)
00057   {
00058     tmp(i) = u(i);
00059   }
00060   for (size_t i = 1; i <= vs; i++)
00061   {
00062     tmp(i + us) = v(i);
00063   }
00064   return tmp;
00065 }
00066 
00067 adstring itoa(int n, int r)
00068 {
00069 #ifndef OPT_LIB
00070   assert(r != 0);
00071   assert(r != 1);
00072 #endif
00073 
00074   int sign = n <= 0 ? 1 : 0;
00075   if (sign)
00076   {
00077     n *= -1;
00078   }
00079   unsigned char buf[50];
00080   size_t ii=0;
00081   do
00082   {
00083     int nr = n % r;
00084     buf[ii++] = (unsigned char)nr;
00085   } while (n /= r);
00086 
00087   if (sign)
00088   {
00089     ii++;
00090   }
00091   adstring s(1, ii);
00092   for (size_t i=0;i<ii;i++)
00093   {
00094     s[ii-i]=(unsigned char)(buf[i]+48);
00095   }
00096   if (sign)
00097   {
00098     s(1) = '-';
00099   }
00100   return s;
00101 }