⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 chwinfo.cpp

📁 Windows上的MUD客户端程序
💻 CPP
📖 第 1 页 / 共 2 页
字号:
/*----------------------------------------------------------------------------
                        _                              _ _       
        /\             | |                            | (_)      
       /  \   _ __   __| |_ __ ___  _ __ ___   ___  __| |_  __ _ 
      / /\ \ | '_ \ / _` | '__/ _ \| '_ ` _ \ / _ \/ _` | |/ _` |
     / ____ \| | | | (_| | | | (_) | | | | | |  __/ (_| | | (_| |
    /_/    \_\_| |_|\__,_|_|  \___/|_| |_| |_|\___|\__,_|_|\__,_|

    The contents of this file are subject to the Andromedia Public
	License Version 1.0 (the "License"); you may not use this file
	except in compliance with the License. You may obtain a copy of
	the License at http://www.andromedia.com/APL/

    Software distributed under the License is distributed on an
	"AS IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
	implied. See the License for the specific language governing
	rights and limitations under the License.

    The Original Code is Pueblo client code, released November 4, 1998.

    The Initial Developer of the Original Code is Andromedia Incorporated.
	Portions created by Andromedia are Copyright (C) 1998 Andromedia
	Incorporated.  All Rights Reserved.

	Andromedia Incorporated                         415.365.6700
	818 Mission Street - 2nd Floor                  415.365.6701 fax
	San Francisco, CA 94103

    Contributor(s):
	--------------------------------------------------------------------------
	   Chaco team:  Dan Greening, Glenn Crocker, Jim Doubek,
	                Coyote Lussier, Pritham Shetty.

					Wrote and designed original codebase.

------------------------------------------------------------------------------

	Contains the implementation of the ChWorldInfo class.

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

// $Header: /home/cvs/chaco/modules/client/msw/ChWorld/ChWInfo.cpp,v 2.25 1996/09/30 19:50:07 pritham Exp $


#include "headers.h"

#include <fstream.h>
#include <ChCore.h>

#include "ChSCWiz.h"
#include "ChWInfo.h"

#if defined( CH_UNIX )

	#include <stdlib.h>

#elif defined( CH_MSW )

	#include <fstream.h>
	#include <ddeml.h>

#if !defined(CH_PUEBLO_PLUGIN)
#include "resource.h"
#else
#include "vwrres.h"
#endif

#endif	// defined( CH_MSW )



/*----------------------------------------------------------------------------
	ChWorldInfo constants
----------------------------------------------------------------------------*/

#define NAME_KEY			"Name"
#define HOST_KEY			"Host"
#define ADDR_KEY			"Addr"
#define PORT_KEY			"Port"
#define TYPE_KEY			"Type"
#define LOGIN_KEY			"Login"
#define LOGIN_KEY_CONNECT		"connect"
#define DESC_KEY			"Desc"
#define USERNAME_KEY		"User"
#define PASSWORD_KEY		"Password"
#define HOMEPAGE_KEY		"HomePage"

#define ADDR_LEN			11				// Length of nn.nn.nn.nn

#define PROGRAM_PATH_LEN	256
#define SHORTCUT_ICON_IDX	1


#define COMMENT_PREFIX		'#'
#define TAG_DELIMITER		'='

#define TAG_SERVER			"server"
#define TAG_PORT			"port"
#define TAG_USERNAME		"username"
#define TAG_PASSWORD		"password"
#define TAG_WORLDNAME		"worldname"
#define TAG_WORLDSERVER		"worldserver"
#define TAG_WORLDPORT		"worldport"
#define TAG_WORLDPORT_DEF		4201
#define TAG_WORLDTYPE		"worldtype"
#define TAG_LOGINTYPE		"logintype"
#define TAG_LOGINTYPE_CONNECT	"connect"
#define TAG_WORLDUSERNAME	"worldusername"
#define TAG_WORLDPASSWORD	"worldpassword"

#if defined( CH_PUEBLO_PLUGIN )
#define TAG_WORLD_LIST		"worldlist"
#define TAG_WORLD_DISCONNECT "worldquit"
#endif




/*----------------------------------------------------------------------------
	ChWorldInfo class
----------------------------------------------------------------------------*/

ChWorldInfo::ChWorldInfo( const string& strCommand ) :
			m_boolValid( false ),
			m_type( otherType ),
			m_loginType( variableLogin ),
			m_sPort( 0 )
{
	string		strTemp = strCommand;

	while (GetKey( strTemp ))
	{
	}
											/* If we still haven't read in an
												explicit login type, then
												try to figure out the login
												type from the server type */
	if (variableLogin == m_loginType)
	{
		m_loginType = m_type.GetLoginType();
	}
											/* If we still don't know, then
												try username/password */
	if (variableLogin == m_loginType)
	{
		m_loginType = unamePwLogin;
	}
}


