source: trunk/yat/utility/stl_utility.cc @ 715

Last change on this file since 715 was 715, checked in by Jari Häkkinen, 16 years ago

Addresses #170.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 2.8 KB
Line 
1// $Id: stl_utility.cc 715 2006-12-22 08:42:39Z 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 "stl_utility.h"
26#include "utility.h"
27
28#include <iostream>
29#include <sstream>
30#include <string>
31#include <vector>
32#include <limits>
33
34namespace theplu {
35namespace yat {
36namespace utility {
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    else 
59      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    else 
84      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    else 
100      return false;
101  }
102
103
104  void to_lower(std::string& s)
105  {
106    transform(s.begin(),s.end(), s.begin(), tolower);
107  }
108
109
110  void to_upper(std::string& s)
111  {
112    transform(s.begin(),s.end(), s.begin(), toupper);
113  }
114
115}}} // end of namespace utility, yat and thep
Note: See TracBrowser for help on using the repository browser.