File operation and Unicode conversion on Symbian OS

xiaoxiao2021-04-04  277

This article is finalized.

Symbian

Some of the experience of file operations on the system is implemented, and the functions of saving, reading TBUF, and TINT variables are implemented, involving transformation between Unicode and ANSI.

Recently studied the file operation on the Symbian operating system, do it to make a review of exchanges. The implementation is as follows: Tbuf16 M_ISEVRNAME and TINT M_IPORT inside a class are saved and read through a file server.txt. Because the TBUF16 in Symbian must read the Unicode character, there are two ways to implement, and the file is saved as a Unicode file and then reads, and the other is to read the ANSI file to the buffer, and the Buffer is converted to Unicode characters. . 1. Write file saving variable TBUF (TBUFC) is a way to represent characters in the Symbian system, the buffer descriptor, the length of the data itself, and the length of the data, and note that this length is the length of the Unicode character, that is, in units of SHORT. The address pointer of the variable character can be obtained by the PTR () member function of the TBUF, and then operate one by one. File * fp = fopen ("c: //server.txt", "w"); if (fp) {char chbufserv [20]; char chbufport [8]; m_isevrname.trim (); int i = 0; for (i = 0; i

First, in the first way, directly read the Unicode file assignment: 1). File * fp = fopen ("c: //server.txt", "rb"); //server.txt is the Unicode format file IF ( FP) {FSeek (FP, 0, 2); // 2 Seek_set Represents Beginning Of File Int Nlen = Ftell (FP); IF (Nlen <1) {FClose (fp); Return;} fseek (fp, 2, 0); // Note that the 2 bytes will be skipped here, that is, the symbol "0xFF" of the Unicode file, "0xfe" Nlen- = 2; wchar_t * wstr = new Wchar_T [Nlen 1]; FREAD (WSTR, NLEN, 1, fp); WSTR [NLEN] = 0; // Add end value fclose (fp); short * psip = (short *) WSTR; short * token = getLine (psip); int i = 0; While (token! = NULL) {Switch (i) {cas 0:

{M_isevrname.append (tuint16 *) token, wcslen (wchar_t *) token);} Break; case 1: {int Nlen = WCSLEN ((wchar_t *) token; char PTMPBUF [5]; int i = 0 ; For (i = 0; i

The Windows system is relatively simple, calling its API function WideChartomultibyte, MultibyTowideChar can transform each other between the two characters, and it is not that simple in the Symbian system: file * fp = fopen ("c: //server.txt", " RB "); if (fp) {fseek (fp, 0, 2); INT NLEN = FTELL (FP); if (Nlen <1) {fclose (fp); return;} unsigned char * PBUF1 = New unsigned char [ NLEN 1]; FSEEK (FP, 0, 0); FREAD (PBUF1, NLEN, 1, FP); PBUF1 [NLEN] = 0; // end value Fclose (FP); fp = null; // convert To unicoDetptrc8 aForeign; aForeign.Set (pBuf1, nLen); CCnvCharacterSetConverter * pcc = CCnvCharacterSetConverter :: NewLC (); if (! pcc-> PrepareToConvertToOrFromL (KCharacterSetIdentifierGbk, iEikonEnv-> FsSession ()) = CCnvCharacterSetConverter :: EAvailable) {CleanupStack :: PopAndDestroy ();} Hbufc * ionfotext = hbufc :: newl (Afeeign.Length ()); tptr16 ptr = Iinfotext-> des (); Tint Astate = ccnvcharacTerSetConverter :: kStatedEfault; PCC-> ConvertTouni Code (PTR, AFOREIGN, ASTATE); Cleanupstack :: PopandDestroy (); Nlen * = 2; Short * Pbuf = (short *) Ptr.ptr (); short * psip = PBUF; Short * token = getLine (PSIP); INT i = 0; while (token! = null) {... // The following implementation is the first method to note that this conversion method cannot run and debug on the Symbian emulator, and can only run on Real Device If you can't debug our programmers, if you choose this method, then pray that God will make it possible to run correctly ^ _ ^.

TBUF variables are required to pay attention to the TBUF variables in SMBIAN.

TBUF m_isevrname;

Void SetDefaultchnl (Const TDesc & AServname, Tint Nroom) {

// m_iservname = aservname; // This method is the wrong m_isevrname.delete (0, m_isevrname.length ()); // must first clear the original content m_isevrname.append (ASERVNAME); m_iroom = nroom;} Using TDESC When the TBUF variable is assigned, the TBUF's delete () function must first be called, otherwise the TBUF variable does not assign a value correctly, although the length of the LENGTH and IBUF pointers that track the TBUF variables are displayed correctly when debugging. .

Use Symbain OS String Descriptor Programming

1 Introduction

When I just started learning Symbian, the first thing I encountered was the processing and use of Symbian OS strings. To learn the use of the Symbian string, you must have a must be a must. But once you master the essentials, it will make it easy.

So, I will explain how I learned the basic Symbian OS.

String processing and remember it.

Note that it is understood that the premise of this article is to understand the working mechanism of the Symbian operating system.

2. Background

The first thing you have to do is remember the string descriptor level

Structure diagram. This is very important, because all the five descriptors you want to use are derived from certain classes, you must understand which classes are derived from them to determine which special descriptors should be used and their usage venues. I am not prepared in this article, what is the meaning of buffering descriptors and the heap descriptor, and what improvisible descriptors and unmodent descriptors. but,

I believe that you have to have enough understandings on the terminology above. The Symbian descriptor hierarchy looks quite wonderful. You can refer to the following figure, which is derived from the NewLC.

3. TPTRC Usage

Its literal meaning is "a point pointing

Pointer to be operated ". About TPTRC , first to remember, it does not contain some of its operation functions, but only constructors and setup methods. In addition, since it is derived from TDESC, it contains All functions of TDESC.

The way the pointer points to the data is below:

· Create an empty TPTRC , then point it to some data with the set (...) function.

· By using any one overloaded

Constructor is transmitted during the construction process.

Let us look at the above description by the following examples:

Example 1: - TPTRC from TBUF and TBUFC:

LIT (KText, "Test Code"); TBUFC <10> BUF (KTEXT); OR (/) TBUF <10> BUF (KText); // Create a TPTRTPTRC PTR (BUF) using the constructor; // creation using member functions TPTRTPTRC PTR1; PTR1.SET (BUF);

Example 2: - Tot TPTRC from TTEXT *:

The following example uses TTEXT16:

Ttext * text = _s ("Hello World / N"); TPTRC PTR (Text); // or TPTRC PTR1; Ptr1.Set; // To store part of Ttext, we can use the following statement // This descriptor pointer will only store HelloTPTRC PTR4 (Text, 5);

Example 3: TPTRC from another TPTRC:

You can easily assign a TPTRC to another TPTRC.

Ttext * text = _s ("Hello WORLD / N"); TPTRC PTR (Text); // From another TPTRC to acquire TPTRCTPTRC P1 (PTR); // or TPTRC P2; P2.SET (PTR); Example 4: - TTEXT *: from TPTRC *:

We can use PTR () members to use Ttext * from TPTRC.

// Set TPTRC_LIT (KText, "Test Code"); TBUFC <10> BUF (KTEXT); TPTRC PTR1 (BUF); // Tott * Ttext * Text1 = (Ttext *) Ptr1.ptr ();

4. TBUFC Usage

About TPTRC use example, it helps to understand TBUFC , but there are several examples below to create TBUFC .

Example 5: - Instantiate TBUFC :

// Installation _LIT (KText, "TestText"); TBUFC <10> BUF (KText); // or TBUFC <10> buf2; buf2 = ktext; // Create a new one with existing TBUFC TBUFCTBUFC <10> BUF3 (BUF2);

TBUFC is generally used for text data. For binary data, TBUFC8 should be displayed. Although TBUFC means that data cannot be modified ('c' represents constant: constant), there are still two ways to change data:

• Data can be replaced using an assignment operator.

• Build a TPTR modified pointer descriptor by using the DES () function to buffer data.

Let us see the first step in the context of TBUFC .

Example 6: - Change the context of TBUFC :

// Some text (ktext, "test text"); _ lit (ktext1, "test1Text"); // Generate TPTRCTBUFC <10> BUF1 (KText); TBUFC <10> BUF2 (KText1); // Change the context buf2 = buf1 of BUF2; // Create an empty TBUFC and assign it to buf1tbufc <10> BUF3; buf3 = buf1;

Another way to change the context content of TBUFC is to use the DES () member function. This member function returns a TPTR modified pointer descriptor using TPTR members. The maximum length of TPTR is the value of TBUFC template parameters. All operating functions are from the base class TDESC.

Example 7: - Use DES () to change the context of TBUFC :

_LIT (KText, "Test Text"); _ LIT (KxtRatext, "New:"); TBUFC <10> BUF1 (ktext); tptr pointer = buf1.des (); // Delete last 4 characters Pointer.delete (Pointer .Length () - 4, 4); // Now make a corresponding length change Tint Len = Pointer.Length (); // Add a new string Pointer.Append (kxtRatext); len = Pointer.Length (); // To completely change the above buffer, we can use the following statement: _LIT (NewText, "New1"); _ LIT (NewText1, "New2"); TBUFC <10> BUF2 (NewText); // Change the context Pointer.copy BUF2); // or directly literally copy Pointer.copy; // All the changes made in the above doctors actually change the context of BUF1 5. Using a heap descriptor HBUFC

When we don't know the size of the data in the descriptor, HBUFC is an optional descriptor. The 'c' here is constant, which means that the data is not changeable, but it can also be changed in both cases, like a change in the case of TBUFC . The first case is to use the assignment operator, the second case is to use the modified pointer descriptor, such as using the TPTR when using the member function. When using HBUFC, you should remember two situations:

· If you need to pass HBUFC to a function with TDESC & as a parameter, you only need to simply cancel the reference to the HBUFC pointer.

· You can change the size of the stack descriptor buffer by using the Realloc function, just like TBUFC .

Example 8: --HBUFC usage:

// Create a heap descriptor, there are two methods // first ways to use new (), newl () or newlc () // let us see an example. Here, an HBUFC will be built: the data space used is 15, but the current size is 0HBUFC * BUF = HBUFC :: NewL (15); // Second method using // existing descriptor's alloc (), allocl (), or alloclc () method. This new heap descriptor is automatically initialized by the descriptor. _LIT (KText, "Test Text"); TBUFC <10> CBUF = ktext; hbufc * buf1 = cbuf.allocl (); // We check the size and length . Here size is 18, and the length is 9 Tint bufsize = buf-> size (); Tint buflength = buf-> length (); // change HBUFC point-to-_LIT (KText1, "text1"); // Use assignment operation Changes to the buffer of KTEXT1 * buf1 = ktext1; // By changing the data TPTR POINTER = BUF1-> des (); Pointer.delete (Pointer.length () - 2, 2) // All operations that can be performed on TBUFC can be used // below is one such operation _LIT (KNEW, "New:"); Pointer.Append (knew);

6. Usage of TPTR

We use it to replace TBUFC and HBUFC because I know it most. Therefore, let us remember how to create TPTR.

· Use another TPTR.

· Create it from TBUFC or by member function DES ().

· From a plurality of external pointers pointing to memory and specify maximum length. · From a plurality of external pointers to memory and specify data and its maximum length.

Example 9: The usage of -tptr:

// Let's take a look at the two ways to get TPTR_LIT (KText, "Test Data"); TBUFC <10> NBUF (KText); tptr Pointer = nbuf.des (); // First method TPTR Pointer2 (Pointer); // Second method uses a memory section for storing data and maximum length Ttext * text = _s ("test second"); TPTR Pointer3 (Text, 11, 12); // Now let's see Take it, how to replace data with TPTR, which can be implemented by // assigning operators or copy functions; _ lit (k2, "text2"); Pointer2 = K1; // data is Text1Pointer .Copy (k2); // data is Text2; // We can also change the length of the data or set it to 0Pointer2.setlength (2); // Left only TE two characters Pointer2.Zero (); // Set the length to 0 // You can use the DELETE function to change the data, as shown in the previous example.

7. Usage of TBUF

In this data structure, the data is not fixed. The calculation, instantiation and assignment are consistent with TBUFC ; for its modification, match TPTR, like copy, delete, assignment, etc. I don't want this part, I don't have to give an example.

转载请注明原文地址:https://www.9cbs.com/read-131952.html

New Post(0)