home *** CD-ROM | disk | FTP | other *** search
/ Aminet 33 / Aminet 33 - October 1999.iso / Aminet / disk / misc / TransADF.lha / TransADF / Source / read_disk.c < prev    next >
Encoding:
C/C++ Source or Header  |  1998-10-05  |  3.5 KB  |  115 lines

  1. /* read_disk.c - Read a disk straight into a file
  2. ** Copyright (C) 1997,1998 Karl J. Ots
  3. ** 
  4. ** This program is free software; you can redistribute it and/or modify
  5. ** it under the terms of the GNU General Public License as published by
  6. ** the Free Software Foundation; either version 2 of the License, or
  7. ** (at your option) any later version.
  8. ** 
  9. ** This program is distributed in the hope that it will be useful,
  10. ** but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  12. ** GNU General Public License for more details.
  13. ** 
  14. ** You should have received a copy of the GNU General Public License
  15. ** along with this program; if not, write to the Free Software
  16. ** Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
  17. */
  18.  
  19. #include <exec/types.h>
  20. #include <exec/memory.h>
  21. #include <dos/dos.h>
  22. #include <clib/exec_protos.h>
  23. #include <clib/dos_protos.h>
  24.  
  25. #include "read_disk.h"
  26. #include "main.h"
  27. #include "mem_chunks.h"
  28. #include "device.h"
  29. #include "util.h"
  30. #include "errors.h"
  31.  
  32.  
  33. #define RD_TBTRACKS 1                           /* Tracks in rd_TrackBuf */
  34. #define RD_TBSIZE   (RD_TBTRACKS * trackSize)   /* Bytes in rd_TrackBuf  */
  35.  
  36. UBYTE *rd_TrackBuf;    /* Our input buffer - reads from disk. */
  37.  
  38.  
  39. /*
  40. ** Read a disk into a file.
  41. ** Expects all fields of adfPkt to be set.
  42. */
  43. void readDisk (struct ADF_Packet *adfPkt)
  44. {
  45.   BYTE IOError;
  46.   LONG DOSError, RWSize;
  47.   int i;
  48.   
  49.   
  50.   /* Output info header */
  51.   FPrintf (StdOut, "Reading from %s unit %ld (%s:) to %s.\n",
  52.                    adfPkt->devInfo->deviceName,
  53.                    adfPkt->devInfo->deviceUnit,
  54.                    adfPkt->devInfo->dosName,
  55.                    adfPkt->ADFileName);
  56.   
  57.   FPrintf (StdOut, "Starting at track %ld, Ending at track %ld.\n",
  58.                    (adfPkt->startTrack/adfPkt->devInfo->numHeads),
  59.                    (adfPkt->endTrack/adfPkt->devInfo->numHeads));
  60.   
  61.   /* Allocate track buffer */
  62.   rd_TrackBuf = (UBYTE *) myAllocMem (RD_TBSIZE, MEMF_CLEAR);
  63.   if (!rd_TrackBuf)
  64.   {
  65.     /* Out of memory! */
  66.     FPrintf (StdErr, "%s: Out of memory.\n", ProgName);
  67.     cleanExit (RETURN_FAIL, ERROR_NO_FREE_STORE);
  68.   }
  69.   
  70.   /* Start reading */
  71.   for (i = adfPkt->startTrack; i <= adfPkt->endTrack; i++)
  72.   {
  73.     /* Check for Control-C break */
  74.     if (CTRL_C)
  75.     {
  76.       FPutC (StdOut, '\n');
  77.       FPrintf (StdErr, "%s - %s\n", breakText, ProgName);
  78.       cleanExit (RETURN_WARN, NULL);
  79.     }
  80.     
  81.     /* Update progress information */
  82.     FPrintf (StdOut, "\rReading track %ld side %ld",
  83.                      (i/adfPkt->devInfo->numHeads),
  84.                      (i%adfPkt->devInfo->numHeads));
  85.     Flush (StdOut);
  86.     
  87.     /* Fill the buffer from disk */
  88.     IOError = readTrack (rd_TrackBuf, RD_TBTRACKS, i, adfPkt->diskReq);
  89.     if (IOError)
  90.     {
  91.       FPutC (StdOut, '\n');
  92.       FPrintf (StdErr, "%s: Error reading from %s: - ", ProgName,
  93.                                                         adfPkt->devInfo->dosName);
  94.       reportIOError (IOError);
  95.       cleanExit (RETURN_ERROR, NULL);
  96.     }
  97.     
  98.     /* Write the buffer to the output file */
  99.     RWSize = Write (adfPkt->ADFile, rd_TrackBuf, RD_TBSIZE);
  100.     if (RWSize != RD_TBSIZE)
  101.     {
  102.       DOSError = IoErr();
  103.       
  104.       FPutC (StdOut, '\n');
  105.       FPrintf (StdErr, "%s: Error writing to %s - ",ProgName,
  106.                                                     adfPkt->ADFileName);
  107.       reportDOSError (DOSError);
  108.       cleanExit (RETURN_ERROR, DOSError);
  109.     }
  110.   }
  111.   
  112.   /* Free buffer */
  113.   myFreeMem (rd_TrackBuf);
  114. }
  115.