home *** CD-ROM | disk | FTP | other *** search
/ Black Box 4 / BlackBox.cdr / progc / emslb216.arj / EMSTEST.C < prev    next >
Encoding:
C/C++ Source or Header  |  1991-06-02  |  5.6 KB  |  241 lines

  1. /***************************************************************************
  2. *   EMSTEST.C                                                              *
  3. *   VERSION: 1.0                                                           *
  4. *   DATE:    06/02/91                                                      *
  5. *                                                                          *
  6. *   Copyright (c) 1991 James W. Birdsall. All Rights Reserved.             *
  7. *                                                                          *
  8. *   Compiles under Turbo C, Turbo C++, Borland C++.                        *
  9. *                                                                          *
  10. *   This is an example of the use of the EMSLIB functions. This simple     *
  11. *   demo program copies one file to another, using EMS as a buffer. There  *
  12. *   must be more bytes EMS available than the length of the file, since    *
  13. *   the entire file is stored in EMS at once.                              *
  14. *                                                                          *
  15. *   If the symbol LONG is defined, a large temporary buffer is used. If    *
  16. *   not, a small temporary buffer is used.                                 *
  17. *                                                                          *
  18. ***************************************************************************/
  19.  
  20. /*
  21. ** system includes <>
  22. */
  23.  
  24. #include <stdio.h>
  25. #include <stdlib.h>
  26. #include <sys\stat.h>
  27.  
  28.  
  29. /*
  30. ** custom includes ""
  31. */
  32.  
  33. #include "emslib.h"
  34.  
  35.  
  36. /*
  37. ** local #defines
  38. */
  39.  
  40. #ifdef LONG
  41. #define BUFFERSIZE 30000
  42. #else
  43. #define BUFFERSIZE 159
  44. #endif
  45.  
  46.  
  47. /*
  48. ** misc: copyright strings, version macros, etc.
  49. */
  50.  
  51. /*
  52. ** typedefs
  53. */
  54.  
  55. /*
  56. ** global variables
  57. */
  58.  
  59. unsigned char buffer[BUFFERSIZE];
  60.  
  61.  
  62. /*
  63. ** static globals
  64. */
  65.  
  66. /*
  67. ** function prototypes
  68. */
  69.  
  70. /*
  71. ** functions
  72. */
  73.  
  74.  
  75. main(int argc, char *argv[])
  76. {
  77.    FILE *infile, *outfile;
  78.    long filrem, MMoffset, freebytes;
  79.    struct stat statbuf;
  80.    int MMhandle;
  81.    unsigned int readsize;
  82.  
  83.    /*
  84.    ** Check for enough arguments.
  85.    */
  86.    if (argc < 3)
  87.    {
  88.       printf("Usage: %s infile outfile\n", argv[0]);
  89.       exit(3);
  90.    }
  91.  
  92.    /*
  93.    ** INITIALIZE EMSLIB! VERY IMPORTANT!
  94.    */
  95.    if (EMMlibinit() != 0)
  96.    {
  97.       printf("Error in EMSLIB initialization.\n");
  98.       exit(3);
  99.    }
  100.  
  101.    /*
  102.    ** Get bytes of EMS left, display.
  103.    */
  104.    freebytes = EMMcoreleft();
  105.    if (_EMMerror != 0)
  106.    {
  107.        printf("Error %x in EMMcoreleft()\n", _EMMerror);
  108.        exit(3);
  109.    }
  110.    printf("%lu bytes of EMS available.\n", freebytes);
  111.  
  112.    /*
  113.    ** Get size of input file so we know how many bytes of EMS to allocate.
  114.    */
  115.    if (stat(argv[1], &statbuf) != 0)
  116.    {
  117.       printf("stat() error.\n");
  118.       exit(3);
  119.    }
  120.    filrem = statbuf.st_size;
  121.    if (filrem > freebytes)
  122.    {
  123.       printf("Not enough EMS available to copy file.\n");
  124.       exit(3);
  125.    }
  126.  
  127.    /*
  128.    ** Allocate EMS.
  129.    */
  130.    MMhandle = EMMalloc((unsigned long) filrem);
  131.    if (MMhandle == EMMOOPS)
  132.    {
  133.       printf("Error %x allocating EMS.\n", (int) _EMMerror);
  134.       exit(3);
  135.    }
  136.  
  137.    /*
  138.    ** Open input file.
  139.    */
  140.    if ((infile = fopen(argv[1], "rb")) == (FILE *) NULL)
  141.    {
  142.       printf("Error opening file %s\n", argv[1]);
  143.       EMMfree(MMhandle);
  144.       exit(3);
  145.    }
  146.  
  147.    /*
  148.    ** Main loop. Data is read from the input file into the temporary buffer,
  149.    ** then copied from the temporary buffer to EMS.
  150.    */
  151.    MMoffset = 0;
  152.    printf("Reading file");
  153.    while (filrem > 0)
  154.    {
  155.       /* figure out how much to read */
  156.       readsize = ((filrem > BUFFERSIZE) ? BUFFERSIZE : (int)filrem);
  157.  
  158.       /* read */
  159.       if (fread(buffer, sizeof(char), readsize, infile) != readsize)
  160.       {
  161.          printf("Error reading file %s\n");
  162.          EMMfree(MMhandle);
  163.          exit(3);
  164.       }
  165.  
  166.       /* copy to EMS */
  167.       if (EMMcopyto((unsigned long) readsize, (unsigned char far *) buffer,
  168.                                 MMhandle, (unsigned long) MMoffset) == EMMOOPS)
  169.       {
  170.          printf("Error %x writing EMS.\n", (int) _EMMerror);
  171.          EMMfree(MMhandle);
  172.          exit(3);
  173.       }
  174.  
  175.       /* display, update variables */
  176.       printf(".");
  177.       filrem -= readsize;
  178.       MMoffset += readsize;
  179.    }
  180.  
  181.    /*
  182.    ** Close input file, open output file.
  183.    */
  184.    fclose(infile);
  185.    if ((outfile = fopen(argv[2], "wb")) == (FILE *) NULL)
  186.    {
  187.       printf("Error opening file %s\n", argv[2]);
  188.       EMMfree(MMhandle);
  189.       exit(3);
  190.    }
  191.  
  192.    /*
  193.    ** The other main loop. Data is copied from EMS to the temporary buffer,
  194.    ** then written to the output file.
  195.    */
  196.    filrem = MMoffset;
  197.    MMoffset = 0;
  198.    printf("\nWriting file");
  199.    while (filrem > 0)
  200.    {
  201.       /* figure out how many bytes to copy */
  202.       readsize = ((filrem > BUFFERSIZE) ? BUFFERSIZE : (int)filrem);
  203.  
  204.       /* copy from EMS */
  205.       if (EMMcopyfrom((unsigned long) readsize, MMhandle,
  206.             (unsigned long) MMoffset, (unsigned char far *) buffer) == EMMOOPS)
  207.       {
  208.          printf("Error %x reading EMS.\n", (int) _EMMerror);
  209.          EMMfree(MMhandle);
  210.          exit(3);
  211.       }
  212.  
  213.       /* write */
  214.       if (fwrite(buffer, sizeof(char), readsize, outfile) != readsize)
  215.       {
  216.          printf("Error writing file %s\n");
  217.          EMMfree(MMhandle);
  218.          exit(3);
  219.       }
  220.  
  221.       /* display, update variables */
  222.       printf(".");
  223.       filrem -= readsize;
  224.       MMoffset += readsize;
  225.    }
  226.  
  227.    /*
  228.    ** Close output file, free EMS.
  229.    */
  230.    fclose(outfile);
  231.    EMMfree(MMhandle);
  232.  
  233.    /*
  234.    ** Done.
  235.    */
  236.    printf("\nDone.\n");
  237.    exit(0);
  238. } /* end of main() */
  239.  
  240.  
  241.