Fawkes API  Fawkes Development Version
type_checker.cpp
1 
2 /***************************************************************************
3  * type_checker.cpp - Interface generator type checker
4  *
5  * Generated: Wed Oct 11 15:39:10 2006
6  * Copyright 2006 Tim Niemueller [www.niemueller.de]
7  *
8  ****************************************************************************/
9 
10 /* This program is free software; you can redistribute it and/or modify
11  * it under the terms of the GNU General Public License as published by
12  * the Free Software Foundation; either version 2 of the License, or
13  * (at your option) any later version.
14  *
15  * This program 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
18  * GNU Library General Public License for more details.
19  *
20  * Read the full text in the LICENSE.GPL file in the doc directory.
21  */
22 
23 #include <interfaces/generator/type_checker.h>
24 #include <interfaces/generator/exceptions.h>
25 #include <core/exception.h>
26 
27 #include <cstdlib>
28 #include <climits>
29 #include <cmath>
30 #include <cerrno>
31 
32 // request setting of INT8_MAX etc. constants
33 #ifndef __STDC_LIMIT_MACROS
34 #define __STDC_LIMIT_MACROS
35 #endif
36 #include <stdint.h>
37 
38 /** @class InterfaceDataTypeChecker <interfaces/generator/type_checker.h>
39  * Type checker for interface types.
40  * This classed is used by the generator to decide if a supplied type is
41  * correct and in the case of constants if the supplied value matches the
42  * field type.
43  *
44  * Valid types are:
45  * - int
46  * - long int
47  * - unsigned int
48  * - unsigned long int
49  * - bool
50  * - float
51  * - double
52  * - byte (unsigned 8-bit number)
53  * - string
54  */
55 
56 
57 /** Check type validity.
58  * @param type type string to check
59  * @param enum_constants an optional vector of enumeration constants that are used for
60  * type validation.
61  * @return true, if type is valid, false otherwise
62  */
63 bool
64 InterfaceDataTypeChecker::validType(const std::string &type, std::vector<InterfaceEnumConstant> *enum_constants)
65 {
66  if ( (type == "int8") ||
67  (type == "int16") ||
68  (type == "int32") ||
69  (type == "int64") ||
70  (type == "uint8") ||
71  (type == "uint16") ||
72  (type == "uint32") ||
73  (type == "uint64") ||
74  (type == "bool") ||
75  (type == "char") ||
76  (type == "float") ||
77  (type == "byte") ||
78  (type == "string") ||
79  (type == "double") ) {
80  return true;
81  } else if ( enum_constants != NULL ) {
82  std::vector<InterfaceEnumConstant>::iterator i;
83  for (i = enum_constants->begin(); i != enum_constants->end(); ++i) {
84  if ( type == (*i).get_name() ) {
85  return true;
86  }
87  }
88  return false;
89  } else {
90  return false;
91  }
92 }
93 
94 
95 /** Check value validity for given type.
96  * @param type type if value
97  * @param value value to check
98  * @return true, if value is valid for type, false otherwise
99  */
100 bool
101 InterfaceDataTypeChecker::validValue(const std::string &type, const std::string &value)
102 {
103  if (type.find("int") != std::string::npos) {
104  errno = 0;
105  char *endptr;
106  long long int rv = strtoll(value.c_str(), &endptr, 10);
107  if ( ((rv == LLONG_MIN) || (rv == LLONG_MAX)) && (errno == ERANGE) ) {
108  throw fawkes::Exception("Could not convert value string '%s' to "
109  "long long int", value.c_str());
110  }
111  if ( (endptr != NULL) && (endptr[0] == '\0')) {
112  if (type == "uint8") {
113  return (rv >= 0) && (rv <= UINT8_MAX);
114  } else if (type == "uint16") {
115  return (rv >= 0) && (rv <= UINT16_MAX);
116  } else if (type == "uint32") {
117  return (rv >= 0) && (rv <= UINT32_MAX);
118  } else if (type == "uint64") {
119  return (rv >= 0) && ((uint64_t)rv <= UINT64_MAX);
120  } else if (type == "int8") {
121  return (rv >= INT8_MIN) && (rv <= INT8_MAX);
122  } else if (type == "int16") {
123  return (rv >= INT16_MIN) && (rv <= INT16_MAX);
124  } else if (type == "int32") {
125  return (rv >= INT32_MIN) && (rv <= INT32_MAX);
126  } else if (type == "int64") {
127  return (rv >= INT64_MIN) && (rv <= INT64_MAX);
128  } else {
129  return false;
130  }
131  } else {
132  return false;
133  }
134  } else if ( type == "bool" ) {
135  return ( (value == "true") ||
136  (value == "false") ||
137  (value == "yes") ||
138  (value == "no") ||
139  (value == "0") ||
140  (value == "1") );
141  } else if ( (type == "float") ||
142  (type == "double") ) {
143  char *endptr;
144  float rv = strtod(value.c_str(), &endptr);
145  if ((rv == HUGE_VAL) || (rv == -HUGE_VAL)) {
146  throw fawkes::Exception("Could not convert string '%s' to float", value.c_str());
147  }
148  return ((endptr != NULL) && (endptr[0] == '\0'));
149  } else if ( type == "string" ) {
150  return true;
151  } else {
152  return false;
153  }
154 }
static bool validValue(const std::string &type, const std::string &value)
Check value validity for given type.
Base class for exceptions in Fawkes.
Definition: exception.h:36
static bool validType(const std::string &type, std::vector< InterfaceEnumConstant > *enum_constants=0)
Check type validity.