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

  • Targeted attacks and advanced threats are customized to infiltrate your unique IT infrastructure, evade conventional defenses, and remain hidden while stealing your corporate data. To detect these criminal intrusions, analysts and security experts agree that organizations should deploy advanced threat protection as part of an expanded security monitoring strategy. For this comparative analysis of breach detection systems, product analysis reports and comparative analysis reports are used to create the security …

  • Agile methodologies give development and test teams the ability to build software at a faster rate than ever before. Combining DevOps with hybrid cloud architectures give teams not just the principles, but also the technology necessary to achieve their goals. By combining hybrid cloud and DevOps: IT departments maintain control, visibility, and security Dev/test teams remain agile and collaborative Organizational barriers are broken down Innovation and automation can thrive Download this white paper to …

Most Popular Programming Stories

More for Developers

Latest Developer Headlines

RSS Feeds