Fix replicate7 under Cygwin
[xapian.git] / xapian-bindings / perl / perl.i
blob27d166086d1d87f2d24ce49255015f3c4372fa7d
1 %module xapian
2 %{
3 /* perl.i: SWIG interface file for the Perl bindings
5 * Copyright (C) 2009 Kosei Moriyama
6 * Copyright (C) 2011,2012,2013,2015,2016 Olly Betts
8 * This program is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU General Public License as
10 * published by the Free Software Foundation; either version 2 of the
11 * License, or (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301
21 * USA
25 /* The XS Xapian never wrapped these, and they're now deprecated. */
26 #define XAPIAN_BINDINGS_SKIP_DEPRECATED_DB_FACTORIES
28 %include ../xapian-head.i
30 /* Rename function next() to increment() since the keyword "next" is already
31 * used in Perl. */
32 %rename(increment) *::next();
33 %rename(increment_weight) *::next(double min_wt);
35 /* Wrapping constant values. */
36 %constant int OP_AND = Xapian::Query::OP_AND;
37 %constant int OP_OR = Xapian::Query::OP_OR;
38 %constant int OP_AND_NOT = Xapian::Query::OP_AND_NOT;
39 %constant int OP_XOR = Xapian::Query::OP_XOR;
40 %constant int OP_AND_MAYBE = Xapian::Query::OP_AND_MAYBE;
41 %constant int OP_FILTER = Xapian::Query::OP_FILTER;
42 %constant int OP_NEAR = Xapian::Query::OP_NEAR;
43 %constant int OP_PHRASE = Xapian::Query::OP_PHRASE;
44 %constant int OP_VALUE_RANGE = Xapian::Query::OP_VALUE_RANGE;
45 %constant int OP_SCALE_WEIGHT = Xapian::Query::OP_SCALE_WEIGHT;
46 %constant int OP_ELITE_SET = Xapian::Query::OP_ELITE_SET;
47 %constant int OP_VALUE_GE = Xapian::Query::OP_VALUE_GE;
48 %constant int OP_SYNONYM = Xapian::Query::OP_SYNONYM;
49 %constant int OP_MAX = Xapian::Query::OP_MAX;
50 %constant int OP_WILDCARD = Xapian::Query::OP_WILDCARD;
51 %constant int OP_VALUE_LE = Xapian::Query::OP_VALUE_LE;
52 %constant int OP_INVALID = Xapian::Query::OP_INVALID;
53 %constant int FLAG_BOOLEAN = Xapian::QueryParser::FLAG_BOOLEAN;
54 %constant int FLAG_PHRASE = Xapian::QueryParser::FLAG_PHRASE;
55 %constant int FLAG_LOVEHATE = Xapian::QueryParser::FLAG_LOVEHATE;
56 %constant int FLAG_BOOLEAN_ANY_CASE = Xapian::QueryParser::FLAG_BOOLEAN_ANY_CASE;
57 %constant int FLAG_WILDCARD = Xapian::QueryParser::FLAG_WILDCARD;
58 %constant int FLAG_PURE_NOT = Xapian::QueryParser::FLAG_PURE_NOT;
59 %constant int FLAG_PARTIAL = Xapian::QueryParser::FLAG_PARTIAL;
60 %constant int FLAG_SPELLING_CORRECTION = Xapian::QueryParser::FLAG_SPELLING_CORRECTION;
61 %constant int FLAG_SYNONYM = Xapian::QueryParser::FLAG_SYNONYM;
62 %constant int FLAG_AUTO_SYNONYMS = Xapian::QueryParser::FLAG_AUTO_SYNONYMS;
63 %constant int FLAG_AUTO_MULTIWORD_SYNONYMS = Xapian::QueryParser::FLAG_AUTO_MULTIWORD_SYNONYMS;
64 %constant int FLAG_CJK_NGRAM = Xapian::QueryParser::FLAG_CJK_NGRAM;
65 %constant int FLAG_DEFAULT = Xapian::QueryParser::FLAG_DEFAULT;
66 %constant int STEM_NONE = Xapian::QueryParser::STEM_NONE;
67 %constant int STEM_SOME = Xapian::QueryParser::STEM_SOME;
68 %constant int STEM_ALL = Xapian::QueryParser::STEM_ALL;
69 %constant int STEM_ALL_Z = Xapian::QueryParser::STEM_ALL_Z;
70 %constant int FLAG_SPELLING = Xapian::TermGenerator::FLAG_SPELLING;
71 // FLAG_CJK_NGRAM already set above from QueryParser (values match).
72 %constant int WILDCARD_LIMIT_ERROR = Xapian::Query::WILDCARD_LIMIT_ERROR;
73 %constant int WILDCARD_LIMIT_FIRST = Xapian::Query::WILDCARD_LIMIT_FIRST;
74 %constant int WILDCARD_LIMIT_MOST_FREQUENT = Xapian::Query::WILDCARD_LIMIT_MOST_FREQUENT;
76 /* Xapian::Enquire */
77 %feature("shadow") Xapian::Enquire::get_mset
79 sub get_mset {
80 my $self = $_[0];
81 my $nargs = scalar(@_);
82 if( $nargs == 4 ) {
83 my $type = ref( $_[2] );
84 if ( $type eq 'Xapian::RSet' ) {
85 # get_mset(first, max, rset)
86 splice @_, 2, 0, (0); # insert checkatleast
89 return Xapianc::Enquire_get_mset( @_ );
93 %feature("shadow") Xapian::Enquire::set_query
95 sub set_query {
96 my $self = shift;
97 my $query = shift;
98 if( ref( $query ) ne 'Xapian::Query' ) {
99 $query = Xapian::Query->new( $query, @_ );
100 Xapianc::Enquire_set_query( $self, $query );
101 return;
103 my $nargs = scalar(@_);
104 if( $nargs > 1) {
105 use Carp;
106 Carp::carp( "USAGE: \$enquire->set_query(\$query) or \$enquire->set_query(\$query, \$length)" );
107 exit;
109 Xapianc::Enquire_set_query( $self, $query, @_ );
113 %feature("shadow") Xapian::Enquire::set_sort_by_key
115 sub set_sort_by_key {
116 my $self = $_[0];
117 my $sorter = $_[1];
118 $self{_sorter} = $sorter;
119 Xapianc::Enquire_set_sort_by_key( @_ );
123 %feature("shadow") Xapian::Enquire::set_sort_by_key_then_relevance
125 sub set_sort_by_key_then_relevance {
126 my $self = $_[0];
127 my $sorter = $_[1];
128 $self{_sorter} = $sorter;
129 Xapianc::Enquire_set_sort_by_key_then_relevance( @_ );
133 %feature("shadow") Xapian::Enquire::set_sort_by_relevance_then_key
135 sub set_sort_by_relevance_then_key {
136 my $self = $_[0];
137 my $sorter = $_[1];
138 $self{_sorter} = $sorter;
139 Xapianc::Enquire_set_sort_by_relevance_then_key( @_ );
143 /* Xapian::ESet */
144 %extend Xapian::ESet {
145 Xapian::ESetIterator FETCH(int index) {
146 return ((*self)[index]);
150 /* Xapian::ESetIterator */
151 %extend Xapian::ESetIterator {
152 std::string get_termname() {
153 return self->operator*();
156 bool equal(Xapian::ESetIterator * that) {
157 return ((*self) == (*that));
160 bool nequal(Xapian::ESetIterator * that) {
161 return ((*self) != (*that));
165 /* Xapian::MSet */
166 %extend Xapian::MSet {
167 Xapian::MSetIterator FETCH(int index) {
168 return ((*self)[index]);
172 /* Xapian::MSetIterator */
173 %extend Xapian::MSetIterator {
174 bool equal(Xapian::MSetIterator * that) {
175 return ((*self) == (*that));
178 bool nequal(Xapian::MSetIterator * that) {
179 return ((*self) != (*that));
183 /* Xapian::PositionIterator */
184 %extend Xapian::PositionIterator {
185 bool equal1(Xapian::PositionIterator * that) {
186 return ((*self) == (*that));
189 bool nequal1(Xapian::PositionIterator * that) {
190 return ((*self) != (*that));
194 /* Xapian::PostingIterator */
195 %extend Xapian::PostingIterator {
196 bool equal(Xapian::PostingIterator * that) {
197 return ((*self) == (*that));
200 bool nequal(Xapian::PostingIterator * that) {
201 return ((*self) != (*that));
205 /* Xapian::Query */
206 %feature("shadow") Xapian::Query::Query
208 sub new {
209 my $class = shift;
210 my $query;
212 if( @_ <= 1 ) {
213 $query = Xapianc::new_Query(@_);
214 } else {
215 use Carp;
216 my $op = $_[0];
217 if( $op !~ /^\d+$/ ) {
218 Carp::croak( "USAGE: $class->new('term') or $class->new(OP, <args>)" );
220 if( $op == 8 ) { # FIXME: 8 is OP_VALUE_RANGE; eliminate hardcoded literal
221 if( @_ != 4 ) {
222 Carp::croak( "USAGE: $class->new(OP_VALUE_RANGE, VALNO, START, END)" );
224 $query = Xapianc::new_Query( @_ );
225 } elsif( $op == 9 ) { # FIXME: OP_SCALE_WEIGHT
226 if( @_ != 3 ) {
227 Carp::croak( "USAGE: $class->new(OP_SCALE_WEIGHT, QUERY, FACTOR)" );
229 $query = Xapianc::new_Query( @_ );
230 } elsif( $op == 11 || $op == 12 ) { # FIXME: OP_VALUE_GE, OP_VALUE_LE; eliminate hardcoded literals
231 if( @_ != 3 ) {
232 Carp::croak( "USAGE: $class->new(OP_VALUE_[GL]E, VALNO, LIMIT)" );
234 $query = Xapianc::new_Query( @_ );
235 } else {
236 shift @_;
237 $query = Xapian::newN( $op, \@_ );
240 return $query;
244 %typemap(in) SV ** {
245 AV *tempav;
246 I32 len;
247 int i;
248 SV **tv;
249 if (!SvROK($input))
250 croak("Argument $argnum is not a reference.");
251 if (SvTYPE(SvRV($input)) != SVt_PVAV)
252 croak("Argument $argnum is not an array.");
253 tempav = (AV*)SvRV($input);
254 len = av_len(tempav);
255 $1 = (SV **) malloc((len+2)*sizeof(SV *));
256 for (i = 0; i <= len; i++) {
257 tv = av_fetch(tempav, i, 0);
258 $1[i] = *tv;
260 $1[i] = NULL;
263 %typemap(freearg) SV ** {
264 free($1);
268 class XapianSWIGQueryItor {
269 AV * array;
271 int i;
273 public:
274 XapianSWIGQueryItor() { }
276 void begin(AV * array_) {
277 array = array_;
278 i = 0;
281 void end(int n) {
282 i = n;
285 XapianSWIGQueryItor & operator++() {
286 ++i;
287 return *this;
290 Xapian::Query operator*() const {
291 SV **svp = av_fetch(array, i, 0);
292 if( svp == NULL )
293 croak("Unexpected NULL returned by av_fetch()");
294 SV *sv = *svp;
296 if ( sv_isa(sv, "Xapian::Query")) {
297 Xapian::Query *q;
298 SWIG_ConvertPtr(sv, (void **)&q, SWIGTYPE_p_Xapian__Query, 0);
299 return *q;
302 if ( SvOK(sv) ) {
303 STRLEN len;
304 const char * ptr = SvPV(sv, len);
305 return Xapian::Query(string(ptr, len));
308 croak( "USAGE: Xapian::Query->new(OP, @TERMS_OR_QUERY_OBJECTS)" );
311 bool operator==(const XapianSWIGQueryItor & o) {
312 return i == o.i;
315 bool operator!=(const XapianSWIGQueryItor & o) {
316 return !(*this == o);
319 typedef std::input_iterator_tag iterator_category;
320 typedef Xapian::Query value_type;
321 typedef Xapian::termcount_diff difference_type;
322 typedef Xapian::Query * pointer;
323 typedef Xapian::Query & reference;
328 %inline %{
329 Xapian::Query * newN(int op_, SV *q_) {
330 Xapian::Query::op op = (Xapian::Query::op)op_;
331 XapianSWIGQueryItor b, e;
333 AV *q = (AV *) SvRV(q_);
335 b.begin(q);
336 e.end(av_len(q) + 1);
338 try {
339 return new Xapian::Query(op, b, e);
340 } catch (const Xapian::Error &error) {
341 croak( "Exception: %s", error.get_msg().c_str() );
346 /* Xapian::QueryParser */
347 %feature("shadow") Xapian::QueryParser::QueryParser
349 sub new {
350 my $class = shift;
351 my $qp = Xapianc::new_QueryParser();
353 bless $qp, $class;
354 $qp->set_database(@_) if scalar(@_) == 1;
356 return $qp;
360 %feature("shadow") Xapian::QueryParser::set_stopper
362 sub set_stopper {
363 my ($self, $stopper) = @_;
364 $self{_stopper} = $stopper;
365 Xapianc::QueryParser_set_stopper( @_ );
369 %feature("shadow") Xapian::QueryParser::add_valuerangeprocessor
371 sub add_valuerangeprocessor {
372 my ($self, $vrproc) = @_;
373 push @{$self{_vrproc}}, $vrproc;
374 Xapianc::QueryParser_add_valuerangeprocessor( @_ );
378 /* Xapian::SimpleStopper */
379 %feature("shadow") Xapian::SimpleStopper::SimpleStopper
381 sub new {
382 my $class = shift;
383 my $stopper = Xapianc::new_SimpleStopper();
385 bless $stopper, $class;
386 foreach (@_) {
387 $stopper->add($_);
390 return $stopper;
394 %extend Xapian::SimpleStopper {
395 bool stop_word(std::string term) {
396 return (*self)(term);
400 /* Xapian::Stem */
401 %extend Xapian::Stem {
402 std::string stem_word(std::string word) {
403 return (*self)(word);
407 /* Xapian::TermIterator */
408 %rename(get_termname) Xapian::TermIterator::get_term;
410 %extend Xapian::TermIterator {
411 bool equal(Xapian::TermIterator * that) {
412 return ((*self) == (*that));
415 bool nequal(Xapian::TermIterator * that) {
416 return ((*self) != (*that));
420 /* Xapian::ValueIterator */
421 %extend Xapian::ValueIterator {
422 bool equal(Xapian::ValueIterator * that) {
423 return ((*self) == (*that));
426 bool nequal(Xapian::ValueIterator * that) {
427 return ((*self) != (*that));
431 /* Xapian::WritableDatabase */
432 %rename(replace_document_by_term) \
433 Xapian::WritableDatabase::replace_document(const std::string &,
434 const Xapian::Document &);
435 %rename(delete_document_by_term) \
436 Xapian::WritableDatabase::delete_document(const std::string &);
438 %feature("shadow") Xapian::WritableDatabase::WritableDatabase
440 sub new {
441 my $pkg = shift;
442 my $self;
443 if( scalar(@_) == 0 ) {
444 # For compatibility with Search::Xapian
445 @_ = ('', $Xapianc::DB_BACKEND_INMEMORY);
447 $self = Xapianc::new_WritableDatabase(@_);
448 bless $self, $pkg if defined($self);
452 %include util.i
453 %include except.i
454 %include ../xapian-headers.i