45 #ifndef TM_IN_SYS_TIME
59 #include <sys/types.h>
96 #ifdef COMPUTE_ENDIAN_AT_RUNTIME
98 dods_int16 i = 0x0100;
99 char *c =
reinterpret_cast<char*
>(&i);
123 if (arg->
type() != dods_str_c)
throw Error(malformed_expr,
"The function requires a string argument.");
127 "The CE Evaluator built an argument list where some constants held no values.");
129 return static_cast<Str*
>(arg)->value();
132 template<
class T>
static void set_array_using_double_helper(Array *a,
double *src,
int src_len)
138 vector<T> values(src_len);
139 for (
int i = 0; i < src_len; ++i)
140 values[i] = (T) src[i];
143 a->set_value(values, src_len);
174 || dest->
var()->
type() == dods_url_c)
175 throw InternalErr(__FILE__, __LINE__,
"The function requires a numeric-type array argument.");
181 if (dest->
length() != src_len)
183 "The source and destination array sizes don't match (" + long_to_string(src_len) +
" versus "
184 + long_to_string(dest->
length()) +
").");
192 set_array_using_double_helper<dods_byte>(dest, src, src_len);
195 set_array_using_double_helper<dods_uint16>(dest, src, src_len);
198 set_array_using_double_helper<dods_int16>(dest, src, src_len);
201 set_array_using_double_helper<dods_uint32>(dest, src, src_len);
204 set_array_using_double_helper<dods_int32>(dest, src, src_len);
207 set_array_using_double_helper<dods_float32>(dest, src, src_len);
210 set_array_using_double_helper<dods_float64>(dest, src, src_len);
215 set_array_using_double_helper<dods_byte>(dest, src, src_len);
218 set_array_using_double_helper<dods_int8>(dest, src, src_len);
221 set_array_using_double_helper<dods_uint64>(dest, src, src_len);
224 set_array_using_double_helper<dods_int64>(dest, src, src_len);
228 "The argument list built by the CE parser contained an unsupported numeric type.");
235 template<
class T>
static double *extract_double_array_helper(Array * a)
239 int length = a->length();
244 double *dest =
new double[length];
245 for (
int i = 0; i < length; ++i)
246 dest[i] = (
double) b[i];
267 || a->
var()->
type() == dods_url_c)
268 throw Error(malformed_expr,
"The function requires a DAP numeric-type array argument.");
271 throw InternalErr(__FILE__, __LINE__,
string(
"The Array '") + a->
name() +
"'does not contain values.");
279 return extract_double_array_helper<dods_byte>(a);
281 return extract_double_array_helper<dods_uint16>(a);
283 return extract_double_array_helper<dods_int16>(a);
285 return extract_double_array_helper<dods_uint32>(a);
287 return extract_double_array_helper<dods_int32>(a);
289 return extract_double_array_helper<dods_float32>(a);
295 return extract_double_array_helper<dods_float64>(a);
299 return extract_double_array_helper<dods_byte>(a);
301 return extract_double_array_helper<dods_int8>(a);
303 return extract_double_array_helper<dods_uint64>(a);
305 return extract_double_array_helper<dods_int64>(a);
308 "The argument list built by the CE parser contained an unsupported numeric type.");
315 template<
class T>
static void extract_double_array_helper(Array * a, vector<double> &dest)
318 assert(dest.size() == (
unsigned long )a->length());
320 int length = a->length();
325 for (
int i = 0; i < length; ++i)
326 dest[i] = (
double) b[i];
349 || a->
var()->
type() == dods_url_c)
350 throw Error(malformed_expr,
"The function requires a DAP numeric-type array argument.");
353 throw InternalErr(__FILE__, __LINE__,
string(
"The Array '") + a->
name() +
"' does not contain values.");
363 return extract_double_array_helper<dods_byte>(a, dest);
365 return extract_double_array_helper<dods_uint16>(a, dest);
367 return extract_double_array_helper<dods_int16>(a, dest);
369 return extract_double_array_helper<dods_uint32>(a, dest);
371 return extract_double_array_helper<dods_int32>(a, dest);
373 return extract_double_array_helper<dods_float32>(a, dest);
375 return a->value(&dest[0]);
380 return extract_double_array_helper<dods_byte>(a, dest);
382 return extract_double_array_helper<dods_int8>(a, dest);
384 return extract_double_array_helper<dods_uint64>(a, dest);
386 return extract_double_array_helper<dods_int64>(a, dest);
389 "The argument list built by the CE parser contained an unsupported numeric type.");
408 throw Error(malformed_expr,
"The function requires a numeric-type argument.");
412 "The Evaluator built an argument list where some constants held no values.");
418 switch (arg->
type()) {
420 return (
double) (
static_cast<Byte*
>(arg)->value());
422 return (
double) (
static_cast<UInt16*
>(arg)->value());
424 return (
double) (
static_cast<Int16*
>(arg)->value());
426 return (
double) (
static_cast<UInt32*
>(arg)->value());
428 return (
double) (
static_cast<Int32*
>(arg)->value());
430 return (
double) (
static_cast<Float32*
>(arg)->value());
432 return static_cast<Float64*
>(arg)->value();
436 return (
double) (
static_cast<Byte*
>(arg)->value());
438 return (
double) (
static_cast<Int8*
>(arg)->value());
440 return (
double) (
static_cast<UInt64*
>(arg)->value());
442 return (
double) (
static_cast<Int64*
>(arg)->value());
446 "The argument list built by the parser contained an unsupported numeric type.");
462 if (name.find_first_of(
' ') == name.npos)
466 unsigned int i = name.find_first_not_of(
' ');
467 string tmp_name = name.substr(i);
470 unsigned int j = tmp_name.find(
'?') + 1;
471 i = tmp_name.find_first_not_of(
' ', j);
472 tmp_name.erase(j, i - j);
481 bool unique_names(vector<BaseType *> l,
const string &var_name,
const string &type_name,
string &msg)
484 vector<string> names(l.size());
487 typedef std::vector<BaseType *>::const_iterator citer;
488 for (citer i = l.begin(); i != l.end(); i++) {
490 names[nelem++] = (*i)->name();
491 DBG(cerr <<
"NAMES[" << nelem - 1 <<
"]=" << names[nelem-1] << endl);
495 sort(names.begin(), names.end());
498 sort(names.begin(), names.end());
501 for (
int j = 1; j < nelem; ++j) {
502 if (names[j - 1] == names[j]) {
504 oss <<
"The variable `" << names[j] <<
"' is used more than once in " <<
type_name <<
" `" << var_name
525 extern "C" const char *
533 extern "C" const char *
548 if (time(&TimBin) == (time_t) -1)
549 return string(
"time() error");
551 char *ctime_value = ctime(&TimBin);
553 string TimStr = ctime_value;
554 return TimStr.substr(0, TimStr.size() - 2);
567 for (
unsigned int i = 0; i < s.length(); i++)
568 s[i] = tolower(s[i]);
578 return (!s.empty() && s[0] ==
'\"' && s[s.length() - 1] ==
'\"');
590 return s.substr(1, s.length() - 2);
598 if (strcmp(name,
"Byte") == 0)
return dods_byte_c;
600 if (strcmp(name,
"Char") == 0)
return dods_char_c;
602 if (strcmp(name,
"Int8") == 0)
return dods_int8_c;
604 if (strcmp(name,
"UInt8") == 0)
return dods_uint8_c;
606 if (strcmp(name,
"Int16") == 0)
return dods_int16_c;
608 if (strcmp(name,
"UInt16") == 0)
return dods_uint16_c;
610 if (strcmp(name,
"Int32") == 0)
return dods_int32_c;
612 if (strcmp(name,
"UInt32") == 0)
return dods_uint32_c;
614 if (strcmp(name,
"Int64") == 0)
return dods_int64_c;
616 if (strcmp(name,
"UInt64") == 0)
return dods_uint64_c;
618 if (strcmp(name,
"Float32") == 0)
return dods_float32_c;
620 if (strcmp(name,
"Float64") == 0)
return dods_float64_c;
622 if (strcmp(name,
"String") == 0)
return dods_str_c;
627 if (strcmp(name,
"Url") == 0 || strcmp(name,
"URL") == 0)
return dods_url_c;
629 if (strcmp(name,
"Enum") == 0)
return dods_enum_c;
631 if (strcmp(name,
"Opaque") == 0)
return dods_opaque_c;
633 if (strcmp(name,
"Array") == 0)
return dods_array_c;
635 if (strcmp(name,
"Structure") == 0)
return dods_structure_c;
637 if (strcmp(name,
"Sequence") == 0)
return dods_sequence_c;
639 if (strcmp(name,
"Grid") == 0)
return dods_grid_c;
655 return string(
"Null");
657 return string(
"Byte");
659 return string(
"Int16");
661 return string(
"UInt16");
663 return string(
"Int32");
665 return string(
"UInt32");
667 return string(
"Float32");
669 return string(
"Float64");
671 return string(
"String");
673 return string(
"Url");
676 return string(
"Array");
677 case dods_structure_c:
678 return string(
"Structure");
679 case dods_sequence_c:
680 return string(
"Sequence");
682 return string(
"Grid");
685 throw InternalErr(__FILE__, __LINE__,
"Unknown type.");
700 return string(
"Null");
702 return string(
"Byte");
704 return string(
"Char");
706 return string(
"Int8");
708 return string(
"UInt8");
710 return string(
"Int16");
712 return string(
"UInt16");
714 return string(
"Int32");
716 return string(
"UInt32");
718 return string(
"Int64");
720 return string(
"UInt64");
722 return string(
"Enum");
725 return string(
"Float32");
727 return string(
"Float64");
730 return string(
"String");
732 return string(
"URL");
735 return string(
"Opaque");
738 return string(
"Array");
740 case dods_structure_c:
741 return string(
"Structure");
742 case dods_sequence_c:
743 return string(
"Sequence");
745 return string(
"Group");
748 throw InternalErr(__FILE__, __LINE__,
"Unknown type.");
805 case dods_structure_c:
806 case dods_sequence_c:
848 case dods_structure_c:
849 case dods_sequence_c:
889 case dods_structure_c:
890 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;
1152 return (sz > 0 && nelem < UINT_MAX / sz);
1173 if (path.length() > 255)
return false;
1177 Regex name(
"[-0-9A-z_./]+");
1178 if (!strict) name =
"[:print:]+";
1181 const Regex strict_name(
"[-0-9A-z_./]+");
1182 const Regex relaxed_name(
"[:print:]+");
1185 string::size_type len = path.length();
1189 result = strict_name.
match(path);
1191 result = relaxed_name.
match(path);
1193 return (result == path.length());
1201 if (len > INT_MAX || result !=
static_cast<int>(len))
return false;
1215 return (
string)
"OPeNDAP DAP/" +
libdap_version() +
": compiled on " + __DATE__ +
":" + __TIME__;
1233 copy(name_template.begin(), name_template.end(), back_inserter(name));
1234 if (!suffix.empty())
1235 copy(suffix.begin(), suffix.end(), back_inserter(name));
1236 name.push_back(
'\0');
1239 int tmpfile = mkstemps(&name[0], suffix.length());
1241 throw Error(internal_error,
"Could not make a temporary file.");
1248 throw Error(internal_error,
"Could not make a temporary file.");
1250 return string(&name[0]);
A multidimensional array of identical data types.
The basic data type for the DODS DAP types.
virtual string name() const
Returns the name of the class instance.
virtual bool read_p()
Has this variable been read?
virtual bool is_simple_type() const
Returns true if the instance is a numeric, string or URL type variable.
virtual Type type() const
Returns the type of the class instance.
A class for error processing.
Holds a 32-bit floating point value.
Holds a 64-bit (double precision) floating point value.
Holds a 16-bit signed integer value.
Holds a 32-bit signed integer.
Holds a64-bit signed integer.
Holds an 8-bit signed integer value.
A class for software fault reporting.
Regular expression matching.
int match(const char *s, int len, int pos=0) const
Does the pattern match.
Holds character string data.
Holds an unsigned 16-bit integer.
Holds a 32-bit unsigned integer.
Holds a 64-bit unsigned integer.
virtual int length() const
virtual void set_read_p(bool state)
Indicates that the data is ready to send.
virtual BaseType * var(const string &name="", bool exact_match=true, btp_stack *s=0)
top level DAP object to house generic methods
Type
Identifies the data type.
string open_temp_fstream(ofstream &f, const string &name_template, const string &suffix)
bool size_ok(unsigned int sz, unsigned int nelem)
sanitize the size of an array. Test for integer overflow when dynamically allocating an array.
string remove_quotes(const string &s)
string path_to_filename(string path)
bool is_host_big_endian()
Does this host use big-endian byte order?
const char * libdap_version()
double extract_double_value(BaseType *arg)
string prune_spaces(const string &name)
void set_array_using_double(Array *dest, double *src, int src_len)
bool is_simple_type(Type t)
Returns true if the instance is a numeric, string or URL type variable.
bool dir_exists(const string &dir)
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...
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...
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...
bool is_constructor_type(Type t)
Returns true if the instance is a constructor (i.e., Structure, Sequence or Grid) type variable.
bool is_vector_type(Type t)
Returns true if the instance is a vector (i.e., array) type variable.
bool is_integer_type(Type t)
int glob(const char *c, const char *s)
string extract_string_argument(BaseType *arg)
ObjectType get_type(const string &value)
double * extract_double_array(Array *a)
bool is_quoted(const string &s)