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
	{
		parser.GetField(y+1,TheData);
		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

Comments

  • Unicode Support?

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

    Originally posted by: Prasad

    Does the Parse class support unicode strings?
    

    Reply
  • 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,
    Roger

    Reply
  • 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);


    Reply
  • 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.

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

Top White Papers and Webcasts

  • Live Event Date: October 29, 2014 @ 11:00 a.m. ET / 8:00 a.m. PT Are you interested in building a cognitive application using the power of IBM Watson? Need a platform that provides speed and ease for rapidly deploying this application? Join Chris Madison, Watson Solution Architect, as he walks through the process of building a Watson powered application on IBM Bluemix. Chris will talk about the new Watson Services just released on IBM bluemix, but more importantly he will do a step by step cognitive …

  • In support of their business continuity and disaster recovery plans, many midsized companies endeavor to avoid putting all their eggs in one basket. Understanding the critical role of last-mile connectivity and always available Internet access for their enterprises, savvy firms utilize redundant connections from multiple service providers. Despite the good intentions, their Internet connectivity risk may still be in a single basket. That is because internet service providers (ISPs) and competitive local …

Most Popular Programming Stories

More for Developers

Latest Developer Headlines

RSS Feeds