39 {
"$Id: util.cc 21699 2009-11-05 00:06:01Z jimg $"
46 #ifndef TM_IN_SYS_TIME
60 #include <sys/types.h>
99 if (name.find_first_of(
' ') == name.npos)
103 unsigned int i = name.find_first_not_of(
' ');
104 string tmp_name = name.substr(i);
107 unsigned int j = tmp_name.find(
'?') + 1;
108 i = tmp_name.find_first_not_of(
' ', j);
109 tmp_name.erase(j, i - j);
120 const string &type_name,
string &msg)
123 vector<string> names(l.size());
126 typedef std::vector<BaseType *>::const_iterator citer ;
127 for (citer i = l.begin(); i != l.end(); i++) {
129 names[nelem++] = (*i)->name();
130 DBG(cerr <<
"NAMES[" << nelem - 1 <<
"]=" << names[nelem-1] << endl);
134 sort(names.begin(), names.end());
137 cout <<
"unique:" << endl;
138 for (
int ii = 0; ii < nelem; ++ii)
139 cout <<
"NAMES[" << ii <<
"]=" << names[ii] << endl;
143 sort(names.begin(), names.end());
146 cout <<
"unique:" << endl;
147 for (
int ii = 0; ii < nelem; ++ii)
148 cout <<
"NAMES[" << ii <<
"]=" << names[ii] << endl;
152 for (
int j = 1; j < nelem; ++j) {
153 if (names[j-1] == names[j]) {
155 oss <<
"The variable `" << names[j]
156 <<
"' is used more than once in " << type_name <<
" `"
191 #if COMPRESSION_FOR_SERVER3
203 DBG(cerr <<
"Entering deflate_exists...");
211 string deflate = (string)
libdap_root() +
"/sbin/deflate";
219 status = (stat(deflate.c_str(), &buf) == 0)
221 || (stat(
".\\deflate", &buf) == 0);
223 || (stat(
"./deflate", &buf) == 0);
228 status &= (buf.st_mode & _S_IEXEC);
230 status &= buf.st_mode & (S_IXUSR | S_IXGRP | S_IXOTH);
232 DBG(cerr <<
" returning " << (status ?
"true." :
"false.") << endl);
233 return (status != 0);
246 if (_pipe(data, 512, O_BINARY | O_NOINHERIT) < 0) {
247 cerr <<
"Could not create IPC channel for compressor process"
257 hStdIn = _dup(_fileno(stdin));
258 hStdOut = _dup(_fileno(stdout));
261 if (_dup2(data[0], _fileno(stdin)) != 0) {
262 cerr <<
"dup of child stdin failed" << endl;
266 if (_dup2(_fileno(output), _fileno(stdout)) != 0) {
267 cerr <<
"dup of child stdout failed" << endl;
272 string deflate =
"deflate.exe";
273 if ((pid = _spawnlp(_P_NOWAIT, deflate.c_str(), deflate.c_str(),
274 "-c",
"5",
"-s", NULL)) < 0) {
275 cerr <<
"Could not spawn to create compressor process" << endl;
280 if (_dup2(hStdIn, _fileno(stdin)) != 0) {
281 cerr <<
"dup of stdin failed" << endl;
284 if (_dup2(hStdOut, _fileno(stdout)) != 0) {
285 cerr <<
"dup of stdout failed" << endl;
294 FILE *input = fdopen(data[1],
"w");
300 FILE *ret_file = NULL ;
304 if (pipe(data) < 0) {
305 cerr <<
"Could not create IPC channel for compressor process"
310 if ((pid = fork()) < 0) {
311 cerr <<
"Could not fork to create compressor process" << endl;
321 ret_file = fdopen(data[1],
"w");
328 dup2(fileno(output), 1);
330 DBG(cerr <<
"Opening compression stream." << endl);
335 string deflate = (string)
libdap_root() +
"/sbin/deflate";
336 (void) execl(deflate.c_str(),
"deflate",
"-c",
"5",
"-s", NULL);
337 (void) execl(
"./deflate",
"deflate",
"-c",
"5",
"-s", NULL);
338 cerr <<
"Warning: Could not start compressor!" << endl;
339 cerr <<
"defalte should be in DODS_ROOT/etc or in the CWD!"
348 #endif // COMPRESSION_FOR_SERVER3
358 if (time(&TimBin) == (time_t) - 1)
359 return string(
"time() error");
361 string TimStr = ctime(&TimBin);
362 return TimStr.substr(0, TimStr.size() - 2);
369 for (
unsigned int i = 0; i < s.length(); i++)
370 s[i] = tolower(s[i]);
376 return (!s.empty() && s[0] ==
'\"' && s[s.length()-1] ==
'\"');
383 return s.substr(1, s.length() - 2);
394 void flush_stream(iostream ios, FILE *out)
399 ios.get(buffer, 512, NULL);
400 while ((nbytes = ios.gcount()) > 0) {
401 fwrite(buffer, 1, nbytes, out);
402 ios.get(buffer, 512, NULL);
416 char digits[] =
"0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
420 if (base > 36 || base < 2) {
422 std::invalid_argument ex(
"The parameter base has an invalid value.");
427 r = ldiv(labs(val), base);
435 str_val += digits[(int)r.rem];
472 return (
string)
"OPeNDAP DAP/" +
libdap_version() +
": compiled on " + __DATE__ +
":" + __TIME__ ;
482 static const char path_sep[] =
486 static const char path_sep[] =
494 string::size_type pos = path.rfind(path_sep);
496 return (pos == string::npos) ? path : path.substr(++pos);
509 while (fread(&c, 1, 1, fp))
525 return (sz > 0 && nelem < UINT_MAX / sz);
547 if (path.length() > 255)
550 Regex name(
"[-0-9A-z_./]+");
554 string::size_type len = path.length();
555 int result = name.match(path.c_str(), len);
558 if (len > INT_MAX || result != static_cast<int>(len))