A class for double zeroterminated strings

Download Source Code and Example


There are several win32 functions that require the use of a string with embedded NULLs and are terminated with an extra NULL (i will call them DZ strings for short).
An example of such a DZ string: "part1\0part2\0part3\0\0".
To name just a few of functions that work with DZ strings:

  • GetPrivateProfileSection
  • WritePrivateProfileSection
  • SQLConfigDataSource
As most string-functions (strcmp, strchr etc) expect a zeroterminated string, they are of little use when you have to parse a DZ string.
The CString class could be used to manipulate them, but you must be carefull not to call the non-DZ-aware CString-methods (such as Right(), Mid(), Left(), Replace() and Find()).
I decided to create a class that helps constructing and parsing those DZ strings. The class is called CDoubleZeroString and is not derived from CString.

An example how to use this class:
	CDoubleZeroString dz;
	dz.Add("a=1"); // add a zeroterminated 'part'
	dz.Add("b=2");
	dz.Add("c=3");
		// operator(const char*)  will return "a=1\0b=2\0c=3\0\0"
	WritePrivateProfileSection("humbug", dz, ".\\humbug.ini");
	char	buffer[1024];
	GetPrivateProfileSection("humbug", buffer, sizeof buffer, ".\\humbug.ini");
		// cleanup dz
	dz.Empty();
		// AddDoubleZero will parse buffer
	dz.AddDoubleZero(buffer);
		// print the outcome
	for(int i=0;i<dz.GetCount();i++)
		cout << dz.Get(i) << endl;
	CString test = dz;
	cout << "cstring " << test << endl;
This will show the following output:
a=1
b=2
c=3
cstring a=1

The CString test will only copy the first substring when it is constructed...
If you want to implement this using CString you would have to do something like:
	CString	dz("a=1\0" "b=2\0" "c=3\0" "\0", 13);
	   // or some prefer:
	CString dz("a=1@b=2@c=3@@");
	int pos;
	   // note that you MUST use ReverseFind (not Find) for this to work
	while((pos=dz.ReverseFind("@"))!=-1)
		dz.SetAt(pos, '\0');

Last updated: August 17, 1998



Comments

  • hi

    Posted by lexito on 05/23/2008 11:04am

    how can you display ex. 234578E, in parts of as
    23
    45
    78
    E
      when it is pases to a function

    Reply
  • Or you could just use std::string...

    Posted by Legacy on 08/09/2002 12:00am

    Originally posted by: DrPizza

    ... which is a counted string, not a null-terminated string.

    Reply
  • How about a C version?

    Posted by Legacy on 11/21/2000 12:00am

    Originally posted by: David Duncan Ross Palmer

    Are you a C++ programmer who hates C?
    If not how about a C version of this, that would be useful.

    For example a structure of pointers to C functions and of course manually passing >this< pointers. Or alternatively non-OOP altogether such as a library of functions for dealing with these types of strings.

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

Top White Papers and Webcasts

  • Hybrid cloud platforms need to think in terms of sweet spots when it comes to application platform interface (API) integration. Cloud Velocity has taken a unique approach to tight integration with the API sweet spot; enough to support the agility of physical and virtual apps, including multi-tier environments and databases, while reducing capital and operating costs. Read this case study to learn how a global-level Fortune 1000 company was able to deploy an entire 6+ TB Oracle eCommerce stack in Amazon Web …

  • Live Event Date: August 14, 2014 @ 2:00 p.m. ET / 11:00 a.m. PT Data protection has long been considered "overhead" by many organizations in the past, many chalking it up to an insurance policy or an extended warranty you may never use. The realities of today make data protection a must-have, as we live in a data driven society. The digital assets we create, share, and collaborate with others on must be managed and protected for many purposes. Check out this upcoming eSeminar and join eVault Chief Technology …

Most Popular Programming Stories

More for Developers

Latest Developer Headlines

RSS Feeds