home *** CD-ROM | disk | FTP | other *** search
/ Power CD-ROM!! 7 / POWERCD7.ISO / prgmming / clipper / llflock.c < prev    next >
C/C++ Source or Header  |  1993-10-14  |  6KB  |  193 lines

  1. /*
  2.  * File......: LLFLOCK.C
  3.  * Author....: Dave Pearson
  4.  * BBS.......: The Dark Knight Returns
  5.  * Net/Node..: 050/069
  6.  * User Name.: Dave Pearson
  7.  * Date......: 23/03/93
  8.  * Revision..: 1.0
  9.  *
  10.  * This is an original work by Dave Pearson and is placed in the public
  11.  * domain.
  12.  *
  13.  * Modification history:
  14.  * ---------------------
  15.  *
  16.  * $Log$
  17.  *
  18.  */
  19.  
  20. // NOTE: This code has been written for and compiled with Borland C++
  21. //       Version 3.1
  22. //
  23.  
  24. #include "gt_mem.h"
  25. #include "Extend.H"
  26.  
  27. Boolean _FileLocking(int, char, long, long);
  28.  
  29. #define LOCK_TYPE_LOCK          0
  30. #define LOCK_TYPE_UNLOCK        1
  31.  
  32. /*  $DOC$
  33.  *  $FUNCNAME$
  34.  *      GT_FLOCK()
  35.  *  $CATEGORY$
  36.  *      File I/O
  37.  *  $ONELINER$
  38.  *      Lock a region of a low level file.
  39.  *  $SYNTAX$
  40.  *      GT_FLock(<nHandle>,<nOffset>,<nLength>) --> lLocked
  41.  *  $ARGUMENTS$
  42.  *      <nHandle> is a numeric file handle.
  43.  *
  44.  *      <nOffset> is the byte position at which the lock will start. This
  45.  *                is a zero based value.
  46.  *
  47.  *      <nLength> is the length of the region to lock.
  48.  *
  49.  *  $RETURNS$
  50.  *      A logical value, .T. if the region was locked, .F. if not.
  51.  *  $DESCRIPTION$
  52.  *      GT_FLock() can be used to lock a region of a low level file.
  53.  *
  54.  *      To use the low-level locking/un-locking functions you will first
  55.  *      need to open the file with fopen(), or perhaps create a file with
  56.  *      fcreate(). Once open you can use the file handle returned by
  57.  *      Clipper to carry out a lock.
  58.  *
  59.  *      When you have locked an area of the file, be sure to store the
  60.  *      offset and length, these values will be required by GT_FUnLock().
  61.  *
  62.  *  $EXAMPLES$
  63.  *
  64.  *      #include "fileio.ch"
  65.  *
  66.  *      if (nFile := fopen("LIFE.42",FO_SHARED)) != F_ERROR
  67.  *
  68.  *         // Lock a region in the file at position 10 (don't forget,
  69.  *         // the offset starts at 0, not 1!
  70.  *
  71.  *         if GT_FLock(nFile,10,2)
  72.  *
  73.  *            // Make some changes to the file.
  74.  *
  75.  *            GT_FUnLock(nFile,10,2)
  76.  *         endif
  77.  *         fclose(nFile)
  78.  *      endif
  79.  *
  80.  *  $SEEALSO$
  81.  *      GT_FUNLOCK()
  82.  *  $END$
  83.  */
  84.  
  85. CLIPPER GT_FLock()
  86. {
  87.         Boolean Result = FALSE;
  88.  
  89.         if (PCOUNT >= 3)
  90.         {
  91.                 if (ISNUM(1) && ISNUM(2) && ISNUM(3))
  92.                 {
  93.                         Result = _FileLocking(_parni(1),LOCK_TYPE_LOCK,_parnl(2),_parnl(3));
  94.                 }
  95.         }
  96.         _retl(Result);
  97. }
  98.  
  99. /*  $DOC$
  100.  *  $FUNCNAME$
  101.  *      GT_FUNLOCK()
  102.  *  $CATEGORY$
  103.  *      File I/O
  104.  *  $ONELINER$
  105.  *      Unlock a region of a low level file.
  106.  *  $SYNTAX$
  107.  *      GT_FUnLock(<nHandle>,<nOffset>,<nLength>) --> lUnLocked
  108.  *  $ARGUMENTS$
  109.  *      <nHandle> is a numeric file handle.
  110.  *
  111.  *      <nOffset> is the byte position at which the unlock will start. This
  112.  *                is a zero based value.
  113.  *
  114.  *      <nLength> is the length of the region to unlock.
  115.  *
  116.  *  $RETURNS$
  117.  *      A logical value, .T. if the region was unlocked, .F. if not.
  118.  *  $DESCRIPTION$
  119.  *      GT_FUnLock() can be used to unlock a region of a low level file
  120.  *      that has been locked withg GT_FLock().
  121.  *
  122.  *      To use the low-level locking/un-locking functions you will first
  123.  *      need to open the file with fopen(), or perhaps create a file with
  124.  *      fcreate(). Once open you can use the file handle returned by
  125.  *      Clipper to carry out a lock.
  126.  *
  127.  *  $EXAMPLES$
  128.  *
  129.  *      #include "fileio.ch"
  130.  *
  131.  *      if (nFile := fopen("LIFE.42",FO_SHARED)) != F_ERROR
  132.  *
  133.  *         // Lock a region in the file at position 10 (don't forget,
  134.  *         // the offset starts at 0, not 1!
  135.  *
  136.  *         if GT_FLock(nFile,10,2)
  137.  *
  138.  *            // Make some changes to the file.
  139.  *              .
  140.  *              .
  141.  *              .
  142.  *            // Now unlock the same region.
  143.  *
  144.  *            GT_FUnLock(nFile,10,2)
  145.  *         endif
  146.  *         fclose(nFile)
  147.  *      endif
  148.  *
  149.  *  $SEEALSO$
  150.  *      GT_FLOCK()
  151.  *  $END$
  152.  */
  153.  
  154. CLIPPER GT_FUnLock()
  155. {
  156.         Boolean Result = FALSE;
  157.  
  158.         if (PCOUNT >= 3)
  159.         {
  160.                 if (ISNUM(1) && ISNUM(2) && ISNUM(3))
  161.                 {
  162.                         Result = _FileLocking(_parni(1),LOCK_TYPE_UNLOCK,_parnl(2),_parnl(3));
  163.                 }
  164.         }
  165.         _retl(Result);
  166. }
  167.  
  168. /*****************************************************************************
  169. * Function: _FileLocking()                                                   *
  170. * Syntax..: Boolean _FileLocking(int Handle, char LockType, long Offset,     *
  171. * ........:                      long Length)                                *
  172. * Usage...: Internal function to do the actual locking.                      *
  173. * By......: David A Pearson                                                  *
  174. *****************************************************************************/
  175.  
  176. static Boolean _FileLocking(int Handle, char LockType, long Offset, long Length)
  177. {
  178.         int     HiOffset = _GT_FP_SEG(Offset);
  179.         int     LoOffset = _GT_FP_OFF(Offset);
  180.         int     HiLength = _GT_FP_SEG(Length);
  181.         int     LoLength = _GT_FP_OFF(Length);
  182.  
  183.         asm     Mov     AH,0x5C;
  184.         asm     Mov     AL,LockType;
  185.         asm     Mov     BX,Handle;
  186.         asm     Mov     CX,HiOffset;
  187.         asm     Mov     DX,LoOffset;
  188.         asm     Mov     SI,HiLength;
  189.         asm     Mov     DI,LoLength;
  190.         asm     Int     0x21;
  191.         return((_FLAGS & 1) == 0);
  192. }
  193.