Click here to Skip to main content
Licence CPOL
First Posted 16 Dec 2002
Views 38,390
Bookmarked 12 times

String Parser Class with Field Validation and Exception Handling

By | 16 Dec 2002 | Article
Another class for extracting fields of string + field validation + exception Handling

Sample Image - CStringParserEx.png

Introduction

In scientific programming we encounter data files, containing textual records as entry, and so we need to split and check them. A few months ago, I read Remon's article, it's a good base for my work. I derived a class from CStringParser and added two additional classes (CFieldAM and CStringParserExceptAM) to it and every thing goes well with them.

  CStringParserAM parser_engine;
  CString example_string(_T("Ali Khanlarkhani alikhus@yahoo.com 1234"));
  int         count;
  CString         first_name, last_name, e_mail;
  int         id;

  parser_engine.Reset();

  parser_engine.AddField(_T("First Name"), CFieldAM::FT_BSTR, _T(""));
  parser_engine.AddField(_T("Last Name"),  CFieldAM::FT_BSTR, _T(""));
  parser_engine.AddField(_T("E-mail"), <BR>    CFieldAM::FT_BSTR, _T(""), _T(""), _T("\\w+@\\w+\\.\\w+"));
  parser_engine.AddField(_T("ID"), CFieldAM::FT_INT,  _T(""));
        
  try{

     parser_engine.ParserEx((LPCTSTR) example_string, poCmdLine, count);

     parser_engine.Field(0).Value(&first_name);
     parser_engine.Field(1).Value(&last_name);
     parser_engine.Field(2).Value(&e_mail);
     parser_engine.Field(3).Value(&id);

  } /// try
  catch(CStringParserExceptAM& e){

     TCHAR  error_message[1024];
     e.GetErrorMessage(error_message, 1024);
     MessageBox(error_message);

  } /// catch(CStringParserExceptAM& e)

Usage

  • CFieldAM uses Regex++, thus you need to download and install it. (see Perlmunger's article for details.)
  • Add CStringParser(.cpp and .h), CStringParserAM(.cpp and .h), CFieldAM(.cpp and .h) to your project.
  • Declare an instance of CStringParserAM, and use AddField method to add each field to it.
  • Call ParserEx with source string.

Conclusion

See demo project for details. If you find it useful I will prepare a complete documentation as soon as possible. Demo project is not too hard for understanding. Enjoy.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

About the Author

Ali Khanlarkhani

Software Developer (Senior)
Barayand Pioneer Technologies Co. Ltd.
Iran (Islamic Republic Of) Iran (Islamic Republic Of)

Member



Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
QuestionWhat do these two new classes do? PinmemberMarc Clifton0:53 17 Dec '02  
AnswerRe: What do these two new classes do? PinmemberAli Khanlarkhani1:24 17 Dec '02  
GeneralRe: What do these two new classes do? PinmemberJohn Simmons / outlaw programmer11:15 19 Dec '02  
GeneralRe: What do these two new classes do? PinmemberAli Khanlarkhani23:13 20 Dec '02  

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Mobile
Web01 | 2.5.120517.1 | Last Updated 17 Dec 2002
Article Copyright 2002 by Ali Khanlarkhani
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid