A Generalized Parsing Class for MFC

One of the many tasks that a programmer has is taking a set of characters and breaking it down ino its discrete parts. This may be a comma delimited data file, a full directory path, or a set of program directives to perform an action verb and supply arguments. I often do this to implement a command handler for other classes. Rather than send the data to the class as disparate arguments I build a command string such as "Title=the Title@Start Date=12/12/1998@price=12.45" and pass it to the class which then parses the command string and sets the appropriate class variables. The advantage is that I don't have a particular order to send the data, one function can handle all the data inout for a class, and I can add more data members or action verbs without altering the public interface to the class at all, even by addition.

The class is very simple to use. All that you need to provide is a data string to be parsed and a set of delimiters that seperate the various fields. In the above case that would be:

parser.Parse("Titlle=the Title@Start Date=12/12/1998@price=12.45","=@");
There is also a class member that will read the data from a file and parse it.

The class is really brain dead to use. Below is a sample of the code used in the demo to parse user input strings:

void CMainView::OnBtnparse() 
	CParseIt	parser;						//	create a parser object
	CString		TheData;					// place to put field data into
	UpdateData();							//	Get it current
	if(!parser.Parse(m_Data,m_Seperator))   // this does all the work 
		return;								//	if here null string passed
	InitList();								//	empty current data from list ctrl
	for ( int y=0; y < parser.GetNumFields();++y) //  loop thru getting all the data
		AddItemToList(y+1,TheData);			//	add it to the list
There are also functions to retrieve the data as strings, CStrings,longs (DWORD),doubles, and ints. The data is all stored as type char so it can be retrieved in any form it can be represented as. Notice above I get everything into a CString. This could just as easily be a double or int if the data could be described as that type of variable. Just pass GetField a reference to the type of data you wish returned and it does the conversion for you. Set Doc.rtf for details on these functions.

Download demo project - 25.6 KB

Download source - 6 KB

About the Author

Richard Stringer

C C++ ASM are my currrent languages of choice. Currently writing MFC apps for insurance companies


  • Unicode Support?

    Posted by Legacy on 08/29/2000 12:00am

    Originally posted by: Prasad

    Does the Parse class support unicode strings?

  • Good, but lacks support for embedded delimiters

    Posted by Legacy on 05/28/2000 12:00am

    Originally posted by: Roger C

    This class could be made much more robust by adding support for string delimiters (sometimes called string identifiers)
    that show where a string begins and ends. Such support should allow escaping the string delimiter by doubling the character. This means that a string could end with three consecutive string delimiters.

    Thanks sharing code,

  • AfxExtractString

    Posted by Legacy on 06/12/1999 12:00am

    Originally posted by: Earl

    There is an undocumented string parsing function I have found useful.  

    Parameters are (from memory, check before use):
    void? AfxExtractString(CString csSource, CString csDest, int position, TCHAR cDelimiter);

  • 2 Errors when compiling in VC++ 6.0

    Posted by Legacy on 04/15/1999 12:00am

    Originally posted by: Joe Farkas

         2 errors occur when compiling in VC++ 6.0. 

    error C2373: 'DDX_Text' : redefinition; different type modifiers
    d:\program files\microsoft visual studio\vc98\mfc\include\afxdd_.h(27) : see declaration of 'DDX_Text'

    This is because VC++ has AFXAPI functions defined for DDX_Text. there are 11 functions of the same name, each with a different third parameter type.
    To fix the error, remove the declaration in the Utility header file, and the golbal function all together, then recompile.

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

Top White Papers and Webcasts

  • Live Event Date: March 19, 2015 @ 1:00 p.m. ET / 10:00 a.m. PT The 2015 Enterprise Mobile Application Survey asked 250 mobility professionals what their biggest mobile challenges are, how many employees they are equipping with mobile apps, and their methods for driving value with mobility. Join Dan Woods, Editor and CTO of CITO Research, and Alan Murray, SVP of Products at Apperian, as they break down the results of this survey and discuss how enterprises are using mobile application management and private …

  • On-demand Event Event Date: February 12, 2015 The evolution of systems engineering with the SysML modeling language has resulted in improved requirements specification, better architectural definition, and better hand-off to downstream engineering. Agile methods have proven successful in the software domain, but how can these methods be applied to systems engineering? Check out this webcast and join Bruce Powel Douglass, author of Real-Time Agility, as he discusses how agile methods have had a tremendous …

Most Popular Programming Stories

More for Developers

RSS Feeds

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