home *** CD-ROM | disk | FTP | other *** search
- /***************************************************************************
- * EMSTEST.C *
- * VERSION: 1.0 *
- * DATE: 06/02/91 *
- * *
- * Copyright (c) 1991 James W. Birdsall. All Rights Reserved. *
- * *
- * Compiles under Turbo C, Turbo C++, Borland C++. *
- * *
- * This is an example of the use of the EMSLIB functions. This simple *
- * demo program copies one file to another, using EMS as a buffer. There *
- * must be more bytes EMS available than the length of the file, since *
- * the entire file is stored in EMS at once. *
- * *
- * If the symbol LONG is defined, a large temporary buffer is used. If *
- * not, a small temporary buffer is used. *
- * *
- ***************************************************************************/
-
- /*
- ** system includes <>
- */
-
- #include <stdio.h>
- #include <stdlib.h>
- #include <sys\stat.h>
-
-
- /*
- ** custom includes ""
- */
-
- #include "emslib.h"
-
-
- /*
- ** local #defines
- */
-
- #ifdef LONG
- #define BUFFERSIZE 30000
- #else
- #define BUFFERSIZE 159
- #endif
-
-
- /*
- ** misc: copyright strings, version macros, etc.
- */
-
- /*
- ** typedefs
- */
-
- /*
- ** global variables
- */
-
- unsigned char buffer[BUFFERSIZE];
-
-
- /*
- ** static globals
- */
-
- /*
- ** function prototypes
- */
-
- /*
- ** functions
- */
-
-
- main(int argc, char *argv[])
- {
- FILE *infile, *outfile;
- long filrem, MMoffset, freebytes;
- struct stat statbuf;
- int MMhandle;
- unsigned int readsize;
-
- /*
- ** Check for enough arguments.
- */
- if (argc < 3)
- {
- printf("Usage: %s infile outfile\n", argv[0]);
- exit(3);
- }
-
- /*
- ** INITIALIZE EMSLIB! VERY IMPORTANT!
- */
- if (EMMlibinit() != 0)
- {
- printf("Error in EMSLIB initialization.\n");
- exit(3);
- }
-
- /*
- ** Get bytes of EMS left, display.
- */
- freebytes = EMMcoreleft();
- if (_EMMerror != 0)
- {
- printf("Error %x in EMMcoreleft()\n", _EMMerror);
- exit(3);
- }
- printf("%lu bytes of EMS available.\n", freebytes);
-
- /*
- ** Get size of input file so we know how many bytes of EMS to allocate.
- */
- if (stat(argv[1], &statbuf) != 0)
- {
- printf("stat() error.\n");
- exit(3);
- }
- filrem = statbuf.st_size;
- if (filrem > freebytes)
- {
- printf("Not enough EMS available to copy file.\n");
- exit(3);
- }
-
- /*
- ** Allocate EMS.
- */
- MMhandle = EMMalloc((unsigned long) filrem);
- if (MMhandle == EMMOOPS)
- {
- printf("Error %x allocating EMS.\n", (int) _EMMerror);
- exit(3);
- }
-
- /*
- ** Open input file.
- */
- if ((infile = fopen(argv[1], "rb")) == (FILE *) NULL)
- {
- printf("Error opening file %s\n", argv[1]);
- EMMfree(MMhandle);
- exit(3);
- }
-
- /*
- ** Main loop. Data is read from the input file into the temporary buffer,
- ** then copied from the temporary buffer to EMS.
- */
- MMoffset = 0;
- printf("Reading file");
- while (filrem > 0)
- {
- /* figure out how much to read */
- readsize = ((filrem > BUFFERSIZE) ? BUFFERSIZE : (int)filrem);
-
- /* read */
- if (fread(buffer, sizeof(char), readsize, infile) != readsize)
- {
- printf("Error reading file %s\n");
- EMMfree(MMhandle);
- exit(3);
- }
-
- /* copy to EMS */
- if (EMMcopyto((unsigned long) readsize, (unsigned char far *) buffer,
- MMhandle, (unsigned long) MMoffset) == EMMOOPS)
- {
- printf("Error %x writing EMS.\n", (int) _EMMerror);
- EMMfree(MMhandle);
- exit(3);
- }
-
- /* display, update variables */
- printf(".");
- filrem -= readsize;
- MMoffset += readsize;
- }
-
- /*
- ** Close input file, open output file.
- */
- fclose(infile);
- if ((outfile = fopen(argv[2], "wb")) == (FILE *) NULL)
- {
- printf("Error opening file %s\n", argv[2]);
- EMMfree(MMhandle);
- exit(3);
- }
-
- /*
- ** The other main loop. Data is copied from EMS to the temporary buffer,
- ** then written to the output file.
- */
- filrem = MMoffset;
- MMoffset = 0;
- printf("\nWriting file");
- while (filrem > 0)
- {
- /* figure out how many bytes to copy */
- readsize = ((filrem > BUFFERSIZE) ? BUFFERSIZE : (int)filrem);
-
- /* copy from EMS */
- if (EMMcopyfrom((unsigned long) readsize, MMhandle,
- (unsigned long) MMoffset, (unsigned char far *) buffer) == EMMOOPS)
- {
- printf("Error %x reading EMS.\n", (int) _EMMerror);
- EMMfree(MMhandle);
- exit(3);
- }
-
- /* write */
- if (fwrite(buffer, sizeof(char), readsize, outfile) != readsize)
- {
- printf("Error writing file %s\n");
- EMMfree(MMhandle);
- exit(3);
- }
-
- /* display, update variables */
- printf(".");
- filrem -= readsize;
- MMoffset += readsize;
- }
-
- /*
- ** Close output file, free EMS.
- */
- fclose(outfile);
- EMMfree(MMhandle);
-
- /*
- ** Done.
- */
- printf("\nDone.\n");
- exit(0);
- } /* end of main() */
-
-