1 | // $Id: stl_utility.cc 675 2006-10-10 12:08:45Z jari $ |
---|
2 | |
---|
3 | /* |
---|
4 | Copyright (C) 2005 Jari Häkkinen, Peter Johansson, Markus Ringnér |
---|
5 | Copyright (C) 2006 Jari Häkkinen |
---|
6 | |
---|
7 | This file is part of the yat library, http://lev.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 "yat/utility/stl_utility.h" |
---|
26 | #include "yat/utility/utility.h" |
---|
27 | |
---|
28 | #include <iostream> |
---|
29 | #include <sstream> |
---|
30 | #include <string> |
---|
31 | #include <vector> |
---|
32 | #include <limits> |
---|
33 | |
---|
34 | namespace theplu { |
---|
35 | namespace utility { |
---|
36 | |
---|
37 | bool read_to_double(std::istream& is, std::vector<double>& vec) |
---|
38 | { |
---|
39 | vec.resize(0); |
---|
40 | std::vector<std::string> vec_str; |
---|
41 | if (read_to_string(is,vec_str)){ |
---|
42 | for (size_t i=0; i<vec_str.size(); i++) { |
---|
43 | if (is_double(vec_str[i])) { |
---|
44 | vec.push_back(atof(vec_str[i].c_str())); |
---|
45 | } |
---|
46 | else if (is_nan(vec_str[i])) { |
---|
47 | vec.push_back(std::numeric_limits<double>::quiet_NaN()); |
---|
48 | } |
---|
49 | else { |
---|
50 | // Jari, this should be communicated with as an exception. |
---|
51 | // std::cerr << "Warning: '" << vec_str[i] |
---|
52 | // << "' is not a number." << std::endl; |
---|
53 | } |
---|
54 | } |
---|
55 | return true; |
---|
56 | } |
---|
57 | else |
---|
58 | return false; |
---|
59 | } |
---|
60 | |
---|
61 | |
---|
62 | |
---|
63 | bool read_to_int(std::istream& is, std::vector<int>& vec) |
---|
64 | { |
---|
65 | vec.resize(0); |
---|
66 | std::vector<std::string> vec_str; |
---|
67 | if (read_to_string(is,vec_str)){ |
---|
68 | for (size_t i=0; i<vec_str.size(); i++) { |
---|
69 | if (is_int(vec_str[i])) { |
---|
70 | vec.push_back(atoi(vec_str[i].c_str())); |
---|
71 | } |
---|
72 | else if (is_nan(vec_str[i])) { |
---|
73 | vec.push_back(std::numeric_limits<int>::quiet_NaN()); |
---|
74 | } |
---|
75 | else { |
---|
76 | // Jari, this should be communicated with as an exception. |
---|
77 | // std::cerr << "Warning: '" << vec_str[i] |
---|
78 | // << "' is not an integer." << std::endl; |
---|
79 | } |
---|
80 | } |
---|
81 | return true; |
---|
82 | } |
---|
83 | else |
---|
84 | return false; |
---|
85 | } |
---|
86 | |
---|
87 | |
---|
88 | |
---|
89 | bool read_to_string(std::istream& is, std::vector<std::string>& vec) |
---|
90 | { |
---|
91 | vec.resize(0); |
---|
92 | std::string s; |
---|
93 | if (getline(is, s, '\n')){ |
---|
94 | std::istringstream line(s); |
---|
95 | std::string tmp_string; |
---|
96 | while (line >> tmp_string) |
---|
97 | vec.push_back(tmp_string); |
---|
98 | return true; |
---|
99 | } |
---|
100 | else |
---|
101 | return false; |
---|
102 | } |
---|
103 | |
---|
104 | }} // end of namespace utility and namespace thep |
---|