string_conv.h
Go to the documentation of this file.
1 /*
2  -------------------------------------------------------------------
3 
4  Copyright (C) 2006-2017, 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 // For screenify()
37 #include <o2scl/misc.h>
38 
39 #ifndef DOXYGEN_NO_O2NS
40 namespace o2scl {
41 #endif
42 
43  /// \name Functions in string_conv.h
44  //@{
45  /** \brief Convert a pointer to a string
46 
47  This uses an \c ostringstream to convert a pointer to a string
48  and is architecture-dependent.
49  */
50  std::string ptos(void *p);
51 
52  /** \brief Convert an integer to a string
53  */
54  std::string itos(int x);
55 
56  /** \brief Convert a size_t to a string
57  */
58  std::string szttos(size_t x);
59 
60  /** \brief Convert a boolean value to a string
61 
62  This returns \c "1" for true and \c "0" for false.
63  */
64  std::string btos(bool b);
65 
66  /** \brief Convert a double to a string
67 
68  If \c auto_prec is false, then the number is converted to
69  a string in the <tt>ios::scientific</tt> mode, otherwise,
70  neither the scientific or fixed mode flags are set and the
71  number is converted to a string in "automatic" mode.
72  */
73  std::string dtos(double x, int prec=6, bool auto_prec=false);
74 
75  /** \brief Returns the number of characters required to display the
76  exponent of \c x in scientific mode
77 
78  This returns 2 or 3, depending on whether or not the absolute
79  magnitude of the exponent is greater than or equal to 100. It
80  uses <tt>stringstream</tt> to convert the number to a string and
81  counts the number of characters directly.
82  */
83  size_t size_of_exponent(double x);
84 
85  /** \brief Convert a double to a string using a specified format
86  */
87  std::string dtos(double x, std::ostream &format);
88 
89  /** \brief Convert a string to an integer
90 
91  If \c err_on_fail is true and the conversion fails, this
92  function calls the error handler, otherwise this function just
93  returns zero.
94 
95  If \o2 is compiled with C++11 support, this function is
96  just a wrapper for <tt>std::stoi</tt>.
97 
98  \warning Because of the presence of <tt>std::stoi()</tt> in
99  C++11, you may have to explicitly provide the
100  namespace, i.e. <tt>o2scl::stoi()</tt> in your code.
101  */
102  int stoi(std::string s, bool err_on_fail=true);
103 
104  /** \brief Convert a string to a size_t
105 
106  If \c err_on_fail is true and the conversion fails, this
107  function calls the error handler, otherwise this function just
108  returns zero.
109  */
110  size_t stoszt(std::string s, bool err_on_fail=true);
111 
112  /** \brief Convert a string to a boolean value
113 
114  This returns true if only if the string has at least one
115  character and the first non-whitespace character is either \c t,
116  \c T, or one of the numbers 1 through 9.
117 
118  If \c err_on_fail is true and the conversion fails, this
119  function calls the error handler, otherwise this function just
120  returns false.
121  */
122  bool stob(std::string s, bool err_on_fail=true);
123 
124  /** \brief Convert a string to a double
125 
126  If \c err_on_fail is true and the conversion fails, this
127  function calls the error handler, otherwise this function just
128  returns 0.0.
129 
130  If \o2 is compiled with C++11 support, this function is
131  just a wrapper for <tt>std::stod</tt>.
132 
133  \warning Because of the presence of <tt>std::stod()</tt> in
134  C++11, you may have to explicitly provide the
135  namespace, i.e. <tt>o2scl::stod()</tt> in your code.
136  */
137  double stod(std::string s, bool err_on_fail=true);
138 
139  /** \brief Find out if the number pointed to by \c x has a minus sign
140 
141  This function returns true if the number pointed to by \c x has
142  a minus sign using the GSL IEEE functions. It is useful, for
143  example, in distinguishing "-0.0" from "+0.0".
144  */
145  bool has_minus_sign(double *x);
146 
147  /** \brief Return true if the string \c s is likely a integral or
148  floating point number
149 
150  \note The test employed is not exhaustive and this function may
151  return \c true for some numbers and may return \c false for some
152  non-numbers.
153  */
154  bool is_number(std::string s);
155 
156  /** \brief Convert a formula to a double
157 
158  This uses \ref o2scl::calculator to convert strings like
159  "-1.0e-3", "1.0/3.0" and "exp(cos(-1.0e-2))" to floating point
160  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
165  and (partially) respecting quotes
166 
167  This function separates a string into words, and handles words
168  that begin with a <tt>"</tt> by adding more words until finding
169  one which ends with another <tt>"</tt>. Strings like
170  \code
171  this is a test
172  \endcode
173  get parsed as "this", "is", "a", "test" and strings like
174  \code
175  "this is" a test
176  \endcode
177  get parsed as "this is", "a", "test".
178 
179  This is used to reformat command descriptions and help text for
180  the screen width in cli::comm_option_help(), to process lines
181  read from a file in cli::comm_option_run(), and to process input
182  in cli::run_interactive().
183 
184  \future Add user-specified delimiters?
185  */
186  void split_string(std::string str, std::vector<std::string> &sv);
187 
188  /** \brief Rewrap a string into a single column, avoiding
189  strings less than a particular number of characters
190  */
191  void rewrap(std::string str, std::vector<std::string> &sv,
192  size_t ncol=79);
193  //@}
194 
195 #ifndef DOXYGEN_NO_O2NS
196 }
197 #endif
198 
199 #endif
200 
The main O<span style=&#39;position: relative; top: 0.3em; font-size: 0.8em&#39;>2</span>scl O$_2$scl names...
Definition: anneal.h:42
void rewrap(std::string str, std::vector< std::string > &sv, size_t ncol=79)
Rewrap a string into a single column, avoiding strings less than a particular number of characters...
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...
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).