plugins/base1/se.lu.thep.wenni/trunk/lib/c++_tools/utility/stl_utility.cc

Code
Comments
Other
Rev Date Author Line
69 11 Feb 06 jari 1 // $Id$
69 11 Feb 06 jari 2
95 05 Apr 06 jari 3 /*
95 05 Apr 06 jari 4   Copyright (C) 2005 Jari Häkkinen, Peter Johansson, Markus Ringnér
95 05 Apr 06 jari 5   Copyright (C) 2006 Jari Häkkinen
95 05 Apr 06 jari 6
95 05 Apr 06 jari 7   This file is part of the thep c++ tools library,
95 05 Apr 06 jari 8                                 http://lev.thep.lu.se/trac/c++_tools
95 05 Apr 06 jari 9
95 05 Apr 06 jari 10   The c++ tools library is free software; you can redistribute it
95 05 Apr 06 jari 11   and/or modify it under the terms of the GNU General Public License
824 26 Nov 08 jari 12   as published by the Free Software Foundation; either version 3 of
95 05 Apr 06 jari 13   the License, or (at your option) any later version.
95 05 Apr 06 jari 14
95 05 Apr 06 jari 15   The c++ tools library is distributed in the hope that it will be
95 05 Apr 06 jari 16   useful, but WITHOUT ANY WARRANTY; without even the implied warranty
95 05 Apr 06 jari 17   of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
95 05 Apr 06 jari 18   General Public License for more details.
95 05 Apr 06 jari 19
95 05 Apr 06 jari 20   You should have received a copy of the GNU General Public License
824 26 Nov 08 jari 21   along with WeNNI. If not, see <http://www.gnu.org/licenses/>.
95 05 Apr 06 jari 22 */
95 05 Apr 06 jari 23
69 11 Feb 06 jari 24 #include <c++_tools/utility/stl_utility.h>
69 11 Feb 06 jari 25 #include <c++_tools/utility/utility.h>
69 11 Feb 06 jari 26
69 11 Feb 06 jari 27 #include <iostream>
69 11 Feb 06 jari 28 #include <sstream>
69 11 Feb 06 jari 29 #include <string>
69 11 Feb 06 jari 30 #include <vector>
110 13 Jun 06 jari 31 #include <limits>
69 11 Feb 06 jari 32
69 11 Feb 06 jari 33 namespace theplu {
69 11 Feb 06 jari 34 namespace utility {
69 11 Feb 06 jari 35
69 11 Feb 06 jari 36   bool read_to_double(std::istream& is, std::vector<double>& vec)
69 11 Feb 06 jari 37   {
69 11 Feb 06 jari 38     vec.resize(0);
69 11 Feb 06 jari 39     std::vector<std::string> vec_str;
69 11 Feb 06 jari 40     if (read_to_string(is,vec_str)){
69 11 Feb 06 jari 41       for (size_t i=0; i<vec_str.size(); i++) {
110 13 Jun 06 jari 42         if (is_double(vec_str[i])) {
110 13 Jun 06 jari 43           vec.push_back(atof(vec_str[i].c_str()));
69 11 Feb 06 jari 44         }
110 13 Jun 06 jari 45         else if (is_nan(vec_str[i])) {
110 13 Jun 06 jari 46           vec.push_back(std::numeric_limits<double>::quiet_NaN());
110 13 Jun 06 jari 47         }
110 13 Jun 06 jari 48         else {
110 13 Jun 06 jari 49           // Jari, this should be communicated with as an exception.
110 13 Jun 06 jari 50           //           std::cerr << "Warning: '" << vec_str[i] 
110 13 Jun 06 jari 51           //                     << "' is not a number." << std::endl;
110 13 Jun 06 jari 52         }
110 13 Jun 06 jari 53       }
69 11 Feb 06 jari 54       return true;
69 11 Feb 06 jari 55     }
69 11 Feb 06 jari 56     else 
69 11 Feb 06 jari 57       return false;
69 11 Feb 06 jari 58   }
69 11 Feb 06 jari 59
69 11 Feb 06 jari 60
69 11 Feb 06 jari 61
69 11 Feb 06 jari 62   bool read_to_int(std::istream& is, std::vector<int>& vec)
69 11 Feb 06 jari 63   {
69 11 Feb 06 jari 64     vec.resize(0);
69 11 Feb 06 jari 65     std::vector<std::string> vec_str;
69 11 Feb 06 jari 66     if (read_to_string(is,vec_str)){
69 11 Feb 06 jari 67       for (size_t i=0; i<vec_str.size(); i++) {
110 13 Jun 06 jari 68         if (is_int(vec_str[i])) {
110 13 Jun 06 jari 69           vec.push_back(atoi(vec_str[i].c_str()));
69 11 Feb 06 jari 70         }
110 13 Jun 06 jari 71         else if (is_nan(vec_str[i])) {
110 13 Jun 06 jari 72           vec.push_back(std::numeric_limits<int>::quiet_NaN());
110 13 Jun 06 jari 73         }
110 13 Jun 06 jari 74         else {
110 13 Jun 06 jari 75           // Jari, this should be communicated with as an exception.
110 13 Jun 06 jari 76           //           std::cerr << "Warning: '" << vec_str[i] 
110 13 Jun 06 jari 77           //                     << "' is not an integer." << std::endl;
110 13 Jun 06 jari 78         }
69 11 Feb 06 jari 79       }
69 11 Feb 06 jari 80       return true;
69 11 Feb 06 jari 81     }
69 11 Feb 06 jari 82     else 
69 11 Feb 06 jari 83       return false;
69 11 Feb 06 jari 84   }
69 11 Feb 06 jari 85
69 11 Feb 06 jari 86
69 11 Feb 06 jari 87
69 11 Feb 06 jari 88   bool read_to_string(std::istream& is, std::vector<std::string>& vec)
69 11 Feb 06 jari 89   {
69 11 Feb 06 jari 90     vec.resize(0);
69 11 Feb 06 jari 91     std::string s;
69 11 Feb 06 jari 92     if (getline(is, s, '\n')){
69 11 Feb 06 jari 93       std::istringstream line(s);
69 11 Feb 06 jari 94       std::string tmp_string;
69 11 Feb 06 jari 95       while (line >> tmp_string) 
69 11 Feb 06 jari 96         vec.push_back(tmp_string);
69 11 Feb 06 jari 97       return true;
69 11 Feb 06 jari 98     }
69 11 Feb 06 jari 99     else 
69 11 Feb 06 jari 100       return false;
69 11 Feb 06 jari 101   }
69 11 Feb 06 jari 102
69 11 Feb 06 jari 103 }} // end of namespace utility and namespace thep