使用 Symbian OS 字符串描述符
本文展示了 Symbian OS 描述符的入门用法。
引言
刚接触 Symbian 时,首先遇到的就是 Symbian OS 的字符串处理和操作。那是我噩梦般的日子 :)。我发现描述符的东西有点难记,但一旦掌握了诀窍,就变得容易了。我没骗你……
所以,我在这里试着解释我是如何学会基本的 Symbian OS 字符串处理并努力记住这些东西的。我不会争论使用描述符是好是坏,因为无论好坏,你都必须使用它 :-(。我也不打算在这里讨论如何使用纯 C++ 来做类似的事情,或者如何使用纯 C++ 的字符串操作技术来完成某项工作。本文只讨论描述符。
本文的前提是具备 Symbian OS 的工作知识。
背景
你需要记住的第一件事是描述符的层级结构。这非常重要,因为你要使用的所有五个描述符都派生自某些类,你必须知道它们派生自哪个类,才能在何种场景下成功决定使用哪个特定的描述符。我不会解释“缓冲区描述符”和“堆描述符”是什么意思,也不会解释“可修改”和“不可修改”描述符的含义。我相信你已经对上述术语有了足够的信息。我也不会使用 8 位和 16 位变体,因为在功能上它们是相似的,而且对于理解描述符来说,它们也不重要。Symbian 描述符的层级结构看起来很棒。(抱歉未能提供描述符层级结构的精美图片)。你可以在 newlc 上查看图片。
TPtrC<n> 的用法
如果想字面理解,那就是“指向一个无法操作的数据的指针”。关于 TPtrC<n>
,需要记住的基本一点是,它本身不包含任何操作功能,只包含构造函数和设置方法,并且由于它直接派生自 TDesC
,所以它包含了 TDesC
的所有功能。
指针会通过以下两种方式之一指向数据:
- 创建一个空的
TPtrC<N>
,然后使用Set(...)
函数将其指向某些数据。 - 通过使用重载的构造函数之一,在构造时传递数据。
下面通过几个例子来看看上面的说法:
- 示例 1:从
TBuf
和TBufC
获取TPtrC
LIT(KText , "Test Code"); TBufC<10> Buf ( KText ); OR(/) TBuf<10> Buf ( KText ); // Creation of TPtr using Constructor TPtrC Ptr (Buf); // Creation of TPtr using Member Function TPtrC Ptr1; Ptr1.Set(Buf);
- 示例 2:从
TText*
获取TPtrC
下面的例子使用了
TText16
TText * text = _S("Hello World\n"); TPtrC ptr(text); // OR TPtrC Ptr1; Ptr1.Set(text); // To store only a part of TText we can use the following // This descriptor pointer will store only Hello TPtrC ptr4 ( text , 5 );
- 示例 3:从另一个
TPtrC
获取TPtrC
你可以轻松地将一个
TPtrC
分配给另一个。TText * text = _S("Hello World\n"); TPtrC ptr(text); // Get TPtrC from another TPtrC TPtrC p1(ptr); // OR TPtrC p2; p2.Set(ptr);
- 示例 4:从
TPtrC
获取TText *
我们可以通过使用
Ptr()
成员从TPtrC
中获取TText *
。// Set the TPtrC _LIT(KText , "Test Code"); TBufC<10> Buf ( KText ); TPtrC Ptr1 (Buf); // Get the TText * TText * Text1 = (TText *)Ptr1.Ptr();
TBufC<n> 的用法
用于描述 TPtrC
工作原理的示例已经让你对 TBufC<n>
的用法有了一些了解,但这里仍有一些关于如何创建 TBufC<n>
的例子。
- 示例 5:实例化
TBufC<N>
// Instantiating the TBufC from Literals _LIT(Ktext, "TestText"); TBufC<10> Buf (Ktext); // or TBufC<10> Buf2; Buf2 = Ktext; // Creating a new TBufC with existing TBufC TBufC<10> Buf3(Buf2);
TBufC<n>
通常用于文本数据。对于二进制数据,则使用显式的TBufC8<n>
。虽然TBufC<n>
表示数据不能被修改('C' 代表 Constant),但仍有两种方式可以修改这些数据:- 可以通过赋值运算符来替换数据。
- 通过使用
Des()
函数为缓冲区数据构造一个可修改的TPtr
指针描述符。
让我们通过第一种方法来改变
TBufC<n>
的内容。 - 示例 6:改变
TBufC<N>
的内容// Describe some literals for Testing example _LIT(Ktext , "Test Text"); _LIT(Ktext1 , "Test1Text"); // Generate TPtrC TBufC<10> Buf1 ( Ktext ); TBufC<10> Buf2 ( Ktext1 ); // Change the contents of Buf2 Buf2 = Buf1; // Create an Empty TBufC and assign it to Buf1 TBufC<10> Buf3; Buf3 = Buf1;
改变
TBufC<n>
内容的另一种方法是使用Des()
成员。此成员函数返回一个可修改的TPtr
指针描述符,并使用TPtr
的成员。TPtr
的最大长度是TBufC<n>
模板参数的值。所有操作函数都来自TDesC
基类。 - 示例 7:使用
Des()
改变TBufC<N>
的内容_LIT(Ktext , "Test Text"); _LIT(KXtraText , "New:"); TBufC<10> Buf1 ( Ktext ); TPtr Pointer = Buf1.Des(); // delete the last 4 characters Pointer.Delete(Pointer.Length()-4, 4 ); // the length should be changed now TInt Len = Pointer.Length(); // Append the new one Pointer.Append(KXtraText); Len = Pointer.Length(); // To completly changed the buffer we can use following _LIT(NewText , "New1"); _LIT(NewText1 , "New2"); TBufC<10> Buf2(NewText); // change the context Pointer.Copy(Buf2); // or Directly from literal Pointer.Copy(NewText1); // All the above changed actually changing the contents of Buf1
使用堆描述符 HBufC
当我们不知道描述符中数据的大小时,HBufC
是首选的描述符。这里的 'C' 代表 Constant(常数),意味着数据是常数,但与 TBufC<n>
类似,它也可以通过两种方式改变。第一种是使用赋值运算符,另一种是使用可修改的指针描述符,即使用 Des()
成员函数的 TPtr
。在使用 HBufC
时有两点需要记住:
- 如果你需要将
HBufC
传递给一个接受TDesC &
作为参数的函数,只需解引用HBufC
指针即可。 - 与
TBufC<N>
不同,堆描述符缓冲区的大小可以使用ReAlloc
函数进行更改。
- 示例 8:
HBufC
的用法// Creation of a Heap Descriptor . There are 2 ways to do it // 1st way use either New(), NewL(), or NewLC() // Let see the example for this. This wills construct the HBufC with // a Data space for 15, but the current size is Zero. HBufC * Buf = HBufC::NewL(15); // 2nd way to used the Alloc(), AllocL(), or AllocLC() of // the existing descriptors. The new Heap descriptor is automatically // initialized with the content of descriptor. _LIT (KText , "Test Text"); TBufC<10> CBuf = KText; HBufC * Buf1 = CBuf.AllocL(); // Lets Check the size and Length . The size will be 18 // and length will be 9 TInt BufSize = Buf->Size(); TInt BufLength = Buf->Length(); // Changing what HBufC is pointing to _LIT ( KText1 , "Text1"); // Change the buffer to Point to KText1 using assignment operator *Buf1 = KText1; // And now changing the Data through Modifiable pointer descriptor TPtr Pointer = Buf1->Des(); Pointer.Delete( Pointer.Length() - 2, 2 ); // All operations that were done in case of TBufC<n> // is applicable here also. // here is one such operation _LIT ( KNew, "New:"); Pointer.Append( KNew );
TPtr 的用法
啊哈,我们已经在 TBufC<N>
和 HBufC
中用到了它,所以我们对它已经很熟悉了。那么,让我们回忆一下如何创建 TPtr
。
- 另一个
TPtr
。 - 从
TBufC<N>
、HBufC
使用Des()
成员函数。 - 从显式的内存指针并指定最大长度。
- 从显式的内存指针并指定数据以及最大长度。
- 示例 9:
TPtr
的用法// First lets see 2nd way of Getting TPtr _LIT(KText, "Test Data"); TBufC<10> NBuf ( KText ); TPtr Pointer = NBuf.Des(); // 1st way TPtr Pointer2 ( Pointer ); //3rd way using a memory area , Data and max length TText * Text = _S("Test Second"); TPtr Pointer3 ( Text ,11, 12); // Now we will see how to replace the data with TPtr ,it can // be done completly by using assignment Operator or copy function _LIT(K1, "Text1"); _LIT(K2, "Text2"); Pointer2 = K1; // Data will be Text1 Pointer.Copy(K2); // Data Will be Text2; // we can also change the length of the data or set it to zero Pointer2.SetLength(2); // Only Te will be there Pointer2.Zero(); // Make length of data as Zero // Data can be altered using the delete function as it used to alter // in the earlier examples.
TBuf<n> 的用法
在这种情况下,数据不是恒定的。操作、实例化和赋值将与 TBufC<n>
类似,并且可以对其应用修改函数,就像在 TPtr
的情况下一样,例如 Copy、Delete、赋值等。我希望这个部分不需要举例说明。
历史
- 最新更新。