home *** CD-ROM | disk | FTP | other *** search
/ InfoMagic Source Code 1993 July / THE_SOURCE_CODE_CD_ROM.iso / gnu / nethack-3.1 / sys / amiga / splitter / multi.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-01-10  |  2.2 KB  |  106 lines

  1. /*     SCCS Id: @(#)multi.c 3.1    93/01/08
  2. /*    Copyright (c) Kenneth Lorber, Bethesda, Maryland, 1992, 1993  */
  3. /* NetHack may be freely redistributed.  See license for details. */
  4.  
  5. /*
  6.  * NB - internal structure under development.  End users should NOT
  7.  *      get too creative!
  8.  */
  9. #include <exec/types.h>
  10. #include <exec/memory.h>
  11. #include <proto/dos.h>
  12. #include <proto/exec.h>
  13. #include <dos.h>
  14. #include <string.h>
  15. #include <assert.h>
  16. #include "multi.h"
  17.  
  18. static int start_next_file(multifh *);    /* should return enum */
  19. BPTR
  20. MultiOpen(char *dirfile, ULONG mode, union multiopts *mo){
  21.     multifh *retval;
  22.  
  23.     assert(mode==MODE_OLDFILE);    /* no chioce this version */
  24.     retval=(multifh *)AllocMem(sizeof(multifh),MEMF_CLEAR);
  25.     if(retval){
  26.         retval->mfh_dirfh=Open(dirfile,MODE_OLDFILE);
  27.         if(retval->mfh_dirfh){
  28.             retval->mfh_mo= *mo;
  29.             if(start_next_file(retval)==1){
  30.                 return((BPTR)retval);        /* success */
  31.             }
  32.         }
  33.     }
  34.  
  35.     if(retval)FreeMem(retval,sizeof(multifh));
  36.     return 0;
  37. }
  38.  
  39. ULONG
  40. MultiRead(BPTR xmfp, ULONG *where, ULONG len){
  41.     multifh *mfp=(multifh *)xmfp;
  42.     ULONG sofar=0;
  43.     ULONG this;
  44.  
  45.     if(len<=0)return len;
  46.     if(mfp->mfh_fh==0)return 0;    /* pending EOF (possibly premature) */
  47.  
  48.     while(sofar<len){
  49.         this=Read(mfp->mfh_fh,where,len-sofar);
  50.         if(this==-1) return -1;
  51.         if(this==0){
  52.             Close(mfp->mfh_fh);
  53.             mfp->mfh_fh=0;
  54.             if(start_next_file(mfp)<=0){
  55.                 return sofar;
  56.             }
  57.         }
  58.         sofar += this; where += this;
  59.     }
  60.     return sofar;
  61. }
  62.  
  63. void
  64. MultiClose(BPTR xmfp){
  65.     multifh *mfp=(multifh *)xmfp;
  66.     if(mfp->mfh_dirfh)Close(mfp->mfh_dirfh);
  67.     if(mfp->mfh_fh)Close(mfp->mfh_fh);
  68.     FreeMem(mfp,sizeof(multifh));
  69. }
  70.  
  71. /* return 0==no more data, -1 error.  else more data available unless file
  72.  * is empty
  73.  */
  74. static
  75. start_next_file(multifh *mfp){
  76.     ULONG t;
  77.     char line[100];        /* should be based on PATHLEN */
  78.     char *eol;
  79.  
  80.     while(1){
  81.         t=Read(mfp->mfh_dirfh,line,99);
  82.         if(t==0)return(0);
  83.         if(t==-1)return(-1);
  84.  
  85.         line[t]='\0';
  86.         eol=strchr(line,'\n');
  87.         if(eol){
  88.             *eol='\0';
  89.             Seek(mfp->mfh_dirfh,-(t-(eol-line))+1,OFFSET_CURRENT);
  90.         }
  91.         switch(line[0]){
  92.         case '\0':
  93.         case '#':
  94.             break;            /* comment, blank lines */
  95.         default:
  96.             if(line[0]==mfp->mfh_mo.r.mor_tag){
  97.                 mfp->mfh_fh=Open(&line[1],MODE_OLDFILE);
  98.                 if(!mfp->mfh_fh){
  99.                     return -1;    /* error */
  100.                 }
  101.                 return 1;
  102.             }
  103.         }
  104.     }
  105. }
  106.