ADMB Documentation  11.2.2853
 All Classes Files Functions Variables Typedefs Friends Defines
str.cpp
Go to the documentation of this file.
00001 /*
00002  * $Id: str.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 <stdio.h>
00010 #include <stdlib.h>
00011 #include <math.h>
00012 #ifdef __NDPX__
00013   #include <dos.h> //itoa
00014 #endif
00015 #ifndef OPT_LIB
00016   #include <cassert>
00017   #include <climits>
00018 #endif
00019 
00020 adstring str(double x, int minwidth, int decplaces)
00021 {
00022    int w = 0;
00023    int d = 0;
00024    char  buffer[50];
00025 
00026    int maxwidth = 0;
00027    if (fabs(x) > 0)
00028      maxwidth = int(log10(fabs(x)));
00029    else
00030      maxwidth = minwidth;
00031 
00032    if (decplaces < 0) // decplaces not specified
00033    {
00034      if (minwidth < 8)
00035        w = 8;
00036      else
00037        w = minwidth;
00038 
00039      if (x < 0)
00040        w++;
00041 
00042      d = minwidth-7;
00043      if (d>10)
00044        d = 10;
00045 
00046      //tmp = new adstring(1,w-1);
00047      //sprintf(tmp->s,"%*.*e",w,d,x);
00048      sprintf(buffer,"%*.*e",w,d,x);
00049    }
00050    else // decplaces specified
00051    {
00052      if (decplaces > 11)
00053        d = 11;
00054      else
00055        d = decplaces;
00056 
00057      w = maxwidth;
00058      if (w < minwidth)
00059        w = minwidth;
00060 
00061      w += d;//+3;
00062      if (x < 0)
00063        w++;
00064      //tmp = new adstring(1,w-1);
00065 
00066      if (d>=0)
00067      {
00068        //sprintf(tmp->s,"%*.*f",w,d,x);
00069        sprintf(buffer,"%*.*f",w,d,x);
00070      }
00071      else
00072      {
00073        //sprintf(tmp->s,"%*.f",w,x);
00074        sprintf(buffer,"%*.f",w,x);
00075      }
00076    }
00077    //return (*tmp);
00078    adstring tmp(buffer);
00079    return (tmp);
00080 }
00081 
00082 void str(const int a, adstring& s)
00083 {
00084 #if defined(_MSC_VER)
00085   char  buffer[50];
00086   itoa(a, buffer, 10);
00087   s = adstring(buffer);
00088 #else
00089   s = itoa(a,10);
00090 #endif
00091 }
00092 
00093 adstring str(const int a)
00094 {
00095 #if defined(_MSC_VER)
00096   char  buffer[50];
00097   itoa(a, buffer, 10);
00098   //adstring* tmp = new adstring(1,strlen(buffer));
00099   //*tmp = adstring(buffer);
00100   adstring tmp((char*)buffer);
00101 #else
00102   adstring tmp = itoa(a,10);
00103 #endif
00104   return(tmp);
00105 }
00106 
00107 adstring chr(int c)
00108 {
00109 #ifndef OPT_LIB
00110   assert(0 <= c && c <= CHAR_MAX);
00111 #endif
00112   return adstring((char)c);
00113 }