[ci] Test Tcl bindings for dragonfly/freebsd
[xapian.git] / xapian-applications / omega / expand.cc
blob91636b7dd168ed629b58be499103e4d2462a645e
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>
31 using namespace std;
33 [[noreturn]]
34 static void
35 parameter_error(const char * msg, const string & scheme)
37 string m(msg);
38 m += ": '";
39 m += scheme;
40 m += "'";
41 throw m;
44 static bool
45 double_param(const char ** p, double * ptr_val)
47 char *end;
48 errno = 0;
49 double v = strtod(*p, &end);
50 if (*p == end || errno) return false;
51 *p = end;
52 *ptr_val = v;
53 return true;
56 void
57 set_expansion_scheme(Xapian::Enquire & enq, const map<string, string> & opt)
59 map<string, string>::const_iterator i = opt.find("expansion");
60 if (i == opt.end()) return;
62 const string & scheme = i->second;
63 if (scheme.empty()) return;
65 if (startswith(scheme, "prob") || startswith(scheme, "trad")) {
66 const char *p = scheme.c_str() + 4;
67 if (*p == '\0') {
68 enq.set_expansion_scheme("prob");
69 return;
71 if (C_isspace(*p)) {
72 // Initialise k just to silence compiler warning.
73 double k = 0.0;
74 if (!double_param(&p, &k))
75 parameter_error("Parameter k is invalid", scheme);
76 if (*p)
77 parameter_error("Extra data after first parameter", scheme);
78 enq.set_expansion_scheme("prob", k);
79 return;
83 if (startswith(scheme, "bo1")) {
84 const char *p = scheme.c_str() + 3;
85 if (*p == '\0') {
86 enq.set_expansion_scheme("bo1");
87 return;
89 if (C_isspace(*p)) {
90 throw "No parameters are required for BO1";
94 throw "Unknown $opt{expansion} setting: " + scheme;