source: trunk/c++_tools/utility/utility.cc @ 609

Last change on this file since 609 was 609, checked in by Markus Ringnér, 15 years ago

Fixes #98

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 2.1 KB
Line 
1// $Id: utility.cc 609 2006-08-30 08:49:45Z markus $
2
3/*
4  Copyright (C) 2005 Jari Häkkinen, Markus Ringnér
5  Copyright (C) 2006 Jari Häkkinen
6
7  This file is part of the thep c++ tools library,
8                                http://lev.thep.lu.se/trac/c++_tools
9
10  The c++ tools library is free software; you can redistribute it
11  and/or modify it under the terms of the GNU General Public License
12  as published by the Free Software Foundation; either version 2 of
13  the License, or (at your option) any later version.
14
15  The c++ tools library is distributed in the hope that it will be
16  useful, but WITHOUT ANY WARRANTY; without even the implied warranty
17  of 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 <c++_tools/utility/utility.h>
27
28#include <c++_tools/utility/stl_utility.h>
29
30#include <sstream>
31#include <string>
32
33namespace theplu {
34namespace utility {
35
36  bool is_double(const std::string& s)
37  {
38    std::stringstream ss(s);
39    double a;
40    ss>>a;
41    if(ss.fail()) 
42      return false;
43    // Check that nothing is left on stream
44    std::string b;
45    ss >> b;
46    return (b.size() ? false : true);
47  }
48 
49  bool is_float(const std::string& s)
50  {
51    std::stringstream ss(s);
52    float a;
53    ss>>a;
54    if(ss.fail()) 
55      return false;
56    // Check that nothing is left on stream
57    std::string b;
58    ss >> b;
59    return (b.size() ? false : true);
60  }
61
62  bool is_int(const std::string& s)
63  {
64    std::stringstream ss(s);
65    int a;
66    ss >> a;
67    if(ss.fail()) 
68      return false;
69    // Check that nothing is left on stream
70    std::string b;
71    ss >> b;
72    return (b.size() ? false : true);
73  }
74
75  bool is_nan(const std::string& s)
76  {
77    std::stringstream ss(s);
78    std::string s2;
79    ss >> s2; // to trim surrounding whitespaces
80    to_lower(s2);
81    // Check that nothing is left on stream
82    std::string s3;
83    ss >> s3;
84    if(s3.size())
85      return false;
86    std::string nan("nan");
87    return (nan==s2);
88  }
89
90}} // end of namespace utility and namespace thep
Note: See TracBrowser for help on using the repository browser.