home *** CD-ROM | disk | FTP | other *** search
- /* TOKEN.PRG - Token functions for multiuser programs in CLipper 5.0
-
- This file contains two functions GETTOKEN() and PUTTOKEN().
- GETTOKEN() receives an optional parameter of either "S" or "E"
- (shared or exclusive). GETTOKEN(), when used in several places in a
- program, allow you to test to see if other users are in the
- program. For example, the following two sets of code can be used at
- the beginning of a program and just before performing maintenance
- that requires exclusive access to a system:
-
- *...............................................
- #command RETURN IF <log> => IF (<log>); RETURN; END
- #define K_ESC 27
-
- DO WHILE !GetToken("S") // "S" means shared
- ? "Cannot access system."
- ? "Waiting three seconds to retry."
- ? "Press [Esc] to quit..."
- RETURN IF (INKEY(3)==K_ESC)
- ENDDO
-
- *...............................................
- #command EXIT IF <log> => IF (<log>); EXIT; END
- #define K_ESC 27
-
- DO WHILE (.T.)
- IF GetToken("E") // "E" means exclusive
- WAIT "We got it EXCL..."
- DO maint
- DO WHILE !GetToken("S")
- ?? "."
- EXIT IF (INKEY(1)==K_ESC)
- ENDDO
- EXIT
- ELSE
- ? "Cannot gain EXCLUSIVE access to system."
- ? "Waiting three seconds to retry."
- ? "Press [Esc] to return to menu..."
- EXIT IF (INKEY(3)==K_ESC)
- ENDIF
- ENDDO
- *...............................................
-
- GETTOKEN() opens a file called TOKEN.SYS in the current directory.
- The function will not work correctly if you are in different DOS
- directories when the function is called. It also leaves the file
- TOKEN.SYS open and thus uses a file handle.
-
- SWITCHES: /n /m /w
-
- Copyright (c) 1990; The DSW Group, Ltd.
-
- */
-
- #include "lang_enh.ch"
- #include "llfio.ch"
- #define MAXCNTDOWN 5
- STATIC nTokenHand:= NIL,;
- cLastMode:= NIL,;
- nCntDown:= MAXCNTDOWN
-
- //..........................................................
- FUNC GetToken(cTokenMode /* "S" or "E" */) && -> lTokenAcquired
- LOCAL cTokenFile:= "TOKEN.SYS",;
- lGotToken:= NO
- DEFAULT cTokenMode TO "S"
- IF (nTokenHand<>NIL); FClose(nTokenHand); END
- FClose(FCreate(cTokenFile,NORMAL_ATTR)) // Create if not there.
- nTokenHand:= FOpen(cTokenFile,IIf(cTokenMode=="S",RW_SHARED,RW_EXCLUSIVE))
- IF (nTokenHand>-1)
- lGotToken:= YES
- cLastMode:= cTokenMode
- ELSEIF (cLastMode<>NIL)
- IF (--nCntDown<0)
- @ 24,0 SAY ""; ? CHR(7) // Sound the alarm!
- ? "Error: "+cTokenFile+" not available."; QUIT
- ELSE
- GetToken(cLastMode) // We MUST get it back
- nCntDown:= MAXCNTDOWN
- ENDIF
- ENDIF
- RETURN(lGotToken)
-
- //..........................................................
- PROC PutToken()
- IF (nTokenHand<>NIL)
- FClose(nTokenHand); nTokenHand:= NIL
- ENDIF
- cLastMode:= NIL; nCntDown:= MAXCNTDOWN
- RETURN
-
- //..........................................................
- //* EOF: TOKEN.PRG