Add vpi_fopen and vpi_get_file.

This commit is contained in:
steve
2003-05-23 04:04:02 +00:00
parent 3770dda0b8
commit e3e4e648d7
4 changed files with 199 additions and 116 deletions
+101 -63
View File
@@ -17,7 +17,7 @@
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
*/
#ifdef HAVE_CVS_IDENT
#ident "$Id: vpi_mcd.cc,v 1.9 2003/05/15 16:51:09 steve Exp $"
#ident "$Id: vpi_mcd.cc,v 1.10 2003/05/23 04:04:02 steve Exp $"
#endif
# include "vpi_priv.h"
@@ -33,12 +33,21 @@
* standard output file, which may be replicated to a logfile
* depending on flags to the command line.
*/
/*
* MCD/FD manipulation macros
*/
#define IS_MCD(mcd) !((mcd)>>31&1)
#define FD_IDX(fd) ((fd)&~(1U<<31))
#define FD_MAX 32
struct mcd_entry {
FILE *fp;
char *filename;
};
static struct mcd_entry mcd_table[31];
static struct mcd_entry fd_table[FD_MAX];
static struct mcd_entry mcd_table[32];
static FILE* logfile;
/* Initialize mcd portion of vpi. Must be called before
@@ -47,7 +56,14 @@ static FILE* logfile;
void vpi_mcd_init(FILE *log)
{
mcd_table[0].fp = stdout;
mcd_table[0].filename = "<stdout>";
mcd_table[0].filename = "stdout";
fd_table[0].fp = stdin;
fd_table[0].filename = "stdin";
fd_table[1].fp = stdout;
fd_table[1].filename = "stdout";
fd_table[2].fp = stderr;
fd_table[2].filename = "stderr";
logfile = log;
}
@@ -55,38 +71,52 @@ void vpi_mcd_init(FILE *log)
/*
* close one or more channels. we silently refuse to close the preopened ones.
*/
extern "C" PLI_UINT32 vpi_mcd_close(unsigned int mcd)
extern "C" PLI_UINT32 vpi_mcd_close(PLI_UINT32 mcd)
{
int i;
int rc;
rc = 0;
for(i = 1; i < 31; i++) {
if( ((mcd>>i) & 1) && mcd_table[i].filename) {
if(fclose(mcd_table[i].fp) != 0)
rc |= 1<<i;
free(mcd_table[i].filename);
mcd_table[i].fp = NULL;
mcd_table[i].filename = NULL;
} else {
rc |= 1<<i;
int rc = 0;
if (IS_MCD(mcd)) {
for(int i = 1; i < 31; i++) {
if(((mcd>>i) & 1) && mcd_table[i].fp) {
if(fclose(mcd_table[i].fp)) rc |= 1<<i;
free(mcd_table[i].filename);
mcd_table[i].fp = NULL;
mcd_table[i].filename = NULL;
} else {
rc |= 1<<i;
}
}
} else {
unsigned idx = FD_IDX(mcd);
if (idx > 2 && idx < FD_MAX && fd_table[idx].fp) {
rc = fclose(fd_table[idx].fp);
free(fd_table[idx].filename);
fd_table[idx].fp = NULL;
fd_table[idx].filename = NULL;
}
}
return rc;
}
extern "C" char *vpi_mcd_name(unsigned int mcd)
extern "C" char *vpi_mcd_name(PLI_UINT32 mcd)
{
int i;
for(i = 0; i < 31; i++) {
if( (mcd>>i) & 1)
return mcd_table[i].filename;
if (IS_MCD(mcd)) {
for(int i = 0; i < 31; i++) {
if((mcd>>i) & 1)
return mcd_table[i].filename;
}
} else {
unsigned idx = FD_IDX(mcd);
if (idx < FD_MAX)
return fd_table[idx].filename;
}
return NULL;
}
extern "C" PLI_UINT32 vpi_mcd_open_x(char *name, char *mode)
extern "C" PLI_UINT32 vpi_mcd_open(char *name)
{
int i;
for(i = 0; i < 31; i++) {
if(mcd_table[i].filename == NULL)
goto got_entry;
@@ -94,45 +124,35 @@ extern "C" PLI_UINT32 vpi_mcd_open_x(char *name, char *mode)
return 0; /* too many open mcd's */
got_entry:
mcd_table[i].fp = fopen(name, mode);
mcd_table[i].fp = fopen(name, "w");
if(mcd_table[i].fp == NULL)
return 0;
mcd_table[i].filename = strdup(name);
return 1<<i;
}
extern "C" PLI_UINT32 vpi_mcd_open(char *name)
{
return vpi_mcd_open_x(name,"w");
}
extern "C" PLI_INT32
vpi_mcd_vprintf(unsigned int mcd, const char*fmt, va_list ap)
vpi_mcd_vprintf(PLI_UINT32 mcd, const char*fmt, va_list ap)
{
int i;
int len;
int rc;
int rc = 0;
rc = len = 0;
for(i = 0; i < 31; i++) {
if( (mcd>>i) & 1) {
if (!IS_MCD(mcd)) return 0;
for(int i = 0; i < 31; i++) {
if((mcd>>i) & 1) {
if(mcd_table[i].fp) {
// echo to logfile
if (i == 0 && logfile)
vfprintf(logfile, fmt, ap);
len = vfprintf(mcd_table[i].fp, fmt, ap);
rc = vfprintf(mcd_table[i].fp, fmt, ap);
} else
rc = EOF;
}
}
if(rc)
return rc;
else
return len;
return rc;
}
extern "C" PLI_INT32 vpi_mcd_printf(unsigned int mcd, const char *fmt, ...)
extern "C" PLI_INT32 vpi_mcd_printf(PLI_UINT32 mcd, const char *fmt, ...)
{
va_list ap;
va_start(ap, fmt);
@@ -141,42 +161,60 @@ extern "C" PLI_INT32 vpi_mcd_printf(unsigned int mcd, const char *fmt, ...)
return r;
}
extern "C" PLI_INT32 vpi_mcd_flush(unsigned int mcd)
extern "C" PLI_INT32 vpi_mcd_flush(PLI_UINT32 mcd)
{
int i, rc = 0;
for(i = 0; i < 31; i++) {
if( (mcd>>i) & 1)
if (fflush(mcd_table[i].fp)) rc |= 1<<i;
int rc = 0;
if (IS_MCD(mcd)) {
for(int i = 0; i < 31; i++) {
if((mcd>>i) & 1) {
if (i == 0 && logfile) fflush(logfile);
if (fflush(mcd_table[i].fp)) rc |= 1<<i;
}
}
} else {
unsigned idx = FD_IDX(mcd);
if (idx < FD_MAX) rc = fflush(fd_table[idx].fp);
}
return rc;
}
int vpi_mcd_fputc(unsigned int mcd, unsigned char x)
/*
* MCD/FD Extensions
*/
extern "C" PLI_INT32 vpi_fopen(const char*name, const char*mode)
{
int i;
for(i = 0; i < 31; i++) {
if( (mcd>>i) & 1) {
return fputc(x, mcd_table[i].fp);
}
unsigned i;
for(i = 0; i < FD_MAX; i++) {
if(fd_table[i].filename == NULL)
goto got_entry;
}
return 0;
return 0; /* too many open fd's */
got_entry:
fd_table[i].fp = fopen(name, mode);
if(fd_table[i].fp == NULL)
return 0;
fd_table[i].filename = strdup(name);
return ((1U<<31)|i);
}
int vpi_mcd_fgetc(unsigned int mcd)
extern "C" FILE *vpi_get_file(PLI_INT32 fd)
{
int i;
// Only deal with FD's
if (IS_MCD(fd)) return NULL;
for(i = 0; i < 31; i++) {
if( (mcd>>i) & 1) {
return fgetc(mcd_table[i].fp);
}
}
return 0;
// Only know about FD_MAX indicies
if (FD_IDX(fd) >= FD_MAX) return NULL;
return fd_table[FD_IDX(fd)].fp;
}
/*
* $Log: vpi_mcd.cc,v $
* Revision 1.10 2003/05/23 04:04:02 steve
* Add vpi_fopen and vpi_get_file.
*
* Revision 1.9 2003/05/15 16:51:09 steve
* Arrange for mcd id=00_00_00_01 to go to stdout
* as well as a user specified log file, set log