Click here to Skip to main content
6,822,123 members and growing! (16,830 online)
Email Password   helpLost your password?
Desktop Development » Files and Folders » Configuration Files     Intermediate License: The GNU Lesser General Public License

A C++ Config File Parser

By freejack

An STL based C++ utility class to parse structured config files.
C++ (VC6, VC7, VC7.1, VC8.0), C++/CLI, C, STL
Posted:17 May 2008
Views:28,424
Bookmarked:30 times
printPrint   add Share
      Discuss Discuss   Broken Article?Report  
12 votes for this article.
Popularity: 4.11 Rating: 3.81 out of 5

1

2
3 votes, 27.3%
3
1 vote, 9.1%
4
7 votes, 63.6%
5

Introduction

This article describes a small, light-weight parser for structured config files. Unlike INI-Files, config files may be sub-structured arbitrarily deep. Config files support the expansion of symbolic values from previously defined variables and environment variables.

Using the code

Basic usage

Consider the following example config file:

# Environment variables used as expansion symbol
# Note: symbol names are case sensitive
tempFolder = %TEMP%

# already defined variables used as expansion symbol
tempSubFolder = %tempFolder%\config-demo

Parsing is as simple as:

Config config("demo.config", envp);

cout << "tempFolder    = '"
   << config.pString("tempFolder") << "'" << endl;
cout << "tempSubFolder = '"
   << config.pString("tempSubFolder") << "'" << endl;

pString() retrieves an unparsed std::string value. The class Config provides additional methods which try to parse the value as a boolean, double, or int.

Config sub groups

Config files may contain sub-groups, as shown in the following example:

# Sub group example
baseName = Welcome Note
message1 = (
#  strings may use ""; it does not make a difference, however.
   name = "%baseName% 1"
   text = Sample message text
)

message2 = (
   name = %baseName% II
   text = Another sample message text
)

Unlike traditional INI-Files, sub-groups may contain further sub-groups up to an arbitrary depth. A sub-group may be accessed by name, or the whole collection of sub-groups may be accessed as stl::map<string, Config*>&.

The following code shows how to parse all sub-groups starting with a prefix, without knowing the exact count in advance:

// get properties for all subgroups starting with prefix
map<string, Config*> messages = config.getGroups(); // all groups
const string messagePrefix = "message"; // prefix for group name
for (map<string, Config*>::iterator i = messages.begin(); i != messages.
end(); ++i) {
       string groupName = i->first;
       Config* group = i->second;

       // test group name for prefix
       if (groupName.substr(0, messagePrefix.length()) == messagePrefix) {
           // display group contents
           cout << group->pString("name") << ":" << endl;
           cout << "   " << group->pString("text") << endl;
       }
}

With this feature, the parser may be also used for structured input documents other than config files. Unlike XML, the representation is more compact, parsing is simpler, and variable expansion is already included.

The distributed source contains very simple error handling. Warnings and error messages are logged to the console. On severe error, the process is aborted. Integrating the source in a project, one may consider to change this to C++ exception handling, if needed.

Only standard C++ functionality is used, so this code is truly cross platform. MSVS Express 2005 project files are included; manual build was tested successfully with GNU g++/Linux, using:

g++ -o ../configDemo *.cpp

History

  • First release.

License

This article, along with any associated source code and files, is licensed under The GNU Lesser General Public License

About the Author

freejack


Member

Location: United States United States

Other popular Files and Folders articles:

Article Top
You must Sign In to use this message board.
FAQ FAQ 
 
Noise Tolerance  Layout  Per page   
 Msgs 1 to 16 of 16 (Total in Forum: 16) (Refresh)FirstPrevNext
GeneralUNICODE support would be nice PinmemberJens Grünewald5:28 5 Oct '09  
GeneralRock On! PinmemberMartin Chapman19:57 4 Sep '09  
GeneralError PinmemberSb4s11:55 1 Sep '09  
GeneralConfig File parser with this file PinmemberDeni Lad13:11 10 Aug '09  
Questionparsing of configuration file using vc++ PinmemberAVIHAR19:29 20 Nov '08  
Generalwhitespace in config Pinmemberbbrandin3:38 11 Jun '08  
GeneralRe: whitespace in config [modified] Pinmemberfreejack20:19 14 Jun '08  
GeneralRe: whitespace in config Pinmemberbbrandin0:10 16 Jun '08  
GeneralSerialization PinmemberRobin17:24 22 May '08  
GeneralNice work Pinmemberregnier0:38 20 May '08  
GeneralRe: Nice work Pinmemberfreejack23:23 23 May '08  
Generalwhy not just XML? PinmemberJimmy Zhang21:49 17 May '08  
AnswerRe: why not just XML? Pinmemberfreejack6:14 18 May '08  
GeneralRe: why not just XML? PinmemberDavid @ SL2:09 20 May '08  
GeneralRe: why not just XML? PinmemberMike Cravens6:51 12 Dec '08  
GeneralRe: why not just XML? PinmemberVinod Khare12:23 13 Jan '09  

General General    News News    Question Question    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

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

PermaLink | Privacy | Terms of Use
Last Updated: 17 May 2008
Editor: Smitha Vijayan
Copyright 2008 by freejack
Everything else Copyright © CodeProject, 1999-2010
Web09 | Advertise on the Code Project