mirror of
https://github.com/XRPLF/rippled.git
synced 2025-12-06 17:27:55 +00:00
Initial check in of section file parser.
This commit is contained in:
70
src/ParseSection.cpp
Normal file
70
src/ParseSection.cpp
Normal file
@@ -0,0 +1,70 @@
|
||||
#include "ParseSection.h"
|
||||
|
||||
#include <iostream>
|
||||
#include <boost/algorithm/string.hpp>
|
||||
#include <boost/foreach.hpp>
|
||||
|
||||
#define SECTION_DEFAULT_NAME ""
|
||||
|
||||
section ParseSection(const std::string strInput, const bool bTrim)
|
||||
{
|
||||
std::string strData(strInput);
|
||||
std::vector<std::string> vLines;
|
||||
section secResult;
|
||||
|
||||
// Convert DOS format to unix.
|
||||
boost::algorithm::replace_all(strData, "\r\n", "\n");
|
||||
|
||||
// Convert MacOS format to unix.
|
||||
boost::algorithm::replace_all(strData, "\r", "\n");
|
||||
|
||||
boost::algorithm::split(vLines, strData, boost::algorithm::is_any_of("\n"));
|
||||
|
||||
// Set the default section name.
|
||||
std::string strSection = SECTION_DEFAULT_NAME;
|
||||
|
||||
// Initialize the default section.
|
||||
secResult[strSection] = section::mapped_type();
|
||||
|
||||
// Parse each line.
|
||||
BOOST_FOREACH(std::string& strValue, vLines)
|
||||
{
|
||||
if (strValue.empty() || strValue[0] == '#')
|
||||
{
|
||||
// Blank line or comment, do nothing.
|
||||
}
|
||||
else if (strValue[0] == '[' && strValue[strValue.length()-1] == ']') {
|
||||
// New section.
|
||||
|
||||
strSection = strValue.substr(1, strValue.length()-2);
|
||||
secResult[strSection] = section::mapped_type();
|
||||
}
|
||||
else
|
||||
{
|
||||
// Another line in a section.
|
||||
if (bTrim)
|
||||
{
|
||||
boost::algorithm::trim_right_if(strValue, boost::algorithm::is_space());
|
||||
boost::algorithm::trim_left_if(strValue, boost::algorithm::is_space());
|
||||
}
|
||||
|
||||
secResult[strSection].push_back(strValue);
|
||||
}
|
||||
}
|
||||
|
||||
return secResult;
|
||||
}
|
||||
|
||||
void PrintSection(section secInput)
|
||||
{
|
||||
std::cerr << "PrintSection>" << std::endl;
|
||||
BOOST_FOREACH(section::value_type& pairSection, secInput)
|
||||
{
|
||||
std::cerr << "[" << pairSection.first << "]" << std::endl;
|
||||
BOOST_FOREACH(std::string& value, pairSection.second)
|
||||
{
|
||||
std::cerr << value << std::endl;
|
||||
}
|
||||
}
|
||||
std::cerr << "PrintSection<" << std::endl;
|
||||
}
|
||||
13
src/ParseSection.h
Normal file
13
src/ParseSection.h
Normal file
@@ -0,0 +1,13 @@
|
||||
#ifndef _PARSE_SECTION_
|
||||
#define _PARSE_SECTION_
|
||||
|
||||
#include <map>
|
||||
#include <vector>
|
||||
#include <string>
|
||||
|
||||
typedef std::map<const std::string, std::vector<std::string> > section;
|
||||
|
||||
section ParseSection(const std::string strInput, const bool bTrim);
|
||||
void PrintSection(section secInput);
|
||||
|
||||
#endif
|
||||
Reference in New Issue
Block a user