song list: get rid of boost::zip_iterator and improve {Const,}SongIterator
[ncmpcpp.git] / src / utility / option_parser.cpp
blobc7c0635e9f9e4abbbc493f16705a7990afe6a841
1 /*
2 * Copyright (c) 2014-2016, Andrzej Rybczak <electricityispower@gmail.com>
3 * All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
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.
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.
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.
33 #include <boost/regex.hpp>
34 #include <iostream>
36 #include "utility/option_parser.h"
38 bool yes_no(const std::string &v)
40 if (v == "yes")
41 return true;
42 else if (v == "no")
43 return false;
44 else
45 invalid_value(v);
48 ////////////////////////////////////////////////////////////////////////////////
50 bool option_parser::run(std::istream &is, bool ignore_errors)
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))
60 if (boost::regex_match(line, match, quoted)
61 || boost::regex_match(line, match, unquoted))
63 std::string option = match[1];
64 auto it = m_parsers.find(option);
65 if (it != m_parsers.end())
67 try
69 it->second.parse(match[2]);
71 catch (std::exception &e)
73 std::cerr << "Error while processing option \"" << option
74 << "\": " << e.what() << "\n";
75 if (!ignore_errors)
76 return false;
79 else
81 std::cerr << "Unknown option: " << option << "\n";
82 if (!ignore_errors)
83 return false;
87 return true;
90 bool option_parser::initialize_undefined(bool ignore_errors)
92 for (auto &pp : m_parsers)
94 auto &p = pp.second;
95 if (!p.used())
97 try
99 p.parse_default();
101 catch (std::exception &e)
103 std::cerr << "Error while initializing option \"" << pp.first
104 << "\": " << e.what() << "\n";
105 if (ignore_errors)
106 return false;
110 return true;