home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / sd386v50.zip / sd386src.zip / FRSTUFF.C < prev    next >
Text File  |  1995-05-04  |  8KB  |  148 lines

  1. /*****************************************************************************/
  2. /* File:                                             IBM INTERNAL USE ONLY   */
  3. /*   frstuff.c                                                               */
  4. /*                                                                           */
  5. /* Description:                                                              */
  6. /*   fr DosOpen, etc. routines collection point.                             */
  7. /*                                                                           */
  8. /* Static Functions:                                                         */
  9. /*                                                                           */
  10. /* External Functions:                                                       */
  11. /*                                                                           */
  12. /*                                                                           */
  13. /* History:                                                                  */
  14. /*                                                                           */
  15. /*   03/28/91 Culled out of ldsource.c to compile routines w/no INCL-16.     */
  16. /*                                                                           */
  17. /*...Release 1.00                                                            */
  18. /*...                                                                        */
  19. /*... 03/28/91  103   first attempts...                                      */
  20. /*... 05/21/91  105   at least one editor (system E) does not add a newline  */
  21. /*                    automatically at the end of file marker. The program   */
  22. /*                    previously assumed this.                               */
  23. /*...                                                                        */
  24. /*...Release 1.00 (Pre-release 1.08 12/05/91)                                */
  25. /*...                                                                        */
  26. /*... 12/27/91  503   Srinivas  Last char of each line missing.              */
  27. /*...                                                                        */
  28. /**Includes*******************************************************************/
  29.  
  30. #include "all.h"                        /* SD86 include files                */
  31.  
  32. ushort FRlength = 0;                                                    /*101*/
  33.  
  34. typedef struct {
  35.   HFILE  handle;  /* OS/2 file handle */ /* changed to HFILE              100*/
  36.   ushort offset;  /* buffer offset of next unused byte */    /* to ushort 100*/
  37.   ushort buflen;  /* # of bytes in buffer (less FRINFO) */   /* to ushort 100*/
  38.   ushort togo;    /* # of bytes unused in buffer */          /* to ushort 100*/
  39. } FRINFO;
  40.  
  41.  uint
  42. FRopen(uchar *filename,uchar *buffer,ushort buflen)
  43. {
  44.     int rc;
  45.     ulong   action;                     /*                                103*/
  46.     FRINFO *p = (FRINFO*)buffer;        /* was register.                  112*/
  47.  
  48.     rc = DosOpen(filename, (PHFILE)&(p->handle), (PULONG)&action,       /*103*/
  49.         0L,     /* primary allocation size for file */
  50.         0x00,   /* file attributes */
  51.         0x0001, /* open existing file */
  52.         0x00A0, /* no inherit, deny write, read only */
  53.         0L);    /* reserved */
  54.  
  55.     if( rc )
  56.      return( rc );
  57.  
  58.     p->togo = 0;
  59.     p->offset = sizeof(FRINFO);
  60.     p->buflen = ( ushort )(buflen - sizeof(FRINFO));
  61.  
  62.     return( 0 );
  63. }
  64.  
  65.     uint
  66. FRclose(uchar *buffer )
  67. {
  68.     return( DosClose( ((FRINFO*)buffer)->handle ) );
  69. }
  70.  
  71.     uchar *
  72. FRread(uchar *buffer)
  73. {
  74.     uchar *cat;
  75.     ushort n, togo;                     /* these are 2-byte variables     100*/
  76.     ulong  BytesRead = 0;               /*                                103*/
  77.     FRINFO *p = (FRINFO*)buffer;
  78.  
  79.     APIRET  rc;                         /*                                101*/
  80.     HFILE   hFile;                      /*                                101*/
  81.     PVOID   pBuffer;                    /*                                101*/
  82.     ULONG   cbRead;                     /*                                101*/
  83.     PULONG  pcbActual;                  /*                                101*/
  84.     uchar   transcat[80];               /* 105 */
  85.  
  86.     for(;;){
  87.         cat = (uchar*)p + p->offset;
  88.         togo = p->togo;
  89.         if( togo )
  90.         {
  91.             n = bindex(cat, togo, '\n');
  92.             if( n < togo )
  93.             {
  94.                 p->togo -= n+1;
  95.                 p->offset += n+1;
  96.                 /*************************************************************/
  97.                 /* In some cases we don't have a \r preceding \n, so the  503*/
  98.                 /* earlier assumption of \r preceding \n is not always    503*/
  99.                 /* valid.                                                 503*/
  100.                 /*************************************************************/
  101.                 if (cat[n-1] == '\r')                                   /*503*/
  102.                 {                                                       /*503*/
  103.                   cat[ n-1 ] = 0;                                       /*503*/
  104.                   FRlength = n-1;                                       /*503*/
  105.                 }                                                       /*503*/
  106.                 else                                                    /*503*/
  107.                 {                                                       /*503*/
  108.                   cat[ n ] = 0;                                         /*503*/
  109.                   FRlength = n;                                         /*503*/
  110.                 }                                                       /*503*/
  111.                 return( cat );
  112.             }
  113. /* With at least one editor (I found this in the E system editor), a newline*/
  114. /* character is NOT put immediately before the EOF marker. The code was     */
  115. /* modified to handle this problem - we assumed that there was always a     */
  116. /* newline. */
  117.  
  118.             /* if the end of file is the last character, and there are still*/
  119.             /* other characters left 105 */
  120.             else if ((cat[togo-1] == 0x001A) &&                         /*105*/
  121.                      (togo > 1))                                        /*105*/
  122.                 {                                                       /*105*/
  123.                 /* put it in a different string so we don't mess with the    */
  124.                 /* original string */
  125.                 strcpy(transcat,cat);                                   /*105*/
  126.                 transcat[togo -1] = 0;  /* return up to EOF char          105*/
  127.                 p->offset += togo - 1;  /* point to EOF                   105*/
  128.                 p->togo = 1;            /* just the EOF char to go        105*/
  129.                 FRlength = (ushort)(togo - 1);                          /*105*/
  130.                 return(transcat);                                       /*105*/
  131.                 }                                                       /*105*/
  132.         }
  133.         if( togo )
  134.             memcpy( (uchar*)p + sizeof(FRINFO), cat, togo );            /*100*/
  135.  
  136.  
  137.         p->offset = sizeof(FRINFO);                                     /*101*/
  138.         hFile = p->handle;                                              /*101*/
  139.         pBuffer = (uchar*)p + togo + p->offset;                         /*101*/
  140.         cbRead = p->buflen - togo;                                      /*101*/
  141.         pcbActual = &BytesRead;                                         /*101*/
  142.         rc = DosRead(hFile, pBuffer, cbRead,pcbActual);                 /*101*/
  143.         if ( rc || (BytesRead == 0) )                                   /*101*/
  144.          return( NULL );                                                /*101*/
  145.         p->togo += BytesRead;                                           /*101*/
  146.     }
  147. }
  148.