home *** CD-ROM | disk | FTP | other *** search
/ 17 Bit Software 1: Collection A / 17Bit_Collection_A.iso / files / 290.dms / 290.adf / quickrif.source / readriff.c < prev    next >
C/C++ Source or Header  |  1989-01-31  |  3KB  |  132 lines

  1.  
  2. /* readriff.c - a slightly hacked file originating with Jim Kent at
  3.    A-Squared Systems.  Basically loads a RIFF file in two steps.  First 
  4.    you call load_riff_head().  This will get the header stuff so you
  5.    can check if you're in the right resolution etc before loading up
  6.    the bulk of the file.  load_riff_body() loads the rest, calling load_vrun,
  7.    which loads a single frame.  */
  8.  
  9. #include <exec/types.h>
  10. #include <graphics/gfx.h>
  11. #include <intuition/intuition.h>
  12. #include <libraries/dos.h>
  13. #include <functions.h>
  14. #include <ctype.h>
  15. #include "jiff.h"
  16. #include "vcomp.h"
  17. #include "playriff.h"
  18.  
  19.  
  20. /* load_vrun() -
  21.     loads one frame from the file "infile" a global variable points to.
  22.     Allocates a buffer to hold the frame, loads the frame, and returns a
  23.     pointer to the buffer if all goes well.  Returns NULL if something
  24.     goes wrong. */
  25. Vcomp_iff *
  26. load_vrun()
  27. {
  28. struct iff_chunk chunk;
  29. struct iff_chunk *vrun;    /* a little type-punning here */
  30.  
  31. if (Read(infile, &chunk, (long)sizeof(chunk)) < sizeof(chunk))
  32.     {
  33.     truncated();
  34.     return(NULL);
  35.     }
  36. if (chunk.iff_type.b4_type != VRUN)
  37.     {
  38.     fatal_load = 1;
  39.     error("not a VRUN!", NULL);
  40.     return(NULL);
  41.     }
  42. if ((vrun = SafeAllocMem(chunk.iff_length+8, 0L)) == NULL)
  43.     {
  44.     outta_memory();
  45.     return(NULL);
  46.     }
  47. *vrun = chunk;    /* fill in the 8 bytes of the vrun */
  48. if (Read(infile, vrun+1, chunk.iff_length) < chunk.iff_length)
  49.     {
  50.     truncated();
  51.     FreeMem(vrun, chunk.iff_length+8);
  52.     return(NULL);
  53.     }
  54. return((Vcomp_iff *)vrun);
  55. }
  56.  
  57.  
  58. /* load_riff_head() - opens infile on name, both globals (yuck!).  
  59.     Reads the header into demo_rh (yet another global).  Returns
  60.     1 if ok, 0 if io error etc. */
  61. load_riff_head()
  62. {
  63. fatal_load = 0;
  64. if ((infile = Open(name, (long)MODE_OLDFILE)) == NULL)
  65.     {
  66.     cant_open();
  67.     return(NULL);
  68.     }
  69. if (Read(infile, &demo_rh, (long)sizeof(demo_rh)) < sizeof(demo_rh) )
  70.     {
  71.     truncated();
  72.     goto BAD_EXIT;
  73.     }
  74. if (demo_rh.iff_type != RIFF)
  75.     {
  76.     error("Not a RIFF!", NULL);
  77.     goto BAD_EXIT;
  78.     }
  79. return(1);
  80. BAD_EXIT:
  81. Close(infile);
  82. infile = NULL;
  83. return(NULL);
  84. }
  85.  
  86. /* load_riff_body() - finishes up the load you started with load_riff_head.
  87.     stuffs the frames one at a time into riff_list and bumps the riff_count
  88.     each time it gets a frame.  Returns 1 on success, 0 on failure, and
  89.     sets fatal_load on really bad failure */
  90.  
  91. load_riff_body()
  92. {
  93. int success = 0;
  94. WORD frames_read = 0;
  95. WORD i;
  96. WORD start;
  97.  
  98. /* skip over the frame offset table ... wonder why I bothered with it... */
  99. if (Seek(infile, (long)demo_rh.frame_count*sizeof(long), 
  100.     (long)OFFSET_CURRENT) 
  101.     == -1)
  102.     {
  103.     truncated();
  104.     goto BAD_EXIT;
  105.     }
  106. start = riff_count;
  107. if (demo_rh.frames_written != 0)
  108.     demo_rh.frame_count = demo_rh.frames_written;
  109. if (demo_rh.frame_count > MAX_RIFFS - start)
  110.     demo_rh.frame_count = MAX_RIFFS - start;
  111. if (demo_rh.jiffies_frame == 0)
  112.     demo_rh.jiffies_frame = (demo_rh.height > 200 ? 8 : 4);
  113. for (i=0; i<demo_rh.frame_count; i++)
  114.     {
  115.     if ((riff_list[i+start] = load_vrun(infile, name)) == NULL)
  116.         {
  117.         if (i == 0)
  118.             goto BAD_EXIT;    /* need at least one frame! */
  119.         break;    /* not enough memory for it all, but let's go on anyways*/
  120.         }
  121.     riff_count++;
  122.     }
  123. Close(infile);
  124. return(riff_count);
  125.  
  126. BAD_EXIT:
  127. if (infile != NULL)
  128.     Close(infile);
  129. return(0);
  130. }
  131.  
  132.