/* ----------------------------------------------------------------------

	CNFTEST sample for Microsoft ActiveX Conferencing

	Copyright (c) 1996, Microsoft Corporation
	All rights reserved.

	rtns.c

	General utility routines, specific to this application

---------------------------------------------------------------------- */

#include "main.h"


/*  D W  I P  A D D R  F R O M  S Z */
/*----------------------------------------------------------------------------
	%%Function: DwIpAddrFromSz

----------------------------------------------------------------------------*/
DWORD DwIpAddrFromSz(LPSTR lpsz)
{
	// REVIEW: there must be a better way...

	DWORD dwAddr = 0;
	DWORD iTemp;
	unsigned short ch;
	int i;

	if (FEmptySz(lpsz))
		return 0;

	for (i = 0; i < 4; i++)
	{
		iTemp = 0;
		for ( ; ;)
		{
			ch = *lpsz++;
			if (('.' == ch) || ('\0' == ch))
				break;
			if (ch > '9')
				break;
			iTemp = (iTemp*10) + ch - '0';
			
		}
		dwAddr = (dwAddr << 8) | iTemp;

		if ('\0' == ch)
			break;
	}

	return dwAddr;
}



/*  D U M P  C O N F  I N F O */
/*----------------------------------------------------------------------------
	%%Function: DumpConfInfo

----------------------------------------------------------------------------*/
VOID DumpConfInfo(LPCONFINFO lpConfInfo)
{
	int  cch;
	CHAR sz[MAX_PATH];

	if (NULL == lpConfInfo)
	{
		Log(LOG_ALWAYS, "<no conference data>");
		return;
	}
	Assert(sizeof(CONFINFO) == lpConfInfo->dwSize);

	Log2(LOG_ALWAYS, "ConfInfo [%s]  %d bytes",
		lpConfInfo->szConferenceName, lpConfInfo->dwSize);
	Log1(LOG_ALWAYS, "  Users: %d", lpConfInfo->cUsers);
	Log1(LOG_ALWAYS, "  HConf: %08X", lpConfInfo->hConf);
	Log1(LOG_ALWAYS, "  GCCID: %08X", lpConfInfo->dwGCCID);

	// determine media type
	SetEmptySz(sz);
	if (lpConfInfo->dwMediaType & CONF_MT_DATA)
		lstrcat(sz, "Data+");
	if (lpConfInfo->dwMediaType & CONF_MT_AUDIO)
		lstrcat(sz, "Audio+");
	if (lpConfInfo->dwMediaType & CONF_MT_VIDEO)
		lstrcat(sz, "Video+");

	cch = CchSz(sz);
	if (0 == cch)
		lstrcpy(sz, "<none>");
	else
		sz[cch-1] = '\0';

	Log1(LOG_ALWAYS, "  Media: %s", sz);
}


/*  D U M P  U S E R  I N F O */
/*----------------------------------------------------------------------------
	%%Function: DumpUserInfo

----------------------------------------------------------------------------*/
VOID DumpUserInfo(LPCONFUSERINFO lpUserInfo)
{
	if (NULL == lpUserInfo)
	{
		Log(LOG_ALWAYS, "<no user data>");
		return;
	}
	Assert(sizeof(CONFUSERINFO) == lpUserInfo->dwSize);

	Log2(LOG_ALWAYS, "UserInfo [%s]  %d bytes",
		lpUserInfo->szUserName, lpUserInfo->dwSize);
	Log2(LOG_ALWAYS, "  dwUserId=%08X (%d)",
		lpUserInfo->dwUserId, lpUserInfo->dwUserId);
}


#define STRING_CASE(val)               case val: pcsz = #val; break


/*  G E T  C O N F N  S T R I N G */
/*----------------------------------------------------------------------------
	%%Function: GetConfnString

----------------------------------------------------------------------------*/
LPCTSTR GetConfnString(DWORD dwCode)
{
	LPCTSTR pcsz;
	static CHAR sz[MAX_PATH];

	switch (dwCode)
		{
	STRING_CASE(CONFN_CONFERENCE_INIT);
	STRING_CASE(CONFN_CONFERENCE_START);
	STRING_CASE(CONFN_CONFERENCE_STOP);
	STRING_CASE(CONFN_CONFERENCE_ERROR);
	STRING_CASE(CONFN_USER_ADDED);
	STRING_CASE(CONFN_USER_REMOVED);
	STRING_CASE(CONFN_USER_UPDATE);
	STRING_CASE(CONFN_PEER_ADDED);
	STRING_CASE(CONFN_PEER_REMOVED);
	STRING_CASE(CONFN_WINDOW_SHARED);
	STRING_CASE(CONFN_WINDOW_UNSHARED);
	STRING_CASE(CONFN_DATA_SENT);
	STRING_CASE(CONFN_DATA_RECEIVED);
	STRING_CASE(CONFN_FILESEND_START);
	STRING_CASE(CONFN_FILESEND_PROGRESS);
	STRING_CASE(CONFN_FILESEND_COMPLETE);
	STRING_CASE(CONFN_FILESEND_ERROR);
	STRING_CASE(CONFN_FILERECEIVE_START);
	STRING_CASE(CONFN_FILERECEIVE_PROGRESS);
	STRING_CASE(CONFN_FILERECEIVE_COMPLETE);
	STRING_CASE(CONFN_FILERECEIVE_ERROR);
	default:
		wsprintf(sz, "CONFN %d (%X)", dwCode);
		pcsz = sz;
		break;
		}

	return pcsz;
}


/*  G E T  C O N F  E R R  S T R I N G */
/*----------------------------------------------------------------------------
	%%Function: GetConferrString

----------------------------------------------------------------------------*/
LPCTSTR GetConferrString(CONFERR dwErr)
{
	LPCTSTR pcsz;
	static CHAR sz[MAX_PATH];

	switch (dwErr)
	{
	default:
		wsprintf(sz, "Error %d (0x%08X)", dwErr, dwErr);
		pcsz = sz;
		break;
	STRING_CASE(CONFERR_INVALID_PARAMETER);
	STRING_CASE(CONFERR_INVALID_HCONF);
	STRING_CASE(CONFERR_INVALID_BUFFER);
	STRING_CASE(CONFERR_BUFFER_TOO_SMALL);
	STRING_CASE(CONFERR_ENUM_COMPLETE);
	STRING_CASE(CONFERR_INVALID_OPERATION);
	STRING_CASE(CONFERR_INVALID_ADDRESS);
	STRING_CASE(CONFERR_FILE_TRANSFER);
	STRING_CASE(CONFERR_FILE_SEND_ABORT);
	STRING_CASE(CONFERR_FILE_RECEIVE_ABORT);
	STRING_CASE(CONFERR_NO_APP_SHARING);
	STRING_CASE(CONFERR_NOT_SHARED);
	STRING_CASE(CONFERR_NOT_SHAREABLE);
	STRING_CASE(CONFERR_ALREADY_SHARED);
	STRING_CASE(CONFERR_OUT_OF_MEMORY);
	STRING_CASE(CONFERR_FILE_NOT_FOUND);
	STRING_CASE(CONFERR_PATH_NOT_FOUND);
	STRING_CASE(CONFERR_ACCESS_DENIED);
	STRING_CASE(CONFERR_RECEIVE_DIR);
	STRING_CASE(CONFERR_NOT_IMPLEMENTED);
	STRING_CASE(CONFERR_INVALID_HWND);
	STRING_CASE(CONFERR_INTERNAL);
	STRING_CASE(CONFERR_SUCCESS);
	}
	return pcsz;
}
