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 *
528 return PACKAGE_VERSION;
531 extern "C" const char *
546 if (time(&TimBin) == (time_t) -1)
547 return string(
"time() error");
549 char *ctime_value = ctime(&TimBin);
551 string TimStr = ctime_value;
552 return TimStr.substr(0, TimStr.size() - 2);
565 for (
unsigned int i = 0; i < s.length(); i++)
566 s[i] = tolower(s[i]);
576 return (!s.empty() && s[0] ==
'\"' && s[s.length() - 1] ==
'\"');
588 return s.substr(1, s.length() - 2);
596 if (strcmp(name,
"Byte") == 0)
return dods_byte_c;
598 if (strcmp(name,
"Char") == 0)
return dods_char_c;
600 if (strcmp(name,
"Int8") == 0)
return dods_int8_c;
602 if (strcmp(name,
"UInt8") == 0)
return dods_uint8_c;
604 if (strcmp(name,
"Int16") == 0)
return dods_int16_c;
606 if (strcmp(name,
"UInt16") == 0)
return dods_uint16_c;
608 if (strcmp(name,
"Int32") == 0)
return dods_int32_c;
610 if (strcmp(name,
"UInt32") == 0)
return dods_uint32_c;
612 if (strcmp(name,
"Int64") == 0)
return dods_int64_c;
614 if (strcmp(name,
"UInt64") == 0)
return dods_uint64_c;
616 if (strcmp(name,
"Float32") == 0)
return dods_float32_c;
618 if (strcmp(name,
"Float64") == 0)
return dods_float64_c;
620 if (strcmp(name,
"String") == 0)
return dods_str_c;
625 if (strcmp(name,
"Url") == 0 || strcmp(name,
"URL") == 0)
return dods_url_c;
627 if (strcmp(name,
"Enum") == 0)
return dods_enum_c;
629 if (strcmp(name,
"Opaque") == 0)
return dods_opaque_c;
631 if (strcmp(name,
"Array") == 0)
return dods_array_c;
633 if (strcmp(name,
"Structure") == 0)
return dods_structure_c;
635 if (strcmp(name,
"Sequence") == 0)
return dods_sequence_c;
637 if (strcmp(name,
"Grid") == 0)
return dods_grid_c;
653 return string(
"Null");
655 return string(
"Byte");
657 return string(
"Int16");
659 return string(
"UInt16");
661 return string(
"Int32");
663 return string(
"UInt32");
665 return string(
"Float32");
667 return string(
"Float64");
669 return string(
"String");
671 return string(
"Url");
674 return string(
"Array");
675 case dods_structure_c:
676 return string(
"Structure");
677 case dods_sequence_c:
678 return string(
"Sequence");
680 return string(
"Grid");
683 throw InternalErr(__FILE__, __LINE__,
"Unknown type.");
698 return string(
"Null");
700 return string(
"Byte");
702 return string(
"Char");
704 return string(
"Int8");
706 return string(
"UInt8");
708 return string(
"Int16");
710 return string(
"UInt16");
712 return string(
"Int32");
714 return string(
"UInt32");
716 return string(
"Int64");
718 return string(
"UInt64");
720 return string(
"Enum");
723 return string(
"Float32");
725 return string(
"Float64");
728 return string(
"String");
730 return string(
"URL");
733 return string(
"Opaque");
736 return string(
"Array");
738 case dods_structure_c:
739 return string(
"Structure");
740 case dods_sequence_c:
741 return string(
"Sequence");
743 return string(
"Group");
746 throw InternalErr(__FILE__, __LINE__,
"Unknown type.");
803 case dods_structure_c:
804 case dods_sequence_c:
846 case dods_structure_c:
847 case dods_sequence_c:
887 case dods_structure_c:
888 case dods_sequence_c:
929 return (stat(dir.c_str(), &buf) == 0) && (buf.st_mode & S_IFDIR);
933 void append_long_to_string(
long val,
int base,
string &str_val)
938 char digits[] =
"0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
942 if (base > 36 || base < 2) {
944 std::invalid_argument ex(
"The parameter base has an invalid value.");
947 if (val < 0) str_val +=
'-';
948 r = ldiv(labs(val), base);
951 if (r.quot > 0) append_long_to_string(r.quot, base, str_val);
955 str_val += digits[(int) r.rem];
959 string long_to_string(
long val,
int base)
962 append_long_to_string(val, base, s);
967 void append_double_to_string(
const double &num,
string &str)
977 string double_to_string(
const double &num)
980 append_double_to_string(num, s);
991 static const char path_sep[] =
995 static const char path_sep[] = {
"/" };
1008 string::size_type pos = path.rfind(path_sep);
1010 return (pos == string::npos) ? path : path.substr(++pos);
1013 #define CHECK_BIT( tab, bit ) ( tab[ (bit)/8 ] & (1<<( (bit)%8 )) )
1014 #define BITLISTSIZE 16
1020 static void globchars(
const char *s,
const char *e,
char *b)
1024 memset(b,
'\0', BITLISTSIZE);
1026 if (*s ==
'^') neg++, s++;
1031 if (s + 2 < e && s[1] ==
'-') {
1032 for (c = s[0]; c <= s[2]; c++)
1033 b[c / 8] |= (1 << (c % 8));
1038 b[c / 8] |= (1 << (c % 8));
1044 for (i = 0; i < BITLISTSIZE; i++)
1069 int glob(
const char *c,
const char *s)
1071 if (!c || !s)
return 1;
1073 char bitlist[BITLISTSIZE];
1082 if (!*s++)
return i;
1088 const char *here = c;
1090 if (!*c++)
return i;
1091 }
while (here == c || *c !=
']');
1096 globchars(here, c, bitlist);
1098 if (!CHECK_BIT(bitlist, *(
unsigned char * )s))
return i;
1104 const char *here = s;
1117 r = *c ?
glob(c, s) : *s ? -1 : 0;
1121 else if (r < 0)
return i;
1131 if (!*c || *s++ != *c++)
return i;
1135 if (*s++ != c[-1])
return i;
1150 return (sz > 0 && nelem < UINT_MAX / sz);
1171 if (path.length() > 255)
return false;
1173 Regex name(
"[-0-9A-z_./]+");
1174 if (!strict) name =
"[:print:]+";
1176 string::size_type len = path.length();
1177 int result = name.
match(path.c_str(), len);
1180 if (len > INT_MAX || result != static_cast<int>(len))
return false;
1193 return (
string)
"OPeNDAP DAP/" +
libdap_version() +
": compiled on " + __DATE__ +
":" + __TIME__;
1211 copy(name_template.begin(), name_template.end(), back_inserter(name));
1212 if (!suffix.empty())
1213 copy(suffix.begin(), suffix.end(), back_inserter(name));
1214 name.push_back(
'\0');
1217 int tmpfile = mkstemps(&name[0], suffix.length());
1219 throw Error(internal_error,
"Could not make a temporary file.");
1226 throw Error(internal_error,
"Could not make a temporary file.");
1228 return string(&name[0]);