/****************************************/ /* mcatio valdes 8 31 82 */ /* */ /* Matched catalog io subroutines */ /****************************************/ #include "focas1.h" #include "match1.h" /*Open catalog file and return fd*/ FILE * mcatopen (file, mode) char *file; int mode; /* If mode = 0 Read */ /*If mode = 1 Write*/ /*if mode = 2 Read / Write*/ { FILE *fd; switch (mode) { case 0: if (strcmp (file, "STDIN") == 0) fd = getstdin; else if ((fd = fopen (file, FOPEN_RO)) == NULL) { focaserr (1, "mcatopen: Can't read ", file); return (NULL); } rdmcathdr (fd, 0); break; case 1: if (strcmp (file, "STDOUT") == 0) fd = stdout; else if ((fd = fopen (file, FOPEN_WO)) == NULL) { focaserr (2, "mcatopen: Can't create / write to ", file); return (NULL); } wtmcathdr (fd, 0); break; case 2: if (access (file, 0)) { if ((fd = fopen (file, FOPEN_WO)) == NULL) { focaserr (3, "mcatopen: Can't create ", file); return (NULL); } wtmcathdr (fd, 0); fclose (fd); } if ((fd = fopen (file, FOPEN_RW)) == NULL) { focaserr (4, "mcatopen: Can't read / write to ", file); return (NULL); } rdmcathdr (fd, 0); break; } return (fd); } /*Read catalog header and return read status*/ rdmcathdr (fd, pos) FILE *fd; int pos; { fseek (fd, 0L, pos); if (fread (&ncats, 2, 1, fd) != 1) return (focaserr (5, "rdmcathdr: Error in reading catalog header", "")); return (0); } /*Write catalog header and return write status */ wtmcathdr (fd, pos) FILE *fd; int pos; { fseek (fd, 0L, pos); if (fwrite (&ncats, 2, 1, fd) != 1) return (focaserr (6, "wtmcathdr: Error in writing catalog header", "")); return (0); } /*Return 1 at eof*/ rdmcatob (fd, mentry, mob) FILE *fd; struct mentry *mentry; struct objrec *mob; { int i; if (fread (mentry, sizeof (struct mentry), 1, fd) == 0) return (1); for (i = 0; i < ncats; i++) { if (ment (i, mentry)) rdcatob (fd, 0, &mob[i]); else mob[i].entnum = -1; } return (0); } wtmcat (fd, mentry, mob) FILE *fd; struct mentry *mentry; struct objrec *mob; { int i; if (fwrite (mentry, sizeof (struct mentry), 1, fd) != 1) return (focaserr (1, "wtmcat: Error on write", "")); for (i = 0; i < ncats; i++) if (ment (i, mentry)) wtcat (&mob[i], fd); return (0); } /*Return the number of 1 bits in a char*/ charbits (a) char a; { int bits; bits = 0; if (a & 01) bits++; if (a & 02) bits++; if (a & 04) bits++; if (a & 010) bits++; if (a & 020) bits++; if (a & 040) bits++; if (a & 0100) bits++; if (a & 0200) bits++; return (bits); } /*Logical function 0 = if object i is not in entry*/ ment (i, mentry) int i; struct mentry *mentry; { return ((mentry -> entries >> i) & 01); }