44 #ifndef TM_IN_SYS_TIME 58 #include <sys/types.h> 95 #ifdef COMPUTE_ENDIAN_AT_RUNTIME 97 dods_int16 i = 0x0100;
98 char *c =
reinterpret_cast<char*
>(&i);
122 if (arg->
type() != dods_str_c)
throw Error(malformed_expr,
"The function requires a string argument.");
126 "The CE Evaluator built an argument list where some constants held no values.");
128 return static_cast<Str*
>(arg)->value();
131 template<
class T>
static void set_array_using_double_helper(
Array *a,
double *src,
int src_len)
137 vector<T> values(src_len);
138 for (
int i = 0; i < src_len; ++i)
139 values[i] = (T) src[i];
142 a->set_value(values, src_len);
173 || dest->
var()->
type() == dods_url_c)
174 throw InternalErr(__FILE__, __LINE__,
"The function requires a numeric-type array argument.");
180 if (dest->
length() != src_len)
182 "The source and destination array sizes don't match (" + long_to_string(src_len) +
" versus " 183 + long_to_string(dest->
length()) +
").");
191 set_array_using_double_helper<dods_byte>(dest, src, src_len);
194 set_array_using_double_helper<dods_uint16>(dest, src, src_len);
197 set_array_using_double_helper<dods_int16>(dest, src, src_len);
200 set_array_using_double_helper<dods_uint32>(dest, src, src_len);
203 set_array_using_double_helper<dods_int32>(dest, src, src_len);
206 set_array_using_double_helper<dods_float32>(dest, src, src_len);
209 set_array_using_double_helper<dods_float64>(dest, src, src_len);
214 set_array_using_double_helper<dods_byte>(dest, src, src_len);
217 set_array_using_double_helper<dods_int8>(dest, src, src_len);
220 set_array_using_double_helper<dods_uint64>(dest, src, src_len);
223 set_array_using_double_helper<dods_int64>(dest, src, src_len);
227 "The argument list built by the CE parser contained an unsupported numeric type.");
234 template<
class T>
static double *extract_double_array_helper(
Array * a)
243 double *dest =
new double[length];
244 for (
int i = 0; i < length; ++i)
245 dest[i] = (
double) b[i];
266 || a->
var()->
type() == dods_url_c)
267 throw Error(malformed_expr,
"The function requires a DAP numeric-type array argument.");
270 throw InternalErr(__FILE__, __LINE__,
string(
"The Array '") + a->
name() +
"'does not contain values.");
278 return extract_double_array_helper<dods_byte>(a);
280 return extract_double_array_helper<dods_uint16>(a);
282 return extract_double_array_helper<dods_int16>(a);
284 return extract_double_array_helper<dods_uint32>(a);
286 return extract_double_array_helper<dods_int32>(a);
288 return extract_double_array_helper<dods_float32>(a);
294 return extract_double_array_helper<dods_float64>(a);
298 return extract_double_array_helper<dods_byte>(a);
300 return extract_double_array_helper<dods_int8>(a);
302 return extract_double_array_helper<dods_uint64>(a);
304 return extract_double_array_helper<dods_int64>(a);
307 "The argument list built by the CE parser contained an unsupported numeric type.");
314 template<
class T>
static void extract_double_array_helper(
Array * a, vector<double> &dest)
317 assert(dest.size() == (
unsigned long )a->
length());
324 for (
int i = 0; i < length; ++i)
325 dest[i] = (
double) b[i];
345 || a->
var()->
type() == dods_url_c)
346 throw Error(malformed_expr,
"The function requires a DAP numeric-type array argument.");
349 throw InternalErr(__FILE__, __LINE__,
string(
"The Array '") + a->
name() +
"'does not contain values.");
359 return extract_double_array_helper<dods_byte>(a, dest);
361 return extract_double_array_helper<dods_uint16>(a, dest);
363 return extract_double_array_helper<dods_int16>(a, dest);
365 return extract_double_array_helper<dods_uint32>(a, dest);
367 return extract_double_array_helper<dods_int32>(a, dest);
369 return extract_double_array_helper<dods_float32>(a, dest);
371 return a->value(&dest[0]);
376 return extract_double_array_helper<dods_byte>(a, dest);
378 return extract_double_array_helper<dods_int8>(a, dest);
380 return extract_double_array_helper<dods_uint64>(a, dest);
382 return extract_double_array_helper<dods_int64>(a, dest);
385 "The argument list built by the CE parser contained an unsupported numeric type.");
404 throw Error(malformed_expr,
"The function requires a numeric-type argument.");
408 "The Evaluator built an argument list where some constants held no values.");
414 switch (arg->
type()) {
416 return (
double) (
static_cast<Byte*
>(arg)->value());
418 return (
double) (
static_cast<UInt16*
>(arg)->value());
420 return (
double) (
static_cast<Int16*
>(arg)->value());
422 return (
double) (
static_cast<UInt32*
>(arg)->value());
424 return (
double) (
static_cast<Int32*
>(arg)->value());
426 return (
double) (
static_cast<Float32*
>(arg)->value());
428 return static_cast<Float64*
>(arg)->value();
432 return (
double) (
static_cast<Byte*
>(arg)->value());
434 return (
double) (
static_cast<Int8*
>(arg)->value());
436 return (
double) (
static_cast<UInt64*
>(arg)->value());
438 return (
double) (
static_cast<Int64*
>(arg)->value());
442 "The argument list built by the parser contained an unsupported numeric type.");
458 if (name.find_first_of(
' ') == name.npos)
462 unsigned int i = name.find_first_not_of(
' ');
463 string tmp_name = name.substr(i);
466 unsigned int j = tmp_name.find(
'?') + 1;
467 i = tmp_name.find_first_not_of(
' ', j);
468 tmp_name.erase(j, i - j);
477 bool unique_names(vector<BaseType *> l,
const string &var_name,
const string &
type_name,
string &msg)
480 vector<string> names(l.size());
483 typedef std::vector<BaseType *>::const_iterator citer;
484 for (citer i = l.begin(); i != l.end(); i++) {
486 names[nelem++] = (*i)->name();
487 DBG(cerr <<
"NAMES[" << nelem - 1 <<
"]=" << names[nelem-1] << endl);
491 sort(names.begin(), names.end());
494 sort(names.begin(), names.end());
497 for (
int j = 1; j < nelem; ++j) {
498 if (names[j - 1] == names[j]) {
500 oss <<
"The variable `" << names[j] <<
"' is used more than once in " << type_name <<
" `" << var_name
521 extern "C" const char *
524 return PACKAGE_VERSION;
527 extern "C" const char *
542 if (time(&TimBin) == (time_t) -1)
543 return string(
"time() error");
545 char *ctime_value = ctime(&TimBin);
547 string TimStr = ctime_value;
548 return TimStr.substr(0, TimStr.size() - 2);
561 for (
unsigned int i = 0; i < s.length(); i++)
562 s[i] = tolower(s[i]);
572 return (!s.empty() && s[0] ==
'\"' && s[s.length() - 1] ==
'\"');
584 return s.substr(1, s.length() - 2);
592 if (strcmp(name,
"Byte") == 0)
return dods_byte_c;
594 if (strcmp(name,
"Char") == 0)
return dods_char_c;
596 if (strcmp(name,
"Int8") == 0)
return dods_int8_c;
598 if (strcmp(name,
"UInt8") == 0)
return dods_uint8_c;
600 if (strcmp(name,
"Int16") == 0)
return dods_int16_c;
602 if (strcmp(name,
"UInt16") == 0)
return dods_uint16_c;
604 if (strcmp(name,
"Int32") == 0)
return dods_int32_c;
606 if (strcmp(name,
"UInt32") == 0)
return dods_uint32_c;
608 if (strcmp(name,
"Int64") == 0)
return dods_int64_c;
610 if (strcmp(name,
"UInt64") == 0)
return dods_uint64_c;
612 if (strcmp(name,
"Float32") == 0)
return dods_float32_c;
614 if (strcmp(name,
"Float64") == 0)
return dods_float64_c;
616 if (strcmp(name,
"String") == 0)
return dods_str_c;
621 if (strcmp(name,
"Url") == 0 || strcmp(name,
"URL") == 0)
return dods_url_c;
623 if (strcmp(name,
"Enum") == 0)
return dods_enum_c;
625 if (strcmp(name,
"Opaque") == 0)
return dods_opaque_c;
627 if (strcmp(name,
"Array") == 0)
return dods_array_c;
629 if (strcmp(name,
"Structure") == 0)
return dods_structure_c;
631 if (strcmp(name,
"Sequence") == 0)
return dods_sequence_c;
633 if (strcmp(name,
"Grid") == 0)
return dods_grid_c;
649 return string(
"Null");
651 return string(
"Byte");
653 return string(
"Int16");
655 return string(
"UInt16");
657 return string(
"Int32");
659 return string(
"UInt32");
661 return string(
"Float32");
663 return string(
"Float64");
665 return string(
"String");
667 return string(
"Url");
670 return string(
"Array");
671 case dods_structure_c:
672 return string(
"Structure");
673 case dods_sequence_c:
674 return string(
"Sequence");
676 return string(
"Grid");
679 throw InternalErr(__FILE__, __LINE__,
"Unknown type.");
694 return string(
"Null");
696 return string(
"Byte");
698 return string(
"Char");
700 return string(
"Int8");
702 return string(
"UInt8");
704 return string(
"Int16");
706 return string(
"UInt16");
708 return string(
"Int32");
710 return string(
"UInt32");
712 return string(
"Int64");
714 return string(
"UInt64");
716 return string(
"Enum");
719 return string(
"Float32");
721 return string(
"Float64");
724 return string(
"String");
726 return string(
"URL");
729 return string(
"Opaque");
732 return string(
"Array");
734 case dods_structure_c:
735 return string(
"Structure");
736 case dods_sequence_c:
737 return string(
"Sequence");
739 return string(
"Group");
742 throw InternalErr(__FILE__, __LINE__,
"Unknown type.");
799 case dods_structure_c:
800 case dods_sequence_c:
844 case dods_structure_c:
845 case dods_sequence_c:
887 case dods_structure_c:
888 case dods_sequence_c:
931 return (stat(dir.c_str(), &buf) == 0) && (buf.st_mode & S_IFDIR);
935 void append_long_to_string(
long val,
int base,
string &str_val)
940 char digits[] =
"0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
944 if (base > 36 || base < 2) {
946 std::invalid_argument ex(
"The parameter base has an invalid value.");
949 if (val < 0) str_val +=
'-';
950 r = ldiv(labs(val), base);
953 if (r.quot > 0) append_long_to_string(r.quot, base, str_val);
957 str_val += digits[(int) r.rem];
961 string long_to_string(
long val,
int base)
964 append_long_to_string(val, base, s);
969 void append_double_to_string(
const double &num,
string &str)
979 string double_to_string(
const double &num)
982 append_double_to_string(num, s);
993 static const char path_sep[] =
997 static const char path_sep[] = {
"/" };
1010 string::size_type pos = path.rfind(path_sep);
1012 return (pos == string::npos) ? path : path.substr(++pos);
1015 #define CHECK_BIT( tab, bit ) ( tab[ (bit)/8 ] & (1<<( (bit)%8 )) ) 1016 #define BITLISTSIZE 16 1022 static void globchars(
const char *s,
const char *e,
char *b)
1026 memset(b,
'\0', BITLISTSIZE);
1028 if (*s ==
'^') neg++, s++;
1033 if (s + 2 < e && s[1] ==
'-') {
1034 for (c = s[0]; c <= s[2]; c++)
1035 b[c / 8] |= (1 << (c % 8));
1040 b[c / 8] |= (1 << (c % 8));
1046 for (i = 0; i < BITLISTSIZE; i++)
1071 int glob(
const char *c,
const char *s)
1073 if (!c || !s)
return 1;
1075 char bitlist[BITLISTSIZE];
1084 if (!*s++)
return i;
1090 const char *here = c;
1092 if (!*c++)
return i;
1093 }
while (here == c || *c !=
']');
1098 globchars(here, c, bitlist);
1100 if (!CHECK_BIT(bitlist, *(
unsigned char * )s))
return i;
1106 const char *here = s;
1119 r = *c ?
glob(c, s) : *s ? -1 : 0;
1123 else if (r < 0)
return i;
1133 if (!*c || *s++ != *c++)
return i;
1137 if (*s++ != c[-1])
return i;
1154 return (sz > 0 && nelem < UINT_MAX / sz);
1175 if (path.length() > 255)
return false;
1177 Regex name(
"[-0-9A-z_./]+");
1178 if (!strict) name =
"[:print:]+";
1180 string::size_type len = path.length();
1181 int result = name.
match(path.c_str(), len);
1184 if (len > INT_MAX || result != static_cast<int>(len))
return false;
1197 return (
string)
"OPeNDAP DAP/" +
libdap_version() +
": compiled on " + __DATE__ +
":" + __TIME__;
1215 copy(name_template.begin(), name_template.end(), back_inserter(name));
1216 if (!suffix.empty())
1217 copy(suffix.begin(), suffix.end(), back_inserter(name));
1218 name.push_back(
'\0');
1221 int tmpfile = mkstemps(&name[0], suffix.length());
1223 throw Error(internal_error,
"Could not make a temporary file.");
1230 throw Error(internal_error,
"Could not make a temporary file.");
1232 return string(&name[0]);
Holds an 8-bit signed integer value.
Holds a64-bit signed integer.
virtual bool read_p()
Has this variable been read?
bool is_constructor_type(Type t)
Returns true if the instance is a constructor (i.e., Structure, Sequence or Grid) type variable...
string prune_spaces(const string &name)
virtual int length() const
string open_temp_fstream(ofstream &f, const string &name_template, const string &suffix)
Holds an unsigned 16-bit integer.
virtual void set_read_p(bool state)
Indicates that the data is ready to send.
string extract_string_argument(BaseType *arg)
bool dir_exists(const string &dir)
bool size_ok(unsigned int sz, unsigned int nelem)
sanitize the size of an array. Test for integer overflow when dynamically allocating an array...
bool is_vector_type(Type t)
Returns true if the instance is a vector (i.e., array) type variable.
Type
Identifies the data type.
virtual Type type() const
Returns the type of the class instance.
Holds a 32-bit floating point value.
A class for software fault reporting.
int match(const char *s, int len, int pos=0)
Does the pattern match.
virtual BaseType * var(const string &name="", bool exact_match=true, btp_stack *s=0)
Holds character string data.
void set_array_using_double(Array *dest, double *src, int src_len)
bool pathname_ok(const string &path, bool strict)
Does the string name a potentially valid pathname? Test the given pathname to verify that it is a val...
double * extract_double_array(Array *a)
Holds a 16-bit signed integer value.
ObjectType get_type(const string &value)
string D4type_name(Type t)
Returns the type of the class instance as a string. Supports all DAP4 types and not the DAP2-only typ...
string path_to_filename(string path)
bool is_quoted(const string &s)
bool is_simple_type(Type t)
Returns true if the instance is a numeric, string or URL type variable.
Holds a 64-bit unsigned integer.
double extract_double_value(BaseType *arg)
virtual string name() const
Returns the name of the class instance.
virtual bool is_simple_type() const
Returns true if the instance is a numeric, string or URL type variable.
string remove_quotes(const string &s)
The basic data type for the DODS DAP types.
string D2type_name(Type t)
Returns the type of the class instance as a string. Supports all DAP2 types and not the DAP4-only typ...
Holds a 64-bit (double precision) floating point value.
A class for error processing.
bool is_host_big_endian()
Does this host use big-endian byte order?
Holds a 32-bit unsigned integer.
const char * libdap_version()
A multidimensional array of identical data types.
int glob(const char *c, const char *s)
bool is_integer_type(Type t)
Holds a 32-bit signed integer.