~scottydelicious666/brewtarget/brewtarget

« back to all changes in this revision

Viewing changes to stringparsing.cpp

  • Committer: Philip Greggory Lee
  • Date: 2009-08-23 16:53:43 UTC
  • Revision ID: git-v1:f8d1a25135bd92f06c46c562293800e4faa42c61
Made a src/ and ui/ directory and moved everything.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/*
2
 
 * stringparsing.cpp is part of Brewtarget, and is Copyright Philip G. Lee
3
 
 * (rocketman768@gmail.com), 2009.
4
 
 *
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.
9
 
 
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.
14
 
 
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/>.
17
 
 */
18
 
 
19
 
#include <string>
20
 
#include <iostream>
21
 
#include <stdlib.h>
22
 
#include <stdio.h>
23
 
#include "stringparsing.h"
24
 
 
25
 
int parseInt( const std::string &str )
26
 
{
27
 
   return atoi(str.c_str());
28
 
}
29
 
 
30
 
long parseLong( const std::string &str )
31
 
{
32
 
   char *endptr;
33
 
   long ret = 0.0;
34
 
   const char *begptr = str.c_str();
35
 
   
36
 
   ret = strtol( begptr, &endptr, 10 );
37
 
   
38
 
   if( endptr == begptr && str.size() > 0 )
39
 
   {
40
 
      std::cerr << "parseLong() could not convert " + str << std::endl;
41
 
      throw ParseException("parseLong() could not convert " + str);
42
 
   }
43
 
   
44
 
   return ret;
45
 
}
46
 
 
47
 
double parseDouble( const std::string &str )
48
 
{
49
 
   char *endptr;
50
 
   double ret = 0.0;
51
 
   const char *begptr = str.c_str();
52
 
   
53
 
   ret = strtod( begptr, &endptr );
54
 
   
55
 
   if( endptr == begptr && str.size() > 0 )
56
 
   {
57
 
      std::cerr << "parseDouble() could not convert " + str << std::endl;
58
 
      throw ParseException("parseDouble() could not convert " + str);
59
 
   }
60
 
   
61
 
   return ret;
62
 
}
63
 
 
64
 
bool parseBool( const std::string &str )
65
 
{
66
 
   if( str == "TRUE" )
67
 
      return true;
68
 
   else if( str == "FALSE" )
69
 
      return false;
70
 
   else
71
 
   {
72
 
      std::cerr << "parseBool() could not convert " + str << std::endl;
73
 
      throw ParseException("parseBool() could not convert " + str);
74
 
   }
75
 
}
76
 
 
77
 
std::string boolToString( bool b )
78
 
{
79
 
   if( b )
80
 
      return "TRUE";
81
 
   else
82
 
      return "FALSE";
83
 
}
84
 
 
85
 
std::string doubleToString( double num )
86
 
{
87
 
   static char s[32];
88
 
   
89
 
   sprintf( s, "%.3lf", num );
90
 
   
91
 
   return std::string(s);
92
 
}
93
 
 
94
 
std::string doubleToStringPrec( double num, unsigned int prec )
95
 
{
96
 
   static char s[32];
97
 
   static char format[32];
98
 
 
99
 
   sprintf( format, "%%.%dlf", prec );
100
 
   sprintf( s, format, num );
101
 
 
102
 
   return std::string(s);
103
 
}
104
 
 
105
 
std::string intToString( int num )
106
 
{
107
 
   static char s[32];
108
 
   
109
 
   sprintf( s, "%d", num );
110
 
   
111
 
   return std::string(s);
112
 
}
113
 
 
114
 
std::string& trim( std::string &str )
115
 
{
116
 
   static const std::string white(" \f\n\r\t\v");
117
 
   std::string::size_type lastNonSpace, firstNonSpace;
118
 
 
119
 
   firstNonSpace = str.find_first_not_of(white);
120
 
   if( firstNonSpace == std::string::npos )
121
 
      return str;
122
 
   str.erase( 0, firstNonSpace );
123
 
 
124
 
   lastNonSpace = str.find_last_not_of(white);
125
 
   if( lastNonSpace == std::string::npos )
126
 
      return str;
127
 
   str.erase( lastNonSpace+1, str.size()-lastNonSpace-1 );
128
 
 
129
 
   return str;
130
 
}
131
 
 
132
 
bool isWhiteSpace( char c )
133
 
{
134
 
   return (c == ' ' || c == '\f' || c == '\n' || c == '\r' || c == '\t'
135
 
           || c == '\v' );
136
 
}
137
 
 
138
 
void iterateUntilDelimiter( std::string::const_iterator &iter )
139
 
{
140
 
   while( !(isWhiteSpace(*iter) || *iter == '=' || *iter == '>') )
141
 
          ++iter;
142
 
}
143
 
 
144
 
void iterateUntilNotDelimiter( std::string::const_iterator &iter )
145
 
{
146
 
   while( isWhiteSpace(*iter) || *iter == '=' || *iter == '>' )
147
 
          ++iter;
148
 
}
149
 
 
150
 
void iterateUntilCharFound( std::string::const_iterator &iter, char c )
151
 
{
152
 
   while( *iter != c )
153
 
      ++iter;
154
 
}