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

📄 modifier.cpp

📁 Symbain mobile code 手机应用程序源代码--基本结构方面
💻 CPP
📖 第 1 页 / 共 2 页
字号:
// Modifier.cpp
//
// Copyright (C) Symbian Software Ltd 2000-2005.  All rights reserved.

// Examples to demonstrate some of the modifying member
// functions of descriptors.

#include "CommonFramework.h"

//
// Common literal text 
//
_LIT(KPressAnyKeyToContinue," (press any key to continue)\n");
_LIT(KTextHelloWorld,"Hello World!");
_LIT(KTextHello,"Hello");
_LIT(KTextXYZ,"XYZ");
_LIT(KTextDinner,"D\214ner \205 la Fran\207ais");

//
// Common format strings
//

_LIT(KFormatfourex,"%4x");
_LIT(KFormatBufLenSizeMax,"\"%S\"; %d; %d; %d\n");
_LIT(KFormatLenSizeMax,"; %d; %d; %d\n");
_LIT(KFormatBufLen,"\"%S\"; Length()=%d; ");
_LIT(KFormatSizeMax,"Size()=%d; MaxLength()=%d\n");
_LIT(KFormatCommon,"0x%02x ");

			// Define and implement
			// the overflow handler used
			// by the AppendFormat() example.

class TestOverflow : public TDesOverflow
	{
	void Overflow(TDes& aDes);
	};


void TestOverflow::Overflow(TDes& aDes)
	{
	_LIT(KTextDescOverflow,"Descriptor overflow - maxlength %d\n");
	console->Printf(KTextDescOverflow,aDes.MaxLength());
	}


