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

📄 activeobjectexample.cpp

📁 《基于symbian os 的手机开发与应用》 源代码 何伟 著
💻 CPP
字号:

//ActiveObjecExample.cpp

//Author:LiuLiPing
//version 1.0
//Date: 2006-1-25
//Example for showing you input words and if you press "Enter" showing how many
//you have inputed.

#include <e32cons.h>

LOCAL_D CConsoleBase* console;    // All messages written to this

// Function prototypes
 void callExampleL();
 void doExampleL();

//Static variable for count-switch-variable and count how many word you have inputed 
static TInt word;
static TInt num;

//////////////////////////////////////////////////////////////////////////////
//
//       CWord (definition)
//
// A class for print and count the word number
//
//////////////////////////////////////////////////////////////////////////////

class CWord:public CBase
{
public:
	static CWord* NewL(TInt aMaxLength);
	static CWord* NewLC(TInt aMaxLength);
private:
	CWord(TInt aMaxLength);
	void ConstructL(TInt aMaxLength);
public:
	TInt WordNumCountL(TChar aChar);	//Count the number of you input word
	void OutputWord();					//Outputing you input word
	TInt WordNum();						//return the number of the word that you input
	TInt Length();						//return the length of the curent word you have input
	TInt MaxLength();					//return the maxlenght of the word for limited your 
										//inputing and printing the word
	~CWord();
private:
	TInt iLength;
	TInt iMaxLength;
	HBufC* iHBufC;
};

//////////////////////////////////////////////////////////////////////////////
//
//       CAOExample (definition)
//
//////////////////////////////////////////////////////////////////////////////

class CAOExample:public CActive
{
public:
	static CAOExample* NewL(CConsoleBase* aConsole);
	static CAOExample* NewLC(CConsoleBase* aConsole);
private:
	CAOExample();
	void ConstructL(CConsoleBase* aConsole);
public:
	void Start();
	void PrintWord();					//Print the word
	void PrintWordNum();				//Print the number of the word
	void KeyPressInput(TChar aChar);	//process the key press input
	~CAOExample();
private:
	void RunL();
	void Cancel();
	void DoCancel();
private:
	CConsoleBase* iConsole;
	CWord* iCWord;

};

////////////////////////////////////////////////////////////////////////////////
//
//		CWord implement
// 
////////////////////////////////////////////////////////////////////////////////

CWord* CWord::NewL(TInt aMaxLength)
{
   CWord* self = NewLC(aMaxLength);
   CleanupStack::Pop(self);
   return self;
}
CWord* CWord::NewLC(TInt aMaxLength)
{
   CWord* self = new(ELeave) CWord(aMaxLength);
   CleanupStack::PushL(self);
   self->ConstructL(aMaxLength);
   return self;
}

void CWord::ConstructL(TInt aMaxLength)
{
   iHBufC = HBufC::NewL(aMaxLength);
}

CWord::CWord(TInt aMaxLength)
{
   iMaxLength = aMaxLength;
   iLength = 0;
}

TInt CWord::WordNumCountL(TChar aChar)
{
   TPtr ptr=iHBufC->Des();//Des():Creates and returns an modifiable pointer descriptor for the data represented by this heap descriptor.
   if(iLength<iMaxLength)
   {
	  ptr.Append(aChar);		 
	  if(!('A'<=aChar && aChar <='Z' || 'a'<=aChar && aChar <='z'))//if your input is not a letter
	  {
		 word = 0;
	  }
	  if('A'<=aChar && aChar <='Z' || 'a'<=aChar && aChar <='z')//if your input is a letter
	  {
     	 if(word==0)
		 {
		    word=1;
			++num;		//sum up the number of word 
		 }
      
	  }
	  iLength++;
      return 1;
   }
	else 
	  return 0;
}

void CWord::OutputWord()
{
   console->Printf(iHBufC->Des());
   iHBufC->Des().Zero();			//Sets the length of the data to zero
   iLength = 0;
}

TInt CWord::WordNum()
{
   return num;
}

TInt CWord::Length()
{
    return iLength;
}

TInt CWord::MaxLength()
{
    return iMaxLength;
}

CWord::~CWord()
{
   delete iHBufC;
   iHBufC = NULL;
   iLength = 0;
   iMaxLength = 0;
}

