Provides a template class for the Symbian dictionary stream.

Source: Internet
Author: User

Dictionary stream is a good storage solution in Symbian.

You can use the same file to store a large number of different types of information with different content, regardless of the Offset they store, it does not need to be divided into several small files for storage (which is a waste of storage space ).

Dictionary stream provides an access mechanism similar to Dictionary lookup. You can use an ID to find the "term" in a dictionary ". This entry can be an integer, a string, or even a list, and a serializable Class Object (implemented void externalizel (rwritestream & astream) const; and void internalizel (rreadstream & astream); Class Object ). When you access one entry, it does not affect other entries.This avoids the disadvantages of reading the entire file by using traditional stream input in order to retrieve a small variable, and does not need to create a file for each small variable to waste a lot of space and effort for maintenance.

The dictionary stream has a root, which is responsible for managing all entries. The ID of the output stream corresponding to the newly created "entry" must be assignl to the root stream.

It is also very convenient to use the dictionary stream, just like using the dictionary, you take an ID to read in the dictionary stream, and the dictionary stream will give you the content you care about. When you write something, the dictionary stream will not be written in disorder, but will be written according to your ID.

Assume that the data segments corresponding to the three IDs are stored in your file, which are A, B, and C. If the size of B changes, the data segments A and C. I just washed away the old B content. the space in B is only related to the size of the last write B.

Zhaotaobenny has an article named "Symbian file operations" http://www.sf.org.cn/Article/symbiandev/200701/19956.html
It provides a piece of code about the dictionary stream. I have studied and found some problems. I can only store one ID. When I replace the ID and use the original file name, the previous ID will be washed out.

After my research, I found that there are two places to change. One way to open a file is to use open. If it fails, replace

In addition, you must read the root stream first, and then save the sub-stream assignl to the root stream. After the modification, it will be more convenient to encapsulate it into a template class.

 

// Dictionarystreamoperator. h: interface for the cdictionarystreamoperator class.
// Save and load object by Using Dictionary streamoperator
// Windcao@peoplemail.com.cn
/**///////////////////////////////////// //////////////////////////////////

# If! Defined (_ dictionarystreamoperator_h __)
# DEFINE _ dictionarystreamoperator_h __

# Include <e32def. h>
# Include <s32file. h> // link with Estor. Lib
Template <typename T>
Class cdictionarystreamoperator
...{
Public:
/**//**
* Loadl
* Load object from stream, leave kerrnotfound (-1) If filenot exist or id not exist
* @ Param [out] aobj an object to be Load
* @ Param [in] aid an uid of the dictionary session ID
* @ Param [in] afilename the filename of the dictionary store file
*/
Static void loadl (T & aobj, tuid aid, const tdesc & afilename );
/**//**
* Savel
* Save object to stream
* @ Param [in] aobj the OBJ to be save
* @ Param [in] aid an uid of the dictionary session ID
* @ Param [in] afilename the filename of the dictionary store file
*/
Static void savel (const T & aobj, tuid aid, const tdesc & afilename );
};


Template <typename T>
Void cdictionarystreamoperator <t>: loadl (T & aobj, tuid aid, const tdesc & afilename)
...{
Rfs fs;
User: leaveiferror (FS. Connect ());
Cleanupclosepushl (FS );
Cfilestore * store;
Store = cdirectfilestore: openlc (FS, afilename, efileread); // create a storage

Cstreamdictionary * dictionary = cstreamdictionary: newlc (); // create a stream dictionary

Rstorereadstream rootstream; // root stream
Rootstream. openlc (* store, store-> root (); // open the root stream
Rootstream> * dictionary; // write stream dictionary
Cleanupstack: popanddestroy ();

Tstreamid;
Id = dictionary-> at (aid );
Cleanupstack: popanddestroy ();
Rstorereadstream stream;
Stream. openlc (* store, ID );
Stream> aobj;

Cleanupstack: popanddestroy ();
Cleanupstack: popanddestroy ();
Cleanupstack: popanddestroy (); // FS
};
Template <typename T>
Void cdictionarystreamoperator <t>: savel (const T & aobj, tuid aid, const tdesc & afilename)
...{
Tint pushcount = 0;
Rfs fs;
User: leaveiferror (FS. Connect ());
Cleanupclosepushl (FS );
Pushcount ++;

Cfilestore * store = NULL;
Cstreamdictionary * dictionary = cstreamdictionary: newlc (); // create a stream dictionary
Pushcount ++;

Trapd (ERR, store = cdirectfilestore: openl (FS, afilename, efilewrite | efileread); // open or create a store
If (ERR = kerrnone)
...{
Cleanupstack: pushl (store );
Pushcount ++;
Store-> settypel (tuidtype (kdirectfilestorelayoutuid, kuidappdlldoc, aid); // set the type

Rstorereadstream rootstreamin;
Rootstreamin. openlc (* store, store-> root (); // open the root stream
Rootstreamin> * dictionary; // read the root stream
Cleanupstack: popanddestroy (); // rootstreamin
}
Else
...{
Store = cdirectfilestore: replacelc (FS, afilename, efilewrite );
Pushcount ++;
Store-> settypel (tuidtype (kdirectfilestorelayoutuid, kuidappdlldoc, aid); // set the type
}


Rstorewritestream stream; // creates a write stream.
Tstreamid id = stream. createlc (* store); // get the stream ID
Stream <aobj;
Stream. commitl ();
Cleanupstack: popanddestroy (); // write object

Dictionary-> assignl (aid, ID); // associate the stream ID in the stream dictionary

Rstorewritestream rootstream;
Tstreamid rootid = rootstream. createlc (* store );
Rootstream <* dictionary;
Rootstream. commitl ();
Cleanupstack: popanddestroy (); // write to the root stream

Store-> setrootl (rootid); // you can specify the root stream ID.
Store-> commitl ();

Cleanupstack: popanddestroy (pushcount );
};

# Endif //! Defined (_ dictionarystreamoperator_h __)

 

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.