Developer Documentation
NumberParsing.cc
1 /* ========================================================================= *
2  * *
3  * OpenFlipper *
4  * Copyright (c) 2001-2015, RWTH-Aachen University *
5  * Department of Computer Graphics and Multimedia *
6  * All rights reserved. *
7  * www.openflipper.org *
8  * *
9  *---------------------------------------------------------------------------*
10  * This file is part of OpenFlipper. *
11  *---------------------------------------------------------------------------*
12  * *
13  * Redistribution and use in source and binary forms, with or without *
14  * modification, are permitted provided that the following conditions *
15  * are met: *
16  * *
17  * 1. Redistributions of source code must retain the above copyright notice, *
18  * this list of conditions and the following disclaimer. *
19  * *
20  * 2. Redistributions in binary form must reproduce the above copyright *
21  * notice, this list of conditions and the following disclaimer in the *
22  * documentation and/or other materials provided with the distribution. *
23  * *
24  * 3. Neither the name of the copyright holder nor the names of its *
25  * contributors may be used to endorse or promote products derived from *
26  * this software without specific prior written permission. *
27  * *
28  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS *
29  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED *
30  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A *
31  * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER *
32  * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, *
33  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, *
34  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR *
35  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF *
36  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING *
37  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS *
38  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. *
39  * *
40  * ========================================================================= */
41 
42 #include "NumberParsing.hh"
43 #include <cmath>
44 #include <sstream>
45 
46 namespace Utils
47 {
48 
49  float getFloat(QTextStream &_source)
50  {
51  QString rawNumber;
52  _source >> rawNumber;
53  //decimal part > 0
54  float highPart = 0.0f;
55  //decimal part < 0
56  float lowPart = 0.0f;
57  //sign of the decimal
58  float sign = 1.0;
59  //counter to concatenate high and lowaprt
60  int n = 0;
61 
62  QString::Iterator it = rawNumber.begin();
63  QString::Iterator end = rawNumber.end();
64  if(*it == QLatin1Char('-'))
65  {
66  //we have a negative float
67  sign = sign * -1.0;
68  ++it;
69  }
70 
71  for (;it != end;++it)
72  {
73  //we have read a digit
74  if(it->isDigit())
75  {
76  highPart *=10;
77  highPart += it->digitValue();
78  }
79  else
80  {
81  //stop counting the highPart if its not a digit
82  ++it;
83  break;
84  }
85  }
86 
87  for (;it != end;++it)
88  {
89  //we assume to have read a digit
90  if(it->isDigit())
91  {
92  lowPart *=10;
93  lowPart += it->digitValue();
94  ++n;
95  }
96  else
97  {
98  //stop counting the highPart dont increment here!
99  // otherwise we cant detect if we successful converted
100  break;
101  }
102  }
103  // if something went wrong during decoding use the standard decoding
104  if(it != end)
105  {
106  return rawNumber.toFloat();
107  }
108  return sign * (highPart + lowPart / std::pow(10.0,n));
109  }
110 
111  float getFloat(std::istream &_source)
112  {
113  std::string rawNumber;
114  _source >> rawNumber;
115 
116  //decimal part > 0
117  float highPart = 0.0f;
118  //decimal part < 0
119  float lowPart = 0.0f;
120  //sign of the decimal
121  float sign = 1.0;
122  //counter to concatenate high and lowaprt
123  int n = 0;
124 
125  std::string::iterator it = rawNumber.begin();
126  std::string::iterator end = rawNumber.end();
127  if(*it == '-')
128  {
129  //we have a negative float
130  sign = sign * -1.0;
131  ++it;
132  }
133 
134  for (;it != end;++it)
135  {
136  //we have read a digit
137  if(isdigit(*it))
138  {
139  highPart *=10;
140  highPart += (int)(*it - '0');
141  }
142  else
143  {
144  //stop counting the highPart if its not a digit
145  ++it;
146  break;
147  }
148  }
149 
150  for (;it != end;++it)
151  {
152  //we assume to have read a digit
153  if(isdigit(*it))
154  {
155  lowPart *=10;
156  lowPart += (int)(*it - '0');
157  ++n;
158  }
159  else
160  {
161  //stop counting the highPart dont increment here!
162  // otherwise we cant detect if we successful converted
163  break;
164  }
165  }
166  // if something went wrong during decoding use the standard decoding
167  if(it != end)
168  {
169  float fallback;
170  std::stringstream converter;
171  converter<<rawNumber;
172  converter>>fallback;
173  return fallback;
174  }
175  return sign * (highPart + lowPart / std::pow(10.0,n));
176  }
177 
178  double getDouble(QTextStream &_source)
179  {
180  QString rawNumber;
181  _source >> rawNumber;
182  //decimal part > 0
183  double highPart = 0.0f;
184  //decimal part < 0
185  double lowPart = 0.0f;
186  //sign of the decimal
187  double sign = 1.0;
188  //counter to concatenate high and lowaprt
189  int n = 0;
190 
191  QString::Iterator it = rawNumber.begin();
192  QString::Iterator end = rawNumber.end();
193  if(*it == QLatin1Char('-'))
194  {
195  //we have a negative float
196  sign = sign * -1.0;
197  ++it;
198  }
199 
200  for (;it != end;++it)
201  {
202  //we have read a digit
203  if(it->isDigit())
204  {
205  highPart *=10;
206  highPart += it->digitValue();
207  }
208  else
209  {
210  //stop counting the highPart if its not a digit
211  ++it;
212  break;
213  }
214  }
215 
216  for (;it != end;++it)
217  {
218  //we assume to have read a digit
219  if(it->isDigit())
220  {
221  lowPart *=10;
222  lowPart += it->digitValue();
223  ++n;
224  }
225  else
226  {
227  //stop counting the highPart dont increment here!
228  // otherwise we cant detect if we successful converted
229  break;
230  }
231  }
232  // if something went wrong during decoding use the standard decoding
233  if(it != end)
234  {
235  return rawNumber.toDouble();
236  }
237  return sign * (highPart + lowPart / std::pow(10.0,n));
238  }
239 }