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