daputil.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786
  1. /*********************************************************************
  2. * Copyright 1993, UCAR/Unidata
  3. * See netcdf/COPYRIGHT file for copying and redistribution conditions.
  4. * $Header: /upc/share/CVS/netcdf-3/libncdap3/daputil.c,v 1.47 2010/05/21 23:24:15 dmh Exp $
  5. *********************************************************************/
  6. #include "config.h"
  7. #include <sys/time.h>
  8. #include "oc.h"
  9. extern int oc_dumpnode(OClink, OCobject);
  10. #include "ncdap3.h"
  11. #include "dapalign.h"
  12. #include "dapodom.h"
  13. #define LBRACKET '['
  14. #define RBRACKET ']'
  15. /**************************************************/
  16. /**
  17. * Provide a hidden interface to allow utilities
  18. * to check if a given path name is really an ncdap3 url.
  19. * If no, return null, else return basename of the url
  20. * minus any extension.
  21. */
  22. int
  23. nc__testurl(const char* path, char** basenamep)
  24. {
  25. NC_URI* uri;
  26. int ok = nc_uriparse(path,&uri);
  27. if(ok) {
  28. char* slash = strrchr(uri->file, '/');
  29. char* dot;
  30. if(slash == NULL) slash = (char*)path; else slash++;
  31. slash = nulldup(slash);
  32. dot = strrchr(slash, '.');
  33. if(dot != NULL && dot != slash) *dot = '\0';
  34. if(basenamep) *basenamep=slash ; else free(slash);
  35. nc_urifree(uri);
  36. }
  37. return ok;
  38. }
  39. /**************************************************/
  40. /*
  41. Given a legal dap name with arbitrary characters,
  42. convert to equivalent legal cdf name
  43. With the new name policy for netcdf, this procedure
  44. does nothing.
  45. */
  46. char*
  47. cdflegalname3(char* dapname)
  48. {
  49. return nulldup(dapname);
  50. }
  51. /* Define the type conversion of the DAP variables
  52. to the external netCDF variable type.
  53. The proper way is to, for example, convert unsigned short
  54. to an int to maintain the values.
  55. Unfortuneately, libnc-dap does not do this:
  56. it translates the types directly. For example
  57. libnc-dap upgrades the DAP byte type, which is unsigned char,
  58. to NC_BYTE, which signed char.
  59. Oh well.
  60. For netcdf-4, we can do proper type conversion.
  61. */
  62. nc_type
  63. nctypeconvert(NCDAPCOMMON* drno, nc_type nctype)
  64. {
  65. nc_type upgrade = NC_NAT;
  66. if(drno->controls.flags & NCF_NC3) {
  67. /* libnc-dap mimic invariant is to maintain type size */
  68. switch (nctype) {
  69. case NC_CHAR: upgrade = NC_CHAR; break;
  70. case NC_BYTE: upgrade = NC_BYTE; break;
  71. case NC_UBYTE: upgrade = NC_BYTE; break;
  72. case NC_SHORT: upgrade = NC_SHORT; break;
  73. case NC_USHORT: upgrade = NC_SHORT; break;
  74. case NC_INT: upgrade = NC_INT; break;
  75. case NC_UINT: upgrade = NC_INT; break;
  76. case NC_INT64: upgrade = NC_INT64; break;
  77. case NC_UINT64: upgrade = NC_UINT64; break;
  78. case NC_FLOAT: upgrade = NC_FLOAT; break;
  79. case NC_DOUBLE: upgrade = NC_DOUBLE; break;
  80. case NC_URL:
  81. case NC_STRING: upgrade = NC_CHAR; break;
  82. default: break;
  83. }
  84. } else if(drno->controls.flags & NCF_NC4) {
  85. /* netcdf-4 conversion is more correct */
  86. switch (nctype) {
  87. case NC_CHAR: upgrade = NC_CHAR; break;
  88. case NC_BYTE: upgrade = NC_BYTE; break;
  89. case NC_UBYTE: upgrade = NC_UBYTE; break;
  90. case NC_SHORT: upgrade = NC_SHORT; break;
  91. case NC_USHORT: upgrade = NC_USHORT; break;
  92. case NC_INT: upgrade = NC_INT; break;
  93. case NC_UINT: upgrade = NC_UINT; break;
  94. case NC_INT64: upgrade = NC_INT64; break;
  95. case NC_UINT64: upgrade = NC_UINT64; break;
  96. case NC_FLOAT: upgrade = NC_FLOAT; break;
  97. case NC_DOUBLE: upgrade = NC_DOUBLE; break;
  98. case NC_URL:
  99. case NC_STRING: upgrade = NC_STRING; break;
  100. default: break;
  101. }
  102. }
  103. return upgrade;
  104. }
  105. nc_type
  106. octypetonc(OCtype etype)
  107. {
  108. switch (etype) {
  109. case OC_Char: return NC_CHAR;
  110. case OC_Byte: return NC_UBYTE;
  111. case OC_UByte: return NC_UBYTE;
  112. case OC_Int16: return NC_SHORT;
  113. case OC_UInt16: return NC_USHORT;
  114. case OC_Int32: return NC_INT;
  115. case OC_UInt32: return NC_UINT;
  116. case OC_Int64: return NC_INT64;
  117. case OC_UInt64: return NC_UINT64;
  118. case OC_Float32: return NC_FLOAT;
  119. case OC_Float64: return NC_DOUBLE;
  120. case OC_String: return NC_STRING;
  121. case OC_URL: return NC_STRING;
  122. case OC_Dataset: return NC_Dataset;
  123. case OC_Sequence: return NC_Sequence;
  124. case OC_Structure: return NC_Structure;
  125. case OC_Grid: return NC_Grid;
  126. case OC_Dimension: return NC_Dimension;
  127. case OC_Primitive: return NC_Primitive;
  128. default: break;
  129. }
  130. return NC_NAT;
  131. }
  132. OCtype
  133. nctypetodap(nc_type nctype)
  134. {
  135. switch (nctype) {
  136. case NC_CHAR: return OC_Char;
  137. case NC_BYTE: return OC_Byte;
  138. case NC_UBYTE: return OC_UByte;
  139. case NC_SHORT: return OC_Int16;
  140. case NC_USHORT: return OC_UInt16;
  141. case NC_INT: return OC_Int32;
  142. case NC_UINT: return OC_UInt32;
  143. case NC_INT64: return OC_Int64;
  144. case NC_UINT64: return OC_UInt64;
  145. case NC_FLOAT: return OC_Float32;
  146. case NC_DOUBLE: return OC_Float64;
  147. case NC_STRING: return OC_String;
  148. default : break;
  149. }
  150. return OC_NAT;
  151. }
  152. size_t
  153. nctypesizeof(nc_type nctype)
  154. {
  155. switch (nctype) {
  156. case NC_CHAR: return sizeof(char);
  157. case NC_BYTE: return sizeof(signed char);
  158. case NC_UBYTE: return sizeof(unsigned char);
  159. case NC_SHORT: return sizeof(short);
  160. case NC_USHORT: return sizeof(unsigned short);
  161. case NC_INT: return sizeof(int);
  162. case NC_UINT: return sizeof(unsigned int);
  163. case NC_INT64: return sizeof(long long);
  164. case NC_UINT64: return sizeof(unsigned long long);
  165. case NC_FLOAT: return sizeof(float);
  166. case NC_DOUBLE: return sizeof(double);
  167. case NC_STRING: return sizeof(char*);
  168. default: PANIC("nctypesizeof");
  169. }
  170. return 0;
  171. }
  172. char*
  173. nctypetostring(nc_type nctype)
  174. {
  175. switch (nctype) {
  176. case NC_NAT: return "NC_NAT";
  177. case NC_BYTE: return "NC_BYTE";
  178. case NC_CHAR: return "NC_CHAR";
  179. case NC_SHORT: return "NC_SHORT";
  180. case NC_INT: return "NC_INT";
  181. case NC_FLOAT: return "NC_FLOAT";
  182. case NC_DOUBLE: return "NC_DOUBLE";
  183. case NC_UBYTE: return "NC_UBYTE";
  184. case NC_USHORT: return "NC_USHORT";
  185. case NC_UINT: return "NC_UINT";
  186. case NC_INT64: return "NC_INT64";
  187. case NC_UINT64: return "NC_UINT64";
  188. case NC_STRING: return "NC_STRING";
  189. case NC_VLEN: return "NC_VLEN";
  190. case NC_OPAQUE: return "NC_OPAQUE";
  191. case NC_ENUM: return "NC_ENUM";
  192. case NC_COMPOUND: return "NC_COMPOUND";
  193. case NC_URL: return "NC_URL";
  194. case NC_SET: return "NC_SET";
  195. case NC_Dataset: return "NC_Dataset";
  196. case NC_Sequence: return "NC_Sequence";
  197. case NC_Structure: return "NC_Structure";
  198. case NC_Grid: return "NC_Grid";
  199. case NC_Dimension: return "NC_Dimension";
  200. case NC_Primitive: return "NC_Primitive";
  201. default: break;
  202. }
  203. return NULL;
  204. }
  205. /* Pad a buffer */
  206. int
  207. alignbuffer3(NCbytes* buf, int alignment)
  208. {
  209. int pad;
  210. unsigned long len;
  211. if(buf == NULL) return 0;
  212. len = ncbyteslength(buf);
  213. pad = nccpadding(len,alignment);
  214. #ifdef TEST
  215. for(;pad > 0;pad--)
  216. ncbytesappend(buf,0x3a); /* 0x3a was chosen at random */
  217. #else
  218. ncbytessetlength(buf,len+pad);
  219. #endif
  220. return 1;
  221. }
  222. size_t
  223. dimproduct3(NClist* dimensions)
  224. {
  225. size_t size = 1;
  226. unsigned int i;
  227. if(dimensions == NULL) return size;
  228. for(i=0;i<nclistlength(dimensions);i++) {
  229. CDFnode* dim = (CDFnode*)nclistget(dimensions,i);
  230. size *= dim->dim.declsize;
  231. }
  232. return size;
  233. }
  234. /* Return value of param or NULL if not found */
  235. const char*
  236. paramvalue34(NCDAPCOMMON* nccomm, const char* key)
  237. {
  238. const char* value;
  239. if(nccomm == NULL || key == NULL) return 0;
  240. if(!nc_urilookup(nccomm->oc.url,key,&value))
  241. return NULL;
  242. return value;
  243. }
  244. static const char* checkseps = "+,:;";
  245. /* Search for substring in value of param. If substring == NULL; then just
  246. check if param is defined.
  247. */
  248. int
  249. paramcheck34(NCDAPCOMMON* nccomm, const char* key, const char* subkey)
  250. {
  251. const char* value;
  252. char* p;
  253. if(nccomm == NULL || key == NULL) return 0;
  254. if(!nc_urilookup(nccomm->oc.url,key,&value))
  255. return 0;
  256. if(subkey == NULL) return 1;
  257. p = strstr(value,subkey);
  258. if(p == NULL) return 0;
  259. p += strlen(subkey);
  260. if(*p != '\0' && strchr(checkseps,*p) == NULL) return 0;
  261. return 1;
  262. }
  263. /* This is NOT UNION */
  264. int
  265. nclistconcat(NClist* l1, NClist* l2)
  266. {
  267. unsigned int i;
  268. for(i=0;i<nclistlength(l2);i++) nclistpush(l1,nclistget(l2,i));
  269. return 1;
  270. }
  271. int
  272. nclistminus(NClist* l1, NClist* l2)
  273. {
  274. unsigned int i,len,found;
  275. len = nclistlength(l2);
  276. found = 0;
  277. for(i=0;i<len;i++) {
  278. if(nclistdeleteall(l1,nclistget(l2,i))) found = 1;
  279. }
  280. return found;
  281. }
  282. int
  283. nclistdeleteall(NClist* l, ncelem elem)
  284. {
  285. int i; /* do not make unsigned */
  286. unsigned int len,found;
  287. found = 0;
  288. len = nclistlength(l);
  289. for(i=len-1;i>=0;i--) {
  290. ncelem test = nclistget(l,i);
  291. if(test==elem) {
  292. nclistremove(l,i);
  293. found=1;
  294. }
  295. }
  296. return found;
  297. }
  298. /* Collect the set of container nodes ending in "container"*/
  299. void
  300. collectnodepath3(CDFnode* node, NClist* path, int withdataset)
  301. {
  302. if(node == NULL) return;
  303. nclistpush(path,(ncelem)node);
  304. while(node->container != NULL) {
  305. node = node->container;
  306. if(!withdataset && node->nctype == NC_Dataset) break;
  307. nclistinsert(path,0,(ncelem)node);
  308. }
  309. }
  310. /* Like collectnodepath3, but in ocspace */
  311. void
  312. collectocpath(OCconnection conn, OCobject node, NClist* path)
  313. {
  314. OCobject container;
  315. OCtype octype;
  316. if(node == OCNULL) return;
  317. oc_inq_class(conn,node,&octype);
  318. if(octype == OC_Dataset) return;
  319. oc_inq_container(conn,node,&container);
  320. if(container != OCNULL)
  321. collectocpath(conn,container,path);
  322. nclistpush(path,(ncelem)node);
  323. }
  324. char*
  325. makeocpathstring3(OCconnection conn, OCobject node, const char* sep)
  326. {
  327. int slen,i,len,first,seplen;
  328. char* pathname;
  329. OCtype octype;
  330. NClist* ocpath = nclistnew();
  331. collectocpath(conn,node,ocpath);
  332. len = nclistlength(ocpath);
  333. assert(len > 0); /* dataset at least */
  334. oc_inq_type(conn,node,&octype);
  335. if(octype == OC_Dataset)
  336. {pathname = nulldup(""); goto done;} /* Dataset */
  337. seplen = strlen(sep);
  338. for(slen=0,i=0;i<len;i++) {
  339. OCobject node = (OCobject)nclistget(ocpath,i);
  340. char* name;
  341. oc_inq_type(conn,node,&octype);
  342. if(octype == OC_Dataset) continue;
  343. oc_inq_name(conn,node,&name);
  344. slen += (name == NULL? 0 : strlen(name));
  345. slen += seplen;
  346. nullfree(name);
  347. }
  348. slen += 1; /* for null terminator*/
  349. pathname = (char*)malloc(slen);
  350. MEMCHECK(pathname,NULL);
  351. pathname[0] = '\0';
  352. for(first=1,i=0;i<len;i++) {
  353. OCobject node = (OCobject)nclistget(ocpath,i);
  354. char* name;
  355. oc_inq_type(conn,node,&octype);
  356. if(octype == OC_Dataset) continue;
  357. oc_inq_name(conn,node,&name);
  358. if(!first) strcat(pathname,sep);
  359. if(name != NULL) strcat(pathname,name);
  360. nullfree(name);
  361. first = 0;
  362. }
  363. done:
  364. nclistfree(ocpath);
  365. return pathname;
  366. }
  367. char*
  368. makepathstring3(NClist* path, const char* separator, int flags)
  369. {
  370. int slen,i,len,first,seplen;
  371. char* pathname;
  372. len = nclistlength(path);
  373. ASSERT(len > 0); /* dataset at least */
  374. seplen = strlen(separator);
  375. ASSERT(seplen > 0);
  376. for(slen=0,i=0;i<len;i++) {
  377. CDFnode* node = (CDFnode*)nclistget(path,i);
  378. if(node->nctype == NC_Dataset) continue;
  379. slen += strlen(node->ncbasename);
  380. slen += seplen;
  381. }
  382. slen += 1; /* for null terminator*/
  383. pathname = (char*)malloc(slen);
  384. MEMCHECK(pathname,NULL);
  385. pathname[0] = '\0';
  386. for(first=1,i=0;i<len;i++) {
  387. CDFnode* node = (CDFnode*)nclistget(path,i);
  388. char* name;
  389. if(!node->elided || (flags & PATHELIDE)==0) {
  390. if(node->nctype != NC_Dataset) {
  391. name = node->ncbasename;
  392. if(!first) strcat(pathname,separator);
  393. strcat(pathname,name);
  394. first = 0;
  395. }
  396. }
  397. }
  398. return pathname;
  399. }
  400. /* convert path to string using the ncname field */
  401. char*
  402. makecdfpathstring3(CDFnode* var, const char* separator)
  403. {
  404. char* spath;
  405. NClist* path = nclistnew();
  406. collectnodepath3(var,path,WITHDATASET); /* <= note */
  407. spath = makepathstring3(path,separator,PATHNC);
  408. nclistfree(path);
  409. return spath;
  410. }
  411. /* Collect the set names of container nodes ending in "container"*/
  412. void
  413. clonenodenamepath3(CDFnode* node, NClist* path, int withdataset)
  414. {
  415. if(node == NULL) return;
  416. /* stop at the dataset container as well*/
  417. if(node->nctype != NC_Dataset)
  418. clonenodenamepath3(node->container,path,withdataset);
  419. if(node->nctype != NC_Dataset || withdataset)
  420. nclistpush(path,(ncelem)nulldup(node->ncbasename));
  421. }
  422. char*
  423. simplepathstring3(NClist* names, char* separator)
  424. {
  425. int i;
  426. size_t len;
  427. char* result;
  428. if(nclistlength(names) == 0) return nulldup("");
  429. for(len=0,i=0;i<nclistlength(names);i++) {
  430. char* name = (char*)nclistget(names,i);
  431. len += strlen(name);
  432. len += strlen(separator);
  433. }
  434. len++; /* null terminator */
  435. result = (char*)malloc(len);
  436. result[0] = '\0';
  437. for(i=0;i<nclistlength(names);i++) {
  438. char* segment = (char*)nclistget(names,i);
  439. if(i > 0) strcat(result,separator);
  440. strcat(result,segment);
  441. }
  442. return result;
  443. }
  444. /* Define a number of location tests */
  445. /* Is node contained (transitively) in a sequence ? */
  446. BOOL
  447. dapinsequence(CDFnode* node)
  448. {
  449. if(node == NULL || node->container == NULL) return TRUE;
  450. for(node=node->container;node->nctype != NC_Dataset;node=node->container) {
  451. if(node->nctype == NC_Sequence) return TRUE;
  452. }
  453. return FALSE;
  454. }
  455. /* Is node a map field of a grid? */
  456. BOOL
  457. dapgridmap(CDFnode* node)
  458. {
  459. if(node != NULL && node->container != NULL
  460. && node->container->nctype == NC_Grid) {
  461. CDFnode* array = (CDFnode*)nclistget(node->container->subnodes,0);
  462. return (node != array);
  463. }
  464. return FALSE;
  465. }
  466. /* Is node an array field of a grid? */
  467. BOOL
  468. dapgridarray(CDFnode* node)
  469. {
  470. if(node != NULL && node->container != NULL
  471. && node->container->nctype == NC_Grid) {
  472. CDFnode* array = (CDFnode*)nclistget(node->container->subnodes,0);
  473. return (node == array);
  474. }
  475. return FALSE;
  476. }
  477. BOOL
  478. dapgridelement(CDFnode* node)
  479. {
  480. return dapgridarray(node)
  481. || dapgridmap(node);
  482. }
  483. /* Is node a top-level grid node? */
  484. BOOL
  485. daptopgrid(CDFnode* grid)
  486. {
  487. if(grid == NULL || grid->nctype != NC_Grid) return FALSE;
  488. return daptoplevel(grid);
  489. }
  490. /* Is node a top-level sequence node? */
  491. BOOL
  492. daptopseq(CDFnode* seq)
  493. {
  494. if(seq == NULL || seq->nctype != NC_Sequence) return FALSE;
  495. return daptoplevel(seq);
  496. }
  497. /* Is node a top-level node? */
  498. BOOL
  499. daptoplevel(CDFnode* node)
  500. {
  501. if(node->container == NULL
  502. || node->container->nctype != NC_Dataset) return FALSE;
  503. return TRUE;
  504. }
  505. unsigned int
  506. modeldecode(int translation, const char* smodel,
  507. const struct NCTMODEL* models,
  508. unsigned int dfalt)
  509. {
  510. for(;models->translation;models++) {
  511. if(translation != models->translation) continue;
  512. if(smodel == models->model
  513. || (models->model != NULL && strcasecmp(smodel,models->model)==0)) {
  514. /* We have a match */
  515. return models->flags;
  516. }
  517. }
  518. return dfalt;
  519. }
  520. unsigned long
  521. getlimitnumber(const char* limit)
  522. {
  523. size_t slen;
  524. unsigned long multiplier = 1;
  525. unsigned long lu;
  526. if(limit == NULL) return 0;
  527. slen = strlen(limit);
  528. if(slen == 0) return 0;
  529. switch (limit[slen-1]) {
  530. case 'G': case 'g': multiplier = GIGBYTE; break;
  531. case 'M': case 'm': multiplier = MEGBYTE; break;
  532. case 'K': case 'k': multiplier = KILBYTE; break;
  533. default: break;
  534. }
  535. sscanf(limit,"%lu",&lu);
  536. return (lu*multiplier);
  537. }
  538. void
  539. dapexpandescapes(char *termstring)
  540. {
  541. char *s, *t, *endp;
  542. /* expand "\" escapes, e.g. "\t" to tab character;
  543. will only shorten string length, never increase it
  544. */
  545. s = termstring;
  546. t = termstring;
  547. while(*t) {
  548. if (*t == '\\') {
  549. t++;
  550. switch (*t) {
  551. case 'a':
  552. *s++ = '\007'; t++; /* will use '\a' when STDC */
  553. break;
  554. case 'b':
  555. *s++ = '\b'; t++;
  556. break;
  557. case 'f':
  558. *s++ = '\f'; t++;
  559. break;
  560. case 'n':
  561. *s++ = '\n'; t++;
  562. break;
  563. case 'r':
  564. *s++ = '\r'; t++;
  565. break;
  566. case 't':
  567. *s++ = '\t'; t++;
  568. break;
  569. case 'v':
  570. *s++ = '\v'; t++;
  571. break;
  572. case '\\':
  573. *s++ = '\\'; t++;
  574. break;
  575. case '?':
  576. *s++ = '\177'; t++;
  577. break;
  578. case 'x':
  579. t++; /* now t points to one or more hex digits */
  580. *s++ = (char) strtol(t, &endp, 16);
  581. t = endp;
  582. break;
  583. case '0':
  584. case '1':
  585. case '2':
  586. case '3':
  587. case '4':
  588. case '5':
  589. case '6':
  590. case '7': {
  591. /* t should now point to 3 octal digits */
  592. int c;
  593. c = t[0];
  594. if(c == 0 || c < '0' || c > '7') goto normal;
  595. c = t[1];
  596. if(c == 0 || c < '0' || c > '7') goto normal;
  597. c = t[2];
  598. if(c == 0 || c < '0' || c > '7') goto normal;
  599. c = ((t[0]-'0')<<6)+((t[1]-'0')<<3)+(t[2]-'0');
  600. *s++ = (char)c;
  601. t += 3;
  602. } break;
  603. default:
  604. if(*t == 0)
  605. *s++ = '\\';
  606. else
  607. *s++ = *t++;
  608. break;
  609. }
  610. } else {
  611. normal: *s++ = *t++;
  612. }
  613. }
  614. *s = '\0';
  615. return;
  616. }
  617. #ifdef HAVE_GETTIMEOFDAY
  618. static struct timeval time0;
  619. static struct timeval time1;
  620. static double
  621. deltatime()
  622. {
  623. double t0, t1;
  624. t0 = ((double)time0.tv_sec);
  625. t0 += ((double)time0.tv_usec) / 1000000.0;
  626. t1 = ((double)time1.tv_sec);
  627. t1 += ((double)time1.tv_usec) / 1000000.0;
  628. return (t1 - t0);
  629. }
  630. #endif
  631. /* Provide a wrapper for oc_fetch so we can log what it does */
  632. OCerror
  633. dap_fetch(NCDAPCOMMON* nccomm, OCconnection conn, const char* ce,
  634. OCdxd dxd, OCobject* rootp)
  635. {
  636. OCerror ocstat;
  637. char* ext;
  638. OCflags flags = 0;
  639. if(dxd == OCDDS) ext = ".dds";
  640. else if(dxd == OCDAS) ext = ".das";
  641. else ext = ".dods";
  642. if(ce != NULL && strlen(ce) == 0)
  643. ce = NULL;
  644. if(FLAGSET(nccomm->controls,NCF_UNCONSTRAINABLE)) {
  645. ce = NULL;
  646. }
  647. if(FLAGSET(nccomm->controls,NCF_ONDISK)) {
  648. flags |= OCONDISK;
  649. }
  650. if(SHOWFETCH) {
  651. /* Build uri string minus the constraint */
  652. char* baseurl = nc_uribuild(nccomm->oc.url,NULL,ext,0);
  653. if(ce == NULL)
  654. LOG1(NCLOGNOTE,"fetch: %s\n",baseurl);
  655. else
  656. LOG2(NCLOGNOTE,"fetch: %s?%s\n",baseurl,ce);
  657. nullfree(baseurl);
  658. #ifdef HAVE_GETTIMEOFDAY
  659. gettimeofday(&time0,NULL);
  660. #endif
  661. }
  662. ocstat = oc_fetchf(conn,ce,dxd,flags,rootp);
  663. if(FLAGSET(nccomm->controls,NCF_SHOWFETCH)) {
  664. #ifdef HAVE_GETTIMEOFDAY
  665. double secs;
  666. gettimeofday(&time1,NULL);
  667. secs = deltatime();
  668. nclog(NCLOGNOTE,"fetch complete: %0.3f secs",secs);
  669. #else
  670. nclog(NCLOGNOTE,"fetch complete.");
  671. #endif
  672. }
  673. #ifdef DEBUG2
  674. fprintf(stderr,"fetch: dds:\n");
  675. oc_dumpnode(conn,*rootp);
  676. #endif
  677. return ocstat;
  678. }
  679. /* Check a name to see if it contains illegal dap characters
  680. */
  681. static char* badchars = "./";
  682. int
  683. dap_badname(char* name)
  684. {
  685. char* p;
  686. if(name == NULL) return 0;
  687. for(p=badchars;*p;p++) {
  688. if(strchr(name,*p) != NULL) return 1;
  689. }
  690. return 0;
  691. }
  692. /* Check a name to see if it contains illegal dap characters
  693. and repair them
  694. */
  695. char*
  696. dap_repairname(char* name)
  697. {
  698. char* newname;
  699. char *p, *q; int c;
  700. if(name == NULL) return NULL;
  701. /* assume that dap_badname was called on this name and returned 1 */
  702. newname = (char*)malloc(1+(3*strlen(name))); /* max needed */
  703. newname[0] = '\0'; /* so we can use strcat */
  704. for(p=name,q=newname;(c=*p);p++) {
  705. if(strchr(badchars,c) != NULL) {
  706. char newchar[8];
  707. snprintf(newchar,sizeof(newchar),"%%%hhx",c);
  708. strcat(newname,newchar);
  709. q += 3; /*strlen(newchar)*/
  710. } else
  711. *q++ = c;
  712. *q = '\0'; /* so we can always do strcat */
  713. }
  714. *q = '\0'; /* ensure trailing null */
  715. return newname;
  716. }