All pastes #926324 Raw Edit

Someone

public cpp v1 · immutable
·published 2008-03-03 17:40 UTC
rendered paste body
#include <string>#include <iostream>#include <fstream>#include <map>#include <boost/regex.hpp>typedef std::map<std::string, std::string> ini_section;typedef std::map<std::string, ini_section> ini_file;ini_file fINI;bool ini_read(const char *fname) {	std::ifstream ini(fname);	if(!ini) {		return false;   	}	fINI.clear();	std::string line;	unsigned int keys = 0, craps = 0;	while(getline(ini, line)) {		boost::regex rxCrap("[[:space:]]*;.*$");		boost::regex rxSection("^\\[([[:word:]]+)\\]");		boost::regex rxKeyVal("^([[:word:]]+)=(.+)$");		boost::regex_replace(line, rxCrap, "", boost::match_default);		boost::match_results<std::string::const_iterator> matches;		std::string currentSection;		if(boost::regex_match(line, matches, rxSection, boost::match_default)) {			currentSection = matches[1].str();		} else if(boost::regex_match(line, matches, rxKeyVal, boost::match_default)) {			(fINI[currentSection])[matches[1].str()] = matches[2].str();			++keys;		} else {		++craps;		}	}	std::cout << "Found " << fINI.size() << " sections, " << keys << " key/value pairs and " << craps << " junk lines." << std::endl;   return true;}int main(int argc, char *argv[]){	if(argc < 2) {		std::cout << "Usage: INI_Reader file.ini" << std::endl;		return EXIT_SUCCESS;         	}    	if(!ini_read(argv[1])) {		std::cout << "File '" << std::string(argv[1]) 		  << "' could not be parsed - exiting" << std::endl;		return EXIT_SUCCESS;  }	std::cout << "Sections: " << std::endl;	for(ini_file::const_iterator sections = fINI.begin(); sections != fINI.end(); ++sections) {		std::cout << "[" << sections->first << "]" << std::endl;	}	std::cout << "End of section list." << std::endl;	return EXIT_SUCCESS;}