
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);
} catch(CStringParserExceptAM& e){
TCHAR error_message[1024];
e.GetErrorMessage(error_message, 1024);
MessageBox(error_message);
}
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.