File Operations and Unicode conversion on Symbian OS

Source: Internet
Author: User
This article summarizes Symbian Some file operations on the system implement the function of saving and reading tbuf and tint variables through the file, which involves conversion of Unicode and ANSI characters.

Recently, I have studied file operations on the Symbian operating system and made some preparations for review and exchange.
The following functions are implemented: The member variables tbuf16 <kmaxservernamelen> m_isevrname and tint m_iportin the class are saved and read by a file server.txt. Because tbuf16 in Symbian must read Unicode characters, there are two methods to achieve this. One is to save the file as a Unicode file and then read it. The other is to read ANSI File to buffer, from Program Converts a buffer to a Unicode character.
1. Write File Save variable
Tbuf (tbufc) is a type of character representation in Symbian. It is a buffer descriptor that contains the data itself and the length of the Data. Note that this length is the length of Unicode characters, it is in the short unit. You can use the PTR () member function of tbuf to obtain the address pointer of the variable character, and then perform operations 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 <m_isevrname.length (); I ++)
Chbufserv [I] = * (m_isevrname.ptr () + I );
Chbufserv [I] = '\ R ';
Chbufserv [I + 1] = '\ n ';
Sprintf (chbufport, "% d \ r \ n", m_iport );
Fwrite (chbufserv, strlen (chbufserv), 1, FP );
Fwrite (chbufport, strlen (chbufport), 1, FP );
Fclose (FP );
}
2. Read characters from a file and assign values to variables
The file storage is very simple. It is mainly used to shift my head to pai_^ when the file is read and assigned a value.
First, how to distinguish Unicode files from ANSI files: For all Unicode-encoded text files, the first two bytes of the file must be "0xff" and "0xfe". If you read these two bytes first, you can determine whether it is a Unicode-encoded file. .
Char cfirst [2];
Fread (ifilehandle, cfirst, 2 );
If (psfirst [0] = '\ xff' & psfirst [1] =' \ xfe ') can be determined using this statement.
First, use the first method to directly read Unicode files and assign values:
1 ).
File * fp = fopen ("C: \ server.txt", "rb"); // server.txt is a Unicode file.
If (FP ){
Fseek (FP,); // 2 that is, seek_set indicates beginning of File
Int nlen = ftell (FP );
If (nlen <1)
{
Fclose (FP );
Return;
}
Fseek (FP,); // note that 2 bytes are skipped here, indicating the Unicode file symbol "0xff", "0xfe"
Nlen-= 2;
Wchar_t * wstr = new wchar_t [nlen + 1];
Fread (wstr, nlen, 1, FP );
Wstr [nlen] = 0; // Add the Terminator
Fclose (FP );
Short * PSIP = (short *) wstr;
Short * token = Getline (PSIP );
Int I = 0;
While (Token! = NULL)
{
Switch (I)
{
Case 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 <wcslen (wchar_t *) token); I ++)
Ptmpbuf [I] = * (token + I );
Ptmpbuf [I] = 0; // Add the Terminator
M_iport = atoi (ptmpbuf );
}
Break;
}
/* Get next token :*/
Token = Getline (PSIP );
I ++;
}
}
Note the case of case 0. If the token is of the short * type, then after m_isevrname calls append, its length () returns the number of Unicode characters, and if it is of the wchar_t * type wstr, the length () function of isevrname returns the number of ANSI characters. It must be set using setlength (), that is, m_isevrname.append (tuint16 *) wstr, wcslen (wstr ));
M_isevrname.setlength (m_isevrname.length ()/2 );
(I wasted a lot of time on this issue ).
The Getline function for reading a line of characters above is implemented as follows:
Short * Getline (lpshort & pbuf)
{
Short * pret = pbuf;
While (* pbuf & * pbuf! = '\ R' & * pbuf! = '\ N') pbuf ++;
* Pbuf = 0;
Pbuf ++;
While (* pbuf & (* pbuf = '\ R' | * pbuf =' \ n') pbuf ++;
If (pret = pbuf | * pret = 0)
Return NULL;
Return pret;
}
2) The above is the implementation of Directly Reading Unicode files. The second method is described below, that is, reading ANSI files to the cache and then converting to unicode characters. Windows systems are relatively simple. calling its API functions widechartomultibyte and multibytetowidechar can convert each other between the two types of characters, but it is not that easy in Symbian systems:
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; // Terminator
Fclose (FP );
Fp = NULL;
// Convert to Unicode
Tptrc8 aforeign;
Aforeign. Set (pbuf1, nlen );
Ccnvcharactersetconverter * PCC = ccnvcharactersetconverter: newlc ();
If (PCC-> preparetoconverttoorfroml (kcharactersetidentifiergbk, ieikonenv-> fssession ())! = Ccnvcharactersetconverter: eavailable)
{
Cleanupstack: popanddestroy ();
Delete pbuf1;
Return;
}
Hbufc * iinfotext = hbufc: newl (aforeign. Length ());
Tptr16 PTR = iinfotext-> des ();
Tint astate = ccnvcharactersetconverter: kstatedefault;
PCC-> converttounicode (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 same as the first method


Note that this conversion method cannot be run or debugged on the Symbian simulator, but can only be run on the real device. debugging is not difficult for our programmers. If you choose this method, then pray to God to make it run correctly ^_^.

Note the need to assign values to tbuf variables in Symbian

Tbuf <maxlen> m_isevrname;

Void setdefaultchnl (const tdesc & aservname, tint nroom)
{

// M_iservname = aservname; // This method is incorrect.
M_isevrname.delete (0, m_isevrname.length (); // The original content must be cleared first.
M_isevrname.append (aservname );
M_iroom = nroom;
}

When tdesc is used to assign values to the tbuf variable, you must first call the delete () function of tbuf to clear the content. Otherwise, if the first method is used, the tbuf variable will not be assigned a correct value, although the length and ibuf pointer content of the tbuf variable are correctly tracked during debugging.

Program with symbain OS string Descriptor 1. Introduction

When I first started learning about Symbian, the first thing I encountered was the processing and usage of the Symbian OS string. To learn how to use the Symbian string, you have to worry about it. But once you have mastered the essentials, it will become much easier.

Therefore, I will explain how I learned how to process and remember the basic Symbian OS string.

Note: The premise of understanding this article is to have a certain understanding of the operating mechanism of the Symbian operating system.

2. Background

The first thing you need to do is remember the layered structure of the string descriptor. This is very important, because in the future, all the five descriptors you want to use are derived from some classes. You must understand which classes they are derived from, to determine which special descriptors should be used and where they are used. In this article, I am not going to explain what is the meaning of the buffer Descriptor and heap descriptor, and what is the meaning of the modifiable and unmodifiable descriptors. However, I believe that you must have a sufficient understanding of the terms above. The hierarchy of Symbian descriptors looks pretty good. For more information, see newlc.

3. tptrc <n> usage

The literal meaning is "a pointer to data that cannot be operated ". For tptrc <n>, remember that it does not contain some operation functions for itself, but only the constructor and setting methods. In addition, since it is derived from tdesc, it contains all functions of tdesc.

The Pointer Points to data in the following two ways:

· Create an empty tptrc <n> first, and then use the Set (...) function to point it to some data.

· Use any overloaded constructor to transmit data during the constructor.

Let's take a look at the above descriptions through the following examples:

Instance 1:-obtain tptrc from tbuf and tbufc:

Encrypt (ktext, "test code ");
Tbufc <10> Buf (ktext); or (/) tbuf <10> Buf (ktext );
// Create a tptr using the constructor
Tptrc PTR (BUF );
// Use the member function to create a tptr
Tptrc ptr1;
Ptr1.set (BUF );

Instance 2:-obtain tptrc from ttext:

The following example uses 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 statement
// This descriptor pointer will only Store Hello
Tptrc ptr4 (text, 5 );

Instance 3:-obtain tptrc from another tptrc:

You can easily assign a tptrc value to another tptrc.

Ttext * text = _ S ("Hello world \ n ");
Tptrc PTR (text );
// Obtain tptrc from another tptrc
Tptrc p1 (PTR );
// Or
Tptrc P2;
P2.set (PTR );

Example 4:-obtain ttext from tptrc *:

We can use the PTR () member to obtain ttext * From tptrc *.

// Set tptrc
_ Partition (ktext, "test code ");
Tbufc <10> Buf (ktext );
Tptrc ptr1 (BUF );
// Obtain ttext *
Ttext * text1 = (ttext *) ptr1.ptr ();

4. tbufc <n> usage

The use example of tptrc above helps to understand the usage of tbufc <n>. However, the following examples show how to create tbufc <n>.

Instance 5:-instantiate tbufc <n>:

// Instantiate with text
_ Partition (ktext, "testtext ");
Tbufc <10> Buf (ktext );
// Or
Tbufc <10> buf2;
Buf2 = ktext;
// Use the existing tbufc to create a new tbufc
Tbufc <10> buf3 (buf2 );

Tbufc <n> is generally used for text data. For binary data, tbufc8 <n> should be used explicitly. Although tbufc <n> means that the data cannot be modified ('C' indicates constant: unchanged), there are two ways to change the data:

· Data can be replaced by the value assignment operator.

· Use the des () function to construct a tptr modifiable pointer descriptor for the buffer data.

Let's take a look at the first step to change the context of tbufc <n>.

Instance 6:-change the context of tbufc <n>:

// Some text for testing
_ Partition (ktext, "test text ");
_ Partition (ktext1, "test1text ");
// Generate tptrc
Tbufc <10> buf1 (ktext );
Tbufc <10> buf2 (ktext1 );
// Change the buf2 Context
Buf2 = buf1;
// Create an empty tbufc and assign it to buf1
Tbufc <10> buf3;
Buf3 = buf1;

Another way to change the context content of tbufc <n> is to use the des () member function. This member function uses the tptr member to return a variable tptr pointer descriptor. The maximum tptr length is the value of the tbufc <n> template parameter. All operation functions are derived from the base class tdesc.

Instance 7:-use des () to change the tbufc <n> context:

_ Partition (ktext, "test text ");
_ Partition (kxtratext, "New :");
Tbufc <10> buf1 (ktext );
Tptr pointer = buf1.des ();
// Delete the last 4 characters
Pointer. Delete (pointer. Length ()-4, 4 );
// Change the length
Tint Len = pointer. Length ();
// Add a new string
Pointer. append (kxtratext );
Len = pointer. Length ();
// To completely change the buffer above, we can use the following statement:
_ Partition (newtext, "new1 ");
_ Partition (newtext1, "new2 ");
Tbufc <10> buf2 (newtext );
// Change the context
Pointer. Copy (buf2 );
// Or directly perform literal copy
Pointer. Copy (newtext1 );
// All the changes made above actually change the buf1 Context

5. Use the heap descriptor hbufc

Hbufc is an optional descriptor when we do not know the data size in the descriptor. Here, 'C' stands for constant, which means that the data cannot be changed, but it can also be changed in either case, just like the change in tbufc <n>. The first case is the use of the value assignment operator, and the second case is the use of modifiable pointer descriptors, such as tptr when using a member function. When using hbufc, remember two situations:

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

· You can use the realloc function to change the buffer size of the heap descriptor, just as in the case of tbufc <n>.

Example 8:-hbufc usage:

// Create a heap descriptor in two ways
// The first method uses one of new (), newl (), or newlc ()
// Let's take an example. Here we will build an hbufc: The data space used is 15, but the current size is 0.
Hbufc * Buf = hbufc: newl (15 );
// Method 2
// The alloc (), allocl (), or alloclc () method of the existing descriptor. The new heap descriptor is automatically initialized with the descriptor content.
_ Partition (ktext, "test text ");
Tbufc <10> cbuf = ktext;
Hbufc * buf1 = cbuf. allocl ();
// Check the size and length. The size is 18 and the length is 9.
Tint bufsize = Buf-> size ();
Tint buflength = Buf-> length ();
// Change the direction of hbufc
_ Partition (ktext1, "text1 ");
// Use the value assignment operator to change the buffer pointing to ktext1
* Buf1 = ktext1;
// The following uses the modifiable pointer descriptor to change the data
Tptr pointer = buf1-> des ();
Pointer. Delete (pointer. Length ()-2, 2 );
// All operations that can be performed on tbufc <n> are available
// The following is an operation
_ Partition (knew, "New :");
Pointer. append (knew );

6. tptr usage

We use it to replace tbufc <n> and hbufc, because I know it best. Therefore, let's first remember how to create tptr.

· Use another tptr.

· Create from tbufc <n> or use hbufc through the member function des.

· From an external pointer pointing to the memory and specifying the maximum length.

· From an external pointer pointing to the memory and specifying the data and its maximum length.

Instance 9:-tptr usage:

// Let's take a look at the two methods for getting tptr
_ Partition (ktext, "Test Data ");
Tbufc <10> nbuf (ktext );
Tptr pointer = nbuf. Des ();
// Method 1
Tptr pointer2 (pointer );
// The second method uses a memory segment for data storage and maximum length.
Ttext * text = _ S ("test second ");
Tptr pointer3 (text, 11, 12 );
// Now let's take a look at how to replace data with tptr.
// Value assignment operator or copy Function
_ Partition (K1, "text1 ");
_ Second (K2, "text2 ");
Pointer2 = k1; // The data is text1
Pointer. Copy (K2); // The data is text2;
// We can also change the data length or set it to 0
Pointer2.setlength (2); // only two characters are left in Te
Pointer2.zero (); // set the length to 0.
// You can use the delete function to change data, as shown in the previous example.

7. tbuf <n> usage

In this data structure, the data is not fixed. Operations, instantiation, and assignment are the same as those of tbufc <n>. The modification operations are the same as those of tptr, such as copying, deleting, and assigning values. I don't want to give an example.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.