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