changeset 187:600754c27c88

Update parser to style and remove useless stuff
author David Demelier <markand@malikania.fr>
date Tue, 26 Nov 2013 20:20:59 +0100
parents d4b8416e9ab1
children ce3e1c3d6fed
files C++/Parser.cpp C++/Parser.h
diffstat 2 files changed, 47 insertions(+), 66 deletions(-) [+]
line wrap: on
line diff
--- a/C++/Parser.cpp	Sat Nov 23 16:14:05 2013 +0100
+++ b/C++/Parser.cpp	Tue Nov 26 20:20:59 2013 +0100
@@ -50,8 +50,7 @@
 	std::string ret;
 
 	for (const Option &o : m_options)
-		if (o.m_key == name)
-		{
+		if (o.m_key == name) {
 			ret = o.m_value;
 			break;
 		}
@@ -64,8 +63,7 @@
 {
 	bool result = false;
 
-	if (hasOption(name))
-	{
+	if (hasOption(name)) {
 		std::string value = findOption(name);
 
 		if (value == "yes" || value == "true"|| value == "1")
@@ -150,10 +148,8 @@
 {
 	size_t end;
 
-	if ((end = line.find_first_of(']')) != std::string::npos)
-	{
-		if (end > 1)
-		{
+	if ((end = line.find_first_of(']')) != std::string::npos) {
+		if (end > 1) {
 			std::string name = line.substr(1, end - 1);
 
 			/*
@@ -162,17 +158,13 @@
 			 * further read options should not be enabled until
 			 * a correct section is found again.
 			 */
-			if (hasSection(name) && (m_tuning & DisableRedefinition))
-			{
+			if (hasSection(name) && (m_tuning & DisableRedefinition)) {
 				if (!(m_tuning & DisableVerbosity))
 					log(lineno, name, "redefinition not allowed");
 				m_sections.back().m_allowed = false;
-			}
-			else
+			} else
 				addSection(name);
-		}
-		else if (!(m_tuning & DisableVerbosity))
-		{
+		} else if (!(m_tuning & DisableVerbosity)) {
 			/*
 			 * Do not add options at this step because it will
 			 * corrupt the previous one.
@@ -190,8 +182,7 @@
 	Section &current = m_sections.back();
 
 	// Error on last section?
-	if (!current.m_allowed)
-	{
+	if (!current.m_allowed) {
 		/*
 		 * If it is the root section, this has been probably set by
 		 * DisableRootSection flag, otherwise an error has occured
@@ -203,15 +194,13 @@
 		return;
 	}
 
-	if ((epos = line.find_first_of('=')) == std::string::npos)
-	{
+	if ((epos = line.find_first_of('=')) == std::string::npos) {
 		if (!(m_tuning & DisableVerbosity))
 			log(lineno, current.m_name, "missing `=' keyword");
 		return;
 	}
 
-	if (epos > 0)
-	{
+	if (epos > 0) {
 		size_t i, begin, last;
 		char c;
 
@@ -230,16 +219,13 @@
 	
 		c = value[0];
 		begin = 0;
-		if (c == '\'' || c == '"')
-		{
+		if (c == '\'' || c == '"') {
 			for (last = begin = 1; value[last] != c && last < value.length(); ++last)
 				continue;
 			if (value[last] != c && !(m_tuning & DisableVerbosity))
 				if (!(m_tuning & DisableVerbosity))
 					log(lineno, current.m_name, "undisclosed std::string");
-		}
-		else
-		{
+		} else {
 			for (last = begin; !isspace(value[last]) && last < value.length(); ++last)
 				continue;
 		}
@@ -265,10 +251,8 @@
 		continue;
 
 	buffer = line.substr(i);
-	if (buffer.length() > 0)
-	{
-		if (buffer[0] != m_commentChar)
-		{
+	if (buffer.length() > 0) {
+		if (buffer[0] != m_commentChar) {
 			if (buffer[0] == '[')
 				readSection(lineno, buffer);
 			else
@@ -312,8 +296,7 @@
 	int lineno = 1;
 
 	file.open(m_path.c_str());
-	if (!file.is_open())
-	{
+	if (!file.is_open()) {
 		m_error = "could not open file " + m_path;	// XXX: add a real error
 		return false;
 	}
@@ -362,14 +345,6 @@
 	throw NotFoundException(name);
 }
 
-const Section &Parser::requireSection(const std::string &name) const
-{
-	if (!hasSection(name))
-		throw NotFoundException(name);
-
-	return getSection(name);
-}
-
 void Parser::log(int number, const std::string &, const std::string &message)
 {
 	std::cout << "line " << number << ": " << message << std::endl;
@@ -377,8 +352,7 @@
 
 void Parser::dump()
 {
-	for (auto s : m_sections)
-	{
+	for (auto s : m_sections) {
 		dumpSection(s);
 
 		for (auto o : s.m_options)
--- a/C++/Parser.h	Sat Nov 23 16:14:05 2013 +0100
+++ b/C++/Parser.h	Tue Nov 26 20:20:59 2013 +0100
@@ -27,10 +27,12 @@
 #include <vector>
 
 /**
+ * @class NotFoundException
+ * @brief Exception raised when a section or option is not found
+ *
  * Thrown when a section or an option is not found.
  */
-class NotFoundException : public std::exception
-{
+class NotFoundException : public std::exception {
 private:
 	std::string m_key;
 
@@ -52,10 +54,12 @@
 };
 
 /**
+ * @struct Option
+ * @brief A key-value pair
+ *
  * An option referenced by a key and a value.
  */
-struct Option
-{
+struct Option {
 	std::string m_key;		/*! option name */
 	std::string m_value;		/*! option value */
 };
@@ -63,12 +67,14 @@
 bool operator==(const Option &o1, const Option &o2);
 
 /**
+ * @class Section
+ * @brief The option container
+ *
  * A list of section found in the file. If root
  * options are allowed (default behavior), the root
  * section is "".
  */
-class Section
-{
+class Section {
 private:
 	const std::string findOption(const std::string &name) const;
 
@@ -131,7 +137,7 @@
 		return getValue<T>(name);
 	}
 
-	friend std::ostream & operator<<(std::ostream & stream, const Section &section)
+	friend std::ostream &operator<<(std::ostream & stream, const Section &section)
 	{
 		stream << "[" << section.getName() << "]" << std::endl;
 
@@ -144,20 +150,24 @@
 
 bool operator==(const Section &s1, const Section &s2);
 
-class Parser
-{
+/**
+ * @class Parser
+ * @brief Config file parser
+ *
+ * Open and read .ini files.
+ */
+class Parser {
 public:
 	/**
 	 * Options available for the parser.
 	 */
-	enum Tuning
-	{
+	enum Tuning {
 		DisableRootSection	= 1,	/*! disable options on root */
 		DisableRedefinition	= 2,	/*! disable multiple redefinition */
 		DisableVerbosity	= 4	/*! be verbose by method */
 	};
 
-	typedef std::function<void (const Section &)> FindFunc;
+	using FindFunc = std::function<void (const Section &)>;
 
 private:
 	std::vector<Section> m_sections;	/*! list of sections found */
@@ -245,16 +255,6 @@
 	const Section &getSection(const std::string &name) const;
 
 	/**
-	 * Same as getSection except that throws an exception if
-	 * the section is not found.
-	 *
-	 * @param name the section name
-	 * @return the section
-	 * @throw NotFoundException if not found
-	 */
-	const Section &requireSection(const std::string &name) const;
-
-	/**
 	 * Logging function, used only if DisableVerbosity is not set. The
 	 * default behavior is to print to stdout something like:
 	 * line 10: syntax error
@@ -290,10 +290,17 @@
 	 */
 	virtual void dumpOption(const Option &option);
 
-	friend std::ostream &operator<<(std::ostream & stream, const Parser &parser)
+	/**
+	 * Write the configuration to the output stream.
+	 *
+	 * @param stream the output
+	 * @param parser the configuration
+	 * @return the stream
+	 */
+	friend std::ostream &operator<<(std::ostream &stream, const Parser &parser)
 	{
 		for (auto s : parser.m_sections)
-			stream << s;;
+			stream << s;
 
 		return stream;
 	}