Object-oriented Scientific Computing Library: Version 0.910
string_conv.h
Go to the documentation of this file.
00001 /*
00002   -------------------------------------------------------------------
00003   
00004   Copyright (C) 2006-2012, 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   /** \brief Convert a pointer to a string 
00042 
00043       This uses an \c ostringstream to convert a pointer to a string
00044       and is architecture-dependent.
00045   */
00046   std::string ptos(void *p);
00047 
00048   /** \brief Convert an integer to a string 
00049    */
00050   std::string itos(int x);
00051 
00052   /** \brief Convert a size_t to a string 
00053    */
00054   std::string uitos(size_t x);
00055 
00056   /** \brief Convert a boolean value to a string 
00057 
00058       This returns \c "1" for true and \c "0" for false.
00059    */
00060   std::string btos(bool b);
00061 
00062   /** \brief Convert a double to a string 
00063 
00064       If \c auto_prec is false, then the number is converted to 
00065       a string in the <tt>ios::scientific</tt> mode, otherwise,
00066       neither the scientific or fixed mode flags are set and the
00067       number is converted to a string in "automatic" mode.
00068    */
00069   std::string dtos(double x, int prec=6, bool auto_prec=false);
00070 
00071   /** \brief Returns the number of characters required to display the 
00072       exponent of \c x in scientific mode
00073       
00074       This returns 2 or 3, depending on whether or not the absolute
00075       magnitude of the exponent is greater than or equal to 100. It
00076       uses <tt>stringstream</tt> to convert the number to a string and
00077       counts the number of characters directly.
00078    */
00079   size_t size_of_exponent(double x);
00080 
00081   /** \brief Convert a double to a string using a specified format
00082    */
00083   std::string dtos(double x, std::ostream &format);
00084 
00085   /** \brief Convert a string to an integer 
00086       
00087       If \c err_on_fail is true and the conversion fails, this
00088       function calls the error handler, otherwise this function just
00089       returns zero.
00090   */
00091   int stoi(std::string s, bool err_on_fail=true);
00092 
00093   /** \brief Convert a string to a size_t
00094       
00095       If \c err_on_fail is true and the conversion fails, this
00096       function calls the error handler, otherwise this function just
00097       returns zero.
00098   */
00099   size_t stoui(std::string s, bool err_on_fail=true);
00100 
00101   /** \brief Convert a string to a boolean value
00102       
00103       This returns true if only if the string has at least one
00104       character and the first non-whitespace character is either \c t,
00105       \c T, or one of the numbers 1 through 9.
00106       
00107       If \c err_on_fail is true and the conversion fails, this
00108       function calls the error handler, otherwise this function just
00109       returns false.
00110   */
00111   bool stob(std::string s, bool err_on_fail=true);
00112 
00113   /** \brief Convert a string to a double 
00114 
00115       If \c err_on_fail is true and the conversion fails, this
00116       function calls the error handler, otherwise this function just
00117       returns 0.0.
00118   */
00119   double stod(std::string s, bool err_on_fail=true);
00120 
00121   /** \brief Convert a double to a string containing IEEE representation
00122       
00123       Modeled after the GSL function \c gsl_ieee_fprintf_double(), but
00124       converts to a \c string instead of a \c FILE *.
00125   */
00126   std::string double_to_ieee_string(const double *x);
00127   
00128   /** \brief Find out if the number pointed to by \c x has a minus sign
00129       
00130       This function returns true if the number pointed to by \c x has
00131       a minus sign using the GSL IEEE functions. It is useful, for
00132       example, in distinguishing "-0.0" from "+0.0".
00133   */
00134   bool has_minus_sign(double *x);
00135 
00136   /** \brief Return true if the string \c s is likely a integral or
00137       floating point number
00138       
00139       \note The test employed is not exhaustive and this function may
00140       return \c true for some numbers and may return \c false for some
00141       non-numbers.
00142    */
00143   bool is_number(std::string s);
00144 
00145 #ifndef DOXYGENP
00146 }
00147 #endif
00148 
00149 #endif
00150 
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines

Documentation generated with Doxygen. Provided under the GNU Free Documentation License (see License Information).

Get Object-oriented Scientific Computing
Lib at SourceForge.net. Fast, secure and Free Open Source software
downloads.