vpi_get_str improvements

Gets rid of a few warning: deprecated conversion from string
constant to 'char*', follows IEEE 1364-2001C 27.10 in more cases,
and fixes at least one real bug (look at the previous use of
strdup/strcat in real_var_get_str() and signal_get_str()).
This commit is contained in:
Larry Doolittle 2007-12-18 15:11:50 -08:00 committed by Stephen Williams
parent 752cf21790
commit 9772068bbc
11 changed files with 83 additions and 189 deletions

View File

@ -145,26 +145,7 @@ static char*vpi_array_get_str(int code, vpiHandle ref)
{ {
struct __vpiArray*obj = ARRAY_HANDLE(ref); struct __vpiArray*obj = ARRAY_HANDLE(ref);
char*bn; return generic_get_str(code, &obj->scope->base, obj->name, NULL);
char*rbuf;
size_t len;
switch (code) {
case vpiFullName:
bn = strdup(vpi_get_str(vpiFullName, &obj->scope->base));
len = strlen(bn)+strlen(obj->name)+2;
rbuf = need_result_buf(len, RBUF_STR);
snprintf(rbuf, len, "%s.%s", bn, obj->name);
free(bn);
return rbuf;
case vpiName:
rbuf = need_result_buf(strlen(obj->name)+1, RBUF_STR);
strcpy(rbuf, obj->name);
return rbuf;
}
return 0;
} }
static vpiHandle vpi_array_get_handle(int code, vpiHandle ref) static vpiHandle vpi_array_get_handle(int code, vpiHandle ref)

View File

