
############################################################
# NOTICE:
#       This file is posted as an example. The author does not
#       support, warranty, or guarantee that it will work or not damage
#       your system. USE AT YOUR OWN RISK. If you do use this or any
#       other publicly available code, be sure to give credit to the author.
#
# OTHER:
#       No animals were harmed during the creation of this module, it
#       is dolphin safe and won't stick to dental work.
#
# Author:
#       Jerry McGowan
#
# Created:
#       11/30/1999  ( note Y2K compliant creation date )
#
# Functions:
#       CreateErrorMessageArray (  )
#       GetErrorMessage (  )
#
# NOTES:
#	None.  Write your own if you need to.
#
######################################################################
# Revision History
#
# Rev  Date            Author              Description
# ===  =========  ==========    ==============================================
# 1.0  11/30/1999   GMM          Initial version.
# 1.1  07/23/2002   MHC          Shorten length of If Loop
#                                It was very long (hard to bebug)
######################################################################


############################################################
# Function:
#       CreateErrorMessageArray ( inout ErrorMsgArray[] )
#
# Description:
#		Creates an array containing the WinRunner error messages
#		which is indexed by the error code.
#		ErrorMsgArray[0] = "E_OK, E_FILE_OK"
#		ErrorMsgArray[-10001] = "E_GENERAL_ERROR"
#
# Parameters:
#       ErrorMsgArray - An array that will store the error messages
#
# Returns:
#       0 on success, or standard WinRunner error code on failure.
#
# Syntax:
#       rc1 = CreateErrorMessageArray ( ErrorMsgArray )
#
# Examples:
# 	public ErrorMsgArray[];
# 	CreateErrorMessageArray ( ErrorMsgArray );
#
############################################################
function CreateErrorMessageArray ( inout ErrorMsgArray[] )
{
	# Location of WinRunner script which defines the error message codes
	auto ErrorMsgFile = getenv ( "M_ROOT" )  & "\\lib\\wr_gen\\script";
	auto rc = E_OK;		# Return code for this function
	auto character; 	# Store a character
	auto line;			# Store a line from file
	auto tmpline;		# Store a line with no tab or spaces
	auto counter;		# Used to strip tab and spaces out of the line
	static tmpArray[];	# Store split (  )  results

	# Delete everything in the ErrorMsgArray
	for  ( counter in ErrorMsgArray )
		delete ErrorMsgArray[counter];

	rc = file_open ( ErrorMsgFile,FO_MODE_READ );
	if  ( rc != E_OK )
		return  ( rc );

	#Parse the file looking for "public const E_"
	while ( file_getline ( ErrorMsgFile,line ) == 0 )
	{
		if  ( index(line, "public const E_") > 0 )
		{
			# Get rid of spaces and tab characters
			tmpline = "";
			for  ( counter = 1; counter <= length(line); counter++ )
			{
				character = substr ( line, counter, 1 );
				if  ( (character != " ") && (character != "\t") )
					tmpline = tmpline & character;
			}
			split  ( tmpline, tmpArray, "=" );

			# The substr ( tmpArray[2],1, length ( tmpArray[2] ) -1 ) gets rid of the trailing semicolon  ( ; )
			# The substr ( tmpArray[1], 12 ) is the error code minus the public const
			if  ( ErrorMsgArray[substr(tmpArray[2], 1, length(tmpArray[2] - 1 )  ) ] )
			{
					ErrorMsgArray[substr(tmpArray[2], 1, length(tmpArray[2] - 1) ) ] =
					ErrorMsgArray[substr(tmpArray[2], 1, length(tmpArray[2] - 1) ) ]&", "&
					substr(tmpArray[1], 12 );
			}
			else
			{
				ErrorMsgArray[substr(tmpArray[2], 1, length(tmpArray[2]) - 1) ] =
				substr(tmpArray[1], 12);
			}
		}
	}
	file_close ( ErrorMsgFile );
	return  ( E_OK );
}

############################################################
# Function:
#       GetErrorMessage ( inout ErrorMsgArray[], in ErrorCode )
#
# Description:
#      Returns error code message  ( i.e., E_GENERAL_ERROR )
#      When passed an error code  ( i.e., -10001 )
#
# Parameters:
#		ErrorMsgArray - The array that stores the error messages
#		defined by calling CreateErrorMessageArray
#		ErrorCode - The error code number to look up.
#
# Returns:
#       The text ( E_* )  for given error code on success, or an empty string ""
#
# Syntax:
#       rc = GetErrorMessage ( ErrorMsgArray, -10001 );
#
# Examples:
# 	pause ( GetErrorMessage ( ErrorMsgArray, -10001 );
#
############################################################
function GetErrorMessage ( inout ErrorMsgArray[], in ErrorCode )
{
	return ( ErrorMsgArray[ErrorCode] );
}

# *****************************************************************************
# ************* End of Function Library ***************************************
# *****************************************************************************
