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

📄 ccommandline.cpp

📁 PGP8.0源码 请认真阅读您的文件包然后写出其具体功能
💻 CPP
字号:
/*____________________________________________________________________________
		Copyright (C) 2002 PGP Corporation
        All rights reserved.

        $Id: CCommandLine.cpp,v 1.2 2002/08/06 20:09:30 dallen Exp $
____________________________________________________________________________*/

#include "pgpClassesConfig.h"
#include <ctype.h>

#include "pgpClientErrors.h"
#include "CCommandLine.h"

_USING_PGP

_UNNAMED_BEGIN

// Constants

const char	*kOptionPrefixes	= "/-";

_UNNAMED_END

// Class CParameter

class PGP::CParameter : public CListableObject<PGP::CParameter>
{
public:
	CParameter() { }
	CParameter(const char *str) {Assign(str);}
	~CParameter() { }

	PGPBoolean	IsOption() const {return mIsOption;}

	char			OptionChar() const {return mOptionChar;}
	const char *	String() const {return mString;}

	void	Assign(const char *str);

private:
	PGPBoolean	mIsOption;

	char		mOptionChar;
	CString		mString;

	PGPBoolean	IsOptionPrefix(char c) const;
};


// Class CParameter member functions

void 
CParameter::Assign(const char *str)
{
	pgpAssertStrValid(str);

	mString.Empty();
	PGPUInt32	charIndex	= 0;

	if (IsOptionPrefix(str[charIndex]))
	{
		mIsOption = TRUE;
		charIndex++;

		// Only one option per parameter.
		if ((str[charIndex] == '\0') || (str[charIndex + 1] != '\0'))
			THROW_PGPERROR(kPGPClientError_InvalidCmdLine);
		else
			mOptionChar = str[charIndex];
	}
	else
	{
		// Not an option, so just copy over the string we have.
		mIsOption = FALSE;
		mString = str;
	}
}

PGPBoolean 
CParameter::IsOptionPrefix(char c) const
{
	CString	prefixes(kOptionPrefixes);
	return (prefixes.Find(c) != -1);
}


// Class CCommandLine member functions

CCommandLine::CCommandLine() : mPCurParam(NULL)
{
}

CCommandLine::CCommandLine(
	const char	*cmdLine, 
	Rule		*pRules, 
	PGPUInt32	numRules) : mPCurParam(NULL)
{
	Init(cmdLine, pRules, numRules);
}

CCommandLine::~CCommandLine()
{
	try
	{
		Clear();
	}
	catch (CComboError&) { }
}

void 
CCommandLine::AllowCatchAll(PGPBoolean allow, Rule& catchAllRule)
{
	mAllowCatchAll = allow;
	mCatchAllRule = catchAllRule;
}

PGPBoolean 
CCommandLine::Parse(
	const char *&			command, 
	CArray<char>&			options, 
	PGPUInt32&				numOptions, 
	CArray<const char *>&	args, 
	PGPUInt32&				numArgs)
{
	command = NULL;

	numOptions = 0;
	numArgs = 0;

	if (IsNull(mPCurParam))
		return FALSE;

	// Find rule for current commmand.
	PGPUInt32	i;
	Rule		*pRule	= NULL;

	for (i = 0; i < mNumRules; i++)
	{
		CString	curString(mPCurParam->String());

		if (curString.CompareNoCase(mRules[i].command))
		{
			pRule = &mRules[i];
			mPCurParam = mParams.Next(mPCurParam);

			break;
		}
	}

	if (IsNull(pRule))
	{
		if (mAllowCatchAll)
			pRule = &mCatchAllRule;
		else
			THROW_PGPERROR(kPGPClientError_InvalidCmdLine);
	}

	command = pRule->command;

	// Gather and validate options.
	while (IsntNull(mPCurParam) && mPCurParam->IsOption())
	{
		CString	validOptions(pRule->validOptions);

		if (validOptions.Find(mPCurParam->OptionChar()) == -1)
			THROW_PGPERROR(kPGPClientError_InvalidCmdLine);

		options.Resize(++numOptions);
		options[numOptions - 1] = mPCurParam->OptionChar();

		mPCurParam = mParams.Next(mPCurParam);
	}

	// Gather args.
	while (IsntNull(mPCurParam))
	{
		if (mPCurParam->IsOption())
			THROW_PGPERROR(kPGPClientError_InvalidCmdLine);

		if (++numArgs > pRule->maxArgs)
			THROW_PGPERROR(kPGPClientError_InvalidCmdLine);

		args.Resize(numArgs);
		args[numArgs - 1] = mPCurParam->String();

		mPCurParam = mParams.Next(mPCurParam);
	}

	if (numArgs < pRule->minArgs)
		THROW_PGPERROR(kPGPClientError_InvalidCmdLine);

	return TRUE;
}

void 
CCommandLine::Init(const char *cmdLine, Rule *pRules, PGPUInt32 numRules)
{
	pgpAssertStrValid(cmdLine);

	Clear();

	// Remember rules.
	mRules.Resize(numRules);
	pgpCopyMemory(pRules, mRules.Get(), numRules * sizeof(Rule));

	mNumRules = numRules;

	// Construct list of parameters;
	CString		curParam;
	PGPBoolean	inQuotes	= FALSE;
	PGPUInt32	length		= strlen(cmdLine);

	for (PGPUInt32 i = 0; i < length + 1; i++)
	{
		char		c			= cmdLine[i];
		PGPBoolean	addParam	= FALSE;

		if (c == '\0')
		{
			addParam = !curParam.IsEmpty();
		}
		else if (isspace(c))
		{
			if (inQuotes)
				curParam.Append(c);
			else
				addParam = !curParam.IsEmpty();
		}
		else if (c == '"')
		{
			if (inQuotes)
				addParam = !curParam.IsEmpty();

			inQuotes = !inQuotes;
		}
		else
		{
			curParam.Append(c);
		}

		if (addParam)
		{
			CParameter	*pParam	= new CParameter(curParam);
			mParams.AddTail(pParam);

			curParam.Empty();
		}
	}

	if (inQuotes)
		THROW_PGPERROR(kPGPClientError_InvalidCmdLine);

	mPCurParam = mParams.Head();
}

void 
CCommandLine::Clear()
{
	mParams.EmptyWithDelete();

	mPCurParam = NULL;
	mAllowCatchAll = FALSE;
}

⌨️ 快捷键说明

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