SET PROCEDURE TO LIBRARY


********************
FUNCTION FileOpen()
********************
PARAMETERS sDBFName, lOpenFile
PRIVATE sWorkArea, nWorkArea

nWorkArea = WorkArea(sDBFName)

IF nWorkArea = 0
	nWorkArea = NextWorkArea()
	SELECT(nWorkArea)
	USE &sDBFName 
	INC arDBFInUse[nWorkArea]       
ELSE
	SELECT(nWorkArea)   
	IF lOpenFile
		INC arDBFInUse[nWorkArea]           
	ENDIF
ENDIF   

RETURN(.T.)


********************
FUNCTION FileClose()
********************
PARAMETERS sDBFName
PRIVATE nWorkArea, nSelect

nWorkArea = WorkArea(sDBFName)
IF nWorkArea <> 0
	
	DEC arDBFInUse[nWorkArea]
	
	IF arDBFInUse[nWorkArea] = 0
		nSelect = SELECT(nWorkArea)
		CLOSE
		SELECT(nSelect)
	ENDIF

ENDIF

RETURN(.F.)


***********************
FUNCTION NextWorkArea()
***********************
PRIVATE nWorkArea, nCount

nWorkArea = 0
FOR nCount = 1 to 26
	IF nCount = 13
		LOOP
	ENDIF
	IF arDBFInUse[nCount] = 0
		nWorkArea = nCount
		EXIT
	ENDIF
NEXT

IF nWorkArea = 0
	BEEP
	WARNING(csError,csWorkArea,387)
ENDIF

RETURN nWorkArea

*******************
FUNCTION WorkArea()
*******************
PARAMETERS sDBFName
PRIVATE nWorkArea, nCount, nSelect

nSelect = SELECT(1)
nWorkArea = 0
nCount = 1

FOR nCount = 1 to 26
	IF nCount = 13          && MEMORY
		LOOP
	ENDIF
	SELECT(nCount)
	IF TrimIt(DBF()) = TrimIt(sDBFName)
		nWorkArea = nCount
		EXIT
	ENDIF
NEXT

SELECT(nSelect)

RETURN nWorkArea

FUNCTION TrimIt
PARAMETERS sName
PRIVATE nPosition

sName = UPPER(sName)

nPosition = RAT('\',SName)
IF nPosition > 0
	sName = RIGHT(sName,LEN(sName) - nPosition)
ENDIF

nPosition = AT('.',sName)
IF nPosition > 0
	sName = LEFT(sName, nPosition -1)
ENDIF

RETURN sName
