Fix masking of bits in serialised query
[xapian.git] / xapian-applications / omega / expand.cc
blobda9160549c33acafdbcb4b7f24c8a6a5ebdffbb4
1 /** @file expand.cc
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 <cstdlib>
29 #include "safeerrno.h"
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 #if !XAPIAN_AT_LEAST(1,3,2)
60 double expand_param_k = 1.0;
61 #endif
63 void
64 set_expansion_scheme(Xapian::Enquire & enq, const map<string, string> & opt)
66 map<string, string>::const_iterator i = opt.find("expansion");
67 if (i == opt.end()) return;
69 const string & scheme = i->second;
70 if (scheme.empty()) return;
72 #if XAPIAN_AT_LEAST(1,3,2)
73 if (startswith(scheme, "trad")) {
74 const char *p = scheme.c_str() + 4;
75 if (*p == '\0') {
76 enq.set_expansion_scheme("trad");
77 return;
79 if (C_isspace((unsigned char)*p)) {
80 // Initialise k just to silence compiler warning.
81 double k = 0.0;
82 if (!double_param(&p, &k))
83 parameter_error("Parameter k is invalid", scheme);
84 if (*p)
85 parameter_error("Extra data after first parameter", scheme);
86 enq.set_expansion_scheme("trad", k);
87 return;
91 if (startswith(scheme, "bo1")) {
92 const char *p = scheme.c_str() + 3;
93 if (*p == '\0') {
94 enq.set_expansion_scheme("bo1");
95 return;
97 if (C_isspace((unsigned char)*p)) {
98 throw "No parameters are required for BO1";
101 #else
102 if (startswith(scheme, "trad")) {
103 const char *p = scheme.c_str() + 4;
104 if (*p == '\0') {
105 return;
107 if (C_isspace((unsigned char)*p)) {
108 // Initialise k just to silence compiler warning.
109 double k = 0.0;
110 if (!double_param(&p, &k))
111 parameter_error("Parameter k is invalid", scheme);
112 if (*p)
113 parameter_error("Extra data after first parameter", scheme);
114 expand_param_k = k;
115 // Avoid "unused parameter" error.
116 (void)enq;
117 return;
120 #endif
122 throw "Unknown $opt{expansion} setting: " + scheme;