/****************************************************************************
*
*
*   $VER: ShowTOC.c  1.0 (22 Jan 1996) by M_Campinoti CD++
*
*   $HISTORY:
*
*   22 Jan 1996 : 001.000 : First and Last release of an example that show
*                           how to read and use a CDROM TableOfContents.
*                           Didactical use, so CLI only      :)
*
****************************************************************************/

#include <proto/exec.h>
#include <exec/devices.h>
#include <exec/io.h>
#include <stdio.h>

#include "atapi_cd.h"


main (UBYTE argc,UBYTE **argv)
{
    struct IOStdReq      *ioreq = NULL ;
    struct MsgPort       *reply = NULL ;
    
    union CDTOC          tocarray[100];  /* Max 99 tracks + summary */
    
    UBYTE                firsttrack;
    UBYTE                lasttrack;
    ULONG                totalsectors;
    UBYTE                entries;

    if (argc==0) return;   /* it came from workbench ..... */

    if( reply = CreateMsgPort() )
    {
      if( ioreq = (struct IOStdReq *)
             CreateIORequest(reply ,sizeof(struct  IOStdReq)) )
      {
        if(!OpenDevice("cd.device",0,(struct IORequest*)ioreq,NULL) )
        {
          ioreq->io_Command = CD_TOCLSN;        /* Retrieve TOC information */
          ioreq->io_Offset  = 0;                /* Start with summary info  */
          ioreq->io_Length  = 100;              /* Max 99 tracks + summary  */
          ioreq->io_Data    = (APTR)tocarray;   /* Here's where we want it  */

          DoIO((struct IORequest*)ioreq);
          
          if (!ioreq->io_Error)                   /* Command succeeded        */
          {
            
            /* Now we read the TOC Summary of our CD ...    */
            
            firsttrack   = tocarray[0].Summary.FirstTrack;
            lasttrack    = tocarray[0].Summary.LastTrack;
            totalsectors = tocarray[0].Summary.LeadOut.LSN - tocarray[1].Entry.Position.LSN;
          
            /* ... and here we printout the results !          */
            
            printf("------------------------------------------\n");
            printf("| ShowTOC ©1995-1996 by M.Campinoti CD++ |\n");
            printf("------------------------------------------\n");
            printf("    First Track : %d\n",firsttrack);
            printf("    Last  Track : %d\n",lasttrack);
            printf("    Total Tracks: %d\n",(lasttrack-firsttrack)+1);
            printf("------------------------------------------\n");
            printf("    Total Sectors: %d\n",totalsectors);
            printf("------------------------------------------\n\n");
            
            printf("---Track Number------Position (LSN Format)--\n");
            for(entries=1;entries<=lasttrack;entries++)
            {
                if(entries<10)
                    printf("    %d                  %d \n",
                            tocarray[entries].Entry.Track,
                            tocarray[entries].Entry.Position);
                else
                    printf("    %d                 %d \n",
                            tocarray[entries].Entry.Track,
                            tocarray[entries].Entry.Position);

            }
            printf("------------------------------------------\n\n");
          
          }
          else printf("I/O error !\n");      /* analyze ioreq->io_Error to know what kind ... */
                                             /* ... see atapi_cd.h for #defines of it !       */
          
          CloseDevice((struct IORequest *)ioreq) ;
        }
        
        DeleteIORequest(ioreq) ;
      }

      DeleteMsgPort(reply);
    }
}