ChWorldInfo::ChWorldInfo(  const string& strName, const string& strDesc,
							const string& strHost, chint16 sPort,
							const ChWorldType& type, ChLoginType login,
							const string& strUsername,
							const string& strPassword,
							const string& strHomePage ) :
			m_strName( strName ),
			m_strDesc( strDesc ),
			m_type( type ),
			m_loginType( login ),
			m_strHost( strHost ),
			m_sPort( sPort ),
			m_strUsername( strUsername ),
			m_strPassword( strPassword ),
			m_strHomePage( strHomePage )
{
	Validate();
}


ChWorldInfo::ChWorldInfo(  const string& strName, const string& strDesc,
						const string& strHost, const string& strAddr,
						chint16 sPort, const ChWorldType& type,
						ChLoginType login, const string& strUsername,
						const string& strPassword,
						const string& strHomePage ) :
			m_strName( strName ),
			m_strDesc( strDesc ),
			m_type( type ),
			m_loginType( login ),
			m_strHost( strHost ),
			m_strAddr( strAddr ),
			m_sPort( sPort ),
			m_strUsername( strUsername ),
			m_strPassword( strPassword ),
			m_strHomePage( strPassword )
{
	Validate();
}

ChWorldInfo::ChWorldInfo( ifstream& ifile ) :

			m_boolValid( false ),
			m_type( otherType ),
			m_loginType( variableLogin ),
			m_sPort( 0 )
{

   	// Read the file and set all the variables
	if (ifile.is_open())
	{

		if ( FindTag( ifile, TAG_WORLDSERVER, m_strHost ) && !m_strHost.IsEmpty() )
		{
			if (FindTag( ifile, TAG_WORLDPORT, m_sPort ) && (0 == m_sPort))
			{
				m_sPort = TAG_WORLDPORT_DEF;
			}

			FindTag( ifile, TAG_WORLDNAME, m_strName );
			FindTag( ifile, TAG_WORLDUSERNAME, m_strUsername );
			FindTag( ifile, TAG_WORLDPASSWORD, m_strPassword );

#if defined( CH_PUEBLO_PLUGIN )
			FindTag( ifile, TAG_WORLD_LIST, m_strHomePage );
			FindTag( ifile, TAG_WORLD_DISCONNECT, m_strOnDisconnect );
#endif

			string	strType;
			if (FindTag( ifile, TAG_WORLDTYPE, strType ))
			{
				m_type.Set( strType );
			}
			else
			{
				m_type.Set( otherType );
			}

			string		strLoginType;
			m_loginType = variableLogin;
			if (FindTag( ifile, TAG_LOGINTYPE, strLoginType ))
			{
				if (strLoginType.CompareNoCase( TAG_LOGINTYPE_CONNECT ))
				{
					m_loginType = m_type.GetLoginType();
				}
				else
				{
					m_loginType = connectLogin;
				}
			}

			if (variableLogin == m_loginType)
			{							// Default to un/pw login

				m_loginType = unamePwLogin;
			}
		}
	}

	if (variableLogin == m_loginType)
	{
		m_loginType = m_type.GetLoginType();
	}
											/* If we still don't know, then
												try username/password */
	if (variableLogin == m_loginType)
	{
		m_loginType = unamePwLogin;
	}

	Validate();
}



void ChWorldInfo::Stringize( string& strWorld )
{
	string		strTemp;

	if (GetName().GetLength())
	{
		strTemp = GetName();
		Escape( strTemp );

		strWorld = "/" NAME_KEY "={";
		strWorld += strTemp;
		strWorld += "}";
	}

	if (GetHost().GetLength())
	{
		strTemp = GetHost();
		Escape( strTemp );

		strWorld += "/" HOST_KEY "={";
		strWorld += strTemp;
		strWorld += "}";
	}

	if (m_strAddr.GetLength())
	{
		strTemp = m_strAddr;
		Escape( strTemp );

		strWorld += "/" ADDR_KEY "={";
		strWorld += strTemp;
		strWorld += "}";
	}

	if (GetPort() > 0)
	{
		char	buffer[10];

		sprintf( buffer, "%d", (int)GetPort() );
		strTemp = buffer;
		Escape( strTemp );

		strWorld += "/" PORT_KEY "={";
		strWorld += strTemp;
		strWorld += "}";
	}

	if (GetType() != otherType)
	{
		strTemp = GetType().GetName();
		Escape( strTemp );

		strWorld += "/" TYPE_KEY "={";
		strWorld += strTemp;
		strWorld += "}";
	}

	if (GetLoginType() == connectLogin)
	{
		strTemp = LOGIN_KEY_CONNECT;
		Escape( strTemp );

		strWorld += "/" LOGIN_KEY "={";
		strWorld += strTemp;
		strWorld += "}";
	}

	if (GetUsername().GetLength())
	{
		strTemp = GetUsername();
		Escape( strTemp );

		strWorld += "/" USERNAME_KEY "={";
		strWorld += strTemp;
		strWorld += "}";
	}

	if (GetPassword().GetLength())
	{
		string		strPassword = GetPassword();

		ChUtil::EncryptString( strPassword, true, '/' );
		strTemp = strPassword;
		Escape( strTemp );

		strWorld += "/" PASSWORD_KEY "={";
		strWorld += strTemp;
		strWorld += "}";
	}

	if (GetHomePage().GetLength())
	{
		strTemp = GetHomePage();
		Escape( strTemp );

		strWorld += "/" HOMEPAGE_KEY "={";
		strWorld += strTemp;
		strWorld += "}";
	}

	if (GetDesc().GetLength())
	{
		strTemp = GetDesc();
		Escape( strTemp );

		strWorld += "/" DESC_KEY "={";
		strWorld += strTemp;
		strWorld += "}";
	}
}


