1 /*
2  * Copyright (c) 2014-2021, Andrzej Rybczak <andrzej@rybczak.net>
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  *
9  * 1. Redistributions of source code must retain the above copyright
10  * notice, this list of conditions and the following disclaimer.
11  *
12  * 2. Redistributions in binary form must reproduce the above copyright
13  * notice, this list of conditions and the following disclaimer in the
14  * documentation and/or other materials provided with the distribution.
15  *
16  * 3. Neither the name of the copyright holder nor the names of its
17  * contributors may be used to endorse or promote products derived from
18  * this software without specific prior written permission.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
23  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
24  * HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
25  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
26  * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
27  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
28  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
29  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
30  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31  */
32 
33 #include <boost/regex.hpp>
34 #include <iostream>
35 
36 #include "utility/option_parser.h"
37 
yes_no(const std::string & v)38 bool yes_no(const std::string &v)
39 {
40 	if (v == "yes")
41 		return true;
42 	else if (v == "no")
43 		return false;
44 	else
45 		invalid_value(v);
46 }
47 
48 ////////////////////////////////////////////////////////////////////////////////
49 
run(std::istream & is,bool ignore_errors)50 bool option_parser::run(std::istream &is, bool ignore_errors)
51 {
52 	// quoted value. leftmost and rightmost quotation marks are the delimiters.
53 	boost::regex quoted("(\\w+)\\h*=\\h*\"(.*)\"[^\"]*");
54 	// unquoted value. whitespaces get trimmed.
55 	boost::regex unquoted("(\\w+)\\h*=\\h*(.*?)\\h*");
56 	boost::smatch match;
57 	std::string line;
58 	while (std::getline(is, line))
59 	{
60 		if (boost::regex_match(line, match, quoted)
61 		||  boost::regex_match(line, match, unquoted))
62 		{
63 			std::string option = match[1];
64 			auto it = m_parsers.find(option);
65 			if (it != m_parsers.end())
66 			{
67 				try
68 				{
69 					it->second.parse(match[2]);
70 				}
71 				catch (std::exception &e)
72 				{
73 					std::cerr << "Error while processing option \"" << option
74 					          << "\": " << e.what() << "\n";
75 					if (!ignore_errors)
76 						return false;
77 				}
78 			}
79 			else
80 			{
81 				std::cerr << "Unknown option: " << option << "\n";
82 				if (!ignore_errors)
83 					return false;
84 			}
85 		}
86 	}
87 	return true;
88 }
89 
initialize_undefined(bool ignore_errors)90 bool option_parser::initialize_undefined(bool ignore_errors)
91 {
92 	for (auto &pp : m_parsers)
93 	{
94 		auto &p = pp.second;
95 		if (!p.used())
96 		{
97 			try
98 			{
99 				p.parse_default();
100 			}
101 			catch (std::exception &e)
102 			{
103 				std::cerr << "Error while initializing option \"" << pp.first
104 				          << "\": " << e.what() << "\n";
105 				if (ignore_errors)
106 					return false;
107 			}
108 		}
109 	}
110 	return true;
111 }
112