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

  1. /* TOKEN.PRG - Token functions for multiuser programs in CLipper 5.0
  2.  
  3.    This file contains two functions GETTOKEN() and PUTTOKEN().
  4.    GETTOKEN() receives an optional parameter of either "S" or "E"
  5.    (shared or exclusive). GETTOKEN(), when used in several places in a
  6.    program, allow you to test to see if other users are in the
  7.    program. For example, the following two sets of code can be used at
  8.    the beginning of a program and just before performing maintenance
  9.    that requires exclusive access to a system:
  10.  
  11.       *...............................................
  12.       #command RETURN IF <log> => IF (<log>); RETURN; END
  13.       #define K_ESC 27
  14.  
  15.       DO WHILE !GetToken("S")          // "S" means shared
  16.          ? "Cannot access system."
  17.          ? "Waiting three seconds to retry."
  18.          ? "Press [Esc] to quit..."
  19.          RETURN IF (INKEY(3)==K_ESC)
  20.       ENDDO
  21.  
  22.       *...............................................
  23.       #command EXIT IF <log> => IF (<log>); EXIT; END
  24.       #define K_ESC 27
  25.  
  26.       DO WHILE (.T.)
  27.          IF GetToken("E")              // "E" means exclusive
  28.             WAIT "We got it EXCL..."
  29.             DO maint
  30.             DO WHILE !GetToken("S")
  31.                ?? "."
  32.                EXIT IF (INKEY(1)==K_ESC)
  33.             ENDDO
  34.             EXIT
  35.          ELSE
  36.             ? "Cannot gain EXCLUSIVE access to system."
  37.             ? "Waiting three seconds to retry."
  38.             ? "Press [Esc] to return to menu..."
  39.             EXIT IF (INKEY(3)==K_ESC)
  40.          ENDIF
  41.       ENDDO
  42.       *...............................................
  43.  
  44.    GETTOKEN() opens a file called TOKEN.SYS in the current directory.
  45.    The function will not work correctly if you are in different DOS
  46.    directories when the function is called. It also leaves the file
  47.    TOKEN.SYS open and thus uses a file handle.
  48.  
  49.    SWITCHES: /n /m /w
  50.  
  51.    Copyright (c) 1990; The DSW Group, Ltd.
  52.  
  53. */
  54.  
  55. #include "lang_enh.ch"
  56. #include "llfio.ch"
  57. #define MAXCNTDOWN 5
  58. STATIC nTokenHand:= NIL,;
  59.        cLastMode:=  NIL,;
  60.        nCntDown:= MAXCNTDOWN
  61.  
  62. //..........................................................
  63. FUNC GetToken(cTokenMode /* "S" or "E" */) && -> lTokenAcquired
  64. LOCAL cTokenFile:= "TOKEN.SYS",;
  65.       lGotToken:= NO
  66.    DEFAULT cTokenMode TO "S"
  67.    IF (nTokenHand<>NIL); FClose(nTokenHand); END
  68.    FClose(FCreate(cTokenFile,NORMAL_ATTR))   // Create if not there.
  69.    nTokenHand:= FOpen(cTokenFile,IIf(cTokenMode=="S",RW_SHARED,RW_EXCLUSIVE))
  70.    IF (nTokenHand>-1)
  71.       lGotToken:= YES
  72.       cLastMode:= cTokenMode
  73.    ELSEIF (cLastMode<>NIL)
  74.       IF (--nCntDown<0)
  75.          @ 24,0 SAY ""; ? CHR(7) // Sound the alarm!
  76.          ? "Error: "+cTokenFile+" not available."; QUIT
  77.       ELSE
  78.          GetToken(cLastMode)    // We MUST get it back
  79.          nCntDown:= MAXCNTDOWN
  80.       ENDIF
  81.    ENDIF
  82. RETURN(lGotToken)
  83.  
  84. //..........................................................
  85. PROC PutToken()
  86.    IF (nTokenHand<>NIL)
  87.       FClose(nTokenHand);  nTokenHand:= NIL
  88.    ENDIF
  89.    cLastMode:= NIL;  nCntDown:= MAXCNTDOWN
  90. RETURN
  91.  
  92. //..........................................................
  93. //* EOF: TOKEN.PRG
  94.