All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
string_conv.h
Go to the documentation of this file.
1 /*
2  -------------------------------------------------------------------
3 
4  Copyright (C) 2006-2014, Andrew W. Steiner
5 
6  This file is part of O2scl.
7 
8  O2scl is free software; you can redistribute it and/or modify
9  it under the terms of the GNU General Public License as published by
10  the Free Software Foundation; either version 3 of the License, or
11  (at your option) any later version.
12 
13  O2scl is distributed in the hope that it will be useful,
14  but WITHOUT ANY WARRANTY; without even the implied warranty of
15  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16  GNU General Public License for more details.
17 
18  You should have received a copy of the GNU General Public License
19  along with O2scl. If not, see <http://www.gnu.org/licenses/>.
20 
21  -------------------------------------------------------------------
22 */
23 #ifndef O2SCL_STRING_CONV_H
24 #define O2SCL_STRING_CONV_H
25 /** \file string_conv.h
26  \brief Various string conversion functions
27 */
28 
29 #include <iostream>
30 #include <cmath>
31 #include <string>
32 #include <vector>
33 #include <fstream>
34 #include <sstream>
35 
36 #ifndef DOXYGEN_NO_O2NS
37 namespace o2scl {
38 #endif
39 
40  /// \name Functions in string_conv.h
41  //@{
42  /** \brief Convert a pointer to a string
43 
44  This uses an \c ostringstream to convert a pointer to a string
45  and is architecture-dependent.
46  */
47  std::string ptos(void *p);
48 
49  /** \brief Convert an integer to a string
50  */
51  std::string itos(int x);
52 
53  /** \brief Convert a size_t to a string
54  */
55  std::string szttos(size_t x);
56 
57  /** \brief Convert an integer to a string (exception-free version)
58  */
59  std::string itos_nothrow(int x) throw();
60 
61  /** \brief Convert a boolean value to a string
62 
63  This returns \c "1" for true and \c "0" for false.
64  */
65  std::string btos(bool b);
66 
67  /** \brief Convert a double to a string
68 
69  If \c auto_prec is false, then the number is converted to
70  a string in the <tt>ios::scientific</tt> mode, otherwise,
71  neither the scientific or fixed mode flags are set and the
72  number is converted to a string in "automatic" mode.
73  */
74  std::string dtos(double x, int prec=6, bool auto_prec=false);
75 
76  /** \brief Returns the number of characters required to display the
77  exponent of \c x in scientific mode
78 
79  This returns 2 or 3, depending on whether or not the absolute
80  magnitude of the exponent is greater than or equal to 100. It
81  uses <tt>stringstream</tt> to convert the number to a string and
82  counts the number of characters directly.
83  */
84  size_t size_of_exponent(double x);
85 
86  /** \brief Convert a double to a string using a specified format
87  */
88  std::string dtos(double x, std::ostream &format);
89 
90  /** \brief Convert a string to an integer
91 
92  If \c err_on_fail is true and the conversion fails, this
93  function calls the error handler, otherwise this function just
94  returns zero.
95 
96  If \o2 is compiled with C++11 support, this function is
97  just a wrapper for <tt>std::stoi</tt>.
98 
99  \warning Because of the presence of <tt>std::stoi()</tt> in
100  C++11, you may have to explicitly provide the
101  namespace, i.e. <tt>o2scl::stoi()</tt> in your code.
102  */
103  int stoi(std::string s, bool err_on_fail=true);
104 
105  /** \brief Convert a string to a size_t
106 
107  If \c err_on_fail is true and the conversion fails, this
108  function calls the error handler, otherwise this function just
109  returns zero.
110  */
111  size_t stoszt(std::string s, bool err_on_fail=true);
112 
113  /** \brief Convert a string to a boolean value
114 
115  This returns true if only if the string has at least one
116  character and the first non-whitespace character is either \c t,
117  \c T, or one of the numbers 1 through 9.
118 
119  If \c err_on_fail is true and the conversion fails, this
120  function calls the error handler, otherwise this function just
121  returns false.
122  */
123  bool stob(std::string s, bool err_on_fail=true);
124 
125  /** \brief Convert a string to a double
126 
127  If \c err_on_fail is true and the conversion fails, this
128  function calls the error handler, otherwise this function just
129  returns 0.0.
130 
131  If \o2 is compiled with C++11 support, this function is
132  just a wrapper for <tt>std::stod</tt>.
133 
134  \warning Because of the presence of <tt>std::stod()</tt> in
135  C++11, you may have to explicitly provide the
136  namespace, i.e. <tt>o2scl::stod()</tt> in your code.
137  */
138  double stod(std::string s, bool err_on_fail=true);
139 
140  /** \brief Find out if the number pointed to by \c x has a minus sign
141 
142  This function returns true if the number pointed to by \c x has
143  a minus sign using the GSL IEEE functions. It is useful, for
144  example, in distinguishing "-0.0" from "+0.0".
145  */
146  bool has_minus_sign(double *x);
147 
148  /** \brief Return true if the string \c s is likely a integral or
149  floating point number
150 
151  \note The test employed is not exhaustive and this function may
152  return \c true for some numbers and may return \c false for some
153  non-numbers.
154  */
155  bool is_number(std::string s);
156 
157  /** \brief Convert a formula to a double
158 
159  This uses \c FunctionParser to convert strings like "-1.0e-3",
160  "1.0/3.0" and "exp(cos(-1.0e-2))" to floating point numbers.
161  */
162  double function_to_double(std::string s, bool err_on_fail=true);
163 
164  /** \brief Split a string into words using whitespace for delimiters and
165  (partially) respecting quotes
166 
167  \todo
168  - More documentation
169  - Add user-specified delimiters?
170  - Add version which ignores quotes
171  - Use this function in acol
172  */
173  void split_string(std::string str, std::vector<std::string> &sv);
174  //@}
175 
176 #ifndef DOXYGEN_NO_O2NS
177 }
178 #endif
179 
180 #endif
181 
std::string btos(bool b)
Convert a boolean value to a string.
size_t size_of_exponent(double x)
Returns the number of characters required to display the exponent of x in scientific mode...
int stoi(std::string s, bool err_on_fail=true)
Convert a string to an integer.
bool stob(std::string s, bool err_on_fail=true)
Convert a string to a boolean value.
bool has_minus_sign(double *x)
Find out if the number pointed to by x has a minus sign.
std::string dtos(double x, int prec=6, bool auto_prec=false)
Convert a double to a string.
bool is_number(std::string s)
Return true if the string s is likely a integral or floating point number.
void split_string(std::string str, std::vector< std::string > &sv)
Split a string into words using whitespace for delimiters and (partially) respecting quotes...
std::string itos_nothrow(int x)
Convert an integer to a string (exception-free version)
size_t stoszt(std::string s, bool err_on_fail=true)
Convert a string to a size_t.
std::string ptos(void *p)
Convert a pointer to a string.
double stod(std::string s, bool err_on_fail=true)
Convert a string to a double.
std::string itos(int x)
Convert an integer to a string.
double function_to_double(std::string s, bool err_on_fail=true)
Convert a formula to a double.
std::string szttos(size_t x)
Convert a size_t to a string.

Documentation generated with Doxygen. Provided under the GNU Free Documentation License (see License Information).
Hosted at Get Object-oriented Scientific Computing
Lib at SourceForge.net. Fast, secure and Free Open Source software
downloads..