2018-12-07 06:47:47 +08:00
|
|
|
/* Copyright 2018, UCAR/Unidata and OPeNDAP, Inc.
|
2012-08-01 04:34:13 +08:00
|
|
|
See the COPYRIGHT file for more information.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "config.h"
|
|
|
|
|
2017-03-09 08:01:10 +08:00
|
|
|
#include <stdlib.h>
|
2018-12-06 10:20:43 +08:00
|
|
|
#include <stdio.h>
|
2017-03-09 08:01:10 +08:00
|
|
|
#ifdef HAVE_SYS_STAT_H
|
|
|
|
#include <sys/stat.h>
|
2013-05-25 05:19:07 +08:00
|
|
|
#endif
|
2013-05-12 03:36:31 +08:00
|
|
|
|
2012-08-01 04:34:13 +08:00
|
|
|
#include "ocinternal.h"
|
|
|
|
#include "ocdebug.h"
|
|
|
|
|
|
|
|
#define MAXLEVEL 1
|
|
|
|
|
|
|
|
/*Forward*/
|
|
|
|
static void dumpocnode1(OCnode* node, int depth);
|
|
|
|
static void dumpdimensions(OCnode* node);
|
|
|
|
static void dumpattvalue(OCtype nctype, char** aset, int index);
|
|
|
|
|
2019-03-31 04:06:20 +08:00
|
|
|
static const char* sindent =
|
|
|
|
" ";
|
2012-08-01 04:34:13 +08:00
|
|
|
|
2019-03-31 04:06:20 +08:00
|
|
|
static const char*
|
2012-08-01 04:34:13 +08:00
|
|
|
dent(int n)
|
|
|
|
{
|
|
|
|
if(n > 100) n = 100;
|
|
|
|
return sindent+(100-n);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* support [dd] leader*/
|
2019-03-31 04:06:20 +08:00
|
|
|
static const char*
|
2012-08-01 04:34:13 +08:00
|
|
|
dent2(int n) {return dent(n+4);}
|
|
|
|
|
|
|
|
static void
|
2017-03-09 08:01:10 +08:00
|
|
|
tabto(int pos, NCbytes* buffer)
|
2012-08-01 04:34:13 +08:00
|
|
|
{
|
|
|
|
int bol,len,pad;
|
2017-03-09 08:01:10 +08:00
|
|
|
len = ncbyteslength(buffer);
|
2012-08-01 04:34:13 +08:00
|
|
|
/* find preceding newline */
|
|
|
|
for(bol=len-1;;bol--) {
|
2017-03-09 08:01:10 +08:00
|
|
|
int c = ncbytesget(buffer,(size_t)bol);
|
2012-08-01 04:34:13 +08:00
|
|
|
if(c < 0) break;
|
|
|
|
if(c == '\n') {bol++; break;}
|
|
|
|
}
|
|
|
|
len = (len - bol);
|
|
|
|
pad = (pos - len);
|
2017-03-09 08:01:10 +08:00
|
|
|
while(pad-- > 0) ncbytescat(buffer," ");
|
2012-08-01 04:34:13 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
ocdumpnode(OCnode* node)
|
|
|
|
{
|
|
|
|
if(node != NULL) {
|
|
|
|
dumpocnode1(node,0);
|
|
|
|
} else {
|
|
|
|
fprintf(stdout,"<NULL>\n");
|
|
|
|
}
|
|
|
|
fflush(stdout);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
dumpocnode1(OCnode* node, int depth)
|
|
|
|
{
|
|
|
|
unsigned int n;
|
|
|
|
switch (node->octype) {
|
|
|
|
case OC_Atomic: {
|
|
|
|
fprintf(stdout,"[%2d]%s ",depth,dent(depth));
|
|
|
|
if(node->name == NULL) OCPANIC("prim without name");
|
|
|
|
fprintf(stdout,"%s %s",octypetostring(node->etype),node->name);
|
|
|
|
dumpdimensions(node);
|
2021-12-24 13:18:56 +08:00
|
|
|
fprintf(stdout," &%p",node);
|
2012-08-01 04:34:13 +08:00
|
|
|
fprintf(stdout,"\n");
|
|
|
|
} break;
|
|
|
|
|
|
|
|
case OC_Dataset: {
|
|
|
|
fprintf(stdout,"[%2d]%s ",depth,dent(depth));
|
|
|
|
fprintf(stdout,"dataset %s\n",
|
|
|
|
(node->name?node->name:""));
|
2017-03-09 08:01:10 +08:00
|
|
|
for(n=0;n<nclistlength(node->subnodes);n++) {
|
|
|
|
dumpocnode1((OCnode*)nclistget(node->subnodes,n),depth+1);
|
2012-08-01 04:34:13 +08:00
|
|
|
}
|
|
|
|
} break;
|
|
|
|
|
|
|
|
case OC_Structure: {
|
|
|
|
fprintf(stdout,"[%2d]%s ",depth,dent(depth));
|
|
|
|
fprintf(stdout,"struct %s",
|
|
|
|
(node->name?node->name:""));
|
|
|
|
dumpdimensions(node);
|
2021-12-24 13:18:56 +08:00
|
|
|
fprintf(stdout," &%p",node);
|
2012-08-01 04:34:13 +08:00
|
|
|
fprintf(stdout,"\n");
|
2017-03-09 08:01:10 +08:00
|
|
|
for(n=0;n<nclistlength(node->subnodes);n++) {
|
|
|
|
dumpocnode1((OCnode*)nclistget(node->subnodes,n),depth+1);
|
2012-08-01 04:34:13 +08:00
|
|
|
}
|
|
|
|
} break;
|
|
|
|
|
|
|
|
case OC_Sequence: {
|
|
|
|
fprintf(stdout,"[%2d]%s ",depth,dent(depth));
|
|
|
|
fprintf(stdout,"sequence %s",
|
|
|
|
(node->name?node->name:""));
|
|
|
|
dumpdimensions(node);
|
2021-12-24 13:18:56 +08:00
|
|
|
fprintf(stdout," &%p",node);
|
2012-08-01 04:34:13 +08:00
|
|
|
fprintf(stdout,"\n");
|
2017-03-09 08:01:10 +08:00
|
|
|
for(n=0;n<nclistlength(node->subnodes);n++) {
|
|
|
|
dumpocnode1((OCnode*)nclistget(node->subnodes,n),depth+1);
|
2012-08-01 04:34:13 +08:00
|
|
|
}
|
|
|
|
} break;
|
|
|
|
|
|
|
|
case OC_Grid: {
|
|
|
|
unsigned int i;
|
|
|
|
fprintf(stdout,"[%2d]%s ",depth,dent(depth));
|
|
|
|
fprintf(stdout,"grid %s",
|
|
|
|
(node->name?node->name:""));
|
|
|
|
dumpdimensions(node);
|
2021-12-24 13:18:56 +08:00
|
|
|
fprintf(stdout," &%p",node);
|
2012-08-01 04:34:13 +08:00
|
|
|
fprintf(stdout,"\n");
|
|
|
|
fprintf(stdout,"%sarray:\n",dent2(depth+1));
|
2017-03-09 08:01:10 +08:00
|
|
|
dumpocnode1((OCnode*)nclistget(node->subnodes,0),depth+2);
|
2012-08-01 04:34:13 +08:00
|
|
|
fprintf(stdout,"%smaps:\n",dent2(depth+1));
|
2017-03-09 08:01:10 +08:00
|
|
|
for(i=1;i<nclistlength(node->subnodes);i++) {
|
|
|
|
dumpocnode1((OCnode*)nclistget(node->subnodes,i),depth+2);
|
2012-08-01 04:34:13 +08:00
|
|
|
}
|
|
|
|
} break;
|
|
|
|
|
|
|
|
case OC_Attribute: {
|
|
|
|
fprintf(stdout,"[%2d]%s ",depth,dent(depth));
|
|
|
|
if(node->name == NULL) OCPANIC("Attribute without name");
|
|
|
|
fprintf(stdout,"%s %s",octypetostring(node->etype),node->name);
|
2017-03-09 08:01:10 +08:00
|
|
|
for(n=0;n<nclistlength(node->att.values);n++) {
|
|
|
|
char* value = (char*)nclistget(node->att.values,n);
|
2012-08-01 04:34:13 +08:00
|
|
|
if(n > 0) fprintf(stdout,",");
|
|
|
|
fprintf(stdout," %s",value);
|
|
|
|
}
|
2021-12-24 13:18:56 +08:00
|
|
|
fprintf(stdout," &%p",node);
|
2012-08-01 04:34:13 +08:00
|
|
|
fprintf(stdout,"\n");
|
|
|
|
} break;
|
|
|
|
|
|
|
|
case OC_Attributeset: {
|
|
|
|
fprintf(stdout,"[%2d]%s ",depth,dent(depth));
|
|
|
|
fprintf(stdout,"%s:\n",node->name?node->name:"Attributes");
|
2017-03-09 08:01:10 +08:00
|
|
|
for(n=0;n<nclistlength(node->subnodes);n++) {
|
|
|
|
dumpocnode1((OCnode*)nclistget(node->subnodes,n),depth+1);
|
2012-08-01 04:34:13 +08:00
|
|
|
}
|
|
|
|
} break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
OCPANIC1("encountered unexpected node type: %x",node->octype);
|
|
|
|
}
|
|
|
|
|
|
|
|
if(node->attributes != NULL) {
|
|
|
|
unsigned int i;
|
2017-03-09 08:01:10 +08:00
|
|
|
for(i=0;i<nclistlength(node->attributes);i++) {
|
|
|
|
OCattribute* att = (OCattribute*)nclistget(node->attributes,i);
|
2012-08-01 04:34:13 +08:00
|
|
|
fprintf(stdout,"%s[%s=",dent2(depth+2),att->name);
|
|
|
|
if(att->nvalues == 0)
|
|
|
|
OCPANIC("Attribute.nvalues == 0");
|
|
|
|
if(att->nvalues == 1) {
|
|
|
|
dumpattvalue(att->etype,att->values,0);
|
|
|
|
} else {
|
2014-03-09 11:41:30 +08:00
|
|
|
int j;
|
2012-08-01 04:34:13 +08:00
|
|
|
fprintf(stdout,"{");
|
|
|
|
for(j=0;j<att->nvalues;j++) {
|
|
|
|
if(j>0) fprintf(stdout,", ");
|
|
|
|
dumpattvalue(att->etype,att->values,j);
|
|
|
|
}
|
|
|
|
fprintf(stdout,"}");
|
|
|
|
}
|
|
|
|
fprintf(stdout,"]\n");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
dumpdimensions(OCnode* node)
|
|
|
|
{
|
|
|
|
unsigned int i;
|
|
|
|
for(i=0;i<node->array.rank;i++) {
|
2017-03-09 08:01:10 +08:00
|
|
|
OCnode* dim = (OCnode*)nclistget(node->array.dimensions,i);
|
2012-08-01 04:34:13 +08:00
|
|
|
fprintf(stdout,"[%s=%lu]",
|
|
|
|
(dim->name?dim->name:"?"),
|
|
|
|
(unsigned long)dim->dim.declsize);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
dumpattvalue(OCtype nctype, char** strings, int index)
|
|
|
|
{
|
|
|
|
if(nctype == OC_String || nctype == OC_URL) {
|
|
|
|
fprintf(stdout,"\"%s\"",strings[index]);
|
|
|
|
} else {
|
|
|
|
fprintf(stdout,"%s",strings[index]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
ocdumpslice(OCslice* slice)
|
|
|
|
{
|
|
|
|
fprintf(stdout,"[");
|
|
|
|
fprintf(stdout,"%lu",(unsigned long)slice->first);
|
|
|
|
if(slice->stride > 1) fprintf(stdout,":%lu",(unsigned long)slice->stride);
|
|
|
|
fprintf(stdout,":%lu",(unsigned long)(slice->first+slice->count)-1);
|
|
|
|
fprintf(stdout,"]");
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
ocdumpclause(OCprojectionclause* ref)
|
|
|
|
{
|
|
|
|
unsigned int i;
|
2017-03-09 08:01:10 +08:00
|
|
|
NClist* path = nclistnew();
|
2012-08-01 04:34:13 +08:00
|
|
|
occollectpathtonode(ref->node,path);
|
2017-03-09 08:01:10 +08:00
|
|
|
for(i=0;i<nclistlength(path);i++) {
|
|
|
|
NClist* sliceset;
|
|
|
|
OCnode* node = (OCnode*)nclistget(path,i);
|
2012-08-01 04:34:13 +08:00
|
|
|
if(node->tree != NULL) continue; /* leave off the root node*/
|
|
|
|
fprintf(stdout,"%s%s",(i>0?PATHSEPARATOR:""),node->name);
|
2017-03-09 08:01:10 +08:00
|
|
|
sliceset = (NClist*)nclistget(ref->indexsets,i);
|
2012-08-01 04:34:13 +08:00
|
|
|
if(sliceset != NULL) {
|
|
|
|
unsigned int j;
|
2017-03-09 08:01:10 +08:00
|
|
|
for(j=0;j<nclistlength(sliceset);j++) {
|
|
|
|
OCslice* slice = (OCslice*)nclistget(sliceset,j);
|
2012-08-01 04:34:13 +08:00
|
|
|
ocdumpslice(slice);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static void
|
2017-11-24 01:55:24 +08:00
|
|
|
addfield(char* field, size_t llen, char* line, int align)
|
2012-08-01 04:34:13 +08:00
|
|
|
{
|
|
|
|
int len,rem;
|
2017-11-24 01:55:24 +08:00
|
|
|
strlcat(line,"|",llen);
|
|
|
|
strlcat(line,field,llen);
|
2012-08-01 04:34:13 +08:00
|
|
|
len = strlen(field);
|
|
|
|
rem = (align - len);
|
2017-11-24 01:55:24 +08:00
|
|
|
while(rem-- > 0) strlcat(line," ",llen);
|
2012-08-01 04:34:13 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2014-03-09 11:41:30 +08:00
|
|
|
dumpfield(size_t index, char* n8, int isxdr)
|
2012-08-01 04:34:13 +08:00
|
|
|
{
|
|
|
|
char line[1024];
|
|
|
|
char tmp[32];
|
|
|
|
|
|
|
|
union {
|
|
|
|
unsigned int uv;
|
|
|
|
int sv;
|
|
|
|
char cv[4];
|
|
|
|
float fv;
|
|
|
|
} form;
|
|
|
|
union {
|
|
|
|
char cv[8];
|
|
|
|
unsigned long long ll;
|
|
|
|
double d;
|
|
|
|
} dform;
|
|
|
|
|
|
|
|
line[0] = '\0';
|
|
|
|
|
|
|
|
/* offset */
|
2023-05-01 06:01:49 +08:00
|
|
|
snprintf(tmp,sizeof(tmp),"%6zd",index);
|
2017-11-24 01:55:24 +08:00
|
|
|
addfield(tmp,sizeof(line),line,5);
|
2012-08-01 04:34:13 +08:00
|
|
|
|
|
|
|
memcpy(form.cv,n8,4);
|
|
|
|
|
|
|
|
/* straight hex*/
|
2023-05-01 06:01:49 +08:00
|
|
|
snprintf(tmp,sizeof(tmp),"%08x",form.uv);
|
2017-11-24 01:55:24 +08:00
|
|
|
addfield(tmp,sizeof(line),line,8);
|
2012-08-01 04:34:13 +08:00
|
|
|
|
|
|
|
if(isxdr) {swapinline32(&form.uv);}
|
|
|
|
|
|
|
|
/* unsigned integer */
|
2023-05-01 06:01:49 +08:00
|
|
|
snprintf(tmp,sizeof(tmp),"%12u",form.uv);
|
2017-11-24 01:55:24 +08:00
|
|
|
addfield(tmp,sizeof(line),line,12);
|
2012-08-01 04:34:13 +08:00
|
|
|
|
|
|
|
/* signed integer */
|
2023-05-01 06:01:49 +08:00
|
|
|
snprintf(tmp,sizeof(tmp),"%12d",form.sv);
|
2017-11-24 01:55:24 +08:00
|
|
|
addfield(tmp,sizeof(line),line,12);
|
2012-08-01 04:34:13 +08:00
|
|
|
|
|
|
|
/* float */
|
2023-05-01 06:01:49 +08:00
|
|
|
snprintf(tmp,sizeof(tmp),"%#g",form.fv);
|
2017-11-24 01:55:24 +08:00
|
|
|
addfield(tmp,sizeof(line),line,12);
|
2012-08-01 04:34:13 +08:00
|
|
|
|
|
|
|
/* char[4] */
|
|
|
|
{
|
|
|
|
/* use raw form (i.e. n8)*/
|
|
|
|
int i;
|
|
|
|
tmp[0] = '\0';
|
|
|
|
for(i=0;i<4;i++) {
|
|
|
|
char stmp[64];
|
|
|
|
unsigned int c = (n8[i] & 0xff);
|
|
|
|
if(c < ' ' || c > 126)
|
2012-12-19 05:08:23 +08:00
|
|
|
snprintf(stmp,sizeof(stmp),"\\%02x",c);
|
2012-08-01 04:34:13 +08:00
|
|
|
else
|
2012-12-19 05:08:23 +08:00
|
|
|
snprintf(stmp,sizeof(stmp),"%c",c);
|
2021-05-15 01:33:03 +08:00
|
|
|
strlcat(tmp,stmp,sizeof(tmp));
|
2012-08-01 04:34:13 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-11-24 01:55:24 +08:00
|
|
|
addfield(tmp,sizeof(line),line,16);
|
2012-08-01 04:34:13 +08:00
|
|
|
|
|
|
|
/* double */
|
2014-03-09 11:41:30 +08:00
|
|
|
memcpy(dform.cv,n8,(size_t)(2*XDRUNIT));
|
2012-08-01 04:34:13 +08:00
|
|
|
if(isxdr) xxdrntohdouble(dform.cv,&dform.d);
|
2023-05-01 06:01:49 +08:00
|
|
|
snprintf(tmp,sizeof(tmp),"%#g",dform.d);
|
2017-11-24 01:55:24 +08:00
|
|
|
addfield(tmp,sizeof(line),line,12);
|
2012-08-01 04:34:13 +08:00
|
|
|
|
|
|
|
fprintf(stdout,"%s\n",line);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
typedmemorydump(char* memory, size_t len, int fromxdr)
|
|
|
|
{
|
|
|
|
unsigned int i,count,rem;
|
|
|
|
char line[1024];
|
|
|
|
char* pmem;
|
|
|
|
char mem[8];
|
|
|
|
|
|
|
|
assert(memory[len] == 0);
|
|
|
|
|
|
|
|
/* build the header*/
|
|
|
|
line[0] = '\0';
|
2017-11-24 01:55:24 +08:00
|
|
|
addfield("offset",sizeof(line),line,6);
|
|
|
|
addfield("hex",sizeof(line),line,8);
|
|
|
|
addfield("uint",sizeof(line),line,12);
|
|
|
|
addfield("int",sizeof(line),line,12);
|
|
|
|
addfield("float",sizeof(line),line,12);
|
|
|
|
addfield("char[4]",sizeof(line),line,16);
|
|
|
|
addfield("double",sizeof(line),line,12);
|
|
|
|
strlcat(line,"\n",sizeof(line));
|
2012-08-01 04:34:13 +08:00
|
|
|
fprintf(stdout,"%s",line);
|
|
|
|
|
|
|
|
count = (len / sizeof(int));
|
|
|
|
rem = (len % sizeof(int));
|
|
|
|
|
|
|
|
for(pmem=memory,i=0;i<count;i++,pmem+=4) {
|
|
|
|
memset(mem,0,8);
|
|
|
|
if(i<(count-1))
|
|
|
|
memcpy(mem,pmem,8);
|
|
|
|
else
|
|
|
|
memcpy(mem,pmem,4);
|
|
|
|
dumpfield(i*sizeof(unsigned int),mem,fromxdr);
|
|
|
|
}
|
|
|
|
if(rem > 0) {
|
|
|
|
memset(mem,0,8);
|
|
|
|
memcpy(mem,pmem,4);
|
|
|
|
dumpfield(i*sizeof(unsigned int),mem,fromxdr);
|
|
|
|
}
|
|
|
|
fflush(stdout);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
simplememorydump(char* memory, size_t len, int fromxdr)
|
|
|
|
{
|
|
|
|
unsigned int i,count,rem;
|
|
|
|
int* imemory;
|
|
|
|
char tmp[32];
|
|
|
|
char line[1024];
|
|
|
|
|
|
|
|
assert(memory[len] == 0);
|
|
|
|
|
|
|
|
/* build the header*/
|
|
|
|
line[0] = '\0';
|
2017-11-24 01:55:24 +08:00
|
|
|
addfield("offset",sizeof(line),line,6);
|
|
|
|
addfield("XDR (hex)",sizeof(line),line,9);
|
|
|
|
addfield("!XDR (hex)",sizeof(line),line,10);
|
2012-08-01 04:34:13 +08:00
|
|
|
fprintf(stdout,"%s\n",line);
|
|
|
|
|
|
|
|
count = (len / sizeof(int));
|
|
|
|
rem = (len % sizeof(int));
|
|
|
|
if(rem != 0)
|
|
|
|
fprintf(stderr,"ocdump: |mem|%%4 != 0\n");
|
|
|
|
imemory = (int*)memory;
|
|
|
|
|
|
|
|
for(i=0;i<count;i++) {
|
|
|
|
unsigned int vx = (unsigned int)imemory[i];
|
|
|
|
unsigned int v = vx;
|
|
|
|
if(!xxdr_network_order) swapinline32(&v);
|
|
|
|
line[0] = '\0';
|
2023-05-01 06:01:49 +08:00
|
|
|
snprintf(tmp,sizeof(tmp),"%6d",i);
|
2017-11-24 01:55:24 +08:00
|
|
|
addfield(tmp,sizeof(line),line,6);
|
2023-05-01 06:01:49 +08:00
|
|
|
snprintf(tmp,sizeof(tmp),"%08x",vx);
|
2017-11-24 01:55:24 +08:00
|
|
|
addfield(tmp,sizeof(line),line,9);
|
2023-05-01 06:01:49 +08:00
|
|
|
snprintf(tmp,sizeof(tmp),"%08x",v);
|
2017-11-24 01:55:24 +08:00
|
|
|
addfield(tmp,sizeof(line),line,10);
|
2012-08-01 04:34:13 +08:00
|
|
|
fprintf(stdout,"%s\n",line);
|
|
|
|
}
|
|
|
|
fflush(stdout);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
ocdumpmemory(char* memory, size_t len, int xdrencoded, int level)
|
|
|
|
{
|
|
|
|
if(level > MAXLEVEL) level = MAXLEVEL;
|
|
|
|
switch (level) {
|
|
|
|
case 1: /* Do a multi-type dump */
|
|
|
|
typedmemorydump(memory,len,xdrencoded);
|
|
|
|
break;
|
|
|
|
case 0: /* Dump a simple linear list of the contents of the memory as 32-bit hex and decimal */
|
|
|
|
default:
|
|
|
|
simplememorydump(memory,len,xdrencoded);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-11-22 07:20:44 +08:00
|
|
|
static OCerror
|
2014-03-09 11:41:30 +08:00
|
|
|
ocreadfile(FILE* file, off_t datastart, char** memp, size_t* lenp)
|
2012-08-01 04:34:13 +08:00
|
|
|
{
|
2014-08-23 05:33:45 +08:00
|
|
|
char* mem = NULL;
|
2012-08-01 04:34:13 +08:00
|
|
|
size_t len;
|
2014-03-09 11:41:30 +08:00
|
|
|
long red;
|
2012-08-01 04:34:13 +08:00
|
|
|
struct stat stats;
|
2014-03-09 11:41:30 +08:00
|
|
|
long pos;
|
2014-11-22 07:20:44 +08:00
|
|
|
OCerror stat = OC_NOERR;
|
2012-08-01 04:34:13 +08:00
|
|
|
|
|
|
|
pos = ftell(file);
|
2016-03-09 04:48:30 +08:00
|
|
|
if(pos < 0) {
|
|
|
|
fprintf(stderr,"ocreadfile: ftell error.\n");
|
|
|
|
stat = OC_ERCFILE;
|
|
|
|
goto done;
|
|
|
|
}
|
|
|
|
|
2012-08-01 04:34:13 +08:00
|
|
|
fseek(file,0,SEEK_SET);
|
2014-08-23 05:25:27 +08:00
|
|
|
if(fseek(file,(long)datastart,SEEK_SET) < 0) {
|
2014-11-22 07:20:44 +08:00
|
|
|
fprintf(stderr,"ocreadfile: fseek error.\n");
|
|
|
|
stat = OC_ERCFILE;
|
|
|
|
goto done;
|
2014-08-23 05:25:27 +08:00
|
|
|
}
|
2012-08-01 04:34:13 +08:00
|
|
|
|
2014-08-26 00:26:57 +08:00
|
|
|
if(fstat(fileno(file),&stats) < 0) {
|
2014-11-22 07:20:44 +08:00
|
|
|
fprintf(stderr,"ocreadfile: fstat error.\n");
|
|
|
|
stat = OC_ERCFILE;
|
|
|
|
goto done;
|
2014-08-26 00:26:57 +08:00
|
|
|
}
|
2012-08-01 04:34:13 +08:00
|
|
|
len = stats.st_size;
|
|
|
|
len -= datastart;
|
2016-03-09 04:48:30 +08:00
|
|
|
|
2012-08-01 04:34:13 +08:00
|
|
|
mem = (char*)calloc(len+1,1);
|
2014-11-22 07:20:44 +08:00
|
|
|
if(mem == NULL) {stat = OC_ENOMEM; goto done;}
|
2012-08-01 04:34:13 +08:00
|
|
|
|
|
|
|
/* Read only the data part */
|
|
|
|
red = fread(mem,1,len,file);
|
|
|
|
if(red < len) {
|
2014-11-22 07:20:44 +08:00
|
|
|
fprintf(stderr,"ocreadfile: short file\n");
|
|
|
|
stat = OC_ERCFILE;
|
|
|
|
goto done;
|
2016-03-09 04:48:30 +08:00
|
|
|
}
|
|
|
|
|
2014-11-22 07:20:44 +08:00
|
|
|
if(fseek(file,pos,SEEK_SET) < 0) {; /* leave it as we found it*/
|
2016-03-12 02:03:01 +08:00
|
|
|
fprintf(stderr,"ocreadfile: fseek error.\n");
|
|
|
|
stat = OC_ERCFILE;
|
|
|
|
goto done;
|
2014-08-23 05:25:27 +08:00
|
|
|
}
|
2014-11-22 07:20:44 +08:00
|
|
|
if(memp) {*memp = mem; mem = NULL;}
|
2016-03-09 04:48:30 +08:00
|
|
|
if(lenp) *lenp = len;
|
2014-08-23 05:33:45 +08:00
|
|
|
|
2016-03-12 02:03:01 +08:00
|
|
|
done:
|
2014-11-22 07:20:44 +08:00
|
|
|
if(mem != NULL)
|
2016-03-12 02:03:01 +08:00
|
|
|
free(mem);
|
2014-11-22 07:20:44 +08:00
|
|
|
return OCTHROW(stat);
|
2012-08-01 04:34:13 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
ocdd(OCstate* state, OCnode* root, int xdrencoded, int level)
|
|
|
|
{
|
|
|
|
char* mem;
|
|
|
|
size_t len;
|
|
|
|
if(root->tree->data.file != NULL) {
|
2014-03-09 11:41:30 +08:00
|
|
|
if(!ocreadfile(root->tree->data.file,
|
|
|
|
root->tree->data.bod,
|
|
|
|
&mem,
|
|
|
|
&len)) {
|
2016-03-12 02:03:01 +08:00
|
|
|
/* ocreadfile allocates memory that must be freed. */
|
|
|
|
if(mem != NULL) free(mem);
|
|
|
|
fprintf(stderr,"ocdd could not read data file\n");
|
|
|
|
return;
|
|
|
|
}
|
2012-08-01 04:34:13 +08:00
|
|
|
ocdumpmemory(mem,len,xdrencoded,level);
|
|
|
|
free(mem);
|
|
|
|
} else {
|
|
|
|
mem = root->tree->data.memory;
|
|
|
|
mem += root->tree->data.bod;
|
|
|
|
len = root->tree->data.datasize;
|
|
|
|
len -= root->tree->data.bod;
|
|
|
|
ocdumpmemory(mem,len,xdrencoded,level);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2017-03-09 08:01:10 +08:00
|
|
|
ocdumpdata(OCstate* state, OCdata* data, NCbytes* buffer, int frominstance)
|
2012-08-01 04:34:13 +08:00
|
|
|
{
|
|
|
|
char tmp[1024];
|
2015-01-16 05:19:51 +08:00
|
|
|
OCnode* pattern = data->pattern;
|
2019-03-31 04:06:20 +08:00
|
|
|
char* smode = NULL;
|
|
|
|
|
2021-12-24 13:18:56 +08:00
|
|
|
snprintf(tmp,sizeof(tmp),"%p:",data);
|
2017-03-09 08:01:10 +08:00
|
|
|
ncbytescat(buffer,tmp);
|
2012-08-01 04:34:13 +08:00
|
|
|
if(!frominstance) {
|
2017-03-09 08:01:10 +08:00
|
|
|
ncbytescat(buffer," node=");
|
|
|
|
ncbytescat(buffer,pattern->name);
|
2012-08-01 04:34:13 +08:00
|
|
|
}
|
|
|
|
snprintf(tmp,sizeof(tmp)," xdroffset=%ld",(unsigned long)data->xdroffset);
|
2017-03-09 08:01:10 +08:00
|
|
|
ncbytescat(buffer,tmp);
|
2015-01-16 05:19:51 +08:00
|
|
|
if(data->pattern->octype == OC_Atomic) {
|
2012-08-01 04:34:13 +08:00
|
|
|
snprintf(tmp,sizeof(tmp)," xdrsize=%ld",(unsigned long)data->xdrsize);
|
2017-03-09 08:01:10 +08:00
|
|
|
ncbytescat(buffer,tmp);
|
2012-08-01 04:34:13 +08:00
|
|
|
}
|
2015-01-16 05:19:51 +08:00
|
|
|
if(ociscontainer(pattern->octype)) {
|
2012-08-01 04:34:13 +08:00
|
|
|
snprintf(tmp,sizeof(tmp)," ninstances=%d",(int)data->ninstances);
|
2017-03-09 08:01:10 +08:00
|
|
|
ncbytescat(buffer,tmp);
|
2015-01-16 05:19:51 +08:00
|
|
|
} else if(pattern->etype == OC_String || pattern->etype == OC_URL) {
|
2012-08-01 04:34:13 +08:00
|
|
|
snprintf(tmp,sizeof(tmp)," nstrings=%d",(int)data->nstrings);
|
2017-03-09 08:01:10 +08:00
|
|
|
ncbytescat(buffer,tmp);
|
2012-08-01 04:34:13 +08:00
|
|
|
}
|
2017-03-09 08:01:10 +08:00
|
|
|
ncbytescat(buffer," container=");
|
2021-12-24 13:18:56 +08:00
|
|
|
snprintf(tmp,sizeof(tmp),"%p",data->container);
|
2017-03-09 08:01:10 +08:00
|
|
|
ncbytescat(buffer,tmp);
|
|
|
|
ncbytescat(buffer," mode=");
|
2019-03-31 04:06:20 +08:00
|
|
|
ncbytescat(buffer,(smode=ocdtmodestring(data->datamode,0)));
|
|
|
|
nullfree(smode);
|
2012-08-01 04:34:13 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
Depth Offset Index Flags Size Type Name
|
|
|
|
0123456789012345678901234567890123456789012345
|
|
|
|
0 1 2 3 4
|
|
|
|
[001] 00000000 0000 FS 0000 Structure person
|
|
|
|
*/
|
|
|
|
static const int tabstops[] = {0,6,15,21,27,32,42};
|
|
|
|
static const char* header =
|
|
|
|
"Depth Offset Index Flags Size Type Name\n";
|
|
|
|
|
|
|
|
void
|
2017-03-09 08:01:10 +08:00
|
|
|
ocdumpdatatree(OCstate* state, OCdata* data, NCbytes* buffer, int depth)
|
2012-08-01 04:34:13 +08:00
|
|
|
{
|
|
|
|
size_t i,rank;
|
2015-01-16 05:19:51 +08:00
|
|
|
OCnode* pattern;
|
2012-08-01 04:34:13 +08:00
|
|
|
char tmp[1024];
|
|
|
|
size_t crossproduct;
|
|
|
|
int tabstop = 0;
|
|
|
|
const char* typename;
|
2019-03-31 04:06:20 +08:00
|
|
|
char* smode = NULL;
|
2012-08-01 04:34:13 +08:00
|
|
|
|
|
|
|
/* If this is the first call, then dump a header line */
|
|
|
|
if(depth == 0) {
|
2017-03-09 08:01:10 +08:00
|
|
|
ncbytescat(buffer,header);
|
2012-08-01 04:34:13 +08:00
|
|
|
}
|
|
|
|
|
2015-01-16 05:19:51 +08:00
|
|
|
/* get info about the pattern */
|
|
|
|
pattern = data->pattern;
|
|
|
|
rank = pattern->array.rank;
|
2012-08-01 04:34:13 +08:00
|
|
|
|
|
|
|
/* get total dimension size */
|
|
|
|
if(rank > 0)
|
2015-01-16 05:19:51 +08:00
|
|
|
crossproduct = octotaldimsize(pattern->array.rank,pattern->array.sizes);
|
2012-08-01 04:34:13 +08:00
|
|
|
|
|
|
|
/* Dump the depth first */
|
|
|
|
snprintf(tmp,sizeof(tmp),"[%03d]",depth);
|
2017-03-09 08:01:10 +08:00
|
|
|
ncbytescat(buffer,tmp);
|
2012-08-01 04:34:13 +08:00
|
|
|
|
|
|
|
tabto(tabstops[++tabstop],buffer);
|
|
|
|
|
|
|
|
snprintf(tmp,sizeof(tmp),"%08lu",(unsigned long)data->xdroffset);
|
2017-03-09 08:01:10 +08:00
|
|
|
ncbytescat(buffer,tmp);
|
2012-08-01 04:34:13 +08:00
|
|
|
|
|
|
|
tabto(tabstops[++tabstop],buffer);
|
|
|
|
|
|
|
|
/* Dump the Index wrt to parent, if defined */
|
|
|
|
if(fisset(data->datamode,OCDT_FIELD)
|
|
|
|
|| fisset(data->datamode,OCDT_ELEMENT)
|
|
|
|
|| fisset(data->datamode,OCDT_RECORD)) {
|
|
|
|
snprintf(tmp,sizeof(tmp),"%04lu ",(unsigned long)data->index);
|
2017-03-09 08:01:10 +08:00
|
|
|
ncbytescat(buffer,tmp);
|
2012-08-01 04:34:13 +08:00
|
|
|
}
|
2016-03-09 04:48:30 +08:00
|
|
|
|
2012-08-01 04:34:13 +08:00
|
|
|
tabto(tabstops[++tabstop],buffer);
|
|
|
|
|
|
|
|
/* Dump the mode flags in compact form */
|
2019-03-31 04:06:20 +08:00
|
|
|
ncbytescat(buffer,(smode=ocdtmodestring(data->datamode,1)));
|
|
|
|
nullfree(smode);
|
2016-03-09 04:48:30 +08:00
|
|
|
|
2012-08-01 04:34:13 +08:00
|
|
|
tabto(tabstops[++tabstop],buffer);
|
|
|
|
|
|
|
|
/* Dump the size or ninstances */
|
|
|
|
if(fisset(data->datamode,OCDT_ARRAY)
|
|
|
|
|| fisset(data->datamode,OCDT_SEQUENCE)) {
|
|
|
|
snprintf(tmp,sizeof(tmp),"%04lu",(unsigned long)data->ninstances);
|
|
|
|
} else {
|
|
|
|
snprintf(tmp,sizeof(tmp),"%04lu",(unsigned long)data->xdrsize);
|
|
|
|
}
|
2017-03-09 08:01:10 +08:00
|
|
|
ncbytescat(buffer,tmp);
|
2012-08-01 04:34:13 +08:00
|
|
|
|
|
|
|
tabto(tabstops[++tabstop],buffer);
|
|
|
|
|
2015-01-16 05:19:51 +08:00
|
|
|
if(pattern->octype == OC_Atomic) {
|
|
|
|
typename = octypetoddsstring(pattern->etype);
|
2012-08-01 04:34:13 +08:00
|
|
|
} else { /*must be container*/
|
2015-01-16 05:19:51 +08:00
|
|
|
typename = octypetoddsstring(pattern->octype);
|
2012-08-01 04:34:13 +08:00
|
|
|
}
|
2017-03-09 08:01:10 +08:00
|
|
|
ncbytescat(buffer,typename);
|
2012-08-01 04:34:13 +08:00
|
|
|
|
|
|
|
tabto(tabstops[++tabstop],buffer);
|
|
|
|
|
2021-05-15 01:33:03 +08:00
|
|
|
ncbytescat(buffer,pattern->name);
|
2012-08-01 04:34:13 +08:00
|
|
|
|
|
|
|
if(rank > 0) {
|
|
|
|
snprintf(tmp,sizeof(tmp),"[%lu]",(unsigned long)crossproduct);
|
2017-03-09 08:01:10 +08:00
|
|
|
ncbytescat(buffer,tmp);
|
2012-08-01 04:34:13 +08:00
|
|
|
}
|
2017-03-09 08:01:10 +08:00
|
|
|
ncbytescat(buffer,"\n");
|
2012-08-01 04:34:13 +08:00
|
|
|
|
|
|
|
/* dump the sub-instance, which might be fields, records, or elements */
|
|
|
|
if(!fisset(data->datamode,OCDT_ATOMIC)) {
|
|
|
|
for(i=0;i<data->ninstances;i++)
|
|
|
|
ocdumpdatatree(state,data->instances[i],buffer,depth+1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2017-03-09 08:01:10 +08:00
|
|
|
ocdumpdatapath(OCstate* state, OCdata* data, NCbytes* buffer)
|
2012-08-01 04:34:13 +08:00
|
|
|
{
|
|
|
|
int i;
|
|
|
|
OCdata* path[1024];
|
|
|
|
char tmp[1024];
|
|
|
|
OCdata* pathdata;
|
2015-01-16 05:19:51 +08:00
|
|
|
OCnode* pattern;
|
2012-08-01 04:34:13 +08:00
|
|
|
int isrecord;
|
|
|
|
|
|
|
|
path[0] = data;
|
|
|
|
for(i=1;;i++) {
|
|
|
|
OCdata* next = path[i-1];
|
|
|
|
if(next->container == NULL) break;
|
|
|
|
path[i] = next->container;
|
2016-03-09 04:48:30 +08:00
|
|
|
}
|
2012-08-01 04:34:13 +08:00
|
|
|
/* Path is in reverse order */
|
|
|
|
for(i=i-1;i>=0;i--) {
|
|
|
|
pathdata = path[i];
|
2015-01-16 05:19:51 +08:00
|
|
|
pattern = pathdata->pattern;
|
2017-03-09 08:01:10 +08:00
|
|
|
ncbytescat(buffer,"/");
|
|
|
|
ncbytescat(buffer,pattern->name);
|
2012-08-01 04:34:13 +08:00
|
|
|
/* Check the mode of the next step in path */
|
|
|
|
if(i > 0) {
|
|
|
|
OCdata* next = path[i-1];
|
|
|
|
if(fisset(next->datamode,OCDT_FIELD)
|
|
|
|
|| fisset(next->datamode,OCDT_ELEMENT)
|
|
|
|
|| fisset(next->datamode,OCDT_RECORD)) {
|
2012-11-22 07:57:42 +08:00
|
|
|
snprintf(tmp,sizeof(tmp),".%lu",(unsigned long)next->index);
|
2017-03-09 08:01:10 +08:00
|
|
|
ncbytescat(buffer,tmp);
|
2012-08-01 04:34:13 +08:00
|
|
|
}
|
|
|
|
}
|
2015-01-16 05:19:51 +08:00
|
|
|
if(pattern->octype == OC_Atomic) {
|
|
|
|
if(pattern->array.rank > 0) {
|
|
|
|
off_t xproduct = octotaldimsize(pattern->array.rank,pattern->array.sizes);
|
2012-08-01 04:34:13 +08:00
|
|
|
snprintf(tmp,sizeof(tmp),"[0..%lu]",(unsigned long)xproduct-1);
|
2017-03-09 08:01:10 +08:00
|
|
|
ncbytescat(buffer,tmp);
|
2012-08-01 04:34:13 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
isrecord = 0;
|
2015-01-16 05:19:51 +08:00
|
|
|
if(pattern->octype == OC_Sequence) {
|
2012-08-01 04:34:13 +08:00
|
|
|
/* Is this a record or a sequence ? */
|
|
|
|
isrecord = (fisset(pathdata->datamode,OCDT_RECORD) ? 1 : 0);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
/* Add suffix to path */
|
2015-01-16 05:19:51 +08:00
|
|
|
if(ociscontainer(pattern->octype)) {
|
2012-08-01 04:34:13 +08:00
|
|
|
/* add the container type, except distinguish record and sequence */
|
2017-03-09 08:01:10 +08:00
|
|
|
ncbytescat(buffer,":");
|
2012-08-01 04:34:13 +08:00
|
|
|
if(isrecord)
|
2017-03-09 08:01:10 +08:00
|
|
|
ncbytescat(buffer,"Record");
|
2012-08-01 04:34:13 +08:00
|
|
|
else
|
2017-03-09 08:01:10 +08:00
|
|
|
ncbytescat(buffer,octypetoddsstring(pattern->octype));
|
2015-01-16 05:19:51 +08:00
|
|
|
} else if(ocisatomic(pattern->octype)) {
|
2012-08-01 04:34:13 +08:00
|
|
|
/* add the atomic etype */
|
2017-03-09 08:01:10 +08:00
|
|
|
ncbytescat(buffer,":");
|
|
|
|
ncbytescat(buffer,octypetoddsstring(pattern->etype));
|
2012-08-01 04:34:13 +08:00
|
|
|
}
|
2021-12-24 13:18:56 +08:00
|
|
|
snprintf(tmp,sizeof(tmp),"->0x%p",pathdata);
|
2017-03-09 08:01:10 +08:00
|
|
|
ncbytescat(buffer,tmp);
|
2012-08-01 04:34:13 +08:00
|
|
|
}
|