CPN
Computational Process Networks
ParseBool.cc
Go to the documentation of this file.
1 //=============================================================================
2 // Computational Process Networks class library
3 // Copyright (C) 1997-2006 Gregory E. Allen and The University of Texas
4 //
5 // This library is free software; you can redistribute it and/or modify it
6 // under the terms of the GNU Library General Public License as published
7 // by the Free Software Foundation; either version 2 of the License, or
8 // (at your option) any later version.
9 //
10 // This library is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 // Library General Public License for more details.
14 //
15 // The GNU Public License is available in the file LICENSE, or you
16 // can write to the Free Software Foundation, Inc., 59 Temple Place -
17 // Suite 330, Boston, MA 02111-1307, USA, or you can find it on the
18 // World Wide Web at http://www.fsf.org.
19 //=============================================================================
23 #include "common_priv.h"
24 #include <cpn/utils/ParseBool.h>
25 #include <ctype.h>
26 
27 
28 bool ParseBool(const std::string &str) {
29  static const char NO_STR[] = "no";
30  static const char FALSE_STR[] = "false";
31  bool result = true;
32  std::string::const_iterator cur = str.begin();
33  const std::string::const_iterator end = str.end();
34  while (cur != end && isspace(*cur)) { ++cur; }
35  if (cur == end) return result;
36  const char *cmpstr = 0;
37  if (tolower(*cur) == NO_STR[0]) {
38  cmpstr = &NO_STR[1];
39  } else if (tolower(*cur) == FALSE_STR[0]) {
40  cmpstr = &FALSE_STR[1];
41  } else if (*cur == '0') {
42  ++cur;
43  while (cur != end && *cur == '0') { ++cur; }
44  while (cur != end && isspace(*cur)) { ++cur; }
45  if (cur == end) { result = false; }
46  return result;
47  }
48  if (cmpstr != 0) {
49  ++cur;
50  int i = 0;
51  while (cur != end && cmpstr[i] != '\0' && tolower(*cur) == cmpstr[i]) {
52  ++cur; ++i;
53  }
54  while (cur != end && isspace(*cur)) { ++cur; }
55  if (cur == end) { result = false; }
56  }
57  return result;
58 }
bool ParseBool(const std::string &str)
Parses string for a boolean value.
Definition: ParseBool.cc:28