home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 5 Edit / 05-Edit.zip / ed32ger1.zip / source / efile.c < prev    next >
C/C++ Source or Header  |  1994-02-19  |  4KB  |  120 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.    ULONG ac;   /* action code (from DosOpen) */         /* (tkg) */
  23.    ULONG rc;   /* return code (from API functions) */   /* (tkg) */
  24.    ULONG rd;   /* bytes actually read */                /* (tkg) */
  25.    ULONG cbSize;   /* file size in bytes */
  26.    ULONG dummy;   /* used when seeking back to beginning of file */ 
  27.    PVOID buffer;  /* inserted tkg. Pointer to the allocated space. Will be given to b as result */
  28.    /* SEL sel;  not longer used (tkg),  segment selector -- for text buffer */
  29.  
  30.    /* try to open existing file --  access */
  31.    rc = DosOpen (fn, &file, &ac, 0L, 0, 0x0001, 0x20C0, 0L);
  32.    if (rc != 0 || ac != 0x0001)    /* failed */
  33.       return CANTREAD;
  34.    
  35.    /* DosChgFilePtr (file, 0L, 2, &cbSize); now: (tkg) */
  36.    DosSetFilePtr (file, 0L, FILE_END, &cbSize);  /* determine file size */
  37.    if (cbSize > 65535L)
  38.       return TOOLONG;
  39.  
  40.    /* DosChgFilePtr (file, 0L, 0, &dummy);  now: (tkg) */
  41.    DosSetFilePtr (file, 0L, FILE_BEGIN, &dummy);   /* reset pointer to beginning */
  42.    
  43.    /* rc = DosAllocSeg ((int)cbSize, &sel, 0); replaced by (tkg): */
  44.    rc = DosAllocMem (&buffer, cbSize, PAG_READ | PAG_WRITE | PAG_COMMIT);
  45.    if (rc != 0)
  46.       return NOMEMORY;
  47.       
  48.    /* *b = MAKEP (sel, 0); not longer needed (tkg), buffer pointer */
  49.    *b = (PCHAR) buffer;
  50.  
  51.    /* read entire file into buffer */   
  52.    DosRead (file, *b, cbSize, &rd);  /* not (int)cbSize but ... (tkg) */
  53.    DosClose (file);
  54.    
  55.    return (LONG) cbSize;
  56. }
  57.  
  58.  
  59. /* create a dynamically allocated buffer to use for writing to file */
  60. /* on success, return 0;  on failure return NOMEMORY                */
  61. LONG MakeWriteBuffer (LONG cb, PCHAR *b)
  62. {
  63.    ULONG rc;   /* return code (from API functions) */    /* tkg */
  64.    PVOID buffer;  /* inserted tkg. Pointer to the allocated space. Will be given to b as result */
  65.    /* SEL sel; not needed (tkg), segment selector -- for text buffer */
  66.    
  67.    /* rc = DosAllocSeg ((int)cb, &sel, 0); replaced by (tkg): */
  68.    rc = DosAllocMem (&buffer, (ULONG) cb, PAG_READ | PAG_WRITE | PAG_COMMIT);
  69.    
  70.    if (rc != 0)
  71.       return NOMEMORY;
  72.       
  73.    /* *b = MAKEP (sel, 0); not longer needed (tkg), buffer pointer */
  74.    *b = (PCHAR) buffer;
  75.    return 0;
  76. }
  77.  
  78.  
  79. /* write buffer to file (cb is number of bytes in buffer) */
  80. /* on success return 0;  on failure return CANTWRITE      */
  81. LONG WriteFile (char *fn, LONG cb, PCHAR b)
  82. {
  83.    HFILE file;   /* handle */
  84.    ULONG ac;   /* action code (from DosOpen) */        /* (tkg) */
  85.    ULONG rc;   /* return code (from API functions) */  /* (tkg) */
  86.    ULONG wr;   /* bytes actually written */            /* (tkg) */
  87.  
  88.    /* create file, overwriting any existing file */
  89.    rc = DosOpen (fn, &file, &ac, 0L, 0, 0x0012, 0x20C1, 0L);
  90.    if (rc == 0 && (ac == 0x0002 || ac == 0x0003)) {    /* ok */
  91.       /* write entire buffer to file */   
  92.       rc = DosWrite (file, b, (USHORT) cb, &wr);
  93.       if (rc != 0 || wr != (USHORT) cb)
  94.          return CANTWRITE;
  95.          
  96.       /* close file */
  97.       rc = DosClose (file);
  98.       if (rc != 0)
  99.          return CANTWRITE;
  100.          
  101.       return 0;
  102.    }
  103.    else
  104.       return CANTWRITE;
  105. }
  106.  
  107.  
  108. /* release storage */
  109. VOID Release (PCHAR b)
  110. {
  111.    /* SEL sel;
  112.    
  113.     * sel = SELECTOROF (b);
  114.     * DosFreeSeg (sel);
  115.     * 
  116.     * much simplier under OS/2 2.0:
  117.     */
  118.     DosFreeMem (b);
  119. }
  120.