/* * topo.c * * Summary: Top level interface for reading topology files. * * Author: David Hardy */ #include #include #include #include "topo.h" #include "topoxp.h" #include "param.h" #include "error.h" int topo_init(Topo *t, Tbuf *tbuf, Param *param) { t->tbuf = tbuf; t->param = param; t->f = NULL; t->reader = NULL; t->type = 0; return 0; } void topo_destroy(Topo *t) { /* reset everything */ memset(t, 0, sizeof(Topo)); } int topo_open(Topo *t, const char *fname, int type) { /* open topology file */ t->f = fopen(fname, "r"); if (t->f == NULL) { mdio_warn("fopen() failed in topo_open() for file: %s\n", fname); return -1; } /* for now there is only one type of topology file: XPLOR PSF */ /* allocate memory for file reader and init */ if (type == TOPO_XPLOR) { t->reader = malloc(sizeof(Topoxp)); if (t->reader == NULL) { mdio_warn("malloc() failed for Topoxp in topo_open()\n"); return -1; } t->type = TOPO_XPLOR; if (topoxp_init((Topoxp *) t->reader, t->tbuf, t->param->pdata, t->f)) { mdio_warn("topoxp_init() failed in topo_open()\n"); return -1; } } else { mdio_warn("file type %d unrecognized in topo_open()\n", type); return -1; } return 0; } int topo_close(Topo *t) { int retval = 0; /* destroy and free memory for file reader */ if (t->type == TOPO_XPLOR) { topoxp_destroy((Topoxp *) t->reader); } else { mdio_warn("file type %d unrecognized in topo_close()\n", t->type); retval = -1; } free(t->reader); t->reader = NULL; t->type = 0; /* close the file handle */ if (fclose(t->f)) { mdio_warn("fclose() failed in topo_close()\n"); retval = -1; } t->f = NULL; return retval; } int topo_read(Topo *t) { if (t->type == TOPO_XPLOR) { return topoxp_read((Topoxp *) t->reader); } else { mdio_warn("file type %d unrecognized in topo_read()\n", t->type); return -1; } }