51double w32strtod(
const char *,
char **);
56#include "dods-limits.h"
69double w32strtod(
const char *val,
char **ptr) {
71 string *sval =
new string(val);
72 string *snan =
new string(
"NaN");
76 if (stricmp(sval->c_str(), snan->c_str()) != 0)
77 return (strtod(val, ptr));
81 *ptr = (
char *)val + strlen(val);
82 return (std::numeric_limits<double>::quiet_NaN());
89void parse_error(
parser_arg *arg,
const char *msg,
const int line_num,
const char *context) {
96 arg->set_status(FALSE);
101 oss +=
"Error parsing the text on line ";
102 append_long_to_string(line_num, 10, oss);
104 oss +=
"Parse error.";
108 oss += (string)
" at or near: " + context + (
string)
"\n" + msg + (string)
"\n";
110 oss += (string)
"\n" + msg + (
string)
"\n";
112 arg->set_error(
new Error(unknown_error, oss));
115void parse_error(
const char *msg,
const int line_num,
const char *context) {
124 oss +=
"Error parsing the text on line ";
125 append_long_to_string(line_num, 10, oss);
127 oss +=
"Parse error.";
131 oss += (string)
" at or near: " + context + (
string)
"\n" + msg + (string)
"\n";
133 oss += (string)
"\n" + msg + (
string)
"\n";
135 throw Error(malformed_expr, oss);
140void parse_error(
const string &msg,
const int line_num,
const char *context) {
141 parse_error(msg.c_str(), line_num, context);
147void save_str(
char *dst,
const char *src,
const int line_num)
149 if (strlen(src) >= ID_MAX)
150 parse_error(
string(
"The word `") +
string(src)
151 +
string(
"' is too long (it should be no longer than ")
152 + long_to_string(ID_MAX) +
string(
")."), line_num);
154 strncpy(dst, src, ID_MAX);
155 dst[ID_MAX - 1] =
'\0';
159void save_str(
string &dst,
const char *src,
const int) { dst = src; }
161bool is_keyword(
string id,
const string &keyword) {
164 DBG(cerr <<
"is_keyword: " << keyword <<
" = " <<
id << endl);
165 return id == keyword;
180 long v = strtol(val, &ptr, 0);
182 if ((v == 0 && val == ptr) || *ptr !=
'\0') {
186 DBG(cerr <<
"v: " << v << endl);
193 if ((v < 0 && v < DODS_SCHAR_MIN) || (v > 0 &&
static_cast<unsigned long>(v) > DODS_UCHAR_MAX))
202int check_int16(
const char *val) {
204 long v = strtol(val, &ptr, 0);
206 if ((v == 0 && val == ptr) || *ptr !=
'\0') {
210 if (v > DODS_SHRT_MAX || v < DODS_SHRT_MIN) {
217int check_uint16(
const char *val) {
219 unsigned long v = strtol(val, &ptr, 0);
221 if ((v == 0 && val == ptr) || *ptr !=
'\0') {
225 if (v > DODS_USHRT_MAX) {
232int check_int32(
const char *val) {
235 long v = strtol(val, &ptr, 0);
237 if ((v == 0 && val == ptr) || *ptr !=
'\0') {
244 if (errno == ERANGE) {
250 else if (v > DODS_INT_MAX || v < DODS_INT_MIN) {
257int check_uint32(
const char *val) {
261 while (c && isspace(*c)) {
264 if (c && (*c ==
'-')) {
270 unsigned long v = strtoul(val, &ptr, 0);
272 if ((v == 0 && val == ptr) || *ptr !=
'\0') {
279 if (errno == ERANGE) {
283 else if (v > DODS_UINT_MAX) {
290int check_int32(
const char *val,
int &v) {
293 long tmp = strtol(val, &ptr, 0);
295 if ((tmp == 0 && val == ptr) || *ptr !=
'\0') {
302 if (errno == ERANGE) {
308 else if (tmp > DODS_INT_MAX || tmp < DODS_INT_MIN) {
316int check_uint32(
const char *val,
unsigned int &v) {
320 while (c && isspace(*c)) {
323 if (c && (*c ==
'-')) {
329 unsigned long tmp = strtoul(val, &ptr, 0);
331 if ((tmp == 0 && val == ptr) || *ptr !=
'\0') {
338 if (errno == ERANGE) {
342 else if (tmp > DODS_UINT_MAX) {
345 v = (
unsigned int)tmp;
350int check_int64(
const char *val) {
353 long long v = strtoll(val, &ptr, 0);
355 if ((v == 0 && val == ptr) || *ptr !=
'\0') {
362 if (errno == ERANGE) {
371 else if (v <= DODS_LLONG_MAX && v >= DODS_LLONG_MIN) {
380int check_uint64(
const char *val) {
384 while (c && isspace(*c)) {
387 if (c && (*c ==
'-')) {
393 unsigned long long v = strtoull(val, &ptr, 0);
395 if ((v == 0 && val == ptr) || *ptr !=
'\0') {
399 if (errno == ERANGE) {
401 }
else if (v > DODS_ULLONG_MAX) {
412int check_float32(
const char *val) {
418 double v = w32strtod(val, &ptr);
420 double v = strtod(val, &ptr);
423 DBG(cerr <<
"v: " << v <<
", ptr: " << ptr <<
", errno: " << errno <<
", val==ptr: " << (val == ptr) << endl);
425 if (errno == ERANGE || (v == 0.0 && val == ptr) || *ptr !=
'\0')
429 if ((v == 0.0 && (val == ptr || errno == HUGE_VAL || errno == ERANGE))
435 DBG(cerr <<
"fabs(" << val <<
") = " << fabs(v) << endl);
436 double abs_val = fabs(v);
437 if (abs_val > DODS_FLT_MAX || (abs_val != 0.0 && abs_val < DODS_FLT_MIN))
443int check_float64(
const char *val) {
444 DBG(cerr <<
"val: " << val << endl);
449 double v = w32strtod(val, &ptr);
451 double v = strtod(val, &ptr);
454 DBG(cerr <<
"v: " << v <<
", ptr: " << ptr <<
", errno: " << errno <<
", val==ptr: " << (val == ptr) << endl);
456 if (errno == ERANGE || (v == 0.0 && val == ptr) || *ptr !=
'\0')
459 if ((v == 0.0 && (val == ptr || errno == HUGE_VAL || errno == ERANGE))
464 DBG(cerr <<
"fabs(" << val <<
") = " << fabs(v) << endl);
465 double abs_val = fabs(v);
466 if (abs_val > DODS_DBL_MAX || (abs_val != 0.0 && abs_val < DODS_DBL_MIN))
472int check_float64(
const char *val,
double &v) {
473 DBG(cerr <<
"val: " << val << endl);
478 v = w32strtod(val, &ptr);
480 v = strtod(val, &ptr);
483 DBG(cerr <<
"v: " << v <<
", ptr: " << ptr <<
", errno: " << errno <<
", val==ptr: " << (val == ptr) << endl);
485 if (errno == ERANGE || (v == 0.0 && val == ptr) || *ptr !=
'\0')
488 if ((v == 0.0 && (val == ptr || errno == HUGE_VAL || errno == ERANGE))
493 DBG(cerr <<
"fabs(" << val <<
") = " << fabs(v) << endl);
494 double abs_val = fabs(v);
495 if (abs_val > DODS_DBL_MAX || (abs_val != 0.0 && abs_val < DODS_DBL_MIN))
501long long get_int64(
const char *val) {
504 long long v = strtoll(val, &ptr, 0);
506 if ((v == 0 && val == ptr) || *ptr !=
'\0') {
507 throw Error(
"Expected a 64-bit integer, but found other characters.");
514 if (errno == ERANGE) {
515 throw Error(
"The 64-bit integer value is out of range.");
525 else if (v > DODS_LLONG_MAX || v < DODS_LLONG_MIN) {
526 throw Error(
"The value '" +
string(val) +
"' is out of range.");
535unsigned long long get_uint64(
const char *val) {
539 while (c && isspace(*c)) {
542 if (c && (*c ==
'-')) {
543 throw Error(
"Expected a valid array index.");
548 unsigned long long v = strtoull(val, &ptr, 0);
550 if ((v == 0 && val == ptr) || *ptr !=
'\0') {
551 throw Error(
"Expected an unsigned 64-bit integer, but found other characters.");
554 if (errno == ERANGE) {
555 throw Error(
"The 64-bit integer value is out of range.");
559 else if (v > DODS_MAX_ARRAY_INDEX) {
560 throw Error(
"The value '" +
string(val) +
"' is out of range.");
568int get_int32(
const char *val) {
571 int v = strtol(val, &ptr, 0);
573 if ((v == 0 && val == ptr) || *ptr !=
'\0') {
574 throw Error(
"Expected a 32-bit integer, but found other characters.");
580 if (errno == ERANGE) {
581 throw Error(
"The 32-bit integer value is out of range.");
586 else if (v > DODS_INT_MAX || v < DODS_INT_MIN) {
595unsigned int get_uint32(
const char *val) {
599 while (c && isspace(*c)) {
602 if (c && (*c ==
'-')) {
603 throw Error(
"Expected an unsigned 32-bit integer, but found other characters.");
608 unsigned int v = strtoul(val, &ptr, 0);
610 if ((v == 0 && val == ptr) || *ptr !=
'\0') {
611 throw Error(
"Expected an unsigned 32-bit integer, but found other characters.");
614 if (errno == ERANGE) {
615 throw Error(
"The 32-bit integer value is out of range.");
618 else if (v > DODS_UINT_MAX) {
625double get_float64(
const char *val) {
626 DBG(cerr <<
"val: " << val << endl);
631 double v = w32strtod(val, &ptr);
633 double v = strtod(val, &ptr);
636 if (errno == ERANGE || (v == 0.0 && val == ptr) || *ptr !=
'\0')
637 throw Error(
"The 64-bit floating point value is out of range.");
640 DBG(cerr <<
"fabs(" << val <<
") = " << fabs(v) << endl);
641 double abs_val = fabs(v);
642 if (abs_val > DODS_DBL_MAX || (abs_val != 0.0 && abs_val < DODS_DBL_MIN))
643 throw Error(
"The 64-bit floating point value is out of range.");
A class for error processing.
int check_url(const char *)
Is the value a valid URL?
int check_byte(const char *val)
Is the value a valid byte?
top level DAP object to house generic methods
string prune_spaces(const string &name)
void save_str(string &dst, const char *src, const int)
Save a string to a temporary variable during the parse.
Pass parameters by reference to a parser.