home *** CD-ROM | disk | FTP | other *** search
/ CD Actual 13 / CDA13.ISO / cdactual / demobin / share / program / C / AXMSLIB.ZIP / XMSTEST.C < prev    next >
Encoding:
C/C++ Source or Header  |  1992-02-28  |  3.3 KB  |  100 lines

  1. /* X M S T E S T . c  -- Demonstration of the XMSLIB functions
  2.  * ---------------------------------------------------------------
  3.  * Michael G.A. Wilson                               February 1992
  4.  * ---------------------------------------------------------------
  5.  */
  6.  
  7. /* ------------------------  Pragmas -------------------------- */
  8.  
  9. /* ---------------------- Include files ----------------------- */
  10. #include <stdio.h>  // for FILE, etc
  11. #include <stdlib.h> // for calloc()
  12. #include <string.h> // for strlen()
  13.  
  14. #include "xmslib.h" // for XMS functions
  15.  
  16. /* -------------------- Local definitions --------------------- */
  17. #define MAX_LINES    10000  // Longest file allowed (small model)
  18. #define LONGEST_LINE   128  // Longest line allowed in file
  19.  
  20. /* ------------------- Function prototypes -------------------- */
  21.  
  22. /* ------------------------- Globals -------------------------- */
  23.  
  24. /* ------------------------------------------------------------ */
  25.  
  26. // m a i n
  27. // -------
  28. // Program mainline. List some text files on stdout, after buffering the
  29. // contents in XMS memory. Illustrates use of some XMSLIB functions.
  30. //
  31. // Arguments:
  32. //  <argc>  Number of command line arguments given + 1
  33. //  <argv>  Expected to hold one or more file names: list these files
  34. //          on standard output, after reading them into extended memory.
  35. //
  36. void main (int argc, char **argv)
  37.   {
  38.   if (XMSinstalled() && XMSopen(0))
  39.     {
  40.     // Make a table to store the handles to the strings we'll put in XMS memory
  41.     XMSHANDLE ** HandleBuf = (XMSHANDLE**)calloc(MAX_LINES,sizeof(XMSHANDLE));
  42.  
  43.     if (HandleBuf != NULL)
  44.       {
  45.       while (argc-- > 1)                                    // For each file...
  46.         {
  47.         FILE * fp = fopen(*++argv, "r");
  48.  
  49.         if (fp != NULL)
  50.           {
  51.           XMSHANDLE * pH = (XMSHANDLE *)HandleBuf;
  52.           char szLine[LONGEST_LINE];
  53.           unsigned uLineCount = 0;
  54.  
  55.           puts(strupr(*argv));
  56.           while (fgets(szLine, LONGEST_LINE, fp) != NULL)   // For each line...
  57.             {
  58.             XMSHANDLE hXM;
  59.             size_t uLen = strlen(szLine) + 1;
  60.  
  61.             if ((hXM = XMSalloc(uLen)) != XMSHNULL &&       // Make XMS space
  62.                  XMSput(hXM, szLine, uLen))                 // Copy to XMS
  63.               *pH++ = hXM;                                  // Store handle
  64.             else
  65.               {
  66.               printf("\n\aXMSalloc() or XMSput() failed.\n");
  67.               break;
  68.               }
  69.             printf("%u\r", ++uLineCount);                   // Show progress
  70.             }
  71.  
  72.           printf("\n%u lines stored.\n\n", uLineCount);
  73.           fclose(fp);
  74.  
  75.           pH = (XMSHANDLE *)HandleBuf;                      // Rewind to start
  76.  
  77.           while (uLineCount-- > 0)
  78.             {
  79.             if (XMSget(szLine, *pH) && XMSfree(*pH++))      // Read line back
  80.               printf("%s", szLine);                         // and free XMS
  81.             else
  82.               {
  83.               printf("\n\aXMSget() or XMSfree() failed.\n");
  84.               break;
  85.               }
  86.             }
  87.           printf("\n----- END OF %s -----\n\n", *argv);
  88.           }
  89.         }
  90.       free(HandleBuf);
  91.       }
  92.     else
  93.       printf("Insufficient memory");
  94.  
  95.     XMSclose();
  96.     }
  97.   else
  98.     printf("Can't access extended memory.\n");
  99.   }
  100.