Expand some query-related testcases
[xapian.git] / xapian-applications / omega / expand.cc
blobb41ea1098ab7eba4b63457b6a82d33f47da74dd8
1 /** @file
2 * @brief Set the query expansion scheme for Omega
3 */
4 /* Copyright (C) 2009,2013,2014,2015 Olly Betts
5 * Copyright (C) 2013 Aarsh Shah
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
22 #include <config.h>
24 #include "expand.h"
26 #include "stringutils.h"
28 #include <cerrno>
29 #include <cstdlib>
30 #include "common/noreturn.h"
32 using namespace std;
34 XAPIAN_NORETURN(static void
35 parameter_error(const char * param, const string & scheme));
37 static void
38 parameter_error(const char * msg, const string & scheme)
40 string m(msg);
41 m += ": '";
42 m += scheme;
43 m += "'";
44 throw m;
47 static bool
48 double_param(const char ** p, double * ptr_val)
50 char *end;
51 errno = 0;
52 double v = strtod(*p, &end);
53 if (*p == end || errno) return false;
54 *p = end;
55 *ptr_val = v;
56 return true;
59 void
60 set_expansion_scheme(Xapian::Enquire & enq, const map<string, string> & opt)
62 map<string, string>::const_iterator i = opt.find("expansion");
63 if (i == opt.end()) return;
65 const string & scheme = i->second;
66 if (scheme.empty()) return;
68 if (startswith(scheme, "trad")) {
69 const char *p = scheme.c_str() + 4;
70 if (*p == '\0') {
71 enq.set_expansion_scheme("trad");
72 return;
74 if (C_isspace(*p)) {
75 // Initialise k just to silence compiler warning.
76 double k = 0.0;
77 if (!double_param(&p, &k))
78 parameter_error("Parameter k is invalid", scheme);
79 if (*p)
80 parameter_error("Extra data after first parameter", scheme);
81 enq.set_expansion_scheme("trad", k);
82 return;
86 if (startswith(scheme, "bo1")) {
87 const char *p = scheme.c_str() + 3;
88 if (*p == '\0') {
89 enq.set_expansion_scheme("bo1");
90 return;
92 if (C_isspace(*p)) {
93 throw "No parameters are required for BO1";
97 throw "Unknown $opt{expansion} setting: " + scheme;