// refcount.inc

[RefCountConstants]
ValueName = "RefCount"

//
//	Get reference count using key.
//
//		$0 = Key
//		$1 = Reference count name (prefixed to default value name).
//
//		$R0 = Boolean value indicating succcess.
//		$R1 = Reference count.
//
[GetRefCountByKey]
	read-syms RefCountConstants

	set Key = $($0)
	set Name = $($1)
	_Enter("GetRefCountByKey", _Show(Key)_Show(Name))

	set Success = FALSE
	set RefCount = "Invalid"

	_GetRegDwordByKey $(Key) $(Name)$(ValueName)
   _AssertGoto($($R0), Exit)

	set Test = $($R1)
	ifint $(Test) < 0
		_Error("Registry RefCount is negative.", _Show(Test))
		goto Exit
	endif
		
	set RefCount = $(Test)
	set Success = TRUE

Exit = +

	_Exit("GetRefCountByKey", _Show(Success)_Show(RefCount))
	Return $(Success) $(RefCount)

//
//	Set reference count using key.
//
//		$0 = Key
//		$1 = Value of count
//		$2 = Reference count name (prefixed to default value name).
//
//		$R0 = Boolean indicating success.
//
[SetRefCountByKey]
	read-syms RefCountConstants

	set Key = $($0)
	set RefCount = $($1)
	set Name = $($2)
	_Enter("SetRefCountByKey", _Show(Key)_Show(RefCount)_Show(Name))

	_SetRegDwordByKey $(Key) $(Name)$(ValueName) $(RefCount)
	set Success = $($R0)

	_Exit("SetRefCountByKey", _Show(Success))
	Return $(Success)

//
//	Modify reference count using key.
//
//		$0 = Key
//		$1 = Delta to count
//		$2 = Reference count name (prefixed to default value name).
//
//		$R0 = Boolean indicating success.
//		$R1 = New count
//
[ModRefCountByKey]

	set Key = $($0)
	set Delta = $($1)
	set Name = $($2)
	_Enter("ModRefCountByKey", _Show(Key)_Show(Delta)_Show(Name))

	set Success = FALSE
	set RefCount = "Invalid"

	_LocalShell GetRefCountByKey $(Key) $(Name)
   _AssertGoto($($R0), Exit)

	set RefCount = $($R1)
	set-add RefCount = $(RefCount), $(Delta)

	_LocalShell SetRefCountByKey $(Key) $(RefCount) $(Name)
   _AssertGoto($($R0), Exit)

	set Success = $($R0)

Exit = +

	_Exit("ModRefCountByKey", _Show(Success)_Show(RefCount))
	Return $(Success) $(RefCount)

//
//	Get reference count using named key path (in HKEY_LOCAL_MACHINE).
//
//		$0 = Key Path
//		$1 = Reference count name (prefixed to default value name).
//
//		$R0 = Boolean value indicating succcess.
//		$R1 = Reference count.
//
[GetRefCountByName]

	set Path = $($0)
	set Name = $($2)
	_Enter("GetRefCountByName", _Show(Path)_Show(Name))

	set Success = FALSE
	set RefCount = "Invalid"

	_OpenRegKey_HKLM($(Path), Key)
	_IfNotValidKey($(Key))
		goto Exit
	endif

	_LocalShell GetRefCountByKey $(Key) $(Name) $(Default)
   _AssertGoto($($R0), Exit)

	set Success = TRUE
	set RefCount = $($R1)

	CloseRegKey $(Key)

Exit = +

	_Exit("GetRefCountByName", _Show(Success)_Show(RefCount))
	Return $(Success) $(RefCount)

//
//	Set reference count using named key path (in HKEY_LOCAL_MACHINE).
//
//		$0 = Key path
//		$1 = Value of count
//		$2 = Reference count name (prefixed to default value name).
//
//		$R0 = Boolean indicating success.
//
[SetRefCountByName]

	set Path = $($0)
	set RefCount = $($1)
	set Name = $($2)
	_Enter("SetRefCountByName", _Show(Key)_Show(RefCount)_Show(Name))

	set Success = FALSE

	_MakeStdRegKey($(!REG_H_LOCAL), $(Path), Key)
	_IfNotValidKey($(Key))
      _AssertGoto(FALSE, Exit)
	endif

	_LocalShell SetRefCountByKey $(Key) $(RefCount) $(Name)
	set Success = $($R0)

	CloseRegKey $(Key)

Exit = +

	_Exit("SetRefCountByName", _Show(Success))
	Return $(Success)

//
//	Modify reference count using named key path (in HKEY_LOCAL_MACHINE).
//
//		$0 = Key path
//		$1 = Delta to count
//		$2 = Reference count name (prefixed to default value name).
//
//		$R0 = Boolean indicating success.
//		$R1 = New count
//
[ModRefCountByName]

	set Path = $($0)
	set Delta = $($1)
	set Name = $($2)
	_Enter("ModRefCountByName", _Show(Path)_Show(Delta)_Show(Name))

	set Success = FALSE
	set RefCount = "Invalid"

	_OpenRegKey_HKLM($(Path), Key)
	_IfNotValidKey($(Key))
		goto Exit
	endif

	_LocalShell ModRefCountByKey $(Key) $(Delta) $(Name)
   _AssertGoto($($R0), Exit)

	set Success = TRUE
	set RefCount = $($R1)

	CloseRegKey $(Key)

Exit = +

	_Exit("ModRefCountByName", _Show(Success)_Show(RefCount))
	Return $(Success) $(RefCount)