////////////////////////////////////////////////////////////////////////////////
//
//		CAOExample implement
// 
////////////////////////////////////////////////////////////////////////////////

CAOExample* CAOExample::NewL(CConsoleBase* aConsole)
{
   CAOExample* self =  NewLC(aConsole);
   CleanupStack::Pop(self);
   return self;
}

CAOExample* CAOExample::NewLC(CConsoleBase* aConsole)
{
   CAOExample* self = new(ELeave) CAOExample();
   CleanupStack::PushL(self);
   self->ConstructL(aConsole);
   return self;
}

void CAOExample::ConstructL(CConsoleBase* aConsole)
{
   iConsole = aConsole;
   iCWord = CWord::NewL(1);
   CActiveScheduler::Add(this);
}

CAOExample::CAOExample():CActive(EPriorityStandard)
{
}
void CAOExample::Start()
{
   iConsole->Read(iStatus);
   SetActive();
}

void CAOExample::PrintWord()
{
	iCWord->OutputWord();
}

void CAOExample::PrintWordNum()
{
	_LIT(KPrint,"You Input %d Word!\n");	//define the output format
	console->Printf(KPrint,iCWord->WordNum());
	//console->Printf(_L("If you want to know how many word you input please press \"Enter\"!"));
	console->Printf(_L("Please input the new word!\n"));
}

void CAOExample::KeyPressInput(TChar aChar)
{
	//if(aChar != EKeyEscape)
	//{
	iCWord->WordNumCountL(aChar);
	if(iCWord->Length() == iCWord->MaxLength())
	{
	   PrintWord();
	}
	if(aChar == EKeyEnter)
	{
		PrintWordNum();
		word = 0;
		num = 0;
	}
    Start();
	//}
	//else
	//{
	//	Cancel();
    //    CActiveScheduler::Stop();
	//}
 
}

void CAOExample::RunL()
{
   KeyPressInput((TChar)iConsole->KeyCode());
}

void CAOExample::Cancel()
{
   DoCancel();
}

void CAOExample::DoCancel()
{
   iConsole->ReadCancel(); //Cancels any pending Read() operations
}

CAOExample::~CAOExample()
{
	delete iCWord;
	iCWord = NULL;
	iConsole = NULL;
}

GLDEF_C TInt E32Main()
{
	
   CTrapCleanup* cleanup=CTrapCleanup::New();	// Get cleanup stack
   TRAPD(error,doExampleL());					// callExampleL() should never leave.
   _LIT(KMsgPanicEpoc32ex,"EPOC32EX");
   __ASSERT_ALWAYS(!error,User::Panic(KMsgPanicEpoc32ex,error));
   delete cleanup;								// destroy the cleanup stack
   return 0;									// return
    }

////////////////////////////////////////////////////////////////////////////////
//
//		doExample() implement
// 
////////////////////////////////////////////////////////////////////////////////

void doExampleL()
{
  	_LIT(KMsgExampleCode,"Symbian OS AOExample");
	console = Console::NewL(KMsgExampleCode,TSize(KConsFullScreen,KConsFullScreen));
	CleanupStack::PushL(console);		// Put console onto the cleanup stack.
	int error;
	TRAP(error,callExampleL());			//TRAPD(error,callExampleL());
	    if(error)
	{
		_LIT(KERROR,"error occured!\n");
		console->Printf(KERROR);
	}
	else{
      _LIT(KNOLEAVE,"No Leave!\n");
		console->Printf(KNOLEAVE);
	}
	console->Getch();
	CleanupStack::PopAndDestroy();		//Remove the cleanupstack and destroy
}

////////////////////////////////////////////////////////////////////////////////
//
//		callExample() implement
// 
////////////////////////////////////////////////////////////////////////////////

void callExampleL() 
    {
     CActiveScheduler* AS=new (ELeave)CActiveScheduler;	
	 CleanupStack::PushL(AS);
	 CActiveScheduler::Install(AS);	
	 CAOExample* activeObjectExample=CAOExample::NewLC(console);			
	 activeObjectExample->Start();							
	 console->Printf(_L("If you want to know how many word you input please press \"Enter\"!"));
	 console->Printf(_L("Please input the new word!\n"));
	 CActiveScheduler::Start();		
	 CleanupStack::PopAndDestroy();
}

⌨️ 快捷键说明

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