char* escapify(char* s0, int quote, size_t len) { int i; char* result; result = poolalloc(1+4*len); /* overkill to support maximal expansion*/ result[0] = '\0'; for(i=0;i<len;i++) { char tmp[8]; escapifychar((unsigned int)s0[i],tmp,quote); strcat(result,tmp); } return result; }
/* Result is a pool string or a constant => do not free*/ char* f77data_const(Constant* ci) { char tmp[64]; char* result = NULL; tmp[0] = '\0'; switch (ci->nctype) { case NC_CHAR: { strcpy(tmp,"'"); escapifychar(ci->value.charv,tmp+1,'\''); strcat(tmp,"'"); } break; case NC_BYTE: sprintf(tmp,"%hhd",ci->value.int8v); break; case NC_SHORT: sprintf(tmp,"%hd",ci->value.int16v); break; case NC_INT: sprintf(tmp,"%d",ci->value.int32v); break; case NC_FLOAT: sprintf(tmp,"%.8g",ci->value.floatv); break; case NC_DOUBLE: { char* p = tmp; /* FORTRAN requires e|E->D */ sprintf(tmp,"%.16g",ci->value.doublev); while(*p) {if(*p == 'e' || *p == 'E') {*p = 'D';}; p++;} } break; case NC_STRING: { Bytebuffer* buf = bbNew(); bbAppendn(buf,ci->value.stringv.stringv,ci->value.stringv.len); f77quotestring(buf); result = bbDup(buf); bbFree(buf); goto done; } break; default: PANIC1("ncstype: bad type code: %d",ci->nctype); } result = pooldup(tmp); done: return result; /*except for NC_STRING and NC_OPAQUE*/ }
static void dumpdataprim(NCConstant* ci, Bytebuffer* buf) { char tmp[64]; ASSERT(isprimplus(ci->nctype) || ci->nctype == NC_FILLVALUE); switch (ci->nctype) { case NC_CHAR: { bbCat(buf,"'"); escapifychar(ci->value.charv,tmp,'\''); bbCat(buf,tmp); bbCat(buf,"'"); } break; case NC_BYTE: sprintf(tmp,"%hhd",ci->value.int8v); bbCat(buf,tmp); break; case NC_SHORT: sprintf(tmp,"%hd",ci->value.int16v); bbCat(buf,tmp); break; case NC_INT: sprintf(tmp,"%d",ci->value.int32v); bbCat(buf,tmp); break; case NC_FLOAT: sprintf(tmp,"%g",ci->value.floatv); bbCat(buf,tmp); break; case NC_DOUBLE: sprintf(tmp,"%lg",ci->value.doublev); bbCat(buf,tmp); break; case NC_UBYTE: sprintf(tmp,"%hhu",ci->value.int8v); bbCat(buf,tmp); break; case NC_USHORT: sprintf(tmp,"%hu",ci->value.uint16v); bbCat(buf,tmp); break; case NC_UINT: sprintf(tmp,"%u",ci->value.uint32v); bbCat(buf,tmp); break; case NC_INT64: sprintf(tmp,"%lld",ci->value.int64v); bbCat(buf,tmp); break; case NC_UINT64: sprintf(tmp,"%llu",ci->value.uint64v); bbCat(buf,tmp); break; case NC_ECONST: sprintf(tmp,"%s",ci->value.enumv->fqn); bbCat(buf,tmp); break; case NC_STRING: bbCat(buf,"\""); bbCat(buf,ci->value.stringv.stringv); bbCat(buf,"\""); break; case NC_OPAQUE: bbCat(buf,"0x"); bbCat(buf,ci->value.opaquev.stringv); break; case NC_FILLVALUE: bbCat(buf,"_"); break; default: PANIC1("dumpdataprim: bad type code:%d",ci->nctype); } }
/* Result is a pool string or a constant => do not free*/ char* cdata_const(Constant* ci) { Bytebuffer* codetmp = bbNew(); char* result; switch (ci->nctype) { case NC_CHAR: { char tmp[64]; tmp[0] = '\0'; escapifychar(ci->value.charv,tmp,'\''); bbCat(codetmp,"'"); bbCat(codetmp,tmp); bbCat(codetmp,"'"); } break; case NC_BYTE: bbprintf(codetmp,"%hhd",ci->value.int8v); break; case NC_SHORT: bbprintf(codetmp,"%hd",ci->value.int16v); break; case NC_INT: bbprintf(codetmp,"%d",ci->value.int32v); break; case NC_FLOAT: bbprintf(codetmp,"%f",ci->value.floatv); break; case NC_DOUBLE: bbprintf(codetmp,"%lf",ci->value.doublev); break; case NC_UBYTE: bbprintf(codetmp,"%hhu",ci->value.uint8v); break; case NC_USHORT: bbprintf(codetmp,"%hu",ci->value.uint16v); break; case NC_UINT: bbprintf(codetmp,"%uU",ci->value.uint32v); break; case NC_INT64: bbprintf(codetmp,"%lldLL",ci->value.int64v); break; case NC_UINT64: bbprintf(codetmp,"%lluLLU",ci->value.uint64v); break; case NC_ECONST: bbprintf(codetmp,"%s",cname(ci->value.enumv)); break; case NC_STRING: { /* handle separately */ char* escaped = escapify(ci->value.stringv.stringv, '"',ci->value.stringv.len); result = poolalloc(1+2+strlen(escaped)); strcpy(result,"\""); strcat(result,escaped); strcat(result,"\""); goto done; } break; case NC_OPAQUE: { char* p; int bslen; bslen=(4*ci->value.opaquev.len); result = poolalloc(bslen+2+1); strcpy(result,"\""); p = ci->value.opaquev.stringv; while(*p) { strcat(result,"\\x"); strncat(result,p,2); p += 2; } strcat(result,"\""); goto done; } break; default: PANIC1("ncstype: bad type code: %d",ci->nctype); } result = pooldup(bbContents(codetmp)); /*except for NC_STRING and NC_OPAQUE*/ bbFree(codetmp); done: return result; }