home *** CD-ROM | disk | FTP | other *** search
/ Power CD-ROM!! 7 / POWERCD7.ISO / prgmming / clipper / userlock.prg < prev    next >
Text File  |  1993-10-14  |  3KB  |  115 lines

  1. /*
  2.  * File......: USERLOCK.PRG
  3.  * Author....: Martin Colloby
  4.  * BBS.......: The Dark Knight Returns
  5.  * Net/Node..: 050/069
  6.  * User Name.: Martin Colloby
  7.  * Date......: 18/4/93
  8.  * Revision..: 1.0
  9.  *
  10.  * This is an original work by Martin Colloby and is placed in the public
  11.  * domain.
  12.  *
  13.  * Modification history:
  14.  * ---------------------
  15.  *
  16.  * $Log$
  17.  *
  18.  */
  19.  
  20.  
  21. /*  $DOC$
  22.  *  $FUNCNAME$
  23.  *      GT_USERLOCK()
  24.  *  $CATEGORY$
  25.  *      Network
  26.  *  $ONELINER$
  27.  *      Apply/release a software semaphore
  28.  *  $SYNTAX$
  29.  *      GT_UserLock( cMaster , lLock )
  30.  *  $ARGUMENTS$
  31.  *      cMaster - Master table to be locked/unlocked
  32.  *      lLock   - If .T., lock the record, otherwise unlock the record
  33.  *  $RETURNS$
  34.  *      The lock status - .T. if locked, .F. if not locked
  35.  *  $DESCRIPTION$
  36.  *      Locking : The userid of the current user is placed in the field
  37.  *      "LOCKEDBY" in the given table.  If the record is locked, or another
  38.  *      user has set the semaphore, .F. is returned.  If the name is applied
  39.  *      .T. is returned
  40.  *
  41.  *      Unlocking : If the userid in the "LOCKEDBY" field matches the userid
  42.  *      of the current user the lock is released, otherwise an error is
  43.  *      flagged and .F. returned
  44.  *  $EXAMPLES$
  45.  *
  46.  *  $SEEALSO$
  47.  *
  48.  *  $INCLUDE$
  49.  *      GT_LIB.CH
  50.  *  $END$
  51.  */
  52.  
  53. *
  54. #include "GT_lib.ch"
  55.  
  56. FUNCTION GT_UserLock( cMaster , lLock )
  57.  
  58. /*****************************************************************************
  59.  Purpose - Place or release a user semaphore lock on a master table
  60.  Returns - .T. if lock applied, .F. otherwise
  61.  Author  - Log
  62.  Created - 26/11/92
  63. ******************************************************************************
  64.  Parameters - cMaster - Master table to be locked/unlocked
  65.               lLock   - If .T., lock the record
  66.                         If .F., unlock the record
  67.  Privates   - None
  68.  Locals     - None
  69.  PUBLICS    - cUserName - Name of user applying lock
  70. *****************************************************************************/
  71.  
  72. LOCAL nCurDB  := SELECT()
  73. LOCAL nField  := 0
  74. LOCAL lLocked := .F.
  75.  
  76. * Select the master table and calculate the ordinal position of the
  77. * "lockedby" field
  78. SELECT ( cMaster )
  79. SET ORDER TO 1
  80. nField := FIELDPOS( "lockedby" )
  81.  
  82. * Are we locking or unlocking ?
  83. IF lLock
  84.     * Locking record, so see if we already have a lock
  85.     IF FIELDGET( nField ) == cUserName
  86.         RLOCK()
  87.         lLocked := .T.
  88.     ENDIF
  89.  
  90.     * See if nobody has a lock
  91.     IF ALLTRIM( FIELDGET( nField ) ) == "" .AND. .NOT. lLocked
  92.         IF .NOT. ( GT_RecLock( LOCKWAITTIME ) )
  93.             lLocked := .F.
  94.         ELSE
  95.             * We can now lock the record
  96.             FIELDPUT( nField , cUserName )
  97.             DBCOMMITALL()
  98.             lLocked := .T.
  99.         ENDIF
  100.     ENDIF
  101. ELSE
  102.     * Unlock the record
  103.     RLOCK()
  104.     FIELDPUT( nField , "" )
  105.     DBCOMMITALL()
  106.     UNLOCK
  107. ENDIF
  108.  
  109. * Go back to the original area
  110. DBSELECTAREA( nCurDB )
  111.  
  112. * Return the lock status
  113. RETURN( lLocked )
  114. *
  115.