@ -251,27 +251,10 @@ struct __vpiStringParam : public __vpiStringConst {
static char* string_param_get_str(int code, vpiHandle obj) static char* string_param_get_str(int code, vpiHandle obj)
{ {
struct __vpiStringParam*rfp = (struct __vpiStringParam*)obj; struct __vpiStringParam*rfp = (struct __vpiStringParam*)obj;
char *bn = strdup(vpi_get_str(vpiFullName, &rfp->scope->base));
char *rbuf = need_result_buf(strlen(bn)+strlen(rfp->basename) + 2,
RBUF_STR);
assert(obj->vpi_type->type_code == vpiParameter); assert(obj->vpi_type->type_code == vpiParameter);
switch (code) { return generic_get_str(code, &rfp->scope->base, rfp->basename, NULL);
case vpiFullName:
sprintf(rbuf, "%s.%s", bn, rfp->basename);
free(bn);
return rbuf;
case vpiName:
strcpy(rbuf, rfp->basename);
free(bn);
return rbuf;
default:
free(bn);
return 0;
}
} }
static vpiHandle string_param_handle(int code, vpiHandle obj) static vpiHandle string_param_handle(int code, vpiHandle obj)
@ -441,26 +424,10 @@ struct __vpiBinaryParam : public __vpiBinaryConst {
static char* binary_param_get_str(int code, vpiHandle obj) static char* binary_param_get_str(int code, vpiHandle obj)
{ {
struct __vpiBinaryParam*rfp = (struct __vpiBinaryParam*)obj; struct __vpiBinaryParam*rfp = (struct __vpiBinaryParam*)obj;
char *bn = strdup(vpi_get_str(vpiFullName, &rfp->scope->base));
char *rbuf = need_result_buf(strlen(bn)+strlen(rfp->basename) + 2,
RBUF_STR);
assert(obj->vpi_type->type_code == vpiParameter); assert(obj->vpi_type->type_code == vpiParameter);
switch (code) { return generic_get_str(code, &rfp->scope->base, rfp->basename, NULL);
case vpiFullName:
sprintf(rbuf, "%s.%s", bn, rfp->basename);
free(bn);
return rbuf;
case vpiName:
strcpy(rbuf, rfp->basename);
free(bn);
return rbuf;
default:
free(bn);
return 0;
}
} }
static vpiHandle binary_param_handle(int code, vpiHandle obj) static vpiHandle binary_param_handle(int code, vpiHandle obj)

View File

@ -29,30 +29,13 @@
# include <string.h> # include <string.h>
# include <assert.h> # include <assert.h>
static char* named_event_str(int code, vpiHandle ref) static char* named_event_get_str(int code, vpiHandle ref)
{ {
assert((ref->vpi_type->type_code==vpiNamedEvent)); assert((ref->vpi_type->type_code==vpiNamedEvent));
struct __vpiNamedEvent*obj = (struct __vpiNamedEvent*)ref; struct __vpiNamedEvent*obj = (struct __vpiNamedEvent*)ref;
char *bn = vpi_get_str(vpiFullName, &obj->scope->base); return generic_get_str(code, &obj->scope->base, obj->name, NULL);
const char *nm = obj->name;
char *rbuf = need_result_buf(strlen(bn) + strlen(nm) + 1, RBUF_STR);
switch (code) {
case vpiFullName:
sprintf(rbuf, "%s.%s", bn, nm);
return rbuf;
case vpiName:
strcpy(rbuf, nm);
return rbuf;
}
return 0;
} }
static vpiHandle named_event_get_handle(int code, vpiHandle ref) static vpiHandle named_event_get_handle(int code, vpiHandle ref)
@ -74,7 +57,7 @@ static const struct __vpirt vpip_named_event_rt = {
vpiNamedEvent, vpiNamedEvent,
0, 0,
named_event_str, named_event_get_str,
0, 0,
0, 0,

View File

@ -103,23 +103,7 @@ static char* memory_get_str(int code, vpiHandle ref)
struct __vpiMemory*rfp = (struct __vpiMemory*)ref; struct __vpiMemory*rfp = (struct __vpiMemory*)ref;
char *bn = strdup(vpi_get_str(vpiFullName, &rfp->scope->base)); return generic_get_str(code, &rfp->scope->base, rfp->name, NULL);
char *rbuf = need_result_buf(strlen(bn)+strlen(rfp->name)+2, RBUF_STR);
switch (code) {
case vpiFullName:
sprintf(rbuf, "%s.%s", bn, rfp->name);
free(bn);
return rbuf;
case vpiName:
strcpy(rbuf, rfp->name);
free(bn);
return rbuf;
}
free(bn);
return 0;
} }
static vpiHandle memory_scan(vpiHandle ref, int) static vpiHandle memory_scan(vpiHandle ref, int)
@ -340,27 +324,10 @@ static char* memory_word_get_str(int code, vpiHandle ref)
struct __vpiMemoryWord*rfp = (struct __vpiMemoryWord*)ref; struct __vpiMemoryWord*rfp = (struct __vpiMemoryWord*)ref;
char *bn = strdup(vpi_get_str(vpiFullName, &rfp->mem->scope->base)); char number[32];
const char *nm = rfp->mem->name; sprintf(number, "%d", rfp->index.value);
char *rbuf = need_result_buf(strlen(bn) + strlen(nm) + 10 + 4, RBUF_STR); return generic_get_str(code, &rfp->mem->scope->base, rfp->mem->name, number);
switch (code) {
case vpiFullName:
sprintf(rbuf, "%s.%s[%d]", bn, nm, rfp->index.value);
free(bn);
return rbuf;
break;
case vpiName: {
sprintf(rbuf, "%s[%d]", nm, rfp->index.value);
free(bn);
return rbuf;
break;
}
}
free(bn);
return 0;
} }
static void memory_word_get_value(vpiHandle ref, s_vpi_value*vp) static void memory_word_get_value(vpiHandle ref, s_vpi_value*vp)

View File

@ -539,5 +539,8 @@ enum vpi_rbuf_t {
/* Storage for *_get_str() */ /* Storage for *_get_str() */
}; };
extern char *need_result_buf(unsigned cnt, vpi_rbuf_t type); extern char *need_result_buf(unsigned cnt, vpi_rbuf_t type);
/* following two routines use need_result_buf(, RBUF_STR) */
extern char *simple_set_rbuf_str(const char *s1);
extern char *generic_get_str(int code, vpiHandle ref, const char *name, const char *index);
#endif #endif

View File

