Mapped File Class


CMappedFile was created after some users complained that my apps were storing too much data in the Registry. I needed a way to preserve the multilevel data heirarchy that the Registry provides, but I needed to do it in external files. In addition, I wanted some protection from curious eyes.

CMappedFile is essentially a CMap of COleVariants that will serialize itself to and from files. It supports long, bool, LPCSTR and double. But, because you can cast just about any basic type into a long, you can store just about anything. Plus, it should be fairly simple to extend this class to hold anything a COleVariant can hold.

To provide key support, three functions, SetBaseKey, SetSubKey and MoveUp are provided.

  • SetBaseKey(key) allows you to set the root key for all subsequent read and write operations.
  • SetSubKey(subKey) moves the current key "down" to the specified subkey.
  • MoveUp() moves the current key "up" to the parent key.

The following example write five values to a CMappedFile, in two different keys, and writes the file. Then, it reads the file back into a different CMappedFile and verifies the results. Pretty simple.

// stupid data values
double pi = 3.14159;
CString hi = "Hi There!";
long fiftyFive = 55;
bool thisWorks = true;
bool thisDoesNotWork = false;

CMappedFile mf;

// start off with a key called "Root"

// Root\Pi : 3.14159
mf.Set("Pi", pi);			

// Root\Hi : Hi There!
mf.Set("Hi", hi);			

// move down a subkey

// Root\Subkey\Success : true
mf.Set("Success", thisWorks);	

// Root\Subkey\55 : 55
mf.Set("55", fiftyFive);	

// Root\Failure : false
mf.Set("Failure", thisDoesNotWork);

// dump this to the debug output window

// save it to a file

// "" now holds the following values : 
// Root\Pi : 3.14159
// Root\Hi : Hi There!
// Root\Subkey\55 : 55
// Root\Subkey\Success : true
// Root\Failure : false

// read it back, to a different CMappedFile, to be safe
CMappedFile mf2;

ASSERT(mf2.Get("Hi", "Bye") == hi);
ASSERT(mf2.Get("Pi", -1.0) == pi);

ASSERT(mf2.Get("55", (long)-1) == fiftyFive);
ASSERT(mf2.Get("Success", false) == true);

ASSERT(mf2.Get("Failure", true) == false);

Download source - 4 KB


  • There are no comments yet. Be the first to comment!

Leave a Comment
  • Your email address will not be published. All fields are required.

Top White Papers and Webcasts

  • On-Demand Webinar Your developers are shifting to a DevOps delivery model to increase the speed of delivering new code and features to your customers. But as a security and IT practitioner, you want security-first, enterprise-wide solutions that support your development teams without compromising on security. During this session, we discuss why it is important to: Secure and manage secrets used by machines (e.g. micro-services, applications, scripts, CI/CD tools, hosts, etc.) and privileged users …

  • Here are the five core principles for monetizing your storage like never before and winning more customer applications than ever for your cloud and hosted services. These are your keys to the next generation data center.

Most Popular Programming Stories

More for Developers

RSS Feeds

Thanks for your registration, follow us on our social networks to keep up-to-date