1 | // $Id: stl_utility.cc 327 2005-05-30 23:00:20Z jari $ |
---|
2 | |
---|
3 | #include <c++_tools/utility/stl_utility.h> |
---|
4 | #include <c++_tools/utility/utility.h> |
---|
5 | |
---|
6 | #include <iostream> |
---|
7 | #include <sstream> |
---|
8 | #include <string> |
---|
9 | #include <vector> |
---|
10 | |
---|
11 | namespace theplu { |
---|
12 | namespace utility { |
---|
13 | |
---|
14 | |
---|
15 | bool read_to_double(std::istream& is, std::vector<double>& vec) |
---|
16 | { |
---|
17 | vec.resize(0); |
---|
18 | std::vector<std::string> vec_str; |
---|
19 | if (read_to_string(is,vec_str)){ |
---|
20 | for (size_t i=0; i<vec_str.size(); i++) { |
---|
21 | if (!is_float(vec_str[i])){ |
---|
22 | std::cerr << "Warning: " << vec_str[i] |
---|
23 | << " is not a number." << std::endl; |
---|
24 | } |
---|
25 | vec.push_back(atof(vec_str[i].c_str())); |
---|
26 | } |
---|
27 | return true; |
---|
28 | } |
---|
29 | else |
---|
30 | return false; |
---|
31 | } |
---|
32 | |
---|
33 | |
---|
34 | |
---|
35 | bool read_to_int(std::istream& is, std::vector<int>& vec) |
---|
36 | { |
---|
37 | vec.resize(0); |
---|
38 | std::vector<std::string> vec_str; |
---|
39 | if (read_to_string(is,vec_str)){ |
---|
40 | for (size_t i=0; i<vec_str.size(); i++) { |
---|
41 | if (!is_int(vec_str[i])){ |
---|
42 | std::cerr << "Warning: " << vec_str[i] |
---|
43 | << " is not an integer." << std::endl; |
---|
44 | } |
---|
45 | vec.push_back(atoi(vec_str[i].c_str())); |
---|
46 | } |
---|
47 | return true; |
---|
48 | } |
---|
49 | else |
---|
50 | return false; |
---|
51 | } |
---|
52 | |
---|
53 | |
---|
54 | |
---|
55 | bool read_to_string(std::istream& is, std::vector<std::string>& vec) |
---|
56 | { |
---|
57 | vec.resize(0); |
---|
58 | std::string s; |
---|
59 | if (getline(is, s, '\n')){ |
---|
60 | std::istringstream line(s); |
---|
61 | std::string tmp_string; |
---|
62 | while (line >> tmp_string) |
---|
63 | vec.push_back(tmp_string); |
---|
64 | return true; |
---|
65 | } |
---|
66 | else |
---|
67 | return false; |
---|
68 | } |
---|
69 | |
---|
70 | |
---|
71 | }} // end of namespace utility and namespace thep |
---|
72 | |
---|