@ -48,39 +48,22 @@ static char* real_var_get_str(int code, vpiHandle ref)
assert(ref->vpi_type->type_code == vpiRealVar); assert(ref->vpi_type->type_code == vpiRealVar);
struct __vpiRealVar*rfp = (struct __vpiRealVar*)ref; struct __vpiRealVar*rfp = (struct __vpiRealVar*)ref;
char *bn = strdup(vpi_get_str(vpiFullName, &rfp->scope->base));
char *nm; char *nm, *ixs;
if (rfp->parent) { if (rfp->parent) {
s_vpi_value vp;
nm = strdup(vpi_get_str(vpiName, rfp->parent)); nm = strdup(vpi_get_str(vpiName, rfp->parent));
strcat(nm, "["); s_vpi_value vp;
vp.format = vpiDecStrVal; vp.format = vpiDecStrVal;
vpi_get_value(rfp->id.index, &vp); vpi_get_value(rfp->id.index, &vp);
strcat(nm, vp.value.str); ixs = vp.value.str; /* do I need to strdup() this? */
strcat(nm, "]");
} else { } else {
nm = strdup(rfp->id.name); nm = strdup(rfp->id.name);
} ixs = NULL;
char *rbuf = need_result_buf(strlen(bn)+strlen(nm) + 2, RBUF_STR);
switch (code) {
case vpiFullName:
sprintf(rbuf, "%s.%s", bn, nm);
free(bn);
free(nm);
return rbuf;
case vpiName:
strcpy(rbuf, nm);
free(bn);
free(nm);
return rbuf;
} }
free(bn); char *rbuf = generic_get_str(code, &rfp->scope->base, nm, ixs);
free(nm); free(nm);
return 0; return rbuf;
} }
static vpiHandle real_var_get_handle(int code, vpiHandle ref) static vpiHandle real_var_get_handle(int code, vpiHandle ref)

View File

@ -109,40 +109,36 @@ static const char* scope_get_type(int code)
static char* scope_get_str(int code, vpiHandle obj) static char* scope_get_str(int code, vpiHandle obj)
{ {
struct __vpiScope*ref = (struct __vpiScope*)obj; struct __vpiScope*ref = (struct __vpiScope*)obj;
char *rbuf;
assert(handle_is_scope(obj)); assert(handle_is_scope(obj));
char buf[4096]; // XXX is a fixed buffer size really reliable?
const char *p=0;
switch (code) { switch (code) {
case vpiFullName: { case vpiFullName:
char buf[4096];
buf[0] = 0; buf[0] = 0;
construct_scope_fullname(ref, buf); construct_scope_fullname(ref, buf);
rbuf = need_result_buf(strlen(buf) + 1, RBUF_STR); p = buf;
strcpy(rbuf, buf); break;
return rbuf;
}
case vpiName: case vpiName:
rbuf = need_result_buf(strlen(ref->name) + 1, RBUF_STR); p = ref->name;
strcpy(rbuf, ref->name); break;
return rbuf;
case vpiDefName: case vpiDefName:
rbuf = need_result_buf(strlen(ref->tname) + 1, RBUF_STR); p = ref->tname;
strcpy(rbuf, ref->tname); break;
return rbuf;
case vpiType: case vpiType:
rbuf = need_result_buf(strlen(scope_get_type(code)) + 1, RBUF_STR); p = scope_get_type(code);
strcpy(rbuf, scope_get_type(code)); break;
return rbuf;
default: default:
fprintf(stderr, "ERROR: invalid code %d.", code); fprintf(stderr, "ERROR: invalid code %d.", code);
assert(0); assert(0);
return 0; return 0;
} }
return simple_set_rbuf_str(p);
} }
static vpiHandle scope_get_handle(int code, vpiHandle obj) static vpiHandle scope_get_handle(int code, vpiHandle obj)

View File

