Merge pull request #237 from Unidata/issue236

ncdump can generate nul characters &#0 ; when dumping ncml using -x.
This commit is contained in:
Ward Fisher 2016-03-22 09:47:56 -07:00
commit f5414bfee2
3 changed files with 13 additions and 3 deletions

View File

@ -49,6 +49,7 @@ typedef int ssize_t;
#include "isnan.h" #include "isnan.h"
#include "cdl.h" #include "cdl.h"
#define XML_VERSION "1.0"
#define int64_t long long #define int64_t long long
#define uint64_t unsigned long long #define uint64_t unsigned long long
@ -393,8 +394,8 @@ done:
static void static void
pr_initx(int ncid, const char *path) pr_initx(int ncid, const char *path)
{ {
printf("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<netcdf xmlns=\"http://www.unidata.ucar.edu/namespaces/netcdf/ncml-2.2\" location=\"%s\">\n", printf("<?xml version=\"%s\" encoding=\"UTF-8\"?>\n<netcdf xmlns=\"http://www.unidata.ucar.edu/namespaces/netcdf/ncml-2.2\" location=\"%s\">\n",
path); XML_VERSION, path);
} }
/* /*
@ -472,6 +473,7 @@ pr_att_string(
*/ */
static void static void
pr_attx_string( pr_attx_string(
const char* attname,
size_t len, size_t len,
const char *string const char *string
) )
@ -480,6 +482,7 @@ pr_attx_string(
const char *cp; const char *cp;
const char *sp; const char *sp;
unsigned char uc; unsigned char uc;
int nulcount = 0;
cp = string; cp = string;
printf ("\""); printf ("\"");
@ -510,6 +513,11 @@ pr_attx_string(
case '\t': case '\t':
printf ("&#x9;"); printf ("&#x9;");
break; break;
case '\0':
printf ("&#0;");
if(nulcount++ == 0)
fprintf(stderr,"Attribute: '%s'; value contains nul characters; producing illegal xml\n",attname);
break;
default: default:
if (iscntrl(uc)) if (iscntrl(uc))
printf ("&#%d;",uc); printf ("&#%d;",uc);
@ -1120,7 +1128,7 @@ pr_attx(
varid != NC_GLOBAL ? " " : "", varid != NC_GLOBAL ? " " : "",
att.name); att.name);
/* print attvals as a string with XML escapes */ /* print attvals as a string with XML escapes */
pr_attx_string(attvalslen, attvals); pr_attx_string(att.name, attvalslen, attvals);
} else { /* non-string attribute */ } else { /* non-string attribute */
char att_type_name[NC_MAX_NAME + 1]; char att_type_name[NC_MAX_NAME + 1];
get_type_name(ncid, att.type, att_type_name); get_type_name(ncid, att.type, att_type_name);

View File

@ -5,6 +5,7 @@
<attribute name="gtatt" value="&lt;, &gt;, ', &quot;, and &amp;" /> <attribute name="gtatt" value="&lt;, &gt;, ', &quot;, and &amp;" />
<attribute name="gnatt" type="int" value="3 4" /> <attribute name="gnatt" type="int" value="3 4" />
<attribute name="gdatt" type="double" value="-7.02788826649782e-09 7.02788826649782e-09" /> <attribute name="gdatt" type="double" value="-7.02788826649782e-09 7.02788826649782e-09" />
<attribute name="gnul" value="&#0;abcd&#0;&#0;hello" />
<variable name="var" shape="t m" type="float"> <variable name="var" shape="t m" type="float">
<attribute name="tatt" value="text attribute value" /> <attribute name="tatt" value="text attribute value" />
<attribute name="natt" type="int" value="1 2" /> <attribute name="natt" type="int" value="1 2" />

View File

@ -10,4 +10,5 @@ variables:
:gtatt = "<, >, \', \", and &" ; :gtatt = "<, >, \', \", and &" ;
:gnatt = 3, 4; :gnatt = 3, 4;
:gdatt = -7.02788826649782e-09, 7.02788826649782e-09 ; :gdatt = -7.02788826649782e-09, 7.02788826649782e-09 ;
char :gnul = "\000abcd\000\000hello\000" ;
} }