home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / snip9707.zip / HUGEREAD.C < prev    next >
C/C++ Source or Header  |  1997-07-05  |  6KB  |  226 lines

  1. /* +++Date last modified: 05-Jul-1997 */
  2.  
  3. /*
  4. **  HUGEREAD.C - "Universal" PC read and write functions using huge data
  5. **               and far pointers.
  6. **
  7. **  NOTES:
  8. **
  9. **  1. If these functions are called with a prototype in scope, passed
  10. **     parameters will be coerced to the proper data types.
  11. **
  12. **  2. Since these call read() and write(), all normal mode flags which
  13. **     are supported by individual compilers will be honored.
  14. **
  15. **  3. In small data memory models (S, T, and M), an intermediate buffer
  16. **     is allocated and used. In large data models (L and C), the data
  17. **     are read/written directly from/to target memory.
  18. **
  19. **  4. Like many mixed-model functions, this may generate lots of warnings
  20. **     with many compilers. Despite this, it really does generate correct
  21. **     code for all major PC compilers.
  22. **
  23. **  Original Copyright 1992 by Bob Stout as part of
  24. **  the MicroFirm Function Library (MFL)
  25. **
  26. **  The user is granted a free limited license to use this source file
  27. **  to create royalty-free programs, subject to the terms of the
  28. **  license restrictions specified in the LICENSE.MFL file.
  29. */
  30.  
  31. #include <dos.h>
  32. #include <io.h>
  33. #include <stdlib.h>
  34. #include <stddef.h>
  35. #include <stdio.h>
  36. #include "extkword.h"
  37. #include "minmax.h"
  38. #include "snpdskio.h"
  39. #include "snpdosys.h"
  40.  
  41. /*
  42. **  Get the largest buffer possible.
  43. */
  44.  
  45. static size_t gettmp(char **buf)
  46. {
  47.       size_t bufsiz;
  48.  
  49.       for (bufsiz = 0x4000; bufsiz >= 128; bufsiz >>= 1)
  50.       {
  51.             if (NULL != (*buf = (char *) malloc(bufsiz)))
  52.                   return bufsiz;
  53.       }
  54.       return 0;
  55. }
  56.  
  57. /*
  58. **  Read any size block to anywhere in memory
  59. */
  60.  
  61. long hugeread(int fh, unsigned char FAR *buf, long size)
  62. {
  63.       long count;
  64.       size_t bufsiz;
  65.       char *tmp;
  66.       long ercode = size;
  67.  
  68.       if (4 > sizeof(void *))
  69.       {
  70.             if (0 == (bufsiz = gettmp(&tmp)))
  71.                   return -1L;
  72.       }
  73.       else
  74.       {
  75.             tmp = (char *)buf;
  76.             bufsiz = 0x4000;
  77.       }
  78.  
  79.       buf = farnormal(buf);
  80.       while (0 < (count = min(size, (long)bufsiz)))
  81.       {
  82.             int i, numread = read(fh, tmp, (size_t)count);
  83.  
  84.             if (1 > numread || numread != (int)count)
  85.                   return -1L;
  86.             if (4 > sizeof(void *))
  87.             {
  88.                   for (i = 0; i < count; ++i)
  89.                         buf[i] = tmp[i];
  90.             }
  91.             buf = farnormal(buf + count);
  92.             size -= count;
  93.             if (2 < sizeof(void *))
  94.                   tmp = (char *)buf;
  95.       }
  96.       return ercode;
  97. }
  98.  
  99. /*
  100. **  Write any size block from anywhere in memory
  101. */
  102.  
  103. long hugewrite(int fh, unsigned char FAR *buf, long size)
  104. {
  105.       long count;
  106.       size_t bufsiz;
  107.       char *tmp;
  108.       long ercode = size;
  109.  
  110.       if (4 > sizeof(void *))
  111.       {
  112.             if (0 == (bufsiz = gettmp(&tmp)))
  113.                   return -1L;
  114.       }
  115.       else
  116.       {
  117.             tmp = (char *)buf;
  118.             bufsiz = 0x4000;
  119.       }
  120.  
  121.       buf = farnormal(buf);
  122.       while (0 < (count = min(size, (long)bufsiz)))
  123.       {
  124.             int i, numwrite;
  125.  
  126.             if (4 > sizeof(void *))
  127.             {
  128.                   for (i = 0; i < count; ++i)
  129.                         tmp[i] = buf[i];
  130.             }
  131.             numwrite = write(fh, tmp, (size_t)count);
  132.             if (1 > numwrite || numwrite != (int)count)
  133.                   return -1L;
  134.             buf = farnormal(buf + count);
  135.             size -= count;
  136.             if (2 < sizeof(void *))
  137.                   tmp = (char *)buf;
  138.       }
  139.       return ercode;
  140. }
  141.  
  142. /*
  143. **  Read any size block to anywhere in memory
  144. */
  145.  
  146. long hugefread(FILE *fp, char FAR *buf, long size)
  147. {
  148.       long count;
  149.       size_t bufsiz;
  150.       char *tmp;
  151.       long ercode = size;
  152.  
  153.       if (4 > sizeof(void *))
  154.       {
  155.             if (0 == (bufsiz = gettmp(&tmp)))
  156.                   return -1L;
  157.       }
  158.       else
  159.       {
  160.             tmp = (char *)buf;
  161.             bufsiz = 0x4000;
  162.       }
  163.  
  164.       buf = farnormal(buf);
  165.       while (0 < (count = min(size, (long)bufsiz)))
  166.       {
  167.             int i, numread = fread(tmp, 1, (size_t)count, fp);
  168.  
  169.             if (1 > numread || numread != (int)count)
  170.                   return -1L;
  171.             if (4 > sizeof(void *))
  172.             {
  173.                   for (i = 0; i < count; ++i)
  174.                         buf[i] = tmp[i];
  175.             }
  176.             buf = farnormal(buf + count);
  177.             size -= count;
  178.             if (2 < sizeof(void *))
  179.                   tmp = (char *)buf;
  180.       }
  181.       return ercode;
  182. }
  183.  
  184. /*
  185. **  Write any size block from anywhere in memory
  186. */
  187.  
  188. long hugefwrite(FILE *fp, char FAR *buf, long size)
  189. {
  190.       long count;
  191.       size_t bufsiz;
  192.       char *tmp;
  193.       long ercode = size;
  194.  
  195.       if (4 > sizeof(void *))
  196.       {
  197.             if (0 == (bufsiz = gettmp(&tmp)))
  198.                   return -1L;
  199.       }
  200.       else
  201.       {
  202.             tmp = (char *)buf;
  203.             bufsiz = 0x4000;
  204.       }
  205.  
  206.       buf = farnormal(buf);
  207.       while (0 < (count = min(size, (long)bufsiz)))
  208.       {
  209.             int i, numwrite;
  210.  
  211.             if (4 > sizeof(void *))
  212.             {
  213.                   for (i = 0; i < count; ++i)
  214.                         tmp[i] = buf[i];
  215.             }
  216.             numwrite = fwrite(tmp, 1, (size_t)count, fp);
  217.             if (1 > numwrite || numwrite != (int)count)
  218.                   return -1L;
  219.             buf = farnormal(buf + count);
  220.             size -= count;
  221.             if (2 < sizeof(void *))
  222.                   tmp = (char *)buf;
  223.       }
  224.       return ercode;
  225. }
  226.