Fix termfreq used in weight calcs for repeated terms
[xapian.git] / xapian-bindings / php / generate-php-exceptions
blob88413e8027ef6aa55ed37ea4fb4243a716b59aed
1 # generate-php-exceptions: generate error handling code for PHP bindings
2 my $copyright = <<'END';
3  Copyright 2006,2007,2010,2011,2012 Olly Betts
4  Copyright 2007 Lemur Consulting Ltd
6  This program is free software; you can redistribute it and/or
7  modify it under the terms of the GNU General Public License as
8  published by the Free Software Foundation; either version 2 of the
9  License, or (at your option) any later version.
11  This program is distributed in the hope that it will be useful,
12  but WITHOUT ANY WARRANTY; without even the implied warranty of
13  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  GNU General Public License for more details.
16  You should have received a copy of the GNU General Public License
17  along with this program; if not, write to the Free Software
18  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301 USA
19 END
21 use strict;
22 use exception_data;
24 open FD, ">except.i" or die $!;
26 $copyright =~ s/^/ */mg;
28 print FD <<"EOF";
30 /** \@file php/except.i
31  * \@brief Custom PHP exception handling.
32  */
33 /* Warning: This file is generated by $0
34  * - do not modify directly!
35  *
36 $copyright */
38 EOF
40 print FD <<'EOF';
41 #include <exception>
42 #include <zend_exceptions.h>
44 static void
45 XapianExceptionHandler()
47     TSRMLS_FETCH();
48     string msg;
49     try {
50         // Rethrow so we can look at the exception if it was a Xapian::Error.
51         throw;
52     } catch (const Xapian::Error &e) {
53         // FIXME: It would be nicer to make the exceptions PHP classes
54         // corresponding to the C++ Xapian::Error class hierarchy.
55         msg = e.get_description();
56     } catch (const std::exception &e) {
57         msg = "std::exception: ";
58         msg += e.what();
59     } catch (...) {
60         msg = "unknown error in Xapian";
61     }
62     // zend_throw_exception takes a non-const char * parameter (sigh).
63     char * message = const_cast<char*>(msg.c_str());
64     zend_throw_exception(NULL, message, SWIG_UnknownError TSRMLS_CC);
68 /* Functions and methods which are marked as "nothrow": */
69 EOF
71 chdir($INC[0]);
72 exception_data::for_each_nothrow(sub { print FD "%exception $_[0];\n" });
74 print FD <<'EOF';
76 %exception {
77     try {
78         $function
79     } catch (...) {
80         XapianExceptionHandler();
81         return;
82     }
84 EOF
86 close FD or die $!;