void ChWorldInfo::CreateShortcut( ChCore* pCore  )
{
	#if defined( CH_MSW )
	{
		CreateWindowsShortcut( pCore );
	}
	#else
	{
		TRACE( "Don't know how to create shortcut for this platform!\n" );
	}
	#endif	// defined( CH_MSW )
}


void ChWorldInfo::Set( const string& strDesc, const string& strHost,
						chint16 sPort, const ChWorldType& type,
						ChLoginType login, const string& strUsername,
						const string& strPassword, const string& strHomePage )
{
	ASSERT( sPort != 0 );

	m_strDesc = strDesc;
	m_strHost = strHost;
	m_sPort = sPort;
	m_type = type;
	m_loginType = login;
	m_strUsername = strUsername;
	m_strPassword = strPassword;
	m_strHomePage = strHomePage;
}


void ChWorldInfo::Set( const string& strDesc, const string& strHost,
						const string& strAddr, chint16 sPort,
						const ChWorldType& type, ChLoginType login,
						const string& strUsername, const string& strPassword,
						const string& strHomePage )
{
	Set( strDesc, strHost, sPort, type, login, strUsername, strPassword,
			strHomePage );

	if (strAddr.GetLength() == ADDR_LEN)
	{
		m_strAddr = strAddr;
	}
}


/*----------------------------------------------------------------------------
	ChWorldInfo protected methods
----------------------------------------------------------------------------*/

bool ChWorldInfo::GetKey( string& strCommand )
{
	if (0 == strCommand.GetLength())
	{
		return false;
	}

	const char*	pstrCommand = strCommand;
	string		strKey;
	string		strValue;

	while (*pstrCommand && (*pstrCommand != '/'))
	{
		pstrCommand++;
	}

	if (*pstrCommand == '/')
	{
		pstrCommand++;
											/* We found a slash, now find the
												equal sign */

		while (*pstrCommand && (*pstrCommand != '='))
		{
			strKey += *pstrCommand;
			pstrCommand++;
		}

		if (*pstrCommand == '=')
		{
			bool	boolQuote;
			bool	boolFoundEnd = false;

			pstrCommand++;
											/* We found the open brace, now
												find the end of the value */

			if (boolQuote = (*pstrCommand == '{'))
			{
											// Value is bracketted by quotes
				pstrCommand++;
			}

			while (*pstrCommand && !boolFoundEnd)
			{
				if (boolQuote && (*pstrCommand == '}'))
				{
					pstrCommand++;
					boolFoundEnd = true;
				}
				else if (!boolQuote && (*pstrCommand == '/'))
				{
					boolFoundEnd = true;
				}
				else
				{
					if (boolQuote && (*pstrCommand == '\\'))
					{
											// Unescape next character
						pstrCommand++;
					}

					if (*pstrCommand)
					{
						strValue += *pstrCommand;
						pstrCommand++;
					}
				}
			}

			if (strValue.GetLength())
			{								// Process the tag and value
				ProcessKey( strKey, strValue );
			}
		}
	}
											/* Strip what we just processed
												off of this string */
	if (*pstrCommand)
	{
		strCommand = strCommand.Mid( pstrCommand -
										(const char*)strCommand );
	}
	else
	{
		strCommand = "";
	}

	return true;
}

void ChWorldInfo::ProcessKey( const string& strKey, const string& strValue )
{
	if (0 == strKey.CompareNoCase( HOST_KEY ))
	{										// Host key found

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -