/*
 * Decodes the field entry data and prints out the field entry data
 * with help of the dictionary.  Returns success if decoding succeeds
 * or failure if decoding fails.
 * fEntry - The field entry data
 * dIter - The decode iterator
 */
RsslRet decodeFieldEntry(RsslFieldEntry* fEntry, RsslDecodeIterator *dIter)
{
	RsslRet ret = 0;
	RsslDataType dataType = RSSL_DT_UNKNOWN;
	RsslUInt64 fidUIntValue = 0;
	RsslInt64 fidIntValue = 0;
	RsslFloat tempFloat = 0;
	RsslDouble tempDouble = 0;
	RsslReal fidRealValue = RSSL_INIT_REAL;
	RsslEnum fidEnumValue;
	RsslFloat fidFloatValue = 0;
	RsslDouble fidDoubleValue = 0;
	RsslQos fidQosValue = RSSL_INIT_QOS; 
	RsslDateTime fidDateTimeValue;
	RsslState fidStateValue;
	RsslBuffer fidBufferValue;
	RsslBuffer fidDateTimeBuf;
	RsslBuffer fidRealBuf;
	RsslBuffer fidStateBuf;
	RsslBuffer fidQosBuf;
	RsslDictionaryEntry* dictionaryEntry = NULL;

	/* get dictionary entry */
	if (!dictionary.entriesArray)
	{
		dumpHexBuffer(&fEntry->encData);
		return RSSL_RET_SUCCESS;
	}
	else
		dictionaryEntry = dictionary.entriesArray[fEntry->fieldId];

	/* return if no entry found */
	if (!dictionaryEntry) 
	{
		printf("\tFid %d not found in dictionary\n", fEntry->fieldId);
		dumpHexBuffer(&fEntry->encData);
		return RSSL_RET_SUCCESS;
	}

	/* print out fid name */
	printf("\t%-20s", dictionaryEntry->acronym.data);
	/* decode and print out fid value */
	dataType = dictionaryEntry->rwfType;
	switch (dataType)
	{
		case RSSL_DT_UINT:
			if ((ret = rsslDecodeUInt(dIter, &fidUIntValue)) == RSSL_RET_SUCCESS)
			{
				printf(RTR_LLU "\n", fidUIntValue);
			}
			else if (ret != RSSL_RET_BLANK_DATA)
			{
				printf("rsslDecodeUInt() failed with return code: %d\n", ret);
				return ret;
			}
			break;
		case RSSL_DT_INT:
			if ((ret = rsslDecodeInt(dIter, &fidIntValue)) == RSSL_RET_SUCCESS)
			{
				printf(RTR_LLD "\n", fidIntValue);
			}
			else if (ret != RSSL_RET_BLANK_DATA)
			{
				printf("rsslDecodeInt() failed with return code: %d\n", ret);
				return ret;
			}
			break;
		case RSSL_DT_FLOAT:
			if ((ret = rsslDecodeFloat(dIter, &fidFloatValue)) == RSSL_RET_SUCCESS) 
			{
				printf("%f\n", fidFloatValue);
			}
			else if (ret != RSSL_RET_BLANK_DATA)
			{
				printf("rsslDecodeFloat() failed with return code: %d\n", ret);
				return ret;
			}
			break;
		case RSSL_DT_DOUBLE:
			if ((ret = rsslDecodeDouble(dIter, &fidDoubleValue)) == RSSL_RET_SUCCESS) 
			{
				printf("%f\n", fidDoubleValue);
			}
			else if (ret != RSSL_RET_BLANK_DATA)
			{
				printf("rsslDecodeDouble() failed with return code: %d\n", ret);
				return ret;
			}
			break;
		case RSSL_DT_REAL:
			if ((ret = rsslDecodeReal(dIter, &fidRealValue)) == RSSL_RET_SUCCESS)
			{
				fidRealBuf.data = (char*)alloca(35);
				fidRealBuf.length = 35;
				rsslRealToString(&fidRealBuf, &fidRealValue);
				printf("%s\n", fidRealBuf.data);
			}
			else if (ret != RSSL_RET_BLANK_DATA)
			{
				printf("rsslDecodeReal() failed with return code: %d\n", ret);
				return ret;
			}
			break;
		case RSSL_DT_ENUM:
			if ((ret = rsslDecodeEnum(dIter, &fidEnumValue)) == RSSL_RET_SUCCESS)
			{
				RsslEnumType *pEnumType = getFieldEntryEnumType(dictionaryEntry, fidEnumValue);
				if (pEnumType)
    				printf("%.*s(%d)\n", pEnumType->display.length, pEnumType->display.data, fidEnumValue);
				else
    				printf("%d\n", fidEnumValue);
			}
			else if (ret != RSSL_RET_BLANK_DATA)
			{
				printf("rsslDecodeEnum() failed with return code: %d\n", ret);
				return ret;
			}
			break;
		case RSSL_DT_DATE:
			if ((ret = rsslDecodeDate(dIter, &fidDateTimeValue.date)) == RSSL_RET_SUCCESS)
			{
				fidDateTimeBuf.data = (char*)alloca(30);
				fidDateTimeBuf.length = 30;
				rsslDateTimeToString(&fidDateTimeBuf, RSSL_DT_DATE, &fidDateTimeValue);
				printf("%s\n", fidDateTimeBuf.data);
			}
			else if (ret != RSSL_RET_BLANK_DATA)
			{
				printf("rsslDecodeDate() failed with return code: %d\n", ret);
				return ret;
			}
			break;
		case RSSL_DT_TIME:
			if ((ret = rsslDecodeTime(dIter, &fidDateTimeValue.time)) == RSSL_RET_SUCCESS)
			{
				fidDateTimeBuf.data = (char*)alloca(30);
				fidDateTimeBuf.length = 30;
				rsslDateTimeToString(&fidDateTimeBuf, RSSL_DT_TIME, &fidDateTimeValue);
				printf("%s\n", fidDateTimeBuf.data);
			}
			else if (ret != RSSL_RET_BLANK_DATA)
			{
				printf("rsslDecodeTime() failed with return code: %d\n", ret);
				return ret;
			}
			break;
		case RSSL_DT_DATETIME:
			if ((ret = rsslDecodeDateTime(dIter, &fidDateTimeValue)) == RSSL_RET_SUCCESS)
			{
				fidDateTimeBuf.data = (char*)alloca(50);
				fidDateTimeBuf.length = 50;
				rsslDateTimeToString(&fidDateTimeBuf, RSSL_DT_DATETIME, &fidDateTimeValue);
				printf("%s\n", fidDateTimeBuf.data);
			}
			else if (ret != RSSL_RET_BLANK_DATA)
			{
				printf("rsslDecodeDateTime() failed with return code: %d\n", ret);
				return ret;
			}
			break;
		case RSSL_DT_QOS:
			if((ret = rsslDecodeQos(dIter, &fidQosValue)) == RSSL_RET_SUCCESS) {
				fidQosBuf.data = (char*)alloca(100);
				fidQosBuf.length = 100;
				rsslQosToString(&fidQosBuf, &fidQosValue);
				printf("%s\n", fidQosBuf.data);
			}
			else if (ret != RSSL_RET_BLANK_DATA)
			{
				printf("rsslDecodeQos() failed with return code: %d\n", ret);
				return ret;
			}
			break;
		case RSSL_DT_STATE:
			if((ret = rsslDecodeState(dIter, &fidStateValue)) == RSSL_RET_SUCCESS) {
				int stateBufLen = 80;
				if (fidStateValue.text.data)
					stateBufLen += fidStateValue.text.length;
				fidStateBuf.data = (char*)alloca(stateBufLen);
				fidStateBuf.length = stateBufLen;
				rsslStateToString(&fidStateBuf, &fidStateValue);
				printf("%.*s\n", fidStateBuf.length, fidStateBuf.data);
			}
			else if (ret != RSSL_RET_BLANK_DATA)
			{
				printf("rsslDecodeState() failed with return code: %d\n", ret);
				return ret;
			}
			break;
		
		/*For an example of array decoding, see fieldListEncDec.c*/
		case RSSL_DT_ARRAY:
		break;
		case RSSL_DT_BUFFER:
		case RSSL_DT_ASCII_STRING:
		case RSSL_DT_UTF8_STRING:
		case RSSL_DT_RMTES_STRING:
			if((ret = rsslDecodeBuffer(dIter, &fidBufferValue)) == RSSL_RET_SUCCESS)
			{
				printf("%.*s\n", fidBufferValue.length, fidBufferValue.data);
			}
			else if (ret != RSSL_RET_BLANK_DATA) 
			{
				printf("rsslDecodeBuffer() failed with return code: %d\n", ret);
				return ret;
			}
			break;
		default:
			printf("Unsupported data type (%d) for fid value\n", dataType);
			break;
	}
	if (ret == RSSL_RET_BLANK_DATA)
	{
		printf("<blank data>\n");
	}

	return RSSL_RET_SUCCESS;
}
/* Decodes the content of a QueueData or QueueDataExpired message. */
RsslBool queueMsgHandlerDecodeQueueData(RsslDecodeIterator *pIter)
{
	RsslFieldList fieldList;
	RsslFieldEntry fieldEntry;
	RsslRet ret;
	RsslBool sendAck = RSSL_FALSE;

	rsslClearFieldList(&fieldList);
	ret = rsslDecodeFieldList(pIter, &fieldList, NULL);
	if (ret != RSSL_RET_SUCCESS)
	{
		printf("\nrsslDecodeFieldList failed: %d\n", ret);
		return RSSL_FALSE;
	}

	rsslClearFieldEntry(&fieldEntry);
	while ((ret = rsslDecodeFieldEntry(pIter, &fieldEntry)) != RSSL_RET_END_OF_CONTAINER)
	{
		RsslRet ret = 0;
		RsslDataType dataType = RSSL_DT_UNKNOWN;
		RsslUInt64 fidUIntValue = 0;
		RsslInt64 fidIntValue = 0;
		RsslFloat tempFloat = 0;
		RsslDouble tempDouble = 0;
		RsslReal fidRealValue = RSSL_INIT_REAL;
		RsslEnum fidEnumValue;
		RsslFloat fidFloatValue = 0;
		RsslDouble fidDoubleValue = 0;
		RsslQos fidQosValue = RSSL_INIT_QOS; 
		RsslDateTime fidDateTimeValue;
		RsslState fidStateValue;
		RsslBuffer fidBufferValue;
		RsslBuffer fidDateTimeBuf;
		RsslBuffer fidRealBuf;
		RsslBuffer fidStateBuf;
		RsslBuffer fidQosBuf;
		RsslDictionaryEntry* dictionaryEntry = NULL;

		if (ret != RSSL_RET_SUCCESS)
		{
			printf("\nrsslDecodeFieldEntry() failed: %d\n", ret);
			return RSSL_FALSE;
		}

		dictionaryEntry = fdmDictionary.entriesArray[fieldEntry.fieldId];

		/* return if no entry found */
		if (!dictionaryEntry) 
		{
			printf("\tFid %d not found in dictionary\n", fieldEntry.fieldId);
		}
		else
		{
			/* print out fid name */
			printf("\t%-20s", dictionaryEntry->acronym.data);
			/* decode and print out fid value */
			dataType = dictionaryEntry->rwfType;
			switch (dataType)
			{
				case RSSL_DT_UINT:
					if ((ret = rsslDecodeUInt(pIter, &fidUIntValue)) == RSSL_RET_SUCCESS)
					{
						printf(""RTR_LLU"\n", fidUIntValue);
					}
					else if (ret != RSSL_RET_BLANK_DATA)
					{
						printf("rsslDecodeUInt() failed with return code: %d\n", ret);
						return RSSL_FALSE;
					}
					break;
				case RSSL_DT_INT:
					if ((ret = rsslDecodeInt(pIter, &fidIntValue)) == RSSL_RET_SUCCESS)
					{
						printf(""RTR_LLD"\n", fidIntValue);
					}
					else if (ret != RSSL_RET_BLANK_DATA)
					{
						printf("rsslDecodeInt() failed with return code: %d\n", ret);
						return RSSL_FALSE;
					}
					break;
				case RSSL_DT_FLOAT:
					if ((ret = rsslDecodeFloat(pIter, &fidFloatValue)) == RSSL_RET_SUCCESS) 
					{
						printf("%f\n", fidFloatValue);
					}
					else if (ret != RSSL_RET_BLANK_DATA)
					{
						printf("rsslDecodeFloat() failed with return code: %d\n", ret);
						return RSSL_FALSE;
					}
					break;
				case RSSL_DT_DOUBLE:
					if ((ret = rsslDecodeDouble(pIter, &fidDoubleValue)) == RSSL_RET_SUCCESS) 
					{
						printf("%f\n", fidDoubleValue);
					}
					else if (ret != RSSL_RET_BLANK_DATA)
					{
						printf("rsslDecodeDouble() failed with return code: %d\n", ret);
						return RSSL_FALSE;
					}
					break;
				case RSSL_DT_REAL:
					if ((ret = rsslDecodeReal(pIter, &fidRealValue)) == RSSL_RET_SUCCESS)
					{
						fidRealBuf.data = (char*)alloca(35);
						fidRealBuf.length = 35;
						rsslRealToString(&fidRealBuf, &fidRealValue);
						printf("%s\n", fidRealBuf.data);
					}
					else if (ret != RSSL_RET_BLANK_DATA)
					{
						printf("rsslDecodeReal() failed with return code: %d\n", ret);
						return RSSL_FALSE;
					}
					break;
				case RSSL_DT_ENUM:
					if ((ret = rsslDecodeEnum(pIter, &fidEnumValue)) == RSSL_RET_SUCCESS)
					{
						RsslEnumType *pEnumType = getFieldEntryEnumType(dictionaryEntry, fidEnumValue);
						if (pEnumType)
							printf("\"%.*s\"(%d)\n", pEnumType->display.length, pEnumType->display.data, fidEnumValue);
						else
							printf("%d\n", fidEnumValue);
					}
					else if (ret != RSSL_RET_BLANK_DATA)
					{
						printf("rsslDecodeEnum() failed with return code: %d\n", ret);
						return RSSL_FALSE;
					}
					break;
				case RSSL_DT_DATE:
					if ((ret = rsslDecodeDate(pIter, &fidDateTimeValue.date)) == RSSL_RET_SUCCESS)
					{
						fidDateTimeBuf.data = (char*)alloca(30);
						fidDateTimeBuf.length = 30;
						rsslDateTimeToString(&fidDateTimeBuf, RSSL_DT_DATE, &fidDateTimeValue);
						printf("%s\n", fidDateTimeBuf.data);
					}
					else if (ret != RSSL_RET_BLANK_DATA)
					{
						printf("rsslDecodeDate() failed with return code: %d\n", ret);
						return RSSL_FALSE;
					}
					break;
				case RSSL_DT_TIME:
					if ((ret = rsslDecodeTime(pIter, &fidDateTimeValue.time)) == RSSL_RET_SUCCESS)
					{
						fidDateTimeBuf.data = (char*)alloca(30);
						fidDateTimeBuf.length = 30;
						rsslDateTimeToString(&fidDateTimeBuf, RSSL_DT_TIME, &fidDateTimeValue);
						printf("%s\n", fidDateTimeBuf.data);
					}
					else if (ret != RSSL_RET_BLANK_DATA)
					{
						printf("rsslDecodeTime() failed with return code: %d\n", ret);
						return RSSL_FALSE;
					}
					break;
				case RSSL_DT_DATETIME:
					if ((ret = rsslDecodeDateTime(pIter, &fidDateTimeValue)) == RSSL_RET_SUCCESS)
					{
						fidDateTimeBuf.data = (char*)alloca(50);
						fidDateTimeBuf.length = 50;
						rsslDateTimeToString(&fidDateTimeBuf, RSSL_DT_DATETIME, &fidDateTimeValue);
						printf("%s\n", fidDateTimeBuf.data);
					}
					else if (ret != RSSL_RET_BLANK_DATA)
					{
						printf("rsslDecodeDateTime() failed with return code: %d\n", ret);
						return RSSL_FALSE;
					}
					break;
				case RSSL_DT_QOS:
					if((ret = rsslDecodeQos(pIter, &fidQosValue)) == RSSL_RET_SUCCESS) {
						fidQosBuf.data = (char*)alloca(100);
						fidQosBuf.length = 100;
						rsslQosToString(&fidQosBuf, &fidQosValue);
						printf("%s\n", fidQosBuf.data);
					}
					else if (ret != RSSL_RET_BLANK_DATA)
					{
						printf("rsslDecodeQos() failed with return code: %d\n", ret);
						return RSSL_FALSE;
					}
					break;
				case RSSL_DT_STATE:
					if((ret = rsslDecodeState(pIter, &fidStateValue)) == RSSL_RET_SUCCESS) {
						int stateBufLen = 80;
						if (fidStateValue.text.data)
							stateBufLen += fidStateValue.text.length;
						fidStateBuf.data = (char*)alloca(stateBufLen);
						fidStateBuf.length = stateBufLen;
						rsslStateToString(&fidStateBuf, &fidStateValue);
						printf("%.*s\n", fidStateBuf.length, fidStateBuf.data);
					}
					else if (ret != RSSL_RET_BLANK_DATA)
					{
						printf("rsslDecodeState() failed with return code: %d\n", ret);
						return RSSL_FALSE;
					}
					break;

					/*For an example of array decoding, see fieldListEncDec.c*/
				case RSSL_DT_ARRAY:
					break;
				case RSSL_DT_BUFFER:
				case RSSL_DT_ASCII_STRING:
				case RSSL_DT_UTF8_STRING:
				case RSSL_DT_RMTES_STRING:
					{

						if((ret = rsslDecodeBuffer(pIter, &fidBufferValue)) == RSSL_RET_SUCCESS)
						{
							RsslBuffer msgTypeBuf;
							msgTypeBuf.length = 1;
							msgTypeBuf.data = (char*)"D";

							printf("%.*s\n", fidBufferValue.length, fidBufferValue.data);

							/* Acknowledge orders. */
							if (fieldEntry.fieldId == 35 &&
									rsslBufferIsEqual(&msgTypeBuf, &fidBufferValue))
								sendAck = RSSL_TRUE;
						}
						else if (ret != RSSL_RET_BLANK_DATA) 
						{
							printf("rsslDecodeBuffer() failed with return code: %d\n", ret);
							return RSSL_FALSE;
						}
						break;
					}
				default:
					printf("Unsupported data type (%d) for fid value\n", dataType);
					break;
			}
		}
		if (ret == RSSL_RET_BLANK_DATA)
		{
			printf("<blank data>\n");
		}

	}

	printf("\n");

	return sendAck;
}