| [0b990d] | 1 | /*!
 | 
|---|
 | 2 |    \file tocread.c
 | 
|---|
 | 3 |    \ingroup (PSIO)
 | 
|---|
 | 4 | */
 | 
|---|
 | 5 | 
 | 
|---|
 | 6 | #include <unistd.h>
 | 
|---|
 | 7 | #include <stdlib.h>
 | 
|---|
 | 8 | #include <util/psi3/libpsio/psio.h>
 | 
|---|
 | 9 | 
 | 
|---|
 | 10 | namespace psi3 {
 | 
|---|
 | 11 | namespace libpsio {
 | 
|---|
 | 12 | 
 | 
|---|
 | 13 | /*!
 | 
|---|
 | 14 | ** PSIO_TOCREAD(): Read the table of contents for file number 'unit'.
 | 
|---|
 | 15 | ** 
 | 
|---|
 | 16 | ** \ingroup (PSIO)
 | 
|---|
 | 17 | */
 | 
|---|
 | 18 | int psio_tocread(unsigned int unit)
 | 
|---|
 | 19 | {
 | 
|---|
 | 20 |   unsigned int i;
 | 
|---|
 | 21 |   int errcod, stream, volume, entry_size;
 | 
|---|
 | 22 |   psio_ud *this_unit;
 | 
|---|
 | 23 |   psio_tocentry *last_entry, *this_entry; 
 | 
|---|
 | 24 | 
 | 
|---|
 | 25 |   this_unit = &(psio_unit[unit]);
 | 
|---|
 | 26 |   entry_size = sizeof(psio_tocentry) - 2*sizeof(psio_tocentry *);
 | 
|---|
 | 27 | 
 | 
|---|
 | 28 |   /* Check that this unit is actually open */
 | 
|---|
 | 29 |   if(this_unit->vol[0].stream == -1) return(0);
 | 
|---|
 | 30 | 
 | 
|---|
 | 31 |   /* Seek vol[0] to its beginning */
 | 
|---|
 | 32 |   stream = this_unit->vol[0].stream;
 | 
|---|
 | 33 |   errcod = lseek(stream, 0L, SEEK_SET);
 | 
|---|
 | 34 |   if(errcod == -1) psio_error(unit,PSIO_ERROR_LSEEK);
 | 
|---|
 | 35 | 
 | 
|---|
 | 36 |   /* Read the TOC from disk */
 | 
|---|
 | 37 |   errcod = read(stream, (char *) &(this_unit->tocaddress.page),
 | 
|---|
 | 38 |                 sizeof(ULI));
 | 
|---|
 | 39 |   if(errcod != sizeof(ULI)) return(1);
 | 
|---|
 | 40 |   errcod = read(stream, (char *) &(this_unit->tocaddress.offset),
 | 
|---|
 | 41 |                 sizeof(ULI));
 | 
|---|
 | 42 |   if(errcod != sizeof(ULI)) return(1);
 | 
|---|
 | 43 |   errcod = read(stream, (char *) &(this_unit->toclen), sizeof(ULI));
 | 
|---|
 | 44 |   if(errcod != sizeof(ULI)) return(1);
 | 
|---|
 | 45 | 
 | 
|---|
 | 46 |   /* Malloc room for the TOC */
 | 
|---|
 | 47 |   this_unit->toc = (psio_tocentry *) malloc(sizeof(psio_tocentry));
 | 
|---|
 | 48 |   this_entry = this_unit->toc;
 | 
|---|
 | 49 |   this_entry->last = NULL;
 | 
|---|
 | 50 |   for(i=1; i < this_unit->toclen; i++) {
 | 
|---|
 | 51 |       last_entry = this_entry;
 | 
|---|
 | 52 |       this_entry = (psio_tocentry *) malloc(sizeof(psio_tocentry));
 | 
|---|
 | 53 |       last_entry->next = this_entry;
 | 
|---|
 | 54 |       this_entry->last = last_entry;
 | 
|---|
 | 55 |     }
 | 
|---|
 | 56 |   this_entry->next = NULL;
 | 
|---|
 | 57 | 
 | 
|---|
 | 58 |   /* Seek the TOC volume to the correct position */
 | 
|---|
 | 59 |   volume = (this_unit->tocaddress.page) % (this_unit->numvols);
 | 
|---|
 | 60 |   errcod = psio_volseek(&(this_unit->vol[volume]), this_unit->tocaddress.page,
 | 
|---|
 | 61 |                         this_unit->tocaddress.offset, this_unit->numvols);
 | 
|---|
 | 62 |   if(errcod == -1) psio_error(unit,PSIO_ERROR_LSEEK);
 | 
|---|
 | 63 | 
 | 
|---|
 | 64 |   /* Read the TOC entry-by-entry */
 | 
|---|
 | 65 |   this_entry = this_unit->toc;
 | 
|---|
 | 66 |   for(i=0; i < this_unit->toclen; i++) {
 | 
|---|
 | 67 | 
 | 
|---|
 | 68 |       /* This read() assumes a fixed ordering on the members of this_entry */
 | 
|---|
 | 69 |       errcod = read(this_unit->vol[volume].stream, (char *) this_entry,
 | 
|---|
 | 70 |                     entry_size);
 | 
|---|
 | 71 |       if(errcod != entry_size) psio_error(unit,PSIO_ERROR_READ);
 | 
|---|
 | 72 | 
 | 
|---|
 | 73 |       this_entry = this_entry->next;
 | 
|---|
 | 74 |     }
 | 
|---|
 | 75 | 
 | 
|---|
 | 76 |   return(0);
 | 
|---|
 | 77 | }
 | 
|---|
 | 78 | 
 | 
|---|
 | 79 | }
 | 
|---|
 | 80 | }
 | 
|---|