Exemple #1
0
int
main(int argc, char** argv)
{
    int i;
    int ret = NC_NOERR;

    if(argc == 1) {
	fprintf(stderr,"usage: nc4printer <file> <file>...\n");
	exit(1);
    }
    for(i=1;i<argc;i++) {
	int ncid;
	char* filename;
        NCbytes* buf;

	filename = argv[i];
	buf = ncbytesnew();

	if((ret = nc_open(filename,NC_NETCDF4,&ncid))) goto fail;
		
	ret = NC4print(buf,ncid);
	ncbytesnull(buf);
	fprintf(stderr,"========== %s ==========\n",filename);
	fprintf(stderr,"%s\n",ncbytescontents(buf));
	ncbytesfree(buf);
    }
    exit(0);

fail:
    fprintf(stderr,"***Fail: (%d) %s\n",ret,nc_strerror(ret));
    exit(1);
}
Exemple #2
0
static void
atomicsToString(D4printer* out, union ATOMICS* value, nc_type type)
{
    char tmp[256];
    ncbytesclear(out->tmp);
    switch (type) {
    case NC_CHAR:
	snprintf(tmp,sizeof(tmp),"'%c'",value->i8[0]);
	break;
    case NC_BYTE:
	snprintf(tmp,sizeof(tmp),"%d",value->i8[0]);
	break;
    case NC_UBYTE:
	snprintf(tmp,sizeof(tmp),"%u",value->u8[0]);
	break;
    case NC_SHORT:
	snprintf(tmp,sizeof(tmp),"%d",value->i16[0]);
	break;
    case NC_USHORT:
	snprintf(tmp,sizeof(tmp),"%u",value->u16[0]);
	break;
    case NC_INT:
	snprintf(tmp,sizeof(tmp),"%d",value->i32[0]);
	break;
    case NC_UINT:
	snprintf(tmp,sizeof(tmp),"%u",value->u32[0]);
	break;
    case NC_INT64:
	snprintf(tmp,sizeof(tmp),"%lld",value->i64[0]);
	break;
    case NC_UINT64:
	snprintf(tmp,sizeof(tmp),"%llu",value->u64[0]);
	break;
    case NC_FLOAT:
	snprintf(tmp,sizeof(tmp),"%g",value->f32[0]);
	break;
    case NC_DOUBLE:
	snprintf(tmp,sizeof(tmp),"%g",value->f64[0]);
	break;
    case NC_STRING:
	ncbytescat(out->tmp,"\"");
	ncbytescat(out->tmp,value->s[0]);
	ncbytescat(out->tmp,"\"");
	break;
    default: abort();
    }
    if(type != NC_STRING) ncbytescat(out->tmp,tmp);
    ncbytesnull(out->tmp);
}
Exemple #3
0
/**
 * @internal Build _NCProperties attribute value.
 *
 * Convert a NCPROPINFO instance to a single string.
 *
 * @param info Properties info.
 * @param propdatap Pointer that gets properties string.
 *
 * @return ::NC_NOERR No error.
 * @return ::NC_EINVAL failed.
 * @author Dennis Heimbigner
 */
