BRL-CAD
booleanize.c
Go to the documentation of this file.
1 /* B O O L E A N I Z E . C
2  * BRL-CAD
3  *
4  * Copyright (c) 2010-2014 United States Government as represented by
5  * the U.S. Army Research Laboratory.
6  *
7  * This library is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU Lesser General Public License
9  * version 2.1 as published by the Free Software Foundation.
10  *
11  * This library is distributed in the hope that it will be useful, but
12  * WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this file; see the file named COPYING for more
18  * information.
19  */
20 
21 #include "common.h"
22 
23 #include <stdlib.h>
24 #include <limits.h>
25 #include <string.h>
26 #include <errno.h>
27 
28 #include "bu/parse.h"
29 #include "bu/str.h"
30 
31 int
32 bu_str_true(const char *str)
33 {
34  long val;
35  size_t len;
36  struct bu_vls vls = BU_VLS_INIT_ZERO;
37  const char *newstr;
38  char *endptr;
39 
40  /* no string */
41  if (!str)
42  return 0;
43 
44  bu_vls_strcpy(&vls, str);
45  bu_vls_trimspace(&vls);
46  newstr = bu_vls_addr(&vls);
47 
48  /* empty string */
49  len = strlen(newstr);
50  if (len == 0) {
51  bu_vls_free(&vls);
52  return 0;
53  }
54 
55  /* starts with 'n', [nN]* looks like 'no' */
56  if (newstr[0] == 'n' || newstr[0] == 'N') {
57  bu_vls_free(&vls);
58  return 0;
59  }
60 
61  /* exactly "0" */
62  if (BU_STR_EQUAL(newstr, "0")) {
63  bu_vls_free(&vls);
64  return 0;
65  }
66 
67  /* variant of "0" (e.g., 000) */
68  val = strtol(newstr, &endptr, 10);
69  if (val == 0 && errno != EINVAL && *endptr == '\0') {
70  bu_vls_free(&vls);
71  return 0;
72  }
73 
74  /* exactly "(null)" */
75  if (BU_STR_EQUAL(newstr, "(null)")) {
76  bu_vls_free(&vls);
77  return 0;
78  }
79 
80  /* true value from here on out */
81 
82  /* starts with 'y', [yY]* looks like 'yes' */
83  if (newstr[0] == 'y' || newstr[0] == 'Y') {
84  bu_vls_free(&vls);
85  return 1;
86  }
87 
88  /* exactly "1" */
89  if (BU_STR_EQUAL(newstr, "1")) {
90  bu_vls_free(&vls);
91  return 1;
92  }
93 
94  /* variant of "1" (e.g., 001) */
95  val = strtol(newstr, &endptr, 10);
96  if (val == 1 && errno != EINVAL && *endptr == '\0') {
97  bu_vls_free(&vls);
98  return 1;
99  }
100 
101  /* done with our string */
102  val = bu_vls_addr(&vls)[0];
103  bu_vls_free(&vls);
104 
105  /* anything else */
106  if ((int)val > 1)
107  return (int)val;
108  return 2;
109 }
110 
111 
112 int
113 bu_str_false(const char *str)
114 {
115  return !bu_str_true(str);
116 }
117 
118 
119 /*
120  * Local Variables:
121  * tab-width: 8
122  * mode: C
123  * indent-tabs-mode: t
124  * c-file-style: "stroustrup"
125  * End:
126  * ex: shiftwidth=4 tabstop=8
127  */
Definition: db_flip.c:35
Header file for the BRL-CAD common definitions.
void bu_vls_free(struct bu_vls *vp)
Definition: vls.c:248
char * bu_vls_addr(const struct bu_vls *vp)
Definition: vls.c:111
void bu_vls_trimspace(struct bu_vls *vp)
Definition: vls.c:678
void bu_vls_strcpy(struct bu_vls *vp, const char *s)
Definition: vls.c:310
int bu_str_false(const char *str)
Definition: booleanize.c:113
int bu_str_true(const char *str)
Definition: booleanize.c:32
#define BU_VLS_INIT_ZERO
Definition: vls.h:84
Definition: vls.h:56
#define BU_STR_EQUAL(s1, s2)
Definition: str.h:126