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

📄 eventcmt.cpp

📁 windows的snmp api源码
💻 CPP
📖 第 1 页 / 共 5 页
字号:
			if (next && (*next != prag))	//first token not #pragma - log an error
			{ 
				if (next->GetAt(0) != comment.GetAt(0))	//check it isn't a comment.
				{
					CString msg;
					msg.LoadString(IDS_MSG18);
					wholebuff += msg;
					wholebuff += NL;
					badlines++;
					SetError(EVCMT_INVALID_COMMAND);
					StatusMif(IDS_SYNTAX_ERROR, FALSE);
				}
				else		//it is a comment, say so in the log
				{
					CString msg;
					msg.LoadString(IDS_MSG17);
					wholebuff += msg;
					wholebuff += NL;
				}

				WriteToLog(&wholebuff);
				delete next;
				continue;	//skip to the next line in the file	
			}
			
			delete next;
			next = CStringStrtok (&CBuff);	//get the next token from the string
			
			if (next)						//this should be the command type
			{
				next->MakeUpper();
				CString AddCom;
				AddCom.LoadString(IDS_ADD);			//add
				CString DelCom;
				DelCom.LoadString(IDS_DEL);			//delete
				CString AddTCom;
				AddTCom.LoadString(IDS_ADDTRAP);	//add_trap_dest
				CString DelTCom;
				DelTCom.LoadString(IDS_DELTRAP);	//delete_trap_dest
				CommandItem * com;
				TrapCommandItem * tcom;

				if (*next == AddCom)							//add translation config
				{
					com = GetCommandArgs(&wholebuff, &CBuff);	//get the rest of the command args.
					
					if (com)	//GetCommandArgs succeded, we have a command to process
					{
						com->SetCommand(Add);	//set the command type
						CommandQ.Add(com);		//add it to the list
						goodlines++;			//increment the numer of good commands found
					}
					else		//GetCommandArgs failed, bad args - set the error and mif
					{
						badlines++;			//increment the numer of bad commands found
						SetError(EVCMT_INVALID_COMMAND);
						StatusMif(IDS_SYNTAX_ERROR, FALSE);
						delete next;
						continue;
					}
				}
				else if (*next == DelCom)	//delete translation config 
				{
					com = GetCommandArgs(&wholebuff, &CBuff); 
					
					if (com)	//GetCommandArgs succeded, we have a command to process
					{
						com->SetCommand(Delete);	//set the command type
						CommandQ.Add(com);			//add it to the list
						goodlines++;				//increment the numer of good commands found
					}
					else		//GetCommandArgs failed, bad args - set the error and mif
					{
						badlines++;			//increment the numer of bad commands found
						SetError(EVCMT_INVALID_COMMAND);
						StatusMif(IDS_SYNTAX_ERROR, FALSE);
						delete next;
						continue;
					}
				}
				else if (*next == AddTCom)	//add trap config
				{
					if(!SNMPinstalled)		//no snmp, set error and mif
					{
						CString msg;
						msg.LoadString(IDS_MSG50);
						wholebuff += msg;
						wholebuff += NL;
						WriteToLog(&wholebuff);
						badlines++;
						StatusMif(IDS_INVALID_SNMP, FALSE);
						delete next;
						continue;
					}

					tcom = GetTrapCommandArgs(&wholebuff, &CBuff); 	//get the rest of the arguments
					
					if (tcom)		//GetTrapCommandArgs succeded, something to process
					{
						tcom->SetCommand(AddTrap);	//set the command type
						TrapQ.Add(tcom);			//add it to the list
						goodlines++;
					}
					else			//GetTrapCommandArgs failed set error and mif
					{
						badlines++;
						SetError(EVCMT_INVALID_COMMAND);
						StatusMif(IDS_SYNTAX_ERROR, FALSE);
						delete next;
						continue;
					}
				}
				else if (*next == DelTCom)	//add trap config
				{
					if(!SNMPinstalled)		//no snmp, set error and mif
					{
						CString msg;
						msg.LoadString(IDS_MSG50);
						wholebuff += msg;
						wholebuff += NL;
						WriteToLog(&wholebuff);
						badlines++;
						StatusMif(IDS_INVALID_SNMP, FALSE);
						delete next;
						continue;
					}

					tcom = GetTrapCommandArgs(&wholebuff, &CBuff);	//get the rest of the arguments 
					
					if (tcom)		//GetTrapCommandArgs succeded, something to process
					{
						tcom->SetCommand(DeleteTrap);	//set the command type
						TrapQ.Add(tcom);				//add it to the list
						goodlines++;
					}
					else 			//GetTrapCommandArgs failed set error and mif
					{
						badlines++;
						SetError(EVCMT_INVALID_COMMAND);
						StatusMif(IDS_SYNTAX_ERROR, FALSE);
						delete next;
						continue;
					}
				}
				else	//we don't have a valid command type set error and mif
				{
					CString msg;
					msg.LoadString(IDS_MSG19);
					wholebuff += msg;
					wholebuff += NL;
					WriteToLog(&wholebuff);
					badlines++;
					SetError(EVCMT_INVALID_COMMAND);
					StatusMif(IDS_SYNTAX_ERROR, FALSE);
					delete next;
					continue;	
				}
			
				delete next;

			} 
		}


		//	Finished reading the file report this to the log
		//	================================================

		CString msg2;
		msg2 += NL;
		msg2 += NL;
		CString read;
		read.LoadString(IDS_MSG20);
		msg2 +=	read;
		msg2 += NL;
		char num[34];
		_ultoa(badlines, num, 10); 
		msg2 += num;
		read.Empty();
		read.LoadString(IDS_MSG21);
		msg2 += read;
		msg2 += NL;
		_ultoa(goodlines, num, 10); 
		msg2 += num;
		read.Empty();
		read.LoadString(IDS_MSG22);
		msg2 += read;
		msg2 += NL;
		msg2 += NL;
		WriteToLog(&msg2);
		CloseHandle(hfile);
	}
}