int
NC4_buildpropinfo(struct NCPROPINFO* info, char** propdatap)
{
    int stat = NC_NOERR;
    int i;
    NCbytes* buffer = NULL;
    char sversion[64];

    LOG((3, "%s", __func__));

    if(info == NULL || info->version == 0 || propdatap == NULL)
    {stat = NC_EINVAL; goto done;}

    *propdatap = NULL;

    buffer = ncbytesnew();
    if(!buffer) {stat = NC_ENOMEM; goto done;}

    /* start with version */
    ncbytescat(buffer,NCPVERSION);
    ncbytesappend(buffer,'=');
    snprintf(sversion,sizeof(sversion),"%d",info->version);
    ncbytescat(buffer,sversion);

    for(i=0;i<nclistlength(info->properties);i+=2) {
        char* value, *name;
        name = nclistget(info->properties,i);
        if(name == NULL) continue;
        value = nclistget(info->properties,i+1);
        ncbytesappend(buffer,NCPROPSSEP2); /* terminate last entry */
        escapify(buffer,name);
        ncbytesappend(buffer,'=');
        escapify(buffer,value);
    }
    /* Force null termination */
    ncbytesnull(buffer);
    *propdatap = ncbytesextract(buffer);

done:
    if(buffer != NULL) ncbytesfree(buffer);
    return stat;
}
Exemple #4
0
static int
readfile(NCD4INFO* state, const NCURI* uri, const char* suffix, NCbytes* packet)
{
    int stat = NC_NOERR;
    NCbytes* tmp = ncbytesnew();
    char* filename = NULL;

    ncbytescat(tmp,uri->path);
    if(suffix != NULL) ncbytescat(tmp,suffix);
    ncbytesnull(tmp);
    filename = ncbytesextract(tmp);
    ncbytesfree(tmp);
#ifdef HAVE_GETTIMEOFDAY
    struct timeval time0;
    struct timeval time1;
#endif

    state->fileproto.filename = filename; /* filename is alloc'd here anyway */

    if(FLAGSET(state->controls.flags,NCF_SHOWFETCH)) {
	char* surl = NULL;
#ifdef HAVE_GETTIMEOFDAY
	gettimeofday(&time0,NULL);
#endif
        surl = ncuribuild((NCURI*)uri,NULL,NULL,NCURIALL);
	nclog(NCLOGDBG,"fetch uri=%s file=%s",surl,filename);
    }
    stat = NC_readfile(filename,packet);
    if(FLAGSET(state->controls.flags,NCF_SHOWFETCH)) {
	double secs;
#ifdef HAVE_GETTIMEOFDAY
   	gettimeofday(&time1,NULL);
	secs = deltatime(time0,time1);
#endif
        nclog(NCLOGDBG,"fetch complete: %0.3f",secs);
    }
    return THROW(stat);
}
Exemple #5
0
int
dcelex(YYSTYPE* lvalp, DCEparsestate* state)
{
    DCElexstate* lexstate = state->lexstate;
    int token;
    int c;
    int len;
    char* p=NULL;
    token = 0;
    ncbytesclear(lexstate->yytext);
    ncbytesnull(lexstate->yytext);
    p=lexstate->next;
    while(token == 0 && (c=*p)) {
	if(c <= ' ' || c >= '\177') {p++; continue;}
	if(c == '"') {
	    int more = 1;
	    ceaddyytext(lexstate,c);
	    /* We have a SCAN_STRINGCONST */
	    while(more && (c=*(++p))) {
		switch (c) {
		case '"': p++; more=0; break;
		case '\\':
		    c=*(++p);
		    switch (c) {
		    case 'r': c = '\r'; break;
		    case 'n': c = '\n'; break;
		    case 'f': c = '\f'; break;
		    case 't': c = '\t'; break;
		    case 'x': {
			int d1,d2;
			c = '?';
			++p;
		        d1 = tohex(*p++);
			if(d1 < 0) {
			    dceerror(state,"Illegal \\xDD in SCAN_STRING");
			} else {
			    d2 = tohex(*p++);
			    if(d2 < 0) {
			        dceerror(state,"Illegal \\xDD in SCAN_STRING");
			    } else {
				c=(((unsigned int)d1)<<4) | (unsigned int)d2;
			    }
			}
		    } break;
		    default: break;
		    }
		    break;
		default: break;
		}
		ceaddyytext(lexstate,c);
	    }
	    token=SCAN_STRINGCONST;
	} else if(strchr(numchars1,c) != NULL) {
	    /* we might have a SCAN_NUMBERCONST */
	    int isnumber = 0;
	    char* yytext;
	    char* endpoint;
	    ceaddyytext(lexstate,c);
	    for(p++;(c=*p);p++) {
		if(strchr(numcharsn,c) == NULL) break;
	        ceaddyytext(lexstate,c);
	    }
	    /* See if this is a number */
	    ncbytesnull(lexstate->yytext);
	    yytext = ncbytescontents(lexstate->yytext);
	    (void)strtoll(yytext,&endpoint,10);
	    if(*yytext != '\0' && *endpoint == '\0')
	        isnumber = 1;
	    else {
	        (void)strtod(yytext,&endpoint);
	        if(*yytext != '\0' && *endpoint == '\0')
	            isnumber = 1; /* maybe */
	    }
	    /* A number followed by an id char is assumed to just be
	       a funny id */
	    if(isnumber && (*p == '\0' || strchr(wordcharsn,*p) == NULL))  {
	        token = SCAN_NUMBERCONST;
	    } else {
		/* Now, if the funny word has a "." in it,
		   we have to back up to that dot */
		char* dotpoint = strchr(yytext,'.');
		if(dotpoint != NULL) {
		    p = dotpoint;
		    *dotpoint = '\0';
		}
		token = SCAN_WORD;
	    }
	} else if(strchr(wordchars1,c) != NULL) {
	    /* we have a SCAN_WORD */
	    ceaddyytext(lexstate,c);
	    for(p++;(c=*p);p++) {
		if(strchr(wordcharsn,c) == NULL) break;
	        ceaddyytext(lexstate,c);
	    }
	    token=SCAN_WORD;
	} else {
	    /* we have a single char token */
	    token = c;
	    ceaddyytext(lexstate,c);
	    p++;
	}
    }
    lexstate->next = p;
    len = ncbyteslength(lexstate->yytext);
    if(len > MAX_TOKEN_LENGTH) len = MAX_TOKEN_LENGTH;
    strncpy(lexstate->lasttokentext,ncbytescontents(lexstate->yytext),len);
    lexstate->lasttokentext[len] = '\0';
    lexstate->lasttoken = token;
    if(dcedebug) dumptoken(lexstate);

    /*Put return value onto Bison stack*/

    if(ncbyteslength(lexstate->yytext) == 0)
        *lvalp = NULL;
    else {
        *lvalp = ncbytesdup(lexstate->yytext);
	nclistpush(lexstate->reclaim,(void*)*lvalp);
    }

    return token;
}
Exemple #6
0
char*
ncuribuild(NCURI* duri, const char* prefix, const char* suffix, int flags)
{
    char* newuri = NULL;
    NCbytes* buf = ncbytesnew();
#ifdef NEWESCAPE
    const int encode = (flags&NCURIENCODE ? 1 : 0);
#else
    const int encode = 0;
#endif

    if(prefix != NULL)
	ncbytescat(buf,prefix);

    ncbytescat(buf,duri->protocol);
    ncbytescat(buf,"://"); /* this will produce file:///... */

    if((flags & NCURIPWD) && duri->user != NULL && duri->password != NULL) {
	/* The user and password must be encoded */
        char* encoded = ncuriencodeonly(duri->user,userpwdallow);
	ncbytescat(buf,encoded);
	nullfree(encoded);
	ncbytescat(buf,":");
	encoded = ncuriencodeonly(duri->password,userpwdallow);
	ncbytescat(buf,encoded);
	nullfree(encoded);
	ncbytescat(buf,"@");
    }
    if(duri->host != NULL) ncbytescat(buf,duri->host);
    if(duri->port != NULL) {
	ncbytescat(buf,":");
	ncbytescat(buf,duri->port);
    }
    if((flags & NCURIPATH)) {
	if(duri->path == NULL)
	    ncbytescat(buf,"/");
	else if(encode) {
	    char* encoded = ncuriencodeonly(duri->path,pathallow);
	    ncbytescat(buf,encoded);
	    nullfree(encoded);
	} else 	
	    ncbytescat(buf,duri->path);
    }

    /* The suffix is intended to some kind of path extension (e.g. .dds)
       so insert here
    */
    if(suffix != NULL)
	ncbytescat(buf,suffix);

    if((flags & NCURIQUERY) && duri->querylist != NULL) {
	char** p;
	int first = 1;
	for(p=duri->querylist;*p;p+=2,first=0) {
	    ncbytescat(buf,(first?"?":"&"));
	    ncbytescat(buf,p[0]);
	    if(p[1] != NULL && strlen(p[1]) > 0) {
		ncbytescat(buf,"=");
		if(encode) {
		    char* encoded = ncuriencodeonly(p[1],queryallow);
		    ncbytescat(buf,encoded);
	            nullfree(encoded);
		} else 	
		    ncbytescat(buf,p[1]);
	    }
	}	
    }
    if((flags & NCURIFRAG) && duri->fraglist != NULL) {
	char** p;
	int first = 1;
	for(p=duri->fraglist;*p;p+=2,first=0) {
	    ncbytescat(buf,(first?"#":"&"));
	    ncbytescat(buf,p[0]);
	    if(p[1] != NULL && strlen(p[1]) > 0) {
		ncbytescat(buf,"=");
		if(encode) {
		    char* encoded = ncuriencodeonly(p[1],queryallow);
		    ncbytescat(buf,encoded);
	            nullfree(encoded);
		} else 	
		    ncbytescat(buf,p[1]);
	    }
	}	
    }
    ncbytesnull(buf);
    newuri = ncbytesextract(buf);
    ncbytesfree(buf);
    return newuri;
}