string_conv.h

Go to the documentation of this file.
00001 /*
00002   -------------------------------------------------------------------
00003   
00004   Copyright (C) 2006, 2007, 2008, Andrew W. Steiner
00005   
00006   This file is part of O2scl.
00007   
00008   O2scl is free software; you can redistribute it and/or modify
00009   it under the terms of the GNU General Public License as published by
00010   the Free Software Foundation; either version 3 of the License, or
00011   (at your option) any later version.
00012   
00013   O2scl is distributed in the hope that it will be useful,
00014   but WITHOUT ANY WARRANTY; without even the implied warranty of
00015   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00016   GNU General Public License for more details.
00017   
00018   You should have received a copy of the GNU General Public License
00019   along with O2scl. If not, see <http://www.gnu.org/licenses/>.
00020 
00021   -------------------------------------------------------------------
00022 */
00023 #ifndef O2SCL_STRING_CONV_H
00024 #define O2SCL_STRING_CONV_H
00025 /** \file string_conv.h
00026     \brief Various string conversion functions
00027 */
00028 
00029 #include <iostream>
00030 #include <cmath>
00031 #include <string>
00032 #include <fstream>
00033 #include <sstream>
00034 #include <o2scl/lib_settings.h>
00035 #include <gsl/gsl_ieee_utils.h>
00036 
00037 #ifndef DOXYGENP
00038 namespace o2scl {
00039 #endif
00040 
00041   /** 
00042       \brief Convert a pointer to a string 
00043 
00044       This uses an \c ostringstream to convert a pointer to a string
00045       and is architecture-dependent.
00046   */
00047   std::string ptos(void *p);
00048 
00049   /** \brief Convert an integer to a string 
00050    */
00051   std::string itos(int x);
00052 
00053   /** \brief Convert a double to a string 
00054    */
00055   std::string dtos(double x, int prec=6, bool auto_prec=false);
00056 
00057   /** \brief Returns the number of characters required to display the 
00058       exponent of \c x in scientific mode
00059 
00060       This usually returns 2 or 3, depending on whether or not the 
00061       absolute magnitude of the exponent is greater than 100.
00062    */
00063   size_t size_of_exponent(double x);
00064 
00065   /** 
00066       \brief Convert a double to a string using a specified format
00067       
00068       \todo Add error checking to this function using \c if(strout << x)
00069   */
00070   std::string dtos(double x, std::ostream &format);
00071 
00072   /** 
00073       \brief Convert a string to an integer 
00074       
00075       If this function fails it will call set_err() and return zero.
00076   */
00077   int stoi(std::string s);
00078 
00079   /** 
00080       \brief Convert a string to a double 
00081 
00082       If this function fails it will call set_err() and return zero.
00083   */
00084   double stod(std::string s);
00085 
00086   /** 
00087       \brief Convert a double to a Latex-like string
00088 
00089       The parameter \c sigfigs is the number of significant figures
00090       to give in the string. The parameters \c ex_min and \c ex_max
00091       represent the base-10 logarithm of the smallest and largest 
00092       numbers to be represented without the string 
00093       \verbatim
00094       $\times 10^{\mathrm{ex}}$
00095       \endverbatim
00096       where \c ex is the relevant exponent.  The number zero is always
00097       converted to "0".
00098 
00099       Note that this function does not warn the user if the number
00100       of significant figures requested is larger than the machine 
00101       precision.
00102 
00103       \todo Fix to ensure final zeros are printed properly
00104       if requested
00105   */
00106   std::string double_to_latex(double x, int sigfigs=5, int ex_min=-2, 
00107                               int ex_max=3);
00108 
00109   /** 
00110       \brief Convert a double to a HTML-like string
00111 
00112       This uses \c \&times; and \c <sup></sup> to convert a double
00113       to a string for use on the web. 
00114 
00115       The parameter \c sigfigs is the number of significant figures to
00116       give in the string. The parameters \c ex_min and \c ex_max
00117       represent the base-10 logarithm of the smallest and largest
00118       numbers to be represented without exponential notation. The
00119       number zero is always converted to "0". Superscripts are
00120       implemented using (can't use greater than size in documentation)
00121 
00122       Note that this function does not warn the user if the number
00123       of significant figures requested is larger than the machine 
00124       precision.
00125   */
00126   std::string double_to_html(double x, int sigfigs=5, int ex_min=-2, 
00127                              int ex_max=3);
00128   
00129   /** 
00130       \brief Convert a double to a string containing IEEE representation
00131       
00132       Modeled after the GSL function \c gsl_ieee_fprintf_double(), but
00133       converts to a \c string instead of a \c FILE *.
00134   */
00135   std::string double_to_ieee_string(double *x);
00136 
00137 #ifndef DOXYGENP
00138 }
00139 #endif
00140 
00141 #endif
00142 

Documentation generated with Doxygen and provided under the GNU Free Documentation License. See License Information for details.

Project hosting provided by SourceForge.net Logo, O2scl Sourceforge Project Page