- Timestamp:
- Jan 1, 2007, 4:58:01 PM (16 years ago)
- Location:
- trunk/yat/utility
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/yat/utility/stl_utility.cc
r715 r723 3 3 /* 4 4 Copyright (C) 2005 Jari Häkkinen, Peter Johansson, Markus Ringnér 5 Copyright (C) 2006 Jari Häkkinen5 Copyright (C) 2006, 2007 Jari Häkkinen 6 6 7 7 This file is part of the yat library, http://lev.thep.lu.se/trac/yat … … 36 36 namespace utility { 37 37 38 bool read_to_double(std::istream& is, std::vector<double>& vec)39 {40 vec.resize(0);41 std::vector<std::string> vec_str;42 if (read_to_string(is,vec_str)){43 for (size_t i=0; i<vec_str.size(); i++) {44 if (is_double(vec_str[i])) {45 vec.push_back(atof(vec_str[i].c_str()));46 }47 else if (is_nan(vec_str[i])) {48 vec.push_back(std::numeric_limits<double>::quiet_NaN());49 }50 else {51 // Jari, this should be communicated with as an exception.52 // std::cerr << "Warning: '" << vec_str[i]53 // << "' is not a number." << std::endl;54 }55 }56 return true;57 }58 else59 return false;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 else84 return false;85 }86 87 88 bool read_to_string(std::istream& is, std::vector<std::string>& vec)89 {90 vec.resize(0);91 std::string s;92 if (getline(is, s, '\n')){93 std::istringstream line(s);94 std::string tmp_string;95 while (line >> tmp_string)96 vec.push_back(tmp_string);97 return true;98 }99 else100 return false;101 }102 103 104 38 void to_lower(std::string& s) 105 39 { -
trunk/yat/utility/stl_utility.h
r715 r723 7 7 Copyright (C) 2004 Jari Häkkinen 8 8 Copyright (C) 2005 Jari Häkkinen, Peter Johansson, Markus Ringnér 9 Copyright (C) 2006 Jari Häkkinen9 Copyright (C) 2006, 2007 Jari Häkkinen 10 10 11 11 This file is part of the yat library, http://lev.thep.lu.se/trac/yat … … 78 78 79 79 /// 80 /// Function reading from istream to vector of doubles. Function81 /// reads the line until next '\\n'. The line is splitted with82 /// respect to whitespaces and push_backed into the vector. The83 /// vector is emptied before the reading starts. Unexpected84 /// characters are currently skipped with a warning message.85 /// NaN is supported to be a double (case-insensitive)86 ///87 /// @return false if end of stream88 ///89 /// @note The functionality of this function will change in the90 /// future. The overall functionality will be the same but the91 /// outcome of unexpected events will change.92 ///93 bool read_to_double(std::istream&, std::vector<double>&);94 95 ///96 /// Function reading from istream to vector of ints. Function97 /// reads the line until next '\\n'. The line is splitted with98 /// respect to whitespaces and push_backed into the vector. The99 /// vector is emptied before the reading starts. Unexpected100 /// characters are currently skipped with a warning message.101 /// NaN is supported to be an int (case-insensitive)102 ///103 /// @return false if end of stream104 ///105 /// @note The functionality of this function will change in the106 /// future. The overall functionality will be the same but the107 /// outcome of unexpected events will change.108 ///109 bool read_to_int(std::istream&, std::vector<int>&);110 111 ///112 /// Function reading from istream to vector of strings. Function113 /// reads the line until next '\\n'. The line is splitted with114 /// respect to whitespaces and push_backed into the vector. The115 /// vector is emptied before the reading starts.116 ///117 /// @return false if end of stream118 ///119 bool read_to_string(std::istream&, std::vector<std::string>&);120 121 ///122 80 /// @brief Function converting a string to lower case 123 81 ///
Note: See TracChangeset
for help on using the changeset viewer.