00001 /* 00002 ------------------------------------------------------------------- 00003 00004 Copyright (C) 2006, 2007, 2008, 2009, 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 /** 00054 \brief Convert a boolean value to a string 00055 00056 This returns \c "1" for true and \c "0" for false. 00057 */ 00058 std::string btos(bool b); 00059 00060 /** \brief Convert a double to a string 00061 00062 If \c auto_prec is false, then the number is converted to 00063 a string in the <tt>ios::scientific</tt> mode, otherwise, 00064 neither the scientific or fixed mode flags are set and the 00065 number is converted to a string in "automatic" mode. 00066 */ 00067 std::string dtos(double x, int prec=6, bool auto_prec=false); 00068 00069 /** \brief Returns the number of characters required to display the 00070 exponent of \c x in scientific mode 00071 00072 This returns 2 or 3, depending on whether or not the 00073 absolute magnitude of the exponent is greater than 100. It 00074 uses <tt>stringstream</tt> to convert the number to a string 00075 and counts the number of characters directly. 00076 */ 00077 size_t size_of_exponent(double x); 00078 00079 /** 00080 \brief Convert a double to a string using a specified format 00081 */ 00082 std::string dtos(double x, std::ostream &format); 00083 00084 /** 00085 \brief Convert a string to an integer 00086 00087 If this function fails it will call O2SCL_ERR() and return zero. 00088 */ 00089 int stoi(std::string s); 00090 00091 /** 00092 \brief Convert a string to a boolean value 00093 00094 This returns true only if the string has at least one character 00095 and the first non-whitespace character is either \c t, \c T, or 00096 one of the numbers 1 through 9. 00097 00098 This function never fails (it just returns false for an empty 00099 string). 00100 */ 00101 bool stob(std::string s); 00102 00103 /** 00104 \brief Convert a string to a double 00105 00106 If this function fails it will call O2SCL_ERR() and return zero. 00107 */ 00108 double stod(std::string s); 00109 00110 /** 00111 \brief Convert a double to a string containing IEEE representation 00112 00113 Modeled after the GSL function \c gsl_ieee_fprintf_double(), but 00114 converts to a \c string instead of a \c FILE *. 00115 */ 00116 std::string double_to_ieee_string(const double *x); 00117 00118 /** 00119 \brief Find out if the number pointed to by \c x has a minus sign 00120 00121 This function returns true if the number pointed to by \c x has 00122 a minus sign using the GSL IEEE functions. It is useful, for 00123 example, in distinguishing "-0.0" from "+0.0". 00124 */ 00125 bool has_minus_sign(double *x); 00126 00127 #ifndef DOXYGENP 00128 } 00129 #endif 00130 00131 #endif 00132
Documentation generated with Doxygen and provided under the GNU Free Documentation License. See License Information for details.
Project hosting provided by
,
O2scl Sourceforge Project Page