39 {
"$Id: util.cc 26028 2012-11-07 20:34:13Z jimg $"
48 #ifndef TM_IN_SYS_TIME
62 #include <sys/types.h>
100 if (name.find_first_of(
' ') == name.npos)
104 unsigned int i = name.find_first_not_of(
' ');
105 string tmp_name = name.substr(i);
108 unsigned int j = tmp_name.find(
'?') + 1;
109 i = tmp_name.find_first_not_of(
' ', j);
110 tmp_name.erase(j, i - j);
121 const string &type_name,
string &msg)
124 vector<string> names(l.size());
127 typedef std::vector<BaseType *>::const_iterator citer ;
128 for (citer i = l.begin(); i != l.end(); i++) {
130 names[nelem++] = (*i)->name();
131 DBG(cerr <<
"NAMES[" << nelem - 1 <<
"]=" << names[nelem-1] << endl);
135 sort(names.begin(), names.end());
138 cout <<
"unique:" << endl;
139 for (
int ii = 0; ii < nelem; ++ii)
140 cout <<
"NAMES[" << ii <<
"]=" << names[ii] << endl;
144 sort(names.begin(), names.end());
147 cout <<
"unique:" << endl;
148 for (
int ii = 0; ii < nelem; ++ii)
149 cout <<
"NAMES[" << ii <<
"]=" << names[ii] << endl;
153 for (
int j = 1; j < nelem; ++j) {
154 if (names[j-1] == names[j]) {
156 oss <<
"The variable `" << names[j]
157 <<
"' is used more than once in " << type_name <<
" `"
192 #if COMPRESSION_FOR_SERVER3
204 DBG(cerr <<
"Entering deflate_exists...");
212 string deflate = (string)
libdap_root() +
"/sbin/deflate";
220 status = (stat(deflate.c_str(), &buf) == 0)
222 || (stat(
".\\deflate", &buf) == 0);
224 || (stat(
"./deflate", &buf) == 0);
229 status &= (buf.st_mode & _S_IEXEC);
231 status &= buf.st_mode & (S_IXUSR | S_IXGRP | S_IXOTH);
233 DBG(cerr <<
" returning " << (status ?
"true." :
"false.") << endl);
234 return (status != 0);
238 compressor(FILE *output,
int &childpid)
247 if (_pipe(data, 512, O_BINARY | O_NOINHERIT) < 0) {
248 cerr <<
"Could not create IPC channel for compressor process"
258 hStdIn = _dup(_fileno(stdin));
259 hStdOut = _dup(_fileno(stdout));
262 if (_dup2(data[0], _fileno(stdin)) != 0) {
263 cerr <<
"dup of child stdin failed" << endl;
267 if (_dup2(_fileno(output), _fileno(stdout)) != 0) {
268 cerr <<
"dup of child stdout failed" << endl;
273 string deflate =
"deflate.exe";
274 if ((pid = _spawnlp(_P_NOWAIT, deflate.c_str(), deflate.c_str(),
275 "-c",
"5",
"-s", NULL)) < 0) {
276 cerr <<
"Could not spawn to create compressor process" << endl;
281 if (_dup2(hStdIn, _fileno(stdin)) != 0) {
282 cerr <<
"dup of stdin failed" << endl;
285 if (_dup2(hStdOut, _fileno(stdout)) != 0) {
286 cerr <<
"dup of stdout failed" << endl;
295 FILE *input = fdopen(data[1],
"w");
301 FILE *ret_file = NULL ;
305 if (pipe(data) < 0) {
306 cerr <<
"Could not create IPC channel for compressor process"
311 if ((pid = fork()) < 0) {
312 cerr <<
"Could not fork to create compressor process" << endl;
322 ret_file = fdopen(data[1],
"w");
329 dup2(fileno(output), 1);
331 DBG(cerr <<
"Opening compression stream." << endl);
336 string deflate = (string)
libdap_root() +
"/sbin/deflate";
337 (void) execl(deflate.c_str(),
"deflate",
"-c",
"5",
"-s", NULL);
338 (void) execl(
"./deflate",
"deflate",
"-c",
"5",
"-s", NULL);
339 cerr <<
"Warning: Could not start compressor!" << endl;
340 cerr <<
"defalte should be in DODS_ROOT/etc or in the CWD!"
349 #endif // COMPRESSION_FOR_SERVER3
362 return (stat(dir.c_str(), &buf) == 0) && (buf.st_mode & S_IFDIR);
377 dir_writable(
const string &dir)
380 string test = dir +
"/test.txt";
381 ofstream ofs(dir.c_str());
382 ofs.write(
"test", 5);
384 unlink(test.c_str());
401 if (time(&TimBin) == (time_t) - 1)
402 return string(
"time() error");
404 string TimStr = ctime(&TimBin);
405 return TimStr.substr(0, TimStr.size() - 2);
412 for (
unsigned int i = 0; i < s.length(); i++)
413 s[i] = tolower(s[i]);
419 return (!s.empty() && s[0] ==
'\"' && s[s.length()-1] ==
'\"');
426 return s.substr(1, s.length() - 2);
437 void flush_stream(iostream ios, FILE *out)
442 ios.get(buffer, 512, NULL);
443 while ((nbytes = ios.gcount()) > 0) {
444 fwrite(buffer, 1, nbytes, out);
445 ios.get(buffer, 512, NULL);
459 char digits[] =
"0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
463 if (base > 36 || base < 2) {
465 std::invalid_argument ex(
"The parameter base has an invalid value.");
470 r = ldiv(labs(val), base);
478 str_val += digits[(int)r.rem];
515 return (
string)
"OPeNDAP DAP/" +
libdap_version() +
": compiled on " + __DATE__ +
":" + __TIME__ ;
525 static const char path_sep[] =
529 static const char path_sep[] =
537 string::size_type pos = path.rfind(path_sep);
539 return (pos == string::npos) ? path : path.substr(++pos);
552 while (fread(&c, 1, 1, fp))
560 wildcmp(
const char *wild,
const char *
string)
564 if (!wild || !
string)
567 const char *cp = NULL, *mp = NULL;
569 while ((*
string) && (*wild !=
'*')) {
570 if ((*wild != *
string) && (*wild !=
'?')) {
584 }
else if ((*wild == *
string) || (*wild ==
'?')) {
593 while (*wild ==
'*') {
600 #define CHECK_BIT( tab, bit ) ( tab[ (bit)/8 ] & (1<<( (bit)%8 )) )
601 #define BITLISTSIZE 16
603 static void globchars(
const char *s,
const char *e,
char *b );
623 glob(
const char *c,
const char *s)
644 const char *here = c;
648 }
while (here == c || *c !=
']');
653 globchars(here, c, bitlist);
655 if (!
CHECK_BIT( bitlist, *(
unsigned char *)s ))
662 const char *here = s;
675 r = *c ?
glob(c, s) : *s ? -1 : 0;
690 if (!*c || *s++ != *c++)
706 static void globchars(
const char *s,
const char *e,
char *b) {
717 if (s + 2 < e && s[1] ==
'-') {
718 for (c = s[0]; c <= s[2]; c++)
719 b[c / 8] |= (1 << (c % 8));
724 b[c / 8] |= (1 << (c % 8));
739 int wmatch(
const char *pat,
const char *s)
745 case '\0':
return (*s ==
'\0');
746 case '?':
return (*s !=
'\0') &&
wmatch(pat+1, s+1);
747 case '*':
return wmatch(pat+1, s) || (*s !=
'\0' &&
wmatch(pat, s+1));
748 default:
return (*s == *pat) &&
wmatch(pat+1, s+1);
763 return (sz > 0 && nelem < UINT_MAX / sz);
785 if (path.length() > 255)
788 Regex name(
"[-0-9A-z_./]+");
792 string::size_type len = path.length();
793 int result = name.match(path.c_str(), len);
796 if (len > INT_MAX || result != static_cast<int>(len))
string prune_spaces(const string &name)
int wmatch(const char *pat, const char *s)
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...
int wildcmp(const char *wild, const char *string)
bool pathname_ok(const string &path, bool strict)
Does the string name a potentailly valid pathname? Test the given pathname to verfiy that it is a val...
string file_to_string(FILE *fp)
string path_to_filename(string path)
bool is_quoted(const string &s)
const char * libdap_root()
void append_long_to_string(long val, int base, string &str_val)
string long_to_string(long val, int base)
#define CHECK_BIT(tab, bit)
string double_to_string(const double &num)
string remove_quotes(const string &s)
bool unique_names(vector< BaseType * > l, const string &var_name, const string &type_name, string &msg)
const char * libdap_version()
int glob(const char *c, const char *s)
void append_double_to_string(const double &num, string &str)
const char * libdap_name()