1 | // $Id: utility.cc 917 2007-09-30 01:26:36Z peter $ |
---|
2 | |
---|
3 | /* |
---|
4 | Copyright (C) 2005, 2006 Jari Häkkinen, Markus Ringnér |
---|
5 | Copyright (C) 2007 Peter Johansson |
---|
6 | |
---|
7 | This file is part of the yat library, http://trac.thep.lu.se/trac/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 "matrix.h" |
---|
28 | #include "stl_utility.h" |
---|
29 | #include "vector.h" |
---|
30 | #include "yat/random/random.h" |
---|
31 | |
---|
32 | #include <sstream> |
---|
33 | #include <string> |
---|
34 | |
---|
35 | namespace theplu { |
---|
36 | namespace yat { |
---|
37 | namespace utility { |
---|
38 | |
---|
39 | bool is_double(const std::string& s) |
---|
40 | { |
---|
41 | if (is_nan(s) || is_equal(s, "inf") || is_equal(s, "-inf")) |
---|
42 | return true; |
---|
43 | std::stringstream ss(s); |
---|
44 | double a; |
---|
45 | ss>>a; |
---|
46 | if(ss.fail()) |
---|
47 | return false; |
---|
48 | // Check that nothing is left on stream |
---|
49 | std::string b; |
---|
50 | ss >> b; |
---|
51 | return (b.size() ? false : true); |
---|
52 | } |
---|
53 | |
---|
54 | |
---|
55 | bool is_equal(std::string s, std::string other) |
---|
56 | { |
---|
57 | std::stringstream ss(s); |
---|
58 | std::string s2; |
---|
59 | ss >> s2; // to trim surrounding whitespaces |
---|
60 | to_lower(s2); |
---|
61 | // Check that nothing is left on stream |
---|
62 | std::string s3; |
---|
63 | ss >> s3; |
---|
64 | if(s3.size()) |
---|
65 | return false; |
---|
66 | return (other==s2); |
---|
67 | } |
---|
68 | |
---|
69 | |
---|
70 | bool is_float(const std::string& s) |
---|
71 | { |
---|
72 | if (is_nan(s) || is_equal(s, "inf") || is_equal(s, "-inf")) |
---|
73 | return true; |
---|
74 | std::stringstream ss(s); |
---|
75 | float a; |
---|
76 | ss>>a; |
---|
77 | if(ss.fail()) |
---|
78 | return false; |
---|
79 | // Check that nothing is left on stream |
---|
80 | std::string b; |
---|
81 | ss >> b; |
---|
82 | return (b.size() ? false : true); |
---|
83 | } |
---|
84 | |
---|
85 | |
---|
86 | bool is_int(const std::string& s) |
---|
87 | { |
---|
88 | if (is_nan(s) || is_equal(s, "inf") || is_equal(s, "-inf")) |
---|
89 | return true; |
---|
90 | std::stringstream ss(s); |
---|
91 | int a; |
---|
92 | ss >> a; |
---|
93 | if(ss.fail()) |
---|
94 | return false; |
---|
95 | // Check that nothing is left on stream |
---|
96 | std::string b; |
---|
97 | ss >> b; |
---|
98 | return (b.size() ? false : true); |
---|
99 | } |
---|
100 | |
---|
101 | bool is_nan(const std::string& s) |
---|
102 | { |
---|
103 | return is_equal(s, "nan"); |
---|
104 | } |
---|
105 | |
---|
106 | |
---|
107 | |
---|
108 | }}} // end of namespace utility, yat and thep |
---|