// Do the example
LOCAL_C void doExampleL()
    {				   

	TInt index;
	TInt counter;

				// Use a TBuf to demonstrate some of these
	TBuf<32> buf(KTextHelloWorld);
		
			//				
			// Copy(),CopyF(), CopyUC() & CopyCP()  * * * 
			//
			// Note that CopyF() CopyUC() & CopyCP() are locale dependent

	_LIT(KTitleCopy,"\n--->Copy(),CopyF(),CopyUC() & CopyCP()\n");
	_LIT(KNoteAboutLocale1,"\nNote that the behaviour of CopyF(), CopyUC() & CopyCP() is\n");
	_LIT(KNoteAboutLocale2,"dependent on the locale.\n");
	console->Printf(KTitleCopy);
	console->Printf(KNoteAboutLocale1);
	console->Printf(KNoteAboutLocale2);
						
				// Show buf's content,length,size and
				// maximum size.
	console->Printf(KFormatBufLen,&buf,buf.Length());
	console->Printf(KFormatSizeMax,buf.Size(),buf.MaxLength());

			 	// Copy normal
	buf.Copy(KTextDinner);

				// Show buf's content,length,size and 
				// maximum size.
	console->Printf(KFormatBufLen,&buf,buf.Length());
	console->Printf(KFormatSizeMax,buf.Size(),buf.MaxLength());
	

				// Copy folded - accents are not preserved
				//
				// (NB the display may use a different code
				// page to that used by the base, in which 
				// case the accents will seem to be preserved)
	buf.CopyF(KTextDinner);
				
				// Show buf's new content,length,size
				// and max size
	console->Printf(KFormatBufLen,&buf,buf.Length());
	console->Printf(KFormatSizeMax,buf.Size(),buf.MaxLength());
				// Copy uppercase
				// Note that accents are preserved.
	buf.CopyUC(KTextDinner);
				
				// Show buf's new content,length,size
				// and maximum size
	console->Printf(KFormatBufLen,&buf,buf.Length());
	console->Printf(KFormatSizeMax,buf.Size(),buf.MaxLength());

				// Copy capitalised
				// Note that accents are preserved.
	_LIT(KTextCatOnMat,"tHe CaT sAt On ThE mAt - voil\205");
	buf.CopyCP(KTextCatOnMat);
				
				// Show buf's new content,length,size
				// and max size
	console->Printf(KFormatBufLen,&buf,buf.Length());
	console->Printf(KFormatSizeMax,buf.Size(),buf.MaxLength());
					  	
				// Length of copied text > 32 causes panic !!
				// 
				// Remove the "//" marks on the next two lines
				// to see this happen
	//_LIT(KTxtCausePanic1,"Panic caused by an attempt to replace text.");
	//buf.Copy(KTxtCausePanic1);
    
			//
	 		// Append() & operator+=  * * * * * * * * * 
			//
	_LIT(KTitleAppend,"\n--->Append & Operator+=");
	console->Printf(KTitleAppend);
	console->Printf(KPressAnyKeyToContinue);
	console->Getch();

				// Show buf's content,length,size and
				// maximum size.
	buf = KTextHelloWorld;
	console->Printf(KFormatBufLen,&buf,buf.Length());
	console->Printf(KFormatSizeMax,buf.Size(),buf.MaxLength());

				// Append:
				//     1. a single character
				//     2. a descriptor
				//     3. another descrptor using operator +=
				//
				// Generates the string
				// "Hello World!@XYZ##" in buf
	buf.Append('@');
	buf.Append(KTextXYZ);
	_LIT(KTextHashHash,"##");
	buf += KTextHashHash;
	console->Printf(KFormatBufLen,&buf,buf.Length());
	console->Printf(KFormatSizeMax,buf.Size(),buf.MaxLength());

			//				
			// Swap()  * * * * * * * * * * * * * * * 
			//
	_LIT(KTitleSwap,"\n--->Swap()");
	console->Printf(KTitleSwap);
	console->Printf(KPressAnyKeyToContinue);
	console->Getch();
	
	_LIT(KWhatANiceDay,"What a nice day");
	TBuf<16> altbuf1(KWhatANiceDay);
	buf = KTextHelloWorld;

				// Show buf and altbuf BEFORE swap
	_LIT(KTextBefore,"          BEFORE...\n");
	console->Printf(KTextBefore);
	console->Printf(KFormatBufLen,&buf,buf.Length());
	console->Printf(KFormatSizeMax,buf.Size(),buf.MaxLength());
	
	console->Printf(KFormatBufLen,&altbuf1,altbuf1.Length());
	console->Printf(KFormatSizeMax,altbuf1.Size(),altbuf1.MaxLength());
	
				// now swap the descriptors and look at
				// buf and altbuf again;
				// The content, length and size of the 
				// descriptors have swapped; their maximum 
				// lengths have NOT changed.
	buf.Swap(altbuf1);

	_LIT(KTextAfter,"          AFTER...\n");
	console->Printf(KTextAfter);
	console->Printf(KFormatBufLen,&buf,buf.Length());
	console->Printf(KFormatSizeMax,buf.Size(),buf.MaxLength());
	
	console->Printf(KFormatBufLen,&altbuf1,altbuf1.Length());
	console->Printf(KFormatSizeMax,altbuf1.Size(),altbuf1.MaxLength()
					  );

				// Swap is ok provided the maximum length
				// of each descriptor is big enough to 
				// hold the other's data.
	
				// altbuf2 is too small to accommodate 
				// buf's data !!
				// 
				// Remove the "//" marks on the next three lines
				// to see this panic
	//_LIT(KTxtxxx,"xxx");
	//TBuf<8>  altbuf2(KTxtxxx);
	//buf = KTextHelloWorld;
	//buf.Swap(altbuf2);
    
	
			//				
			// Repeat()  * * * * * * * * * * * * * * * 
			//
	_LIT(KTitleRepeat,"\n--->Repeat()");
	console->Printf(KTitleRepeat);
	console->Printf(KPressAnyKeyToContinue);
	console->Getch();

				// Set current length of buf to 16 and
				// copy repeat the characters "Hello".
				// The descriptor is filled up to 
				// its CURRENT LENGTH.
				// Result is the 16 charcters
				// "HelloHelloHelloH"
				//
				// Note the truncation. 
	buf.SetLength(16);
	buf.Repeat(KTextHello);

				// Look at it.
	console->Printf(KFormatBufLen,&buf,buf.Length());
	console->Printf(KFormatSizeMax,buf.Size(),buf.MaxLength());

				// Now set the length to 8 characters
				// and do Repeat again.
				// Result is the 8 characters
				// "HelloHel"

	buf.SetLength(8);
	buf.Repeat(KTextHello);

				// Look at it
	console->Printf(KFormatBufLen,&buf,buf.Length());
	console->Printf(KFormatSizeMax,buf.Size(),buf.MaxLength());
	
	 		//				
			// Insert() & Delete()  * * * * * * * * *
			//
	_LIT(KTitleInsert,"\n--->Insert() & Delete()");
	console->Printf(KTitleInsert);
	console->Printf(KPressAnyKeyToContinue);
	console->Getch();

				// set buf to contain the text "abc" and 
				// look at it
	_LIT(KTextAbc,"abc");
	buf = KTextAbc;
	console->Printf(KFormatBufLenSizeMax,
					&buf,
					buf.Length(),
					buf.Size(),
					buf.MaxLength()
				   );

				// Insert the descriptor at the beginning
				// of buf to give "XYZabc"
				//
	
	buf.Insert(0,KTextXYZ);

				// Look at it
	console->Printf(KFormatBufLenSizeMax,
					&buf,
					buf.Length(),
					buf.Size(),
					buf.MaxLength()
				   );
	
				// Now insert another descriptor at pos 2
				// to give "XijklmnYZabc"
				//
	_LIT(KTextijklmn,"ijklmn");
	buf.Insert(1,KTextijklmn);

				// Show result
	console->Printf(KFormatBufLenSizeMax,
					&buf,
					buf.Length(),
					buf.Size(),
					buf.MaxLength()
				   );
					  				
	
				// Insertion point out of range
				// (> current length of descriptor)
				// 
				// Remove the "//" marks on the next line
				// to see this panic
	//_LIT(KTxtqwerty,"qwerty");
	//buf.Insert(buf.Length()+1,KTxtqwerty);
	
	
				// Resulting length of data
				// is > maximum length.
				// 
				// Remove the "//" marks on the next line
				// to see this panic
	//_LIT(KTxtatoz,"abcdefghijklmnopqrstuvwxyz");
	//buf.Insert(12,KTxtatoz);
	
	
				// Now delete the 3 data 
				// items (characters) at the start
				// of buf to give "klmnYZabc"
	buf.Delete(0,3);

				// Show result
	console->Printf(KFormatBufLenSizeMax,
					&buf,
					buf.Length(),
					buf.Size(),
					buf.MaxLength()
				   );
	
				// Now delete the 4 data items (characters) at
				// position 4 to give "klmnc"
				//
	buf.Delete(4,4);
	
				// Show result
	console->Printf(KFormatBufLenSizeMax,
					&buf,
					buf.Length(),
					buf.Size(),
					buf.MaxLength()
				   );
	
	 			// An excessive length for deletion is 
	 			// truncated to a sensible value. Deletes 
	 			// all the data starting at pos 1 to 
	 			// give "k"
	 			// (Note that the length actually
				//  deleted is 4 NOT 25000).
							
	buf.Delete(1,25000);
	
				// Show result
	console->Printf(KFormatBufLenSizeMax,
				    &buf,
				    buf.Length(),
				    buf.Size(),
				    buf.MaxLength()
				   );
		 
			//				
			// TrimLeft() & TrimRight()  * * * * * * * * *
			//
	_LIT(KTitleTrimLeft,"\n--->TrimLeft() & TrimRight()");
	console->Printf(KTitleTrimLeft);
	console->Printf(KPressAnyKeyToContinue);
	console->Getch();

				// set buf up and show the detail
	buf.Fill(' ',18);
	buf.Replace(3,(&KTextHelloWorld)->Length(),KTextHelloWorld);
	console->Printf(KFormatBufLenSizeMax,  
	                &buf,
					buf.Length(),
					buf.Size(),
					buf.MaxLength()
				   );

				// Remove left hand spaces 	
	buf.TrimLeft();	
	console->Printf(KFormatBufLenSizeMax,  
	                &buf,
					buf.Length(),
					buf.Size(),
					buf.MaxLength()
				   );

				// Remove right hand spaces 	
	buf.TrimRight();	
	console->Printf(KFormatBufLenSizeMax,  
	                &buf,
					buf.Length(),
					buf.Size(),
					buf.MaxLength()
				   );

				// Trim() removes both left and right
				// hand spaces 

		 
 			//				
			// FillZ() & Fill()  * * * * * * * * * *  
			//
	_LIT(KTitleFill,"\n--->FillZ() & Fill()");
	console->Printf(KTitleFill);
	console->Printf(KPressAnyKeyToContinue);
	console->Getch();
			    
				// Set current length of buf
				// Fillz() fills buf up to its current length
				// with 0x00.
	buf.SetLength(8);
	buf.FillZ();

				// Show it
	counter = buf.Length();
	for (index = 0; index < counter; index++)
		console->Printf(KFormatCommon,buf[index]);
		
	console->Printf(KFormatLenSizeMax,
		            buf.Length(),
					buf.Size(),
					buf.MaxLength()
				   );

				// Fills buf up to its current length 
				// with 'A'
	buf.Fill('A');
	
				// show it
	console->Printf(KFormatBufLenSizeMax,  
	                &buf,
					buf.Length(),
					buf.Size(),
					buf.MaxLength()
				   );
	
			//				
			// Num(), NumUC(), AppendNum() & AppendNumUC()
			//
	_LIT(KTitleNum,"\n--->Num(), NumUC(), AppendNum()");
	console->Printf(KTitleNum);
	console->Printf(KPressAnyKeyToContinue);
	console->Getch();

				// convert a signed integer to 
				// decimal characters
	buf.Num(-176);

				// show it
	console->Printf(KFormatBufLenSizeMax,  
	                &buf,
					buf.Length(),
					buf.Size(),
					buf.MaxLength()
				   );

				// convert another signed integer
	buf.Num(12345);

				// show it
	console->Printf(KFormatBufLenSizeMax,  
	                &buf,
				    buf.Length(),
					buf.Size(),
					buf.MaxLength()
				   );

				// convert an unsigned integer
	TUint nosign = 176;
	
				// ... into decimal characters (the default,
				// if not explicitly specified)
	buf.Num(nosign);
	console->Printf(KFormatBufLenSizeMax,  
	                &buf,
				    buf.Length(),
					buf.Size(),
					buf.MaxLength()
				   );
				 	
				// ... into binary characters
	buf.Num(nosign,EBinary);
	console->Printf(KFormatBufLenSizeMax,  
	                &buf,
					buf.Length(),
					buf.Size(),
					buf.MaxLength()
				   );
	
				// ... into octal characters
	buf.Num(nosign,EOctal);
	console->Printf(KFormatBufLenSizeMax,  
	                &buf,
					buf.Length(),
					buf.Size(),
					buf.MaxLength()
				   );
	
				// ... into lower case hex characters
	buf.Num(nosign,EHex);
	console->Printf(KFormatBufLenSizeMax,  
	                &buf,
					buf.Length(),
					buf.Size(),
					buf.MaxLength()
				   );				  
	
				// ... into upper case hex characters
	buf.NumUC(nosign,EHex);
	console->Printf(KFormatBufLenSizeMax,  
	                &buf,
				    buf.Length(),
				    buf.Size(),
				    buf.MaxLength()
				   );
			
				// Append functions the same, except 
				// they append the converted data.
				// Put "xyz" into the descriptor, convert
				// and concatenate the variations.

	_LIT(KTextxyz,"xyz");				
	buf = KTextxyz;

	buf.AppendNum(nosign);
	buf.AppendNum(nosign,EBinary);
	buf.AppendNum(nosign,EOctal);
	buf.AppendNum(nosign,EHex);
	buf.AppendNumUC(nosign,EHex);
	console->Printf(KFormatBufLenSizeMax,  
	                &buf,
					buf.Length(),
					buf.Size(),
					buf.MaxLength()
				   );
					  	
			//				
			// Justify() & AppendJustify()* * * 
			//
	_LIT(KTitleJustify,"\n--->Justify() & AppendJustify()");
	console->Printf(KTitleJustify);
	console->Printf(KPressAnyKeyToContinue);
	
	console->Getch();

				// source descriptor for this example has
				// length 12. 
	TBufC<40> src(KTextHelloWorld);
				
				// target field in buf has width 16 (NB this

⌨️ 快捷键说明

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