65.9K
CodeProject is changing. Read more.
Home

Yet another string parser

starIconstarIconstarIcon
emptyStarIcon
starIcon
emptyStarIcon

3.44/5 (9 votes)

Apr 14, 2001

Public Domain
viewsIcon

118330

downloadIcon

1757

Another class for extracting fields of string

Sample screenshot

Introduction

This is just another string parser that can be used either for command line or for CSV file for instance. This implementation uses the minimum amount of memory and time to parse a string into tokens.

Sample code

CStringParser args;
args.Parse( ::GetCommandLine(), poCmdLine ); 
args.Parse( _T("\"BOS011\" \"2001/04/09 08:49:09:0"
  "\" \"Hooker\" \"137.3.0.240\" \"Boston\" \"No SMS "
  "client detected\" \"No\" \"No\""), poCsvLine ); 
args.Parse( _T("BOS011,\"GHA\",\"149.19.0.49\",\"149.19.0.0\","
  "Microsoft Windows NT Advanced Server 4.0,BOSTON,BOSTON,"
  "Administrator,GUID:01C7F260-799B-11D4-875E-00508B9B1A22,1,"
  "SSMS1,2.00.1493.2009,5,1"), CParseOptions( ',', '"', '\0', FALSE ) );

Details

There are 2 projects to try this class - One is a simple console, the other is a complete GUI dialog box application to check each possibility of the class (using MFC). Have a nice trip with it.

Sorry for the lack of proper documentation but still have no time for it. Just read the code -both classes are not too hard.

Changes

By the way, I did some changes in the parser :

  • Can keep the quotes in the parsed argument by SetKeepQuote(true).
  • Can get the trailing null argument by SetRTrim(false).