home *** CD-ROM | disk | FTP | other *** search
/ Education Sampler 1992 [NeXTSTEP] / Education_1992_Sampler.iso / SoundAndMusic / cmix / tape.backup / sfcodes.c < prev    next >
C/C++ Source or Header  |  1991-02-08  |  2KB  |  81 lines

  1. #include <stdio.h>
  2. #include <sys/file.h>
  3. #include "../H/sfheader.h"
  4.  
  5. /* Find location in header for the coded information. 
  6.    Return a pointer to the beginning of the SFCODE structure not
  7.    to the information structure itself. The srate, number of channels, magic
  8.    number, number of bytes per channel are NOT coded via these routines. */
  9.  
  10. char *getsfcode(hd,code)
  11.     SFHEADER *hd;
  12. {
  13.     register char *sfc;
  14.     register SFCODE *sp;
  15.     char *hdend = (char *) hd + sizeof(SFHEADER);
  16.  
  17.     sfc = &sfcodes(hd); 
  18.     while(sfc < hdend) {
  19.         sp = (SFCODE *) sfc;
  20.         if(sp->code == code)
  21.             return(sfc);
  22.         if(sp->code == SF_END)
  23.             break;
  24.         /* Catch possible wrap around on stack from bad header */
  25.         /* or a zero struct size from bad header */
  26.         if(sp->bsize == 0 || sfc + sp->bsize < &sfcodes(hd))
  27.             break;
  28.         else
  29.             sfc += sp->bsize;
  30.     }
  31.     return(NULL);
  32. }
  33.  
  34. SFCODE endcode = {
  35.     SF_END,
  36.     sizeof(SFCODE)
  37. } ;
  38.  
  39. putsfcode(hd,ptr,codeptr)
  40.     SFHEADER *hd;
  41.     char *ptr;
  42.     SFCODE *codeptr;
  43. {
  44.     register char *sfc;
  45.     register SFCODE *sp;
  46.     int wasendcode = 0;
  47.     char *hdend = (char *) hd + sizeof(SFHEADER);
  48.  
  49.     sfc = &sfcodes(hd); 
  50.     while(sfc < hdend) {
  51.         sp = (SFCODE *) sfc;
  52.         if(sp->code == codeptr->code)
  53.             break;
  54.         if(sp->code == SF_END) {
  55.             wasendcode = 1;
  56.             break;
  57.         }
  58.         /* Catch possible wrap around on stack from bad header */
  59.         if(sp->bsize == 0 || sfc + sp->bsize < (char *) hd) 
  60.             sp->code = SF_END; /* Force an end */
  61.         else
  62.             sfc += sp->bsize;
  63.     }
  64.     
  65.     /* No space left */
  66.     if(sfc + codeptr->bsize > hdend)
  67.         return(-1);
  68.  
  69.     if(!wasendcode)  /* Not a new one */
  70.         if(codeptr->bsize != sp->bsize) /* Size changed */
  71.             return(-1);
  72.  
  73.     bcopy((char *) codeptr, sfc, sizeof(SFCODE));
  74.     bcopy(ptr, sfc + sizeof(SFCODE), codeptr->bsize - sizeof(SFCODE));
  75.  
  76.     if(wasendcode) 
  77.         bcopy(&endcode,sfc + codeptr->bsize,sizeof(endcode));
  78.  
  79.     return(0);
  80. }
  81.