home *** CD-ROM | disk | FTP | other *** search
- #define INCL_WINHELP
- #define INCL_WIN
- #define INCL_GPI
- #define INCL_DOS
- #include <os2.h>
- #include <stdio.h>
- #include <string.h>
- #include <stdlib.h>
- #include "efile.h"
- #include "edit.h"
-
- /* open a file, read file into dynamically allocated buffer */
- /* b is a pointer to the beginning of the buffer */
- /* On success, return file size (in bytes) */
- /* On failure, return: */
- /* CANTREAD if unable to open file */
- /* TOOLONG if file too big (> 64K) */
- /* NOMEMORY if unable to allocate memory */
- LONG ReadFile (char *fn, PCHAR *b)
- {
- HFILE file; /* handle */
- ULONG ac; /* action code (from DosOpen) */ /* (tkg) */
- ULONG rc; /* return code (from API functions) */ /* (tkg) */
- ULONG rd; /* bytes actually read */ /* (tkg) */
- ULONG cbSize; /* file size in bytes */
- ULONG dummy; /* used when seeking back to beginning of file */
- PVOID buffer; /* inserted tkg. Pointer to the allocated space. Will be given to b as result */
- /* SEL sel; not longer used (tkg), segment selector -- for text buffer */
-
- /* try to open existing file -- access */
- rc = DosOpen (fn, &file, &ac, 0L, 0, 0x0001, 0x20C0, 0L);
- if (rc != 0 || ac != 0x0001) /* failed */
- return CANTREAD;
-
- /* DosChgFilePtr (file, 0L, 2, &cbSize); now: (tkg) */
- DosSetFilePtr (file, 0L, FILE_END, &cbSize); /* determine file size */
- if (cbSize > 65535L)
- return TOOLONG;
-
- /* DosChgFilePtr (file, 0L, 0, &dummy); now: (tkg) */
- DosSetFilePtr (file, 0L, FILE_BEGIN, &dummy); /* reset pointer to beginning */
-
- /* rc = DosAllocSeg ((int)cbSize, &sel, 0); replaced by (tkg): */
- rc = DosAllocMem (&buffer, cbSize, PAG_READ | PAG_WRITE | PAG_COMMIT);
- if (rc != 0)
- return NOMEMORY;
-
- /* *b = MAKEP (sel, 0); not longer needed (tkg), buffer pointer */
- *b = (PCHAR) buffer;
-
- /* read entire file into buffer */
- DosRead (file, *b, cbSize, &rd); /* not (int)cbSize but ... (tkg) */
- DosClose (file);
-
- return (LONG) cbSize;
- }
-
-
- /* create a dynamically allocated buffer to use for writing to file */
- /* on success, return 0; on failure return NOMEMORY */
- LONG MakeWriteBuffer (LONG cb, PCHAR *b)
- {
- ULONG rc; /* return code (from API functions) */ /* tkg */
- PVOID buffer; /* inserted tkg. Pointer to the allocated space. Will be given to b as result */
- /* SEL sel; not needed (tkg), segment selector -- for text buffer */
-
- /* rc = DosAllocSeg ((int)cb, &sel, 0); replaced by (tkg): */
- rc = DosAllocMem (&buffer, (ULONG) cb, PAG_READ | PAG_WRITE | PAG_COMMIT);
-
- if (rc != 0)
- return NOMEMORY;
-
- /* *b = MAKEP (sel, 0); not longer needed (tkg), buffer pointer */
- *b = (PCHAR) buffer;
- return 0;
- }
-
-
- /* write buffer to file (cb is number of bytes in buffer) */
- /* on success return 0; on failure return CANTWRITE */
- LONG WriteFile (char *fn, LONG cb, PCHAR b)
- {
- HFILE file; /* handle */
- ULONG ac; /* action code (from DosOpen) */ /* (tkg) */
- ULONG rc; /* return code (from API functions) */ /* (tkg) */
- ULONG wr; /* bytes actually written */ /* (tkg) */
-
- /* create file, overwriting any existing file */
- rc = DosOpen (fn, &file, &ac, 0L, 0, 0x0012, 0x20C1, 0L);
- if (rc == 0 && (ac == 0x0002 || ac == 0x0003)) { /* ok */
- /* write entire buffer to file */
- rc = DosWrite (file, b, (USHORT) cb, &wr);
- if (rc != 0 || wr != (USHORT) cb)
- return CANTWRITE;
-
- /* close file */
- rc = DosClose (file);
- if (rc != 0)
- return CANTWRITE;
-
- return 0;
- }
- else
- return CANTWRITE;
- }
-
-
- /* release storage */
- VOID Release (PCHAR b)
- {
- /* SEL sel;
-
- * sel = SELECTOROF (b);
- * DosFreeSeg (sel);
- *
- * much simplier under OS/2 2.0:
- */
- DosFreeMem (b);
- }
-