2
* stringparsing.cpp is part of Brewtarget, and is Copyright Philip G. Lee
3
* (rocketman768@gmail.com), 2009.
5
* Brewtarget is free software: you can redistribute it and/or modify
6
* it under the terms of the GNU General Public License as published by
7
* the Free Software Foundation, either version 3 of the License, or
8
* (at your option) any later version.
10
* Brewtarget is distributed in the hope that it will be useful,
11
* but WITHOUT ANY WARRANTY; without even the implied warranty of
12
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13
* GNU General Public License for more details.
15
* You should have received a copy of the GNU General Public License
16
* along with this program. If not, see <http://www.gnu.org/licenses/>.
23
#include "stringparsing.h"
25
int parseInt( const std::string &str )
27
return atoi(str.c_str());
30
long parseLong( const std::string &str )
34
const char *begptr = str.c_str();
36
ret = strtol( begptr, &endptr, 10 );
38
if( endptr == begptr && str.size() > 0 )
40
std::cerr << "parseLong() could not convert " + str << std::endl;
41
throw ParseException("parseLong() could not convert " + str);
47
double parseDouble( const std::string &str )
51
const char *begptr = str.c_str();
53
ret = strtod( begptr, &endptr );
55
if( endptr == begptr && str.size() > 0 )
57
std::cerr << "parseDouble() could not convert " + str << std::endl;
58
throw ParseException("parseDouble() could not convert " + str);
64
bool parseBool( const std::string &str )
68
else if( str == "FALSE" )
72
std::cerr << "parseBool() could not convert " + str << std::endl;
73
throw ParseException("parseBool() could not convert " + str);
77
std::string boolToString( bool b )
85
std::string doubleToString( double num )
89
sprintf( s, "%.3lf", num );
91
return std::string(s);
94
std::string doubleToStringPrec( double num, unsigned int prec )
97
static char format[32];
99
sprintf( format, "%%.%dlf", prec );
100
sprintf( s, format, num );
102
return std::string(s);
105
std::string intToString( int num )
109
sprintf( s, "%d", num );
111
return std::string(s);
114
std::string& trim( std::string &str )
116
static const std::string white(" \f\n\r\t\v");
117
std::string::size_type lastNonSpace, firstNonSpace;
119
firstNonSpace = str.find_first_not_of(white);
120
if( firstNonSpace == std::string::npos )
122
str.erase( 0, firstNonSpace );
124
lastNonSpace = str.find_last_not_of(white);
125
if( lastNonSpace == std::string::npos )
127
str.erase( lastNonSpace+1, str.size()-lastNonSpace-1 );
132
bool isWhiteSpace( char c )
134
return (c == ' ' || c == '\f' || c == '\n' || c == '\r' || c == '\t'
138
void iterateUntilDelimiter( std::string::const_iterator &iter )
140
while( !(isWhiteSpace(*iter) || *iter == '=' || *iter == '>') )
144
void iterateUntilNotDelimiter( std::string::const_iterator &iter )
146
while( isWhiteSpace(*iter) || *iter == '=' || *iter == '>' )
150
void iterateUntilCharFound( std::string::const_iterator &iter, char c )