home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / mnth0107.zip / Anderson / listings / efile.cls < prev    next >
Text File  |  1992-02-27  |  5KB  |  112 lines

  1.             Listing for efile.c                                         Page 31
  2.  
  3.          1430|   #define INCL_WINHELP
  4.          1431|   #define INCL_WIN
  5.          1432|   #define INCL_GPI
  6.          1433|   #define INCL_DOS
  7.          1434|   #include <os2.h>
  8.          1435|   #include <stdio.h>
  9.          1436|   #include <string.h>
  10.          1437|   #include <stdlib.h>
  11.          1438|   #include "efile.h"
  12.          1439|   #include "edit.h"
  13.          1440|   
  14.          1441|   /* open a file, read file into dynamically allocated buffer */
  15.          1442|   /* b is a pointer to the beginning of the buffer            */
  16.          1443|   /* On success, return file size (in bytes)                  */
  17.          1444|   /* On failure, return:                                      */
  18.          1445|   /*               CANTREAD if unable to open file            */ 
  19.          1446|   /*               TOOLONG if file too big (> 64K)            */
  20.          1447|   /*               NOMEMORY if unable to allocate memory      */
  21.          1448|   LONG ReadFile (char *fn, PCHAR *b)
  22.          1449|   {
  23.          1450|      HFILE file;   /* handle */
  24.          1451|      USHORT ac;   /* action code (from DosOpen) */
  25.          1452|      USHORT rc;   /* return code (from API functions) */
  26.          1453|      USHORT rd;   /* bytes actually read */
  27.          1454|      ULONG cbSize;   /* file size in bytes */
  28.          1455|      ULONG dummy;   /* used when seeking back to beginning of file */ 
  29.          1456|      SEL sel;   /* segment selector -- for text buffer */
  30.          1457|   
  31.          1458|      /* try to open existing file --  access */
  32.          1459|      rc = DosOpen (fn, &file, &ac, 0L, 0, 0x0001, 0x20C0, 0L);
  33.          1460|      if (rc != 0 || ac != 0x0001)    /* failed */
  34.          1461|         return CANTREAD;
  35.          1462|      
  36.          1463|      DosChgFilePtr (file, 0L, 2, &cbSize);   /* determine file size */
  37.          1464|      if (cbSize > 65535L)
  38.          1465|         return TOOLONG;
  39.          1466|   
  40.          1467|      DosChgFilePtr (file, 0L, 0, &dummy);   /* reset pointer to beginning */
  41.          1468|      
  42.          1469|      rc = DosAllocSeg ((int)cbSize, &sel, 0);
  43.          1470|      if (rc != 0)
  44.          1471|         return NOMEMORY;
  45.          1472|         
  46.          1473|      *b = MAKEP (sel, 0);   /* buffer pointer */
  47.          1474|   
  48.          1475|      /* read entire file into buffer */   
  49.          1476|      DosRead (file, *b, (int)cbSize, &rd);
  50.          1477|      DosClose (file);
  51.          1478|      
  52.          1479|      return (LONG) cbSize;
  53.          1480|   }
  54.          1481|   
  55.          1482|   
  56.          1483|   /* create a dynamically allocated buffer to use for writing to file */
  57.          1484|   /* on success, return 0;  on failure return NOMEMORY                */
  58. efile.c                                                                 Page 32
  59.  
  60.          1485|   LONG MakeWriteBuffer (LONG cb, PCHAR *b)
  61.          1486|   {
  62.          1487|      USHORT rc;   /* return code (from API functions) */
  63.          1488|      SEL sel;   /* segment selector -- for text buffer */
  64.          1489|      
  65.          1490|      rc = DosAllocSeg ((int)cb, &sel, 0);
  66.          1491|      if (rc != 0)
  67.          1492|         return NOMEMORY;
  68.          1493|         
  69.          1494|      *b = MAKEP (sel, 0);   /* buffer pointer */
  70.          1495|      return 0;
  71.          1496|   }
  72.          1497|   
  73.          1498|   
  74.          1499|   /* write buffer to file (cb is number of bytes in buffer) */
  75.          1500|   /* on success return 0;  on failure return CANTWRITE      */
  76.          1501|   LONG WriteFile (char *fn, LONG cb, PCHAR b)
  77.          1502|   {
  78.          1503|      HFILE file;   /* handle */
  79.          1504|      USHORT ac;   /* action code (from DosOpen) */
  80.          1505|      USHORT rc;   /* return code (from API functions) */
  81.          1506|      USHORT wr;   /* bytes actually written */
  82.          1507|   
  83.          1508|      /* create file, overwriting any existing file */
  84.          1509|      rc = DosOpen (fn, &file, &ac, 0L, 0, 0x0012, 0x20C1, 0L);
  85.          1510|      if (rc == 0 && (ac == 0x0002 || ac == 0x0003)) {    /* ok */
  86.          1511|         /* write entire buffer to file */   
  87.          1512|         rc = DosWrite (file, b, (USHORT) cb, &wr);
  88.          1513|         if (rc != 0 || wr != (USHORT) cb)
  89.          1514|            return CANTWRITE;
  90.          1515|            
  91.          1516|         /* close file */
  92.          1517|         rc = DosClose (file);
  93.          1518|         if (rc != 0)
  94.          1519|            return CANTWRITE;
  95.          1520|            
  96.          1521|         return 0;
  97.          1522|      }
  98.          1523|      else
  99.          1524|         return CANTWRITE;
  100.          1525|   }
  101.          1526|   
  102.          1527|   
  103.          1528|   /* release storage */
  104.          1529|   VOID Release (PCHAR b)
  105.          1530|   {
  106.          1531|      SEL sel;
  107.          1532|      
  108.          1533|      sel = SELECTOROF (b);
  109.          1534|      DosFreeSeg (sel);
  110.          1535|   }
  111.          1536|   
  112.