home *** CD-ROM | disk | FTP | other *** search
/ Piper's Pit BBS/FTP: ibm 0010 - 0019 / ibm0010-0019 / ibm0010.tar / ibm0010 / CLIPB52.ZIP / SCHINKEL.ZIP / LLOCK.PRG < prev    next >
Encoding:
Text File  |  1990-05-16  |  3.1 KB  |  102 lines

  1. /* LLOCK.PRG - Logical File/Record Locking functions for Clipper 5.0
  2.  
  3.    This file contains LRLOCK(), LFLOCK(), LUNLOCK(), and LUNLOCKALL()
  4.    which provide logical record and file locking and unlocking,
  5.    respectively. Their purpose is to allow you to lock a record or
  6.    file by convention without any of the limitations of RLOCK() and
  7.    FLOCK() when you need to ignore the lock (as in transaction
  8.    processing). In other words, the give the programmer complete
  9.    control to screw up.
  10.  
  11.    The other functions FINDELEMENT(), LLOCK(), PUTLLOCKINLIST(), and
  12.    REMOVELLOCKFROMLIST() are internal support functions (and thus
  13.    STATIC).
  14.  
  15.    SWITCHES: /n /m /w
  16.  
  17.    REQUIRES: DBFHAND.PRG, ASCANNIL.PRG
  18.  
  19.    Copyright (c) 1990; The DSW Group, Ltd.
  20.  
  21. */
  22. #define BEGIN_OFFSET    (2147483648)  // 2^31
  23. #define LOCK_IT         (.T.)
  24. #define UNLOCK_IT       (.F.)
  25.  
  26. #define FileOffSet()    BEGIN_OFFSET
  27. #define RecordOffSet()  BEGIN_OFFSET+32*(1+FCount())+1+(RecSize()*(RecNo()-1))
  28.  
  29. #define FileBytes()     (1073741823)
  30. #define RecordBytes()   (RecSize())
  31. #define FindFree()      AScanNil(aLockList)
  32.  
  33. #define AreaNo(a)       a[1]
  34. #define AreaOfs(a)      a[2]
  35. #define AreaBytes(a)    a[3]
  36.  
  37. STATIC aLockList:= {}
  38.  
  39. //...........................................................
  40. STATIC FUNC FindElement() // -> nElementForCurrentSelectArea
  41. LOCAL nEle:= 0
  42.    IF (Len(aLockList)>0)
  43.       nEle:= AScan(aLockList,{|e| IIf(ValType(e)=="A",AreaNo(e)==Select(),.F.)})
  44.    ENDIF
  45. RETURN (nEle)
  46.  
  47. //..........................................................
  48. STATIC PROC PutLLockInList(nOffSet,nBytesToLock)
  49. LOCAL nEle:= FindElement(),aEle
  50.    aEle:= { Select(), nOffSet, nBytesToLock }
  51.    IF (nEle==0)                  // SELECT area not already listed
  52.       IF (nEle:= FindFree())==0  // No free elements
  53.          AAdd(aLockList,NIL)     // Add a new element
  54.          nEle:= Len(aLockList)   // Get it's element number
  55.       ENDIF
  56.    ENDIF
  57.    aLockList[nEle]:= aEle        // Store that element
  58. RETURN
  59.  
  60. //..........................................................
  61. STATIC PROC RemoveLLockFromList(nEle)
  62.    aLockList[nEle]:= NIL
  63. RETURN
  64.  
  65. //..........................................................
  66. STATIC FUNC LLock(nOfs,nBytes)
  67. LOCAL lLocked
  68.    LUnLock()
  69.    IF (lLocked:= FLockRange(DbfHand(),nOfs,nBytes,LOCK_IT))
  70.       PutLLockInList(nOfs,nBytes)
  71.    ENDIF
  72. RETURN(lLocked)
  73.  
  74. //..........................................................
  75. FUNC LFLock()
  76. RETURN(LLock(FileOffSet(),FileBytes()))
  77.  
  78. //..........................................................
  79. FUNC LRLock()
  80. RETURN(LLock(RecordOffSet(),RecordBytes()))
  81.  
  82. //..........................................................
  83. PROC LUnLock()
  84. LOCAL nEle:= FindElement(), aArea
  85.    IF (nEle>0)
  86.       IF ValType(aArea:= aLockList[nEle])=="A"
  87.          FLockRange(DbfHand(),AreaOfs(aArea),AreaBytes(aArea),UNLOCK_IT)
  88.          RemoveLLockFromList(nEle)
  89.       ENDIF
  90.    ENDIF
  91. RETURN
  92.  
  93. //..........................................................
  94. PROC LUnLockAll()
  95.    STATIC aLockList:= {}
  96. RETURN
  97.  
  98. //..........................................................
  99. // EOF: LLOCK.PRG
  100.  
  101.  
  102.