CPN
Computational Process Networks
Functions
ParseBool.cc File Reference
#include "common_priv.h"
#include <cpn/utils/ParseBool.h>
#include <ctype.h>
+ Include dependency graph for ParseBool.cc:

Go to the source code of this file.

Functions

bool ParseBool (const std::string &str)
 Parses string for a boolean value. More...
 

Detailed Description

Author
John Bridgman

Definition in file ParseBool.cc.

Function Documentation

bool ParseBool ( const std::string &  str)

Parses string for a boolean value.

Considers no, false, 0 and any abreviation like " f" to be false and everything else to be true (including the empty string). Ignores whitespace. Note "false blah" is true!

Returns
true or false
Parameters
strthe string to parse

Definition at line 28 of file ParseBool.cc.

Referenced by CPN::NodeBasePrivate::CoerceParam< bool >::Coerce().

28  {
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 }

+ Here is the caller graph for this function: