[VC] Use carchive objects

Source: Internet
Author: User

MFC provides the carchive class to implement data buffer read and write, and defines the storage and read solutions for class objects.

The following is an analysis of the internal implementation of carchvie.

1. Overview

2. Internal data

3. Read and Write basic data

4. Buffer update

5. Read and Write Data paragraphs with specified length

6. Read and Write strings

7. Read and Write the cobject derived object

1. Overview

Carchive uses a buffer zone, that is, a piece of memory space is used as a temporary data storage location. The read and write operations on carchive are arranged in the buffer zone in sequence. When the buffer zone is full or required by the user, read and Write the sorted data to the specified storage class.

When creating a carchive object, specify whether the mode is used for buffer read or for buffer write.

It can be understood that the carchive object is equivalent to the Railway Freight Train Dispatching Station, where scattered goods are collected. When the total amount reaches the train volume, the train ships.

When receiving the goods from a train, the goods are distributed to their respective cargo owners. Unlike freight transportation, delivery and pick-up are performed in chronological order, rather than by ticket. Therefore, it is necessary to ensure that the shipping and receiving owners are stored or retrieved in the same order.

For large cargo, it is divided into train units, transport, pick up each part of the goods, in turn, assembled into the original thing.

2. Internal data

The buffer pointer byte * m_lpbufstart points to the buffer. This buffer may be provided by the underlying cfile (such as the cmemfile derived class) object, but it is generally created by carchive itself.

Buffer tail pointer byte * m_lpbufmax;

The current buffer position pointer byte * m_lpbufcur;

During initialization, if it is in Read mode, the current position is at the end. If it is in write mode, the current position is at the header:

M_lpbufcur = (isloading ())? M_lpbufmax: m_lpbufstart;

3. Read and Write basic data

For basic data types, such as byte and double-character, you can directly use the >>and <symbol to read and write data.

The process of reading objects using carchive is as follows:

// Sample code 2
// Define file objects and file exception objects
Cfile file;
Cfileexception Fe;
// Open the file in Read mode
If (! File. Open (filename, cfile: moderead, & Fe ))
...{
Fe. reporterror ();
Return;
}

// Construct a carchive object
Carchive AR (& file, carchive: load );
Ar> obj1> obj2> obj3....> objn;
Ar. Flush ();
// Close the file stream after reading
Ar. Close ();
File. Close ();
Carchive writes an object as follows:
// Sample code 3
// Define file objects and file exception objects
Cfile file;
Cfileexception Fe;
// Open the file in Read mode
If (! File. Open (filename, cfile: modewrite | cfile: modecreate, & Fe ))
...{
Fe. reporterror ();
Return;
}

// Construct a carchive object
Carchive AR (& file, carchive: load );
Ar <obj1 <obj2 <obj3.... <objn;
Ar. Flush ();
// Close the file stream after writing.
Ar. Close ();
File. Close ();

When you select the File menu/open file (id_file_open) on the interface, the onfileopen function of the cwinapp derived class is automatically called. It is created through the document template (MDI)/reuse (SDI) framework, document, and view object, and finally call cdocument: onopendocument to read the file. The process of cdocument: onopendocument is as follows:
// Sample code 4
Bool cdocument: onopendocument (lpctstr lpszpathname)
...{
If (ismodified ())
Trace0 ("Warning: onopendocument replaces an unsaved document .");

Cfileexception Fe;
Cfile * pfile = GetFile (lpszpathname,
Cfile: moderead | cfile: sharedenywrite, & Fe );
If (pfile = NULL)
...{
Reportsaveloadexception (lpszpathname, & Fe,
False, afx_idp_failed_to_open_doc );
Return false;
}

Deletecontents ();
Setmodifiedflag (); // dirty during de-serialize

Carchive loadarchive (pfile, carchive: Load | carchive: bnoflushondelete );
Loadarchive. m_pdocument = this;
Loadarchive. m_bforceflat = false;
Try
...{
Cwaitcursor wait;
If (pfile-> getlength ()! = 0)
Serialize (loadarchive); // load me
Loadarchive. Close ();
Releasefile (pfile, false );
}
Catch_all (E)
...{
Releasefile (pfile, true );
Deletecontents (); // remove failed Contents

Try
...{
Reportsaveloadexception (lpszpathname, e,
False, afx_idp_failed_to_open_doc );
}
End_try
Delete_exception (E );
Return false;
}
End_catch_all

Setmodifiedflag (false); // start off with unmodified

Return true;
}

 

Similarly, when you choose to save the menu file/file (id_file_save) or file/Save... (id_file_saveas), cwinapp: onfilesave and cwinapp: onfilesaveas are used to call cdocument: onsavedocument. This function is processed as follows:

// Sample code 5
Bool cdocument: onsavedocument (lpctstr lpszpathname)
...{
Cfileexception Fe;
Cfile * pfile = NULL;
Pfile = GetFile (lpszpathname, cfile: modecreate |
Cfile: modereadwrite | cfile: Export exclusive, & Fe );

If (pfile = NULL)
...{
Reportsaveloadexception (lpszpathname, & Fe,
True, afx_idp_invalid_filename );
Return false;
}

Carchive savearchive (pfile, carchive: store | carchive: bnoflushondelete );
Savearchive. m_pdocument = this;
Savearchive. m_bforceflat = false;
Try
...{
Cwaitcursor wait;
Serialize (savearchive); // save me
Savearchive. Close ();
Releasefile (pfile, false );
}
Catch_all (E)
...{
Releasefile (pfile, true );

Try
...{
Reportsaveloadexception (lpszpathname, e,
True, afx_idp_failed_to_save_doc );
}
End_try
Delete_exception (E );
Return false;
}
End_catch_all

Setmodifiedflag (false); // back to unmodified

Return true; // success
}

 

From the previous two sections of code, we can see that the structure of File Read and file write is basically the same, and cobject is finally called :: the serialize function reads and writes the document itself (see save me and load me in the comment ). For the MDI and SDI automatically generated by Appwizard, the system automatically generates the overload implementation of this function. The default implementation is:

// Sample code 6
Void cmydoc: serialize (carchive & AR)
...{
If (AR. isstoring ())
...{
// Todo: Add storing code here
}
Else
...{
// Todo: Add loading code here
}
}

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.