51double w32strtod(
const char *,
char **);
57#include "dods-limits.h"
69double w32strtod(
const char *val,
char **ptr)
72 string *sval =
new string(val);
73 string *snan =
new string(
"NaN");
77 if (stricmp(sval->c_str(), snan->c_str()) != 0)
78 return (strtod(val, ptr));
82 *ptr = (
char *) val + strlen(val);
83 return (std::numeric_limits < double >::quiet_NaN());
91parse_error(parser_arg * arg,
const char *msg,
const int line_num,
100 arg->set_status(FALSE);
105 oss +=
"Error parsing the text on line ";
106 append_long_to_string(line_num, 10, oss);
109 oss +=
"Parse error.";
113 oss += (string)
" at or near: " + context + (
string)
"\n" + msg
116 oss += (string)
"\n" + msg + (
string)
"\n";
118 arg->set_error(
new Error(unknown_error, oss));
122parse_error(
const char *msg,
const int line_num,
const char *context)
132 oss +=
"Error parsing the text on line ";
133 append_long_to_string(line_num, 10, oss);
136 oss +=
"Parse error.";
140 oss += (string)
" at or near: " + context + (
string)
"\n" + msg
143 oss += (string)
"\n" + msg + (
string)
"\n";
145 throw Error(malformed_expr, oss);
151parse_error(
const string & msg,
const int line_num,
const char *context)
153 parse_error(msg.c_str(), line_num, context);
159void save_str(
char *dst,
const char *src,
const int line_num)
161 if (strlen(src) >= ID_MAX)
162 parse_error(
string(
"The word `") +
string(src)
163 +
string(
"' is too long (it should be no longer than ")
164 + long_to_string(ID_MAX) +
string(
")."), line_num);
166 strncpy(dst, src, ID_MAX);
167 dst[ID_MAX - 1] =
'\0';
171void save_str(
string & dst,
const char *src,
const int)
176bool is_keyword(
string id,
const string & keyword)
180 DBG(cerr <<
"is_keyword: " << keyword <<
" = " <<
id << endl);
181 return id == keyword;
197 long v = strtol(val, &ptr, 0);
199 if ((v == 0 && val == ptr) || *ptr !=
'\0') {
203 DBG(cerr <<
"v: " << v << endl);
210 if ((v < 0 && v < DODS_SCHAR_MIN)
211 || (v > 0 &&
static_cast < unsigned long >(v) > DODS_UCHAR_MAX))
220int check_int16(
const char *val)
223 long v = strtol(val, &ptr, 0);
225 if ((v == 0 && val == ptr) || *ptr !=
'\0') {
229 if (v > DODS_SHRT_MAX || v < DODS_SHRT_MIN) {
236int check_uint16(
const char *val)
239 unsigned long v = strtol(val, &ptr, 0);
241 if ((v == 0 && val == ptr) || *ptr !=
'\0') {
245 if (v > DODS_USHRT_MAX) {
252int check_int32(
const char *val)
256 long v = strtol(val, &ptr, 0);
258 if ((v == 0 && val == ptr) || *ptr !=
'\0') {
265 if (errno == ERANGE) {
271 else if (v > DODS_INT_MAX || v < DODS_INT_MIN) {
279int check_uint32(
const char *val)
284 while (c && isspace(*c)) {
287 if (c && (*c ==
'-')) {
293 unsigned long v = strtoul(val, &ptr, 0);
295 if ((v == 0 && val == ptr) || *ptr !=
'\0') {
302 if (errno == ERANGE) {
306 else if (v > DODS_UINT_MAX) {
314int check_int32(
const char *val,
int &v)
318 long tmp = strtol(val, &ptr, 0);
320 if ((tmp == 0 && val == ptr) || *ptr !=
'\0') {
327 if (errno == ERANGE) {
333 else if (tmp > DODS_INT_MAX || tmp < DODS_INT_MIN) {
342int check_uint32(
const char *val,
unsigned int &v)
347 while (c && isspace(*c)) {
350 if (c && (*c ==
'-')) {
356 unsigned long tmp = strtoul(val, &ptr, 0);
358 if ((tmp == 0 && val == ptr) || *ptr !=
'\0') {
365 if (errno == ERANGE) {
369 else if (tmp > DODS_UINT_MAX) {
373 v = (
unsigned int)tmp;
378int check_int64(
const char *val)
382 long long v = strtoll(val, &ptr, 0);
384 if ((v == 0 && val == ptr) || *ptr !=
'\0') {
391 if (errno == ERANGE) {
400 else if (v <= DODS_LLONG_MAX && v >= DODS_LLONG_MIN) {
409int check_uint64(
const char *val)
414 while (c && isspace(*c)) {
417 if (c && (*c ==
'-')) {
423 unsigned long long v = strtoull(val, &ptr, 0);
425 if ((v == 0 && val == ptr) || *ptr !=
'\0') {
429 if (errno == ERANGE) {
432 else if (v > DODS_ULLONG_MAX) {
444int check_float32(
const char *val)
451 double v = w32strtod(val, &ptr);
453 double v = strtod(val, &ptr);
456 DBG(cerr <<
"v: " << v <<
", ptr: " << ptr
457 <<
", errno: " << errno <<
", val==ptr: " << (val == ptr) << endl);
459 if (errno == ERANGE || (v == 0.0 && val == ptr) || *ptr !=
'\0')
463 if ((v == 0.0 && (val == ptr || errno == HUGE_VAL || errno == ERANGE))
469 DBG(cerr <<
"fabs(" << val <<
") = " << fabs(v) << endl);
470 double abs_val = fabs(v);
471 if (abs_val > DODS_FLT_MAX
472 || (abs_val != 0.0 && abs_val < DODS_FLT_MIN))
478int check_float64(
const char *val)
480 DBG(cerr <<
"val: " << val << endl);
485 double v = w32strtod(val, &ptr);
487 double v = strtod(val, &ptr);
490 DBG(cerr <<
"v: " << v <<
", ptr: " << ptr
491 <<
", errno: " << errno <<
", val==ptr: " << (val == ptr) << endl);
494 if (errno == ERANGE || (v == 0.0 && val == ptr) || *ptr !=
'\0')
497 if ((v == 0.0 && (val == ptr || errno == HUGE_VAL || errno == ERANGE))
502 DBG(cerr <<
"fabs(" << val <<
") = " << fabs(v) << endl);
503 double abs_val = fabs(v);
504 if (abs_val > DODS_DBL_MAX
505 || (abs_val != 0.0 && abs_val < DODS_DBL_MIN))
511int check_float64(
const char *val,
double &v)
513 DBG(cerr <<
"val: " << val << endl);
518 v = w32strtod(val, &ptr);
520 v = strtod(val, &ptr);
523 DBG(cerr <<
"v: " << v <<
", ptr: " << ptr
524 <<
", errno: " << errno <<
", val==ptr: " << (val == ptr) << endl);
527 if (errno == ERANGE || (v == 0.0 && val == ptr) || *ptr !=
'\0')
530 if ((v == 0.0 && (val == ptr || errno == HUGE_VAL || errno == ERANGE))
535 DBG(cerr <<
"fabs(" << val <<
") = " << fabs(v) << endl);
536 double abs_val = fabs(v);
537 if (abs_val > DODS_DBL_MAX
538 || (abs_val != 0.0 && abs_val < DODS_DBL_MIN))
544long long get_int64(
const char *val)
548 long long v = strtoll(val, &ptr, 0);
550 if ((v == 0 && val == ptr) || *ptr !=
'\0') {
551 throw Error(
"Expected a 64-bit integer, but found other characters.");
558 if (errno == ERANGE) {
559 throw Error(
"The 64-bit integer value is out of range.");
569 else if (v > DODS_LLONG_MAX || v < DODS_LLONG_MIN) {
570 throw Error(
"The value '" +
string(val) +
"' is out of range.");
579unsigned long long get_uint64(
const char *val)
584 while (c && isspace(*c)) {
587 if (c && (*c ==
'-')) {
588 throw Error(
"Expected a valid array index.");
593 unsigned long long v = strtoull(val, &ptr, 0);
595 if ((v == 0 && val == ptr) || *ptr !=
'\0') {
596 throw Error(
"Expected an unsigned 64-bit integer, but found other characters.");
599 if (errno == ERANGE) {
600 throw Error(
"The 64-bit integer value is out of range.");
604 else if (v > DODS_MAX_ARRAY_INDEX) {
605 throw Error(
"The value '" +
string(val) +
"' is out of range.");
613int get_int32(
const char *val)
617 int v = strtol(val, &ptr, 0);
619 if ((v == 0 && val == ptr) || *ptr !=
'\0') {
620 throw Error(
"Expected a 32-bit integer, but found other characters.");
626 if (errno == ERANGE) {
627 throw Error(
"The 32-bit integer value is out of range.");
632 else if (v > DODS_INT_MAX || v < DODS_INT_MIN) {
641unsigned int get_uint32(
const char *val)
646 while (c && isspace(*c)) {
649 if (c && (*c ==
'-')) {
650 throw Error(
"Expected an unsigned 32-bit integer, but found other characters.");
655 unsigned int v = strtoul(val, &ptr, 0);
657 if ((v == 0 && val == ptr) || *ptr !=
'\0') {
658 throw Error(
"Expected an unsigned 32-bit integer, but found other characters.");
661 if (errno == ERANGE) {
662 throw Error(
"The 32-bit integer value is out of range.");
665 else if (v > DODS_UINT_MAX) {
673double get_float64(
const char *val)
675 DBG(cerr <<
"val: " << val << endl);
680 double v = w32strtod(val, &ptr);
682 double v = strtod(val, &ptr);
685 if (errno == ERANGE || (v == 0.0 && val == ptr) || *ptr !=
'\0')
686 throw Error(
"The 64-bit floating point value is out of range.");;
688 DBG(cerr <<
"fabs(" << val <<
") = " << fabs(v) << endl);
689 double abs_val = fabs(v);
690 if (abs_val > DODS_DBL_MAX || (abs_val != 0.0 && abs_val < DODS_DBL_MIN))
691 throw Error(
"The 64-bit floating point value is out of range.");;
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.