//============================================================================
//  EventConfigModifier::GetTrapCommandArgs
//
//  This private method is used to read the arguments specified in a line of
//	text supplied as a parameter. If there is an error it returns NULL and
//	writes to the log. If it succedes it returns a TrapCommandItem.
//
//
//  Parameters:
//
//      CString * buffer		A pointer to the CString which is the complete
//								command line used to write to the log.
//
//		CString * comline		A pointer to the CString which is the partial
//								command line containing the arguments to be
//								read.
//
//  Returns:
//
//      TrapCommandItem *		A pointer to a TrapCommandItem. The arguments
//								stripped from the input line and inserted into
//								this class. This is NULL if there is an error. 
//
//============================================================================

TrapCommandItem * EventConfigModifier::GetTrapCommandArgs(CString * buffer, CString * comline)
{
	TrapCommandItem * newCom = NULL;
	CString * comm;
	CString * addr;
	comm = MyStrtok(comline);


	if(!comm)	//error no community name
	{
		CString add;
		add.LoadString(IDS_MSG23);
		*buffer += add;
		*buffer += NL;
		WriteToLog(buffer);
		return newCom;
	}
				
	addr = MyStrtok(comline);
	
	if(!addr)	//error no address specified
	{
		delete comm;

		CString add;
		add.LoadString(IDS_MSG25);
		*buffer += add;
		*buffer += NL;
		WriteToLog(buffer);
		return newCom;
	}

	CString * extra = CStringStrtok(comline);

	if(extra)	//error too many arguments
	{
		delete comm;
		delete addr;
		delete extra;

		CString add;
		add.LoadString(IDS_MSG26);
		*buffer += add;
		*buffer += NL;
		WriteToLog(buffer);
		return newCom;
	}
	

	//	If we got this far we've got something to process create the TrapCommandItem
	//	============================================================================

	newCom = new TrapCommandItem(AddTrap, //just for a default value
								comm, addr, hkey_machine);

	CString add;
	add.LoadString(IDS_MSG27);
	*buffer += add;
	*buffer += NL;
	WriteToLog(buffer);

 	return newCom;
}


//============================================================================
//  EventConfigModifier::CStringStrtok
//
//  This private method is used to read a space separated token in a line of
//	text supplied as a parameter. If there is an error it returns NULL. If it
//	succedes it returns a pointer to the CString token. (A bit like strtok).
//
//
//  Parameters:
//
//      CString * In		A pointer to the CString which is to be used to
//							be read from. This CString is MODIFIED by this
//							function. This CString has the leading token from
//							it and any leading whitespace removed from it.
//
//
//  Returns:
//
//      CString *			A pointer to the CString containing the token. This
//							is NULL if there is an error or an empty string input.
//
//============================================================================

CString * EventConfigModifier::CStringStrtok(CString * In)
{
	CString * ret = NULL;
    BOOL    more;
    int     i;

	if (!In)		//no input specified
		return ret;

	In->TrimLeft();	//remove leading whitespace

	if (!In->GetLength())	//if after removing whitespace there is no length return
		return ret;

	CString wSpace;
	wSpace.LoadString(IDS_SPACE);


	//	The outer for loop steps through the input string character by character
	//	The inner for loop steps through all possible whitespace characters
	//	========================================================================

    more = TRUE;
	for (i = 0; i < In->GetLength(); i++)
	{
		for (int j = 0; more && j < wSpace.GetLength(); j++)
		{
            if (In->GetAt(i) == wSpace.GetAt(j))
                more = FALSE;
		}
        if (!more)
            break;
	}

	ret = new CString;
	
	//	If we get here we have a valid token within the input string
	//	Copy the string upto this point to the string that we return
	//	============================================================

	for (int k = 0; k < i; k++)
	{
		*ret += In->GetAt(k);
		In->SetAt(k,tab.GetAt(0));
	}

	In->TrimLeft();	//remove leading whitespace from the input string
	return ret;

}


