Wiley.Developing.Software.for.Symbian.OS.2nd.Edition.Dec.2007 (779887), страница 39
Текст из файла (страница 39)
The data to be copied can be specified as an 8- or 16-bitdescriptor, a NULL-terminated string, or a buffer specified with a pointerand size.In addition to Copy(), you can use CopyC(), CopyF(), CopyCP(),CopyLC(), or CopyUC(). These variations are equivalent to Copy()except that each will perform a specific transformation on the databefore the copy. CopyC() and CopyF() will collate and fold the data,respectively, before the copy. These can be used to normalize strings fortolerant sorts and compares.CopyCP(), CopyLC(), CopyUC() will perform case conversions –capitalization, lower case, and upper case, respectively – before copying(these are performed depending on locale).Here is an example of the copy functions:void CopyExample(){TUint8 binData[6] = {0xB0,0xB1,0xB2,0xB3,0xB4,0xB5};/* Copy standard C array into binary descriptor */TBuf8<sizeof(binData)> binDes;binDes.Copy(binData,sizeof(binData) );_LIT(KFormat1,"binDes[0]=%x binDes[1]=%x\n");console->Printf(KFormat1,binDes[0],binDes[1]);/* Copy binary descriptor to another 8 bit binary descriptor */TBuf8<20> buf8;buf8.Copy(binDes) ;_LIT(KFormat2,"buf8[0]=%x buf8[1]=%x\n");console->Printf(KFormat2,buf8[0],buf8[1]);/* Copy literal into descriptor */_LIT(KString1,"My string");TBuf<20> buf16;buf16.Copy(KString1) ;_LIT(KFormat3,"buf16 = %S\n");console->Printf(KFormat3,&buf16);/* Copy C style 8-bit string into descriptor (first 8-bit then 16-bit)*/TUint8 *C_str=(TUint8 *) "Hello there."; /* NULL-terminated8-bit string */buf8.Copy(C_str) ;buf16.Copy(buf8) ;console->Printf(KFormat3,&buf16); /* Printf just prints 16-bitdescriptor strings *//* Copy, converting to upper case */TBuf<20> newBuf;192STRINGS, BUFFERS, AND DATA COLLECTIONSnewBuf.CopyUC(buf16) ;_LIT(KFormat4,"CopyUC(): newBuf = %S\n");console->Printf(KFormat4,&newBuf);/* Copy, converting to lower case */newBuf.CopyLC(buf16) ;_LIT(KFormat5,"CopyLC(): newBuf = %S\n");console->Printf(KFormat5,&newBuf);/* Copy, capitalize */newBuf.CopyCP(buf16) ;_LIT(KFormat6,"CopyCP(): newBuf = %S\n");console->Printf(KFormat6,&newBuf);}The output is as follows:binDes[0]=b0 binDes[1]=b1buf8[0]=b0 buf8[1]=b1buf16 = My stringbuf16 = Hello there.CopyIC(): newBuf = HELLO THERE.CopyUC(): newBuf = hello there.CopyCP(): newBuf = Hello there.In addition to copying data to a descriptor, you can fill the descriptorwith repeating data using Fill().
Fill() will fill the data buffer withthe specified character (TChar), for the specified number of characters,starting from the beginning. If length is not specified, the data is filled upto the current length. FillZ() works the same way, except that the fillcharacter is always 0. For example:void FillExample(){TBuf<40> buf;buf.Fill(’*’,10);_LIT(KFormat1,"buf = \"%S\"\n");console->Printf(KFormat1,&buf);buf.Fill(’-’);console->Printf(KFormat1,&buf);}The output is as follows:buf = "********** "buf = "----------"Appending data to a descriptorWe have also discussed Append() previously – it behaves like Copy()except that it concatenates the specified data to the descriptor insteadDESCRIPTOR METHODS193of replacing it.
The data specified to Append() can be an 8- or 16-bitdescriptor, a NULL-terminated string, a buffer pointer with length, or aTChar.Here are a few other functions that append data to a descriptor.AppendFill() appends a specified number of repeats of a specifiedcharacter (a TChar) to the descriptor. AppendJustify() will justify aspecified string (left, center, or right) and append it to the descriptor.AppendNum() will convert an integer to a string and append it tothe descriptor. You can also specify a radix, which can be binary, octal,hexadecimal, or decimal.
AppendNumFixedWidth() will result in afixed-width number string being added, with leading zeros if needed.The methods with UC at the end will result in uppercase letters beingappended for hexadecimal numbers.The following code shows the appending functions in action:void AppendExample(){_LIT(KMyString1,"String:");_LIT(KMyString2,"num vals are");_LIT(KMyString3,"Justify");TInt num=0x0b4a;TBuf<40> str(KMyString1);/* Simple Append of a literal */str.Append(KMyString2) ;/* Append num in various forms */str.AppendNum(num) ;str.Append(’ ’) ;str.AppendNum(num,EHex) ;str.Append(’ ’) ;str.AppendNumUC(num,EHex) ;str.Append(’ ’) ;str.AppendNumFixedWidthUC(num,EHex,5) ;_LIT(KFormat1,"str = %S\n");console->Printf(KFormat1,&str);TBuf<40> just;/* Justify a string */just.AppendJustify(KMyString3,12,ERight,’ ’);/* can replace "" with any fill character */_LIT(KFormat2,"%S\n");console->Printf(KFormat2,&just);/* Add repeated character */just.AppendFill(’!’,5) ;console->Printf(KFormat2,&just);}The output from the code is as follows:str = String: num vals are 2890 b4a B4A 00B4AJustifyJustify!!!!!194STRINGS, BUFFERS, AND DATA COLLECTIONSFormatting descriptor dataIt’s handy to be able to format a string in the same way as in C’ssprintf() and printf() functions.
The descriptor method Format() does this.The format string supplied to Format() is very similar to the formatstring in C, supporting %d, %s, %f, etc. There are also some SymbianOS-specific formats, however. For example, the format indicator %S takesa descriptor and outputs the descriptor’s string contents.Format(), like Copy(), replaces any existing descriptor data. Alternatively, you can use AppendFormat() to append the formatted stringto the descriptor.The following example shows the format functions in action:void FormatExample(){_LIT(KString1,"My string");TInt value=10,value1=20;_LIT(KMyDesFormat,"Descriptor = %S, value = %d");TBuf<100> buf;buf.Format(KMyDesFormat,&KString1,value) ;_LIT(KMyDesFormat1,"--also value1 = %d");buf.AppendFormat(KMyDesFormat1,value1) ;_LIT(KFormat1,"%S\n");console->Printf(KFormat1,&buf);}The output is as follows:Descriptor = My string, value = 10--also value1 = 20Changing the case of a descriptor stringUse Capitalize() to capitalize a descriptor string (performed asdefined by the phone’s locale).
Use LowerCase() and UpperCase()to convert all characters in the descriptor to lower and upper case,respectively.Here is an example of the case-switching functions:void CaseExample(){_LIT(KString1,"hillary");TBuf<7> name(KString1);name.UpperCase() ;_LIT(KFormat1,"name=%S\n");console->Printf(KFormat1,&name);name.LowerCase() ;console->Printf(KFormat1,&name);name.Capitalize() ;DESCRIPTOR METHODS195console->Printf(KFormat1,&name);}The output of the previous example is as follows:name=HILLARYname=hillaryname=HillaryDeleting data from a descriptorUse Delete() to remove a selected portion of a descriptor buffer.For example:des1.Delete(2,3);deletes the data in des1 starting at position two, for a length ofthree.
So if des1 contained ‘abcdedf’ before this line, it would contain ‘abdf’ after, and the length of des1 would be changed from sevento four.Descriptors also have methods for removing unwanted spaces instrings. TrimLeft() and TrimRight() will delete leading and trailingspaces, respectively. TrimAll() will delete leading and trailing spaces,as well as trimming any consecutive spaces in the data to one space.
Thedeletion functions are illustrated as follows:void DeletionExample(){_LIT(KString1,"Heeeello");_LIT(KString2,"Thisisa test ");TBuf<20> buf1(KString1);TBuf<40> buf2(KString2);_LIT(KFormat1,"buf1=\"%S\"buf1 length = %d\n");console->Printf(KFormat1,&buf1,buf1.Length());/* delete characters starting at position 1, length* 3 –will reduce descriptor size appropriately*/buf1.Delete(1,3) ;console->Printf(KFormat1,&buf1,buf1.Length());_LIT(KFormat2,"buf2=\"%S\"buf2 length = %d\n");console->Printf(KFormat2,&buf2,buf2.Length());/* TrimLeft deletes leading spaces */buf2.TrimLeft() ;_LIT(KFormat3,"TrimLeft(): buf2=\"%S\"buf2 length = %d\n");console->Printf(KFormat3,&buf2,buf2.Length());/* reset string to KString2 */buf2.Copy(KString2);/* Trim right deletes trailing spaces */buf2.TrimRight() ;_LIT(KFormat4,"TrimRight(): buf2=\"%S\"buf2 length = %d\n");196STRINGS, BUFFERS, AND DATA COLLECTIONSconsole->Printf(KFormat4,&buf2,buf2.Length());/* reset string to KString2 */buf2.Copy(KString2);/* Trim deletes leading and trailing spaces */buf2.Trim() ;_LIT(KFormat5,"Trim(): buf2=\"%S\"buf2 length = %d\n");console->Printf(KFormat5,&buf2,buf2.Length());/* reset string to KString2 */buf2.Copy(KString2);/* Trimall deletes leading and trailing spaces, and extra* spaces in middle*/buf2.TrimAll() ;_LIT(KFormat6,"TrimAll(): buf2=\"%S\"buf2 length = %d\n");console->Printf(KFormat6,&buf2,buf2.Length());}The output of the previous code is as follows:buf1="Heeeello" buf1 length = 8buf1="Hello" buf1 length = 5buf2="Thisisa test" buf2 length = 24TrimLeft(): buf2= "Thisisa test" buf2 length = 21TrimRight(): buf2="Thisisa test" buf2 length = 22Trim(): buf2="Thisisa test" buf2 length = 19TrimAll(): buf2="This is a test" buf2 length = 14Converting to NULL-terminated stringsSometimes it’s useful to convert a descriptor string to a C-style NULLterminated string – especially in cases where you are working with portedC code in the Symbian OS environment.
Descriptors provide a fewmethods to help with this.ZeroTerminate() adds a NULL to the end of the descriptor data.The descriptor length is not updated. This is used for translating the stringto a C-style NULL-terminated string, as you will see in the example.PtrZ() is the same as ZeroTerminate() except that it returns apointer to the NULL-terminated descriptor data.