39 {
"$Id: util.cc 25112 2011-12-29 21:44:54Z jimg $"
46 #ifndef TM_IN_SYS_TIME
60 #include <sys/types.h>
98 if (name.find_first_of(
' ') == name.npos)
102 unsigned int i = name.find_first_not_of(
' ');
103 string tmp_name = name.substr(i);
106 unsigned int j = tmp_name.find(
'?') + 1;
107 i = tmp_name.find_first_not_of(
' ', j);
108 tmp_name.erase(j, i - j);
119 const string &type_name,
string &msg)
122 vector<string> names(l.size());
125 typedef std::vector<BaseType *>::const_iterator citer ;
126 for (citer i = l.begin(); i != l.end(); i++) {
128 names[nelem++] = (*i)->name();
129 DBG(cerr <<
"NAMES[" << nelem - 1 <<
"]=" << names[nelem-1] << endl);
133 sort(names.begin(), names.end());
136 cout <<
"unique:" << endl;
137 for (
int ii = 0; ii < nelem; ++ii)
138 cout <<
"NAMES[" << ii <<
"]=" << names[ii] << endl;
142 sort(names.begin(), names.end());
145 cout <<
"unique:" << endl;
146 for (
int ii = 0; ii < nelem; ++ii)
147 cout <<
"NAMES[" << ii <<
"]=" << names[ii] << endl;
151 for (
int j = 1; j < nelem; ++j) {
152 if (names[j-1] == names[j]) {
154 oss <<
"The variable `" << names[j]
155 <<
"' is used more than once in " << type_name <<
" `"
190 #if COMPRESSION_FOR_SERVER3
202 DBG(cerr <<
"Entering deflate_exists...");
210 string deflate = (string)
libdap_root() +
"/sbin/deflate";
218 status = (stat(deflate.c_str(), &buf) == 0)
220 || (stat(
".\\deflate", &buf) == 0);
222 || (stat(
"./deflate", &buf) == 0);
227 status &= (buf.st_mode & _S_IEXEC);
229 status &= buf.st_mode & (S_IXUSR | S_IXGRP | S_IXOTH);
231 DBG(cerr <<
" returning " << (status ?
"true." :
"false.") << endl);
232 return (status != 0);
245 if (_pipe(data, 512, O_BINARY | O_NOINHERIT) < 0) {
246 cerr <<
"Could not create IPC channel for compressor process"
256 hStdIn = _dup(_fileno(stdin));
257 hStdOut = _dup(_fileno(stdout));
260 if (_dup2(data[0], _fileno(stdin)) != 0) {
261 cerr <<
"dup of child stdin failed" << endl;
265 if (_dup2(_fileno(output), _fileno(stdout)) != 0) {
266 cerr <<
"dup of child stdout failed" << endl;
271 string deflate =
"deflate.exe";
272 if ((pid = _spawnlp(_P_NOWAIT, deflate.c_str(), deflate.c_str(),
273 "-c",
"5",
"-s", NULL)) < 0) {
274 cerr <<
"Could not spawn to create compressor process" << endl;
279 if (_dup2(hStdIn, _fileno(stdin)) != 0) {
280 cerr <<
"dup of stdin failed" << endl;
283 if (_dup2(hStdOut, _fileno(stdout)) != 0) {
284 cerr <<
"dup of stdout failed" << endl;
293 FILE *input = fdopen(data[1],
"w");
299 FILE *ret_file = NULL ;
303 if (pipe(data) < 0) {
304 cerr <<
"Could not create IPC channel for compressor process"
309 if ((pid = fork()) < 0) {
310 cerr <<
"Could not fork to create compressor process" << endl;
320 ret_file = fdopen(data[1],
"w");
327 dup2(fileno(output), 1);
329 DBG(cerr <<
"Opening compression stream." << endl);
334 string deflate = (string)
libdap_root() +
"/sbin/deflate";
335 (void) execl(deflate.c_str(),
"deflate",
"-c",
"5",
"-s", NULL);
336 (void) execl(
"./deflate",
"deflate",
"-c",
"5",
"-s", NULL);
337 cerr <<
"Warning: Could not start compressor!" << endl;
338 cerr <<
"defalte should be in DODS_ROOT/etc or in the CWD!"
347 #endif // COMPRESSION_FOR_SERVER3
357 if (time(&TimBin) == (time_t) - 1)
358 return string(
"time() error");
360 string TimStr = ctime(&TimBin);
361 return TimStr.substr(0, TimStr.size() - 2);
368 for (
unsigned int i = 0; i < s.length(); i++)
369 s[i] = tolower(s[i]);
375 return (!s.empty() && s[0] ==
'\"' && s[s.length()-1] ==
'\"');
382 return s.substr(1, s.length() - 2);
393 void flush_stream(iostream ios, FILE *out)
398 ios.get(buffer, 512, NULL);
399 while ((nbytes = ios.gcount()) > 0) {
400 fwrite(buffer, 1, nbytes, out);
401 ios.get(buffer, 512, NULL);
415 char digits[] =
"0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
419 if (base > 36 || base < 2) {
421 std::invalid_argument ex(
"The parameter base has an invalid value.");
426 r = ldiv(labs(val), base);
434 str_val += digits[(int)r.rem];
471 return (
string)
"OPeNDAP DAP/" +
libdap_version() +
": compiled on " + __DATE__ +
":" + __TIME__ ;
481 static const char path_sep[] =
485 static const char path_sep[] =
493 string::size_type pos = path.rfind(path_sep);
495 return (pos == string::npos) ? path : path.substr(++pos);
508 while (fread(&c, 1, 1, fp))
518 if (!wild || !
string)
521 const char *cp = NULL, *mp = NULL;
523 while ((*
string) && (*wild !=
'*')) {
524 if ((*wild != *
string) && (*wild !=
'?')) {
538 }
else if ((*wild == *
string) || (*wild ==
'?')) {
547 while (*wild ==
'*') {
553 #define CHECK_BIT( tab, bit ) ( tab[ (bit)/8 ] & (1<<( (bit)%8 )) )
554 #define BITLISTSIZE 16
556 static void globchars(
const char *s,
const char *e,
char *b );
576 glob(
const char *c,
const char *s)
597 const char *here = c;
601 }
while (here == c || *c !=
']');
606 globchars(here, c, bitlist);
608 if (!
CHECK_BIT( bitlist, *(
unsigned char *)s ))
615 const char *here = s;
628 r = *c ?
glob(c, s) : *s ? -1 : 0;
643 if (!*c || *s++ != *c++)
659 static void globchars(
const char *s,
const char *e,
char *b) {
670 if (s + 2 < e && s[1] ==
'-') {
671 for (c = s[0]; c <= s[2]; c++)
672 b[c / 8] |= (1 << (c % 8));
677 b[c / 8] |= (1 << (c % 8));
692 int wmatch(
const char *pat,
const char *s)
698 case '\0':
return (*s ==
'\0');
699 case '?':
return (*s !=
'\0') &&
wmatch(pat+1, s+1);
700 case '*':
return wmatch(pat+1, s) || (*s !=
'\0' &&
wmatch(pat, s+1));
701 default:
return (*s == *pat) &&
wmatch(pat+1, s+1);
716 return (sz > 0 && nelem < UINT_MAX / sz);
738 if (path.length() > 255)
741 Regex name(
"[-0-9A-z_./]+");
745 string::size_type len = path.length();
746 int result = name.match(path.c_str(), len);
749 if (len > INT_MAX || result != static_cast<int>(len))