//============================================================================
//  EventConfigModifier::MyStrtok
//
//  This private method is used to read a space/quote separated token in a line
//	of text supplied as a parameter. If there is an error it returns NULL. If
//	it succedes it returns a pointer to the CString token. (A bit like strtok).
//
//
//  Parameters:
//
//      CString * In		A pointer to the CString which is to be used to
//							be read from. This CString is MODIFIED by this
//							function. This CString has the leading token from
//							it and any leading whitespace removed from it.
//
//
//  Returns:
//
//      CString *			A pointer to the CString containing the token. This
//							is NULL if there is an error or an empty string input.
//
//============================================================================

CString * EventConfigModifier::MyStrtok(CString * In)
{
	CString * ret = CStringStrtok(In);

	if (!ret)	//nothing (maybe whitespace) in input string
		return ret;

	CString temp(*ret);
	CString space;
	space.LoadString(IDS_ASPACE);
	CString quote;
	quote.LoadString(IDS_QUOTE);
	delete ret;
	ret = NULL;
	int length = temp.GetLength();


	//	This loop steps keeps getting tokens until start and end quotes match
	//	i.e. for multiple worded quoted strings e.g. "Print Manager"
	//	=====================================================================

	while ( (temp.GetAt(0) == quote.GetAt(0)) &&
				(temp.GetAt(length - 1) != quote.GetAt(0)) )
	{
		CString * extra = CStringStrtok(In); //Get the next token

		if (!extra)
		{
			ret = new CString(temp);
			return ret; //return the string with the quote!
		}

		temp += space; //the space was stripped by strtok.
		temp += *extra;
		delete extra;
		length = temp.GetLength();
	}


	//	Get rid of the quotes only if the length is greater than one - could be a single "
	//	==================================================================================

	if ((length > 1) && 
			(temp.GetAt(0) == quote.GetAt(0)) && 
			(temp.GetAt(length - 1) == quote.GetAt(0)))
	{
		temp.SetAt(0, tab.GetAt(0));
		temp.TrimLeft();
		length = temp.GetLength();
		temp.SetAt(length - 1, tab.GetAt(0));
		temp.TrimRight();
		length = temp.GetLength();
	}

	if (length)
	{
		ret = new CString(temp);	//create the return string from what we have built
	}

	return ret;
}


//============================================================================
//  EventConfigModifier::GetCommandArgs
//
//  This private method is used to read the arguments specified in a line of
//	text supplied as a parameter. If there is an error it returns NULL and
//	writes to the log. If it succedes it returns a TrapCommandItem.
//
//
//  Parameters:
//
//      CString * buffer		A pointer to the CString which is the complete
//								command line used to write to the log.
//
//		CString * comline		A pointer to the CString which is the partial
//								command line containing the arguments to be
//								read.
//
//  Returns:
//
//      CommandItem *			A pointer to a CommandItem. The arguments
//								stripped from the input line and inserted into
//								this class. This is NULL if there is an error. 
//
//============================================================================

CommandItem * EventConfigModifier::GetCommandArgs(CString * buffer, CString * comline)
{
	CommandItem * newCom = NULL;
	CString * evlog;
	CString * evsrc;
	CString * evid;
	CString * evcnt;
	CString * evtm;
	DWORD evtid = 0;
	DWORD evtcount = 0;
	DWORD evttime = 0;
	evlog = MyStrtok(comline);
	
	if(!evlog)	//no eventlog argument
	{
		CString add;
		add.LoadString(IDS_MSG28);
		*buffer += add;
		*buffer += NL;
		WriteToLog(buffer);
		return newCom;
	}
				
	evsrc = MyStrtok(comline);
	
	if(!evsrc)	//no event source argument
	{
		delete evlog;

		CString add;
		add.LoadString(IDS_MSG29);
		*buffer += add;
		*buffer += NL;
		WriteToLog(buffer);
		return newCom;
	}

	evid = MyStrtok(comline);

	if(evid)	//eventid argument has to be converted into a DWORD
	{
		BOOL badstr = StrToDword(evid, &evtid);	//convert the id into a DWORD
		delete evid;

		if (badstr)	//invalid eventid
		{
			delete evlog;
			delete evsrc;

			//Add it to the list of bad lines - invalid or missing command

			CString add;
			add.LoadString(IDS_MSG30);
			*buffer += add;
			*buffer += NL;
			WriteToLog(buffer);
			return newCom;
		}

	}
	else	//no eventid argument was specified
	{
		delete evlog;
		delete evsrc;
		
		CString add;
		add.LoadString(IDS_MSG30);
		*buffer += add;
		*buffer += NL;

⌨️ 快捷键说明

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