home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / mnth0107.zip / Anderson / source / efile.c < prev    next >
C/C++ Source or Header  |  1992-01-25  |  3KB  |  107 lines

  1. #define INCL_WINHELP
  2. #define INCL_WIN
  3. #define INCL_GPI
  4. #define INCL_DOS
  5. #include <os2.h>
  6. #include <stdio.h>
  7. #include <string.h>
  8. #include <stdlib.h>
  9. #include "efile.h"
  10. #include "edit.h"
  11.  
  12. /* open a file, read file into dynamically allocated buffer */
  13. /* b is a pointer to the beginning of the buffer            */
  14. /* On success, return file size (in bytes)                  */
  15. /* On failure, return:                                      */
  16. /*               CANTREAD if unable to open file            */ 
  17. /*               TOOLONG if file too big (> 64K)            */
  18. /*               NOMEMORY if unable to allocate memory      */
  19. LONG ReadFile (char *fn, PCHAR *b)
  20. {
  21.    HFILE file;   /* handle */
  22.    USHORT ac;   /* action code (from DosOpen) */
  23.    USHORT rc;   /* return code (from API functions) */
  24.    USHORT rd;   /* bytes actually read */
  25.    ULONG cbSize;   /* file size in bytes */
  26.    ULONG dummy;   /* used when seeking back to beginning of file */ 
  27.    SEL sel;   /* segment selector -- for text buffer */
  28.  
  29.    /* try to open existing file --  access */
  30.    rc = DosOpen (fn, &file, &ac, 0L, 0, 0x0001, 0x20C0, 0L);
  31.    if (rc != 0 || ac != 0x0001)    /* failed */
  32.       return CANTREAD;
  33.    
  34.    DosChgFilePtr (file, 0L, 2, &cbSize);   /* determine file size */
  35.    if (cbSize > 65535L)
  36.       return TOOLONG;
  37.  
  38.    DosChgFilePtr (file, 0L, 0, &dummy);   /* reset pointer to beginning */
  39.    
  40.    rc = DosAllocSeg ((int)cbSize, &sel, 0);
  41.    if (rc != 0)
  42.       return NOMEMORY;
  43.       
  44.    *b = MAKEP (sel, 0);   /* buffer pointer */
  45.  
  46.    /* read entire file into buffer */   
  47.    DosRead (file, *b, (int)cbSize, &rd);
  48.    DosClose (file);
  49.    
  50.    return (LONG) cbSize;
  51. }
  52.  
  53.  
  54. /* create a dynamically allocated buffer to use for writing to file */
  55. /* on success, return 0;  on failure return NOMEMORY                */
  56. LONG MakeWriteBuffer (LONG cb, PCHAR *b)
  57. {
  58.    USHORT rc;   /* return code (from API functions) */
  59.    SEL sel;   /* segment selector -- for text buffer */
  60.    
  61.    rc = DosAllocSeg ((int)cb, &sel, 0);
  62.    if (rc != 0)
  63.       return NOMEMORY;
  64.       
  65.    *b = MAKEP (sel, 0);   /* buffer pointer */
  66.    return 0;
  67. }
  68.  
  69.  
  70. /* write buffer to file (cb is number of bytes in buffer) */
  71. /* on success return 0;  on failure return CANTWRITE      */
  72. LONG WriteFile (char *fn, LONG cb, PCHAR b)
  73. {
  74.    HFILE file;   /* handle */
  75.    USHORT ac;   /* action code (from DosOpen) */
  76.    USHORT rc;   /* return code (from API functions) */
  77.    USHORT wr;   /* bytes actually written */
  78.  
  79.    /* create file, overwriting any existing file */
  80.    rc = DosOpen (fn, &file, &ac, 0L, 0, 0x0012, 0x20C1, 0L);
  81.    if (rc == 0 && (ac == 0x0002 || ac == 0x0003)) {    /* ok */
  82.       /* write entire buffer to file */   
  83.       rc = DosWrite (file, b, (USHORT) cb, &wr);
  84.       if (rc != 0 || wr != (USHORT) cb)
  85.          return CANTWRITE;
  86.          
  87.       /* close file */
  88.       rc = DosClose (file);
  89.       if (rc != 0)
  90.          return CANTWRITE;
  91.          
  92.       return 0;
  93.    }
  94.    else
  95.       return CANTWRITE;
  96. }
  97.  
  98.  
  99. /* release storage */
  100. VOID Release (PCHAR b)
  101. {
  102.    SEL sel;
  103.    
  104.    sel = SELECTOROF (b);
  105.    DosFreeSeg (sel);
  106. }
  107.