source: branches/0.4-stable/yat/utility/utility.cc @ 1743

Last change on this file since 1743 was 1743, checked in by Peter, 12 years ago

updating copyright statements

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 2.3 KB
Line 
1// $Id: utility.cc 1743 2009-01-23 14:20:30Z peter $
2
3/*
4  Copyright (C) 2005, 2006 Jari Häkkinen, Markus Ringnér
5  Copyright (C) 2007 Jari Häkkinen, Peter Johansson
6  Copyright (C) 2008 Peter Johansson
7
8  This file is part of the yat library, http://dev.thep.lu.se/yat
9
10  The yat library is free software; you can redistribute it and/or
11  modify it under the terms of the GNU General Public License as
12  published by the Free Software Foundation; either version 2 of the
13  License, or (at your option) any later version.
14
15  The yat library is distributed in the hope that it will be useful,
16  but WITHOUT ANY WARRANTY; without even the implied warranty of
17  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18  General Public License for more details.
19
20  You should have received a copy of the GNU General Public License
21  along with this program; if not, write to the Free Software
22  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
23  02111-1307, USA.
24*/
25
26#include "utility.h"
27
28#include "stl_utility.h"
29
30#include <sstream>
31#include <string>
32
33namespace theplu {
34namespace yat {
35namespace utility {
36
37  bool is_double(const std::string& s)
38  {
39    if (is_nan(s) || is_equal(s, "inf") || is_equal(s, "-inf"))
40      return true;
41    std::stringstream ss(s);
42    double a;
43    ss>>a;
44    if(ss.fail()) 
45      return false;
46    // Check that nothing is left on stream
47    std::string b;
48    ss >> b;
49    return (b.size() ? false : true);
50  }
51 
52
53  bool is_equal(std::string s, std::string other)
54  {
55    std::stringstream ss(s);
56    std::string s2;
57    ss >> s2; // to trim surrounding whitespaces
58    to_lower(s2);
59    // Check that nothing is left on stream
60    std::string s3;
61    ss >> s3;
62    if(s3.size())
63      return false;
64    return (other==s2);
65  }
66
67
68  bool is_float(const std::string& s)
69  {
70    if (is_nan(s) || is_equal(s, "inf") || is_equal(s, "-inf"))
71      return true;
72    std::stringstream ss(s);
73    float a;
74    ss>>a;
75    if(ss.fail()) 
76      return false;
77    // Check that nothing is left on stream
78    std::string b;
79    ss >> b;
80    return (b.size() ? false : true);
81  }
82
83
84  bool is_int(const std::string& s)
85  {
86    if (is_nan(s) || is_equal(s, "inf") || is_equal(s, "-inf"))
87      return true;
88    std::stringstream ss(s);
89    int a;
90    ss >> a;
91    if(ss.fail()) 
92      return false;
93    // Check that nothing is left on stream
94    std::string b;
95    ss >> b;
96    return (b.size() ? false : true);
97  }
98
99  bool is_nan(const std::string& s)
100  {
101    return is_equal(s, "nan");
102  }
103
104
105
106}}} // end of namespace utility, yat and thep
Note: See TracBrowser for help on using the repository browser.