51 double w32strtod(
const char *,
char **);
57 #include "dods-limits.h" 69 double 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());
91 parse_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));
122 parse_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);
151 parse_error(
const string & msg,
const int line_num,
const char *context)
153 parse_error(msg.c_str(), line_num, context);
156 void save_str(
char *dst,
const char *src,
const int line_num)
158 if (strlen(src) >= ID_MAX)
159 parse_error(
string(
"The word `") +
string(src)
160 +
string(
"' is too long (it should be no longer than ")
161 + long_to_string(ID_MAX) +
string(
")."), line_num);
163 strncpy(dst, src, ID_MAX);
164 dst[ID_MAX - 1] =
'\0';
167 void save_str(
string & dst,
const char *src,
const int)
172 bool is_keyword(
string id,
const string & keyword)
176 DBG(cerr <<
"is_keyword: " << keyword <<
" = " <<
id << endl);
177 return id == keyword;
193 long v = strtol(val, &ptr, 0);
195 if ((v == 0 && val == ptr) || *ptr !=
'\0') {
199 DBG(cerr <<
"v: " << v << endl);
206 if ((v < 0 && v < DODS_SCHAR_MIN)
207 || (v > 0 && static_cast < unsigned long >(v) > DODS_UCHAR_MAX))
216 int check_int16(
const char *val)
219 long v = strtol(val, &ptr, 0);
221 if ((v == 0 && val == ptr) || *ptr !=
'\0') {
225 if (v > DODS_SHRT_MAX || v < DODS_SHRT_MIN) {
232 int check_uint16(
const char *val)
235 unsigned long v = strtol(val, &ptr, 0);
237 if ((v == 0 && val == ptr) || *ptr !=
'\0') {
241 if (v > DODS_USHRT_MAX) {
248 int check_int32(
const char *val)
252 long v = strtol(val, &ptr, 0);
254 if ((v == 0 && val == ptr) || *ptr !=
'\0') {
261 if (errno == ERANGE) {
267 else if (v > DODS_INT_MAX || v < DODS_INT_MIN) {
275 int check_uint32(
const char *val)
280 while (c && isspace(*c)) {
283 if (c && (*c ==
'-')) {
289 unsigned long v = strtoul(val, &ptr, 0);
291 if ((v == 0 && val == ptr) || *ptr !=
'\0') {
298 if (errno == ERANGE) {
302 else if (v > DODS_UINT_MAX) {
310 int check_int32(
const char *val,
int &v)
314 long tmp = strtol(val, &ptr, 0);
316 if ((tmp == 0 && val == ptr) || *ptr !=
'\0') {
323 if (errno == ERANGE) {
329 else if (tmp > DODS_INT_MAX || tmp < DODS_INT_MIN) {
338 int check_uint32(
const char *val,
unsigned int &v)
343 while (c && isspace(*c)) {
346 if (c && (*c ==
'-')) {
352 unsigned long tmp = strtoul(val, &ptr, 0);
354 if ((tmp == 0 && val == ptr) || *ptr !=
'\0') {
361 if (errno == ERANGE) {
365 else if (tmp > DODS_UINT_MAX) {
369 v = (
unsigned int)tmp;
374 int check_int64(
const char *val)
378 long long v = strtoll(val, &ptr, 0);
380 if ((v == 0 && val == ptr) || *ptr !=
'\0') {
387 if (errno == ERANGE) {
396 else if (v <= DODS_LLONG_MAX && v >= DODS_LLONG_MIN) {
405 int check_uint64(
const char *val)
410 while (c && isspace(*c)) {
413 if (c && (*c ==
'-')) {
419 unsigned long long v = strtoull(val, &ptr, 0);
421 if ((v == 0 && val == ptr) || *ptr !=
'\0') {
425 if (errno == ERANGE) {
428 else if (v > DODS_ULLONG_MAX) {
440 int check_float32(
const char *val)
447 double v = w32strtod(val, &ptr);
449 double v = strtod(val, &ptr);
452 DBG(cerr <<
"v: " << v <<
", ptr: " << ptr
453 <<
", errno: " << errno <<
", val==ptr: " << (val == ptr) << endl);
455 if (errno == ERANGE || (v == 0.0 && val == ptr) || *ptr !=
'\0')
459 if ((v == 0.0 && (val == ptr || errno == HUGE_VAL || errno == ERANGE))
465 DBG(cerr <<
"fabs(" << val <<
") = " << fabs(v) << endl);
466 double abs_val = fabs(v);
467 if (abs_val > DODS_FLT_MAX
468 || (abs_val != 0.0 && abs_val < DODS_FLT_MIN))
474 int check_float64(
const char *val)
476 DBG(cerr <<
"val: " << val << endl);
481 double v = w32strtod(val, &ptr);
483 double v = strtod(val, &ptr);
486 DBG(cerr <<
"v: " << v <<
", ptr: " << ptr
487 <<
", errno: " << errno <<
", val==ptr: " << (val == ptr) << endl);
490 if (errno == ERANGE || (v == 0.0 && val == ptr) || *ptr !=
'\0')
493 if ((v == 0.0 && (val == ptr || errno == HUGE_VAL || errno == ERANGE))
498 DBG(cerr <<
"fabs(" << val <<
") = " << fabs(v) << endl);
499 double abs_val = fabs(v);
500 if (abs_val > DODS_DBL_MAX
501 || (abs_val != 0.0 && abs_val < DODS_DBL_MIN))
507 int check_float64(
const char *val,
double &v)
509 DBG(cerr <<
"val: " << val << endl);
514 v = w32strtod(val, &ptr);
516 v = strtod(val, &ptr);
519 DBG(cerr <<
"v: " << v <<
", ptr: " << ptr
520 <<
", errno: " << errno <<
", val==ptr: " << (val == ptr) << endl);
523 if (errno == ERANGE || (v == 0.0 && val == ptr) || *ptr !=
'\0')
526 if ((v == 0.0 && (val == ptr || errno == HUGE_VAL || errno == ERANGE))
531 DBG(cerr <<
"fabs(" << val <<
") = " << fabs(v) << endl);
532 double abs_val = fabs(v);
533 if (abs_val > DODS_DBL_MAX
534 || (abs_val != 0.0 && abs_val < DODS_DBL_MIN))
540 long long get_int64(
const char *val)
544 long long v = strtoll(val, &ptr, 0);
546 if ((v == 0 && val == ptr) || *ptr !=
'\0') {
547 throw Error(
"Expected a 64-bit integer, but found other characters.");
554 if (errno == ERANGE) {
555 throw Error(
"The 64-bit integer value is out of range.");
565 else if (v > DODS_LLONG_MAX || v < DODS_LLONG_MIN) {
566 throw Error(
"The value '" +
string(val) +
"' is out of range.");
575 unsigned long long get_uint64(
const char *val)
580 while (c && isspace(*c)) {
583 if (c && (*c ==
'-')) {
584 throw Error(
"Expected a valid array index.");
589 unsigned long long v = strtoull(val, &ptr, 0);
591 if ((v == 0 && val == ptr) || *ptr !=
'\0') {
592 throw Error(
"Expected an unsigned 64-bit integer, but found other characters.");
595 if (errno == ERANGE) {
596 throw Error(
"The 64-bit integer value is out of range.");
600 else if (v > DODS_MAX_ARRAY_INDEX) {
601 throw Error(
"The value '" +
string(val) +
"' is out of range.");
609 int get_int32(
const char *val)
613 int v = strtol(val, &ptr, 0);
615 if ((v == 0 && val == ptr) || *ptr !=
'\0') {
616 throw Error(
"Expected a 32-bit integer, but found other characters.");
622 if (errno == ERANGE) {
623 throw Error(
"The 32-bit integer value is out of range.");
628 else if (v > DODS_INT_MAX || v < DODS_INT_MIN) {
637 unsigned int get_uint32(
const char *val)
642 while (c && isspace(*c)) {
645 if (c && (*c ==
'-')) {
646 throw Error(
"Expected an unsigned 32-bit integer, but found other characters.");
651 unsigned int v = strtoul(val, &ptr, 0);
653 if ((v == 0 && val == ptr) || *ptr !=
'\0') {
654 throw Error(
"Expected an unsigned 32-bit integer, but found other characters.");
657 if (errno == ERANGE) {
658 throw Error(
"The 32-bit integer value is out of range.");
661 else if (v > DODS_UINT_MAX) {
669 double get_float64(
const char *val)
671 DBG(cerr <<
"val: " << val << endl);
676 double v = w32strtod(val, &ptr);
678 double v = strtod(val, &ptr);
681 if (errno == ERANGE || (v == 0.0 && val == ptr) || *ptr !=
'\0')
682 throw Error(
"The 64-bit floating point value is out of range.");;
684 DBG(cerr <<
"fabs(" << val <<
") = " << fabs(v) << endl);
685 double abs_val = fabs(v);
686 if (abs_val > DODS_DBL_MAX || (abs_val != 0.0 && abs_val < DODS_DBL_MIN))
687 throw Error(
"The 64-bit floating point value is out of range.");;
string prune_spaces(const string &name)
top level DAP object to house generic methods
int check_url(const char *)
Is the value a valid URL?
A class for error processing.
int check_byte(const char *val)
Is the value a valid byte?
void save_str(char *dst, const char *src, const int line_num)
Save a string to a temporary variable during the parse.