@ -46,7 +46,7 @@
* just perform the lookup in a table. This only takes 256 bytes, * just perform the lookup in a table. This only takes 256 bytes,
* which is not many executable instructions:-) * which is not many executable instructions:-)
* *
* The table is calculated as compile time, therefore, by the * The table is calculated at compile time, therefore, by the
* draw_tt.c program. * draw_tt.c program.
*/ */
extern const char hex_digits[256]; extern const char hex_digits[256];
@ -75,6 +75,42 @@ char *need_result_buf(unsigned cnt, vpi_rbuf_t type)
return result_buf[type]; return result_buf[type];
} }
char *simple_set_rbuf_str(const char *s1)
{
char *res = need_result_buf(strlen(s1)+1, RBUF_STR);
if (res) strcpy(res,s1);
return res;
}
char *generic_get_str(int code, vpiHandle ref, const char *name, const char *index)
{
size_t len = strlen(name) + 1; /* include space for null termination */
char *bn = NULL;
if (code == vpiFullName) {
bn = strdup(vpi_get_str(code,ref));
len += strlen(bn) + 1; /* include space for "." separator */
}
if (index != NULL) len += strlen(index) + 2; /* include space for brackets */
char *res = need_result_buf(len, RBUF_STR);
if (!res) return NULL;
*res=0; /* start with nothing */
/* if this works, I can make it more efficient later */
if (bn != NULL) {
strcat(res, bn);
strcat(res, ".");
free(bn);
}
strcat(res, name);
if (index != NULL) {
strcat(res, "[");
strcat(res, index);
strcat(res, "]");
}
return res;
}
struct __vpiSignal* vpip_signal_from_handle(vpiHandle ref) struct __vpiSignal* vpip_signal_from_handle(vpiHandle ref)
{ {
if ((ref->vpi_type->type_code != vpiNet) if ((ref->vpi_type->type_code != vpiNet)
@ -134,39 +170,21 @@ static char* signal_get_str(int code, vpiHandle ref)
struct __vpiSignal*rfp = (struct __vpiSignal*)ref; struct __vpiSignal*rfp = (struct __vpiSignal*)ref;
char *bn = strdup(vpi_get_str(vpiFullName, &rfp->scope->base)); char *nm, *ixs;
char *nm;
if (rfp->parent) { if (rfp->parent) {
nm = vpi_get_str(vpiName, rfp->parent);
s_vpi_value vp; s_vpi_value vp;
nm = strdup(vpi_get_str(vpiName, rfp->parent));
strcat(nm, "[");
vp.format = vpiDecStrVal; vp.format = vpiDecStrVal;
vpi_get_value(rfp->id.index, &vp); vpi_get_value(rfp->id.index, &vp);
strcat(nm, vp.value.str); ixs = vp.value.str; /* do I need to strdup() this? */
strcat(nm, "]");
} else { } else {
nm = strdup(rfp->id.name); nm = strdup(rfp->id.name);
} ixs = NULL;
char *rbuf = need_result_buf(strlen(bn) + strlen(nm) + 2, RBUF_STR);
switch (code) {
case vpiFullName:
sprintf(rbuf, "%s.%s", bn, nm);
free(bn);
free(nm);
return rbuf;
case vpiName:
strcpy(rbuf, nm);
free(bn);
free(nm);
return rbuf;
} }
free(bn); char *rbuf = generic_get_str(code, &rfp->scope->base, nm, ixs);
free(nm); free(nm);
return 0; return rbuf;
} }
static vpiHandle signal_get_handle(int code, vpiHandle ref) static vpiHandle signal_get_handle(int code, vpiHandle ref)

View File

@ -111,14 +111,10 @@ static char *systask_get_str(int type, vpiHandle ref)
assert((ref->vpi_type->type_code == vpiSysTaskCall) assert((ref->vpi_type->type_code == vpiSysTaskCall)
|| (ref->vpi_type->type_code == vpiSysFuncCall)); || (ref->vpi_type->type_code == vpiSysFuncCall));
const char *bn = rfp->defn->info.tfname;
char *rbuf = need_result_buf(strlen(bn) + 1, RBUF_STR);
switch (type) { switch (type) {
case vpiName: case vpiName:
strcpy(rbuf,bn); return simple_set_rbuf_str(rfp->defn->info.tfname);
return rbuf;
} }
return 0; return 0;

View File

@ -107,7 +107,7 @@ static char* timevar_time_get_str(int code, vpiHandle ref)
{ {
switch (code) { switch (code) {
case vpiName: case vpiName:
return "$time"; return simple_set_rbuf_str("$time");
default: default:
fprintf(stderr, "Code: %d\n", code); fprintf(stderr, "Code: %d\n", code);
assert(0); assert(0);
@ -119,7 +119,7 @@ static char* timevar_simtime_get_str(int code, vpiHandle ref)
{ {
switch (code) { switch (code) {
case vpiName: case vpiName:
return "$simtime"; return simple_set_rbuf_str("$simtime");
default: default:
fprintf(stderr, "Code: %d\n", code); fprintf(stderr, "Code: %d\n", code);
assert(0); assert(0);
@ -131,7 +131,7 @@ static char* timevar_realtime_get_str(int code, vpiHandle ref)
{ {
switch (code) { switch (code) {
case vpiName: case vpiName:
return "$realtime"; return simple_set_rbuf_str("$realtime");
default: default:
fprintf(stderr, "Code: %d\n", code); fprintf(stderr, "Code: %d\n", code);
assert(0); assert(0);

View File

@ -105,8 +105,8 @@ static char* vthr_vec_get_str(int code, vpiHandle ref)
switch (code) { switch (code) {
case vpiFullName: case vpiFullName: /* should this be vpiName? */
return (char*)rfp->name; return simple_set_rbuf_str(rfp->name);
} }
return 0; return 0;