From 50621437e67d7cbb392bdc174ff7dbfec5fd3b11 Mon Sep 17 00:00:00 2001 From: Arthur Britto Date: Thu, 5 Apr 2012 16:19:48 -0700 Subject: [PATCH] Add helpers for parsing section files. --- src/ParseSection.cpp | 43 +++++++++++++++++++++++++++++++++++++++++++ src/ParseSection.h | 3 +++ 2 files changed, 46 insertions(+) diff --git a/src/ParseSection.cpp b/src/ParseSection.cpp index 574069849..08fcd4cbe 100644 --- a/src/ParseSection.cpp +++ b/src/ParseSection.cpp @@ -68,3 +68,46 @@ void PrintSection(section secInput) } std::cerr << "PrintSection<" << std::endl; } + +section::mapped_type* sectionEntries(section& secSource, std::string strSection) +{ + section::iterator it; + section::mapped_type* smtResult; + + it = secSource.find(strSection); + if (it == secSource.end()) + { + smtResult = 0; + } + else + { + //section::mapped_type& vecEntries = it->second; + + smtResult = &(it->second); + } + + return smtResult; +} + +int sectionCount(section& secSource, std::string strSection) +{ + section::mapped_type* pmtEntries = sectionEntries(secSource, strSection); + + return pmtEntries ? -1 : pmtEntries->size(); +} + +bool sectionSingleB(section& secSource, std::string strSection, std::string& strValue) +{ + section::mapped_type* pmtEntries = sectionEntries(secSource, strSection); + bool bSingle = pmtEntries && 1 == pmtEntries->size(); + + if (bSingle) + { + strValue = (*pmtEntries)[0]; + } + + return bSingle; +} + + +// vim:ts=4 diff --git a/src/ParseSection.h b/src/ParseSection.h index 754301983..1f7a4aa7c 100644 --- a/src/ParseSection.h +++ b/src/ParseSection.h @@ -9,5 +9,8 @@ typedef std::map > section; section ParseSection(const std::string strInput, const bool bTrim); void PrintSection(section secInput); +bool sectionSingleB(section& secSource, std::string strSection, std::string& strValue); +int sectionCount(section& secSource, std::string strSection); +section::mapped_type* sectionEntries(section& secSource, std::string strSection); #endif