home *** CD-ROM | disk | FTP | other *** search
/ Media Share 9 / MEDIASHARE_09.ISO / mag&info / msjv7_4.zip / OPEN.ARJ / FILE_IO.C < prev    next >
Text File  |  1992-07-01  |  5KB  |  252 lines

  1. /*
  2.       Copyright 1991 by David Thielen, All Rights Reserved.
  3.  
  4.       This code example is from a commercial product and has restricted
  5.       rights.  This code, or any code derived from this code may be
  6.       incorporated into any programs with the following restrictions;
  7.       1) It cannot be sold as source code, and 2) It cannot be sold in a
  8.       product which provides this code as an API.
  9. */
  10.  
  11.  
  12. #include "file_io.h"
  13.  
  14. static unsigned _Write (int hFil, void const *pBuf, unsigned uNum,
  15.                         unsigned *puErr);
  16.  
  17.  
  18. // These are all the 'other' file handle calls.  They are included here
  19. // for 2 reasons;
  20. // 1) Most c run-time libraries don't go direct to the DOS calls so you
  21. //    can't intermix FileOpen with a write.
  22. // 2) The other files can use these calls.
  23.  
  24. // Because these calls are so straightforward, comments are minimal.
  25.  
  26.  
  27. unsigned FileClose (int hFil)
  28. {
  29. unsigned uRtn;
  30.  
  31.    _asm
  32.       {
  33.       mov      [uRtn], 0
  34.       mov      ah, 3Eh
  35.       mov      bx, [hFil]
  36.       int      21h                  ; close the file
  37.       jnc      l1
  38.       mov      [uRtn], ax
  39. l1:
  40.       }
  41.  
  42.    return (uRtn);
  43. }
  44.  
  45.  
  46. unsigned FileDelete (BYTE const *pFile)
  47. {
  48. unsigned uRtn;
  49.  
  50.    _asm
  51.       {
  52.       mov      [uRtn], 0
  53.       mov      ah, 41h
  54.       mov      dx, [pFile]
  55.       int      21h                  ; delete the file
  56.       jnc      Ok
  57.       mov      [uRtn], ax
  58. Ok:
  59.       }
  60.  
  61.    return (uRtn);
  62. }
  63.  
  64. int FileDup (int hFil)
  65. {
  66. int hRtn;
  67.  
  68.    _asm
  69.       {
  70.       mov      [hRtn], -1
  71.       mov      ah, 45h
  72.       mov      bx, [hFil]
  73.       int      21h
  74.       jc    l1
  75.       mov      [hRtn], ax
  76. l1:
  77.       }
  78.  
  79.    return (hRtn);
  80. }
  81.  
  82. unsigned long FileGetSize (int hFil)
  83. {
  84. long lRtn;
  85.  
  86.    _asm
  87.       {
  88.       mov      word ptr [lRtn], -1        ; in case of error
  89.       mov      word ptr [lRtn+2], -1
  90.  
  91.       ; Save the offset
  92.       mov      ax, 4201h
  93.       mov      bx, [hFil]
  94.       xor      cx, cx
  95.       mov      dx, cx
  96.       int      21h                  ; Get current position
  97.       jc    Err2
  98.       push  cx
  99.       push  dx
  100.  
  101.       ; Get the length
  102.       mov      ax, 4202h
  103.       xor      cx, cx
  104.       mov      dx, cx
  105.       int      21h                  ; Get length
  106.       jc    Err1
  107.       mov      word ptr [lRtn], ax
  108.       mov      word ptr [lRtn+2], dx
  109.  
  110. Err1:    ; Restore old position
  111.       mov      ax, 4200h
  112.       pop      dx
  113.       pop      cx
  114.       int      21h                  ; Restore current position
  115.  
  116. Err2:
  117.       }
  118.  
  119.    return (lRtn);
  120. }
  121.  
  122. unsigned FileRead (int hFil, void *pBuf, unsigned uNum)
  123. {
  124. unsigned uRtn;
  125.  
  126.    // File I/O calls are expensive.  Therefore, we handle 0 FAST
  127.    if (! uNum)
  128.       return (0);
  129.  
  130.    _asm
  131.       {
  132.       mov      [uRtn], 0
  133.       mov      ah, 3Fh
  134.       mov      bx, [hFil]
  135.       mov      cx, [uNum]
  136.       mov      dx, [pBuf]
  137.       int      21h
  138.       jc    Err
  139.       mov      [uRtn], ax
  140. Err:
  141.       }
  142.  
  143.    return (uRtn);
  144. }
  145.  
  146. unsigned FileSeek (int hFil, unsigned long ulOffset)
  147. {
  148. unsigned uRtn=0;
  149.  
  150.    _asm
  151.       {
  152.       mov      ax, 4200h
  153.       mov      bx, [hFil]
  154.       mov      dx, word ptr [ulOffset]
  155.       mov      cx, word ptr [ulOffset+2]
  156.       int      21h                  ; seek
  157.       jnc      l1
  158.       mov      [uRtn], ax
  159. l1:
  160.       }
  161.  
  162.    return (uRtn);
  163. }
  164.  
  165. unsigned FileSeekRead (int hFil, unsigned long ulOffset, void *pBuf,
  166.                        unsigned uNum)
  167. {
  168.  
  169.    if (FileSeek (hFil, ulOffset))
  170.       return (0);
  171.  
  172.    return (FileRead (hFil, pBuf, uNum));
  173. }
  174.  
  175. unsigned FileSeekWrite (int hFil, unsigned long ulOffset, void const *pBuf,
  176.                         unsigned uNum)
  177. {
  178.  
  179.    if (FileSeek (hFil, ulOffset))
  180.       return (0);
  181.  
  182.    return (FileWrite (hFil, pBuf, uNum));
  183. }
  184.  
  185.  
  186. // If we are extending the file - write one byte to ensure it extends
  187. // A write past the end of the disk doesn't error out - it just returns
  188. // 0 bytes written.
  189.  
  190. unsigned FileSetSize (int hFil, unsigned long ulSize)
  191. {
  192. unsigned uRtn, uNum;
  193.  
  194.    if (FileGetSize (hFil) < ulSize)
  195.       {
  196.       ulSize--;
  197.       uNum = 1;
  198.       }
  199.    else
  200.       uNum = 0;
  201.  
  202.    if ((uRtn = FileSeek (hFil, ulSize)) != 0)
  203.       return (uRtn);
  204.  
  205.    if (_Write (hFil, &uRtn, uNum, &uRtn) != uNum)
  206.       {
  207.       if (uRtn)
  208.          return (uRtn);
  209.       return (0x1E);
  210.       }
  211.    return (uRtn);
  212. }
  213.  
  214. unsigned FileWrite (int hFil, void const *pBuf, unsigned uNum)
  215. {
  216. unsigned uErr;
  217.  
  218.    // A write of 0 bytes sets a file size.  Use SetSize above to do this
  219.    if (! uNum)
  220.       return (0);
  221.  
  222.    return (_Write (hFil, pBuf, uNum, &uErr));
  223. }
  224.  
  225. static unsigned _Write (int hFil, void const *pBuf, unsigned uNum,
  226.                         unsigned *puErr)
  227. {
  228. unsigned uRtn, uErr;
  229.  
  230.    _asm
  231.       {
  232.       mov      [uRtn], 0
  233.       mov      [uErr], 0
  234.       mov      ah, 40h
  235.       mov      bx, [hFil]
  236.       mov      cx, [uNum]
  237.       mov      dx, [pBuf]
  238.       int      21h
  239.       jc    Err
  240.       mov      [uRtn], ax
  241.       jmp      Ok
  242.  
  243. Err:
  244.       mov      [uErr], ax
  245.  
  246. Ok:
  247.       }
  248.  
  249.    *puErr = uErr;
  250.    return (uRtn);
  251. }
  252.