home *** CD-ROM | disk | FTP | other *** search
/ Stars of Shareware: Programmierung / SOURCE.mdf / programm / msdos / c / dmkit / flat / support.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-12-31  |  2.5 KB  |  130 lines

  1. //** SUPPORT.C    Simple support functions to load files into memory and do
  2. //**                        basic memory allocation.    Used by the DIGPAK & MIDPAK
  3. //**                        loader program.  Requires DOSCALLS.ASM and DOSCALLS.H
  4. //**                        to do low 1mb memory allocation.
  5. /*                                                                                                                                                     */
  6. /*        Written by John W. Ratcliff (c) 1994                                                                     */
  7. /*             Compuserve: 70253,3237                                                                                          */
  8. /*             Genie: J.RATCLIFF3                                                                                                  */
  9. /*             BBS: 1-314-939-0200                                                                                                 */
  10. /*             Addresss:                                                                                                                     */
  11. /*                     747 Napa Lane                                                                                                     */
  12. /*                     St. Charles, MO 63304                                                                                     */
  13. /*                                                                                                                                                     */
  14. #include <stdio.h>
  15. #include <stdlib.h>
  16. #include <malloc.h>
  17.  
  18. #include "doscalls.h"
  19. #include "keys.h"
  20. #include "support.h"
  21.  
  22. #define MAXLOWMEM 256 // Maximum number of memory allocations in low-mem.
  23.  
  24. static char *lowmemloc[MAXLOWMEM];
  25. static short lowmemselect[MAXLOWMEM];
  26. static lowmemcount = 0;
  27.  
  28. int keystat(void)
  29. {
  30.     return( kbhit() );
  31. }
  32.  
  33. int getkey(void)
  34. {
  35.     int ch;
  36.  
  37.     ch = getch();
  38.     if ( ch == 0 ) ch = getch()+256;
  39.     return(ch);
  40. }
  41.  
  42. char *fload(char *fname,long int *size)
  43. {
  44.     long int fsize;
  45.     char *data=0;
  46.     FILE *fph;
  47.  
  48.     fph = fopen(fname, "rb");
  49.     if ( fph )
  50.     {
  51.         fseek(fph, 0L, SEEK_END);
  52.         fsize = ftell(fph);
  53.         fseek(fph, 0L, SEEK_SET);
  54.         data = memalloc(fsize);
  55.         if ( data )
  56.         {
  57.             fread(data, fsize, 1, fph);
  58.         }
  59.         fclose(fph);
  60.         *size = fsize;
  61.     }
  62.     return(data);
  63. }
  64.  
  65.  
  66. char *floadlow(char *fname,long int *size)
  67. {
  68.     long int fsize;
  69.     char *data=0;
  70.     FILE *fph;
  71.  
  72.     fph = fopen(fname, "rb");
  73.     if ( fph )
  74.     {
  75.         fseek(fph, 0L, SEEK_END);
  76.         fsize = ftell(fph);
  77.         fseek(fph, 0L, SEEK_SET);
  78.         data = realalloc(fsize);
  79.         if ( data )
  80.         {
  81.             fread(data, fsize, 1, fph);
  82.         }
  83.         fclose(fph);
  84.         *size = fsize;
  85.     }
  86.     return(data);
  87. }
  88.  
  89. char *memalloc(long int size)
  90. {
  91.     return( malloc( size ) );
  92. }
  93.  
  94. void memfree(char *mem)
  95. {
  96.     free(mem);
  97. }
  98.  
  99. char *realalloc(long int size)
  100. {
  101.     char *found=0;
  102.  
  103.     if ( lowmemcount < MAXLOWMEM )
  104.     {
  105.         found = AllocLowMem(size, &lowmemselect[lowmemcount] );
  106.         if ( found )
  107.         {
  108.             lowmemloc[lowmemcount++] = found;
  109.         }
  110.     }
  111.     return( found );
  112. }
  113.  
  114. void realfree(char *mem)
  115. {
  116.     int i;
  117.  
  118.     for (i=0; i<lowmemcount; i++)
  119.     {
  120.         if ( lowmemloc[i] == mem )
  121.         {
  122.             FreeLowMem(lowmemselect[i]); // Free memory by selector.
  123.             lowmemcount--;
  124.             lowmemloc[i] = lowmemloc[lowmemcount]; // move it down.
  125.             lowmemselect[i] = lowmemselect[lowmemcount];
  126.             break;
  127.         }
  128.     }
  129. }
  130.