1 | // $Id: utility.cc 1392 2008-07-28 19:35:30Z peter $ |
---|
2 | |
---|
3 | /* |
---|
4 | Copyright (C) 2005, 2006 Jari Häkkinen, Markus Ringnér |
---|
5 | Copyright (C) 2007 Jari Häkkinen, Peter Johansson |
---|
6 | |
---|
7 | This file is part of the yat library, http://dev.thep.lu.se/yat |
---|
8 | |
---|
9 | The yat library is free software; you can redistribute it and/or |
---|
10 | modify it under the terms of the GNU General Public License as |
---|
11 | published by the Free Software Foundation; either version 2 of the |
---|
12 | License, or (at your option) any later version. |
---|
13 | |
---|
14 | The yat library is distributed in the hope that it will be useful, |
---|
15 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
16 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
---|
17 | General Public License for more details. |
---|
18 | |
---|
19 | You should have received a copy of the GNU General Public License |
---|
20 | along with this program; if not, write to the Free Software |
---|
21 | Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA |
---|
22 | 02111-1307, USA. |
---|
23 | */ |
---|
24 | |
---|
25 | #include "utility.h" |
---|
26 | |
---|
27 | #include "stl_utility.h" |
---|
28 | |
---|
29 | #include <sstream> |
---|
30 | #include <string> |
---|
31 | |
---|
32 | namespace theplu { |
---|
33 | namespace yat { |
---|
34 | namespace utility { |
---|
35 | |
---|
36 | bool is_double(const std::string& s) |
---|
37 | { |
---|
38 | if (is_nan(s) || is_equal(s, "inf") || is_equal(s, "-inf")) |
---|
39 | return true; |
---|
40 | std::stringstream ss(s); |
---|
41 | double a; |
---|
42 | ss>>a; |
---|
43 | if(ss.fail()) |
---|
44 | return false; |
---|
45 | // Check that nothing is left on stream |
---|
46 | std::string b; |
---|
47 | ss >> b; |
---|
48 | return (b.size() ? false : true); |
---|
49 | } |
---|
50 | |
---|
51 | |
---|
52 | bool is_equal(std::string s, std::string other) |
---|
53 | { |
---|
54 | std::stringstream ss(s); |
---|
55 | std::string s2; |
---|
56 | ss >> s2; // to trim surrounding whitespaces |
---|
57 | to_lower(s2); |
---|
58 | // Check that nothing is left on stream |
---|
59 | std::string s3; |
---|
60 | ss >> s3; |
---|
61 | if(s3.size()) |
---|
62 | return false; |
---|
63 | return (other==s2); |
---|
64 | } |
---|
65 | |
---|
66 | |
---|
67 | bool is_float(const std::string& s) |
---|
68 | { |
---|
69 | if (is_nan(s) || is_equal(s, "inf") || is_equal(s, "-inf")) |
---|
70 | return true; |
---|
71 | std::stringstream ss(s); |
---|
72 | float a; |
---|
73 | ss>>a; |
---|
74 | if(ss.fail()) |
---|
75 | return false; |
---|
76 | // Check that nothing is left on stream |
---|
77 | std::string b; |
---|
78 | ss >> b; |
---|
79 | return (b.size() ? false : true); |
---|
80 | } |
---|
81 | |
---|
82 | |
---|
83 | bool is_int(const std::string& s) |
---|
84 | { |
---|
85 | if (is_nan(s) || is_equal(s, "inf") || is_equal(s, "-inf")) |
---|
86 | return true; |
---|
87 | std::stringstream ss(s); |
---|
88 | int a; |
---|
89 | ss >> a; |
---|
90 | if(ss.fail()) |
---|
91 | return false; |
---|
92 | // Check that nothing is left on stream |
---|
93 | std::string b; |
---|
94 | ss >> b; |
---|
95 | return (b.size() ? false : true); |
---|
96 | } |
---|
97 | |
---|
98 | bool is_nan(const std::string& s) |
---|
99 | { |
---|
100 | return is_equal(s, "nan"); |
---|
101 | } |
---|
102 | |
---|
103 | |
---|
104 | |
---|
105 | }}} // end of namespace utility, yat and thep |
---|