/* See {OctfIO.h} */ #include /* Last edited on 2023-02-12 07:47:30 by stolfi */ #include #include #include #include #include #include #include #include #define _GNU_SOURCE #include #include #include #define OctfIO_C_copyright \ "Copyright © 1998 Universidade Estadual de Campinas (UNICAMP)" #define OCTF_FILE_TYPE "facet-edge" #define OCTF_FILE_VERSION "2007-02-03" /* File type and version for {FOctfWrite} files (Stolfi's 2007 format) */ void OctfWrite(char *name, char *tag, Place_vec_t root, char *cmt) char *fileName = jsprintf("%s%s.fe", name, tag); FILE *wr = open_write(fileName, TRUE); FOctfWrite(wr, root, cmt); fclose(wr); } void OctfRead(char *name, char *tag, Place_vec_t *rootP, char **cmtP) char *fileName = jsprintf("%s%s.fe", name, tag); FILE *rd = open_read(fileName, TRUE); FOctfRead(rd, rootP, cmtP); fclose(rd); } void FOctfWrite(FILE *wr, Place_vec_t root, char *cmt) { uint NR = root.ne; /* Get the wedges: */ Wedge_vec_t wtb = EnumWedges(root, NULL); uint NW = wtb.ne; /* Write the file header line and the client comments: */ filefmt_write_header(wr, OCTF_FILE_TYPE, OCTF_FILE_VERSION); filefmt_write_comment(wr, cmt, '|'); /* Write the element counts and other global fields: */ fprintf(wr, "wedges = %d\n", NW); fprintf(wr, "roots = %d\n", NR); int i; /* Write the wedge data: */ uint wWidth = (NW <= 1 ? 1 : digits(NW-1)); /* Max digits in wedge indices. */ fprintf(wr, "# wedges:\n"); for (i = 0; i < NW; i++) { Wedge_t w = wtb.e[i]; fprintf(wr, "%*d ", wWidth, i); PrintWedge(wr, w, wWidth); fprintf(wr, "\n"); } /* Write the root places: */ uint rWidth = (NR <= 1 ? 1 : digits(NR-1)); /* Max digits in root indices. */ fprintf(wr, "# roots:\n"); for (i = 0; i < NR; i++) { Place_t r = root.e[i]; fprintf(wr, "%*d ", rWidth, i); PrintPlace(wr, r, wWidth, TRUE); } /* Finish off: */ filefmt_write_footer(wr, OCTF_FILE_TYPE); fflush(wr); } void FOctfRead(FILE *rd, Place_vec_t *rootP, char **cmtP) { char *cmt; /* Read the file header and comments: */ filefmt_read_header(rd, OCTF_FILE_TYPE, OCTF_FILE_VERSION); cmt = filefmt_read_comment(rd, '|'); /* Wedge and root counts: */ uint NW = nget_uint32(rd, "wedges", 10); fget_eol(rd); uint NR = nget_uint32(rd, "roots", 10); fget_eol(rd); int i; /* Create the wedge table and the wedge records: */ Wedge_vec_t wtb = Wedge_vec_new(NW); for (i = 0; i < NW; i++) { Wedge_t w = MakeWedge(); wtb.e[i] = w; w->num = i; } /* Read wedge data: */ fget_match(rd, "# wedges:"); fget_eol(rd); for (i = 0; i < NW; i++) { /* Read the wedge index: */ uint wn = fget_uint32(rd, 10); /* Wedge index. */ Wedge_t w = wtb.e[wn]; demand(wn == i, "wedge index mismatch"); /* Read the wedge-to-wedge pointers {w->next[0..3]}: */ ReadWedge(rd, w, &wtb); /* Skip the end-of-line character: */ fget_eol(rd); } /* Read the root places: */ Place_vec_t root = Place_vec_new(NR); fget_match(rd, "# roots:"); fget_eol(rd); for (i = 0; i < NR; i++) { /* Read the root index: */ uint rn = fget_uint32(rd, 10); /* Wedge index. */ demand(rn == i, "wedge index mismatch"); root.e[rn] = ReadPlace(rd, &wtb); /* Skip the end-of-line character: */ fget_eol(rd); } filefmt_read_footer(rd, OCTF_FILE_TYPE); if (rootP->e != NULL) { free(rootP->e); } (*rootP) = root; if (cmtP != NULL) { (*cmtP) = cmt; } else { free(cmt); } } #define OctfIO_C_author \ "Created 2007 by J. Stolfi, based on Modula-3 code by\n" \ " R. M. Rosi and J. Stolfi (1994-1996) and L.A.P.Lozada (1999-2000).\n" \ "Revisions:\n" \ " 2007-02-03 Split off {FOctfRead} from {FReadTopology}.\n" \ " 2007-02-03 Split off {FOctfWrite} from {FWriteTopology}."