Support: quest -f cjk_ngram
[xapian.git] / xapian-core / common / gnu_getopt.h
blob3e7de75a2ca21fd8e4e861761c0852acab3f91b0
1 /** @file gnu_getopt.h
2 * @brief Wrappers to allow GNU getopt to be used cleanly from C++ code.
3 */
4 /* Copyright (C) 2004,2009,2010 Olly Betts
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
19 * USA
22 #ifndef XAPIAN_INCLUDED_GNU_GETOPT_H
23 #define XAPIAN_INCLUDED_GNU_GETOPT_H
25 // We need to include a header to get __GLIBC__ defined. Hopefully <cctype>
26 // is a safe bet.
27 #include <cctype>
29 #define GNU_GETOPT_INTERFACE_VERSION 2
30 #if defined __GLIBC__ && __GLIBC__ >= 2
31 # include <gnu-versions.h>
32 # if _GNU_GETOPT_INTERFACE_VERSION == GNU_GETOPT_INTERFACE_VERSION
33 # define USE_GLIBC_GNUGETOPT
34 # endif
35 #endif
37 #ifdef USE_GLIBC_GNUGETOPT
39 #include <getopt.h>
41 inline int
42 gnu_getopt(int argc_, char *const *argv_, const char *shortopts_) {
43 return getopt(argc_, argv_, shortopts_);
46 inline int
47 gnu_getopt_long(int argc_, char *const *argv_, const char *shortopts_,
48 const struct option *longopts_, int *optind_) {
49 return getopt_long(argc_, argv_, shortopts_, longopts_, optind_);
52 inline int
53 gnu_getopt_long_only(int argc_, char *const *argv_, const char *shortopts_,
54 const struct option *longopts_, int *optind_) {
55 return getopt_long_only(argc_, argv_, shortopts_, longopts_, optind_);
58 #else
60 #ifdef __CYGWIN__
61 // Cygwin has __declspec(dllimport) magic on optarg, etc, so just pull in the
62 // header there rather than trying to duplicate that.
63 # include <getopt.h>
64 #else
65 extern "C" {
66 extern char *optarg;
67 extern int optind;
68 extern int opterr;
69 extern int optopt;
72 struct option {
73 const char *name;
74 int has_arg;
75 int * flag;
76 int val;
79 # define no_argument 0
80 # define required_argument 1
81 # define optional_argument 2
82 #endif
84 // For internal use only.
85 int
86 gnu_getopt_internal_(int, char *const *, const char *, const struct option *,
87 int *, int);
89 inline int
90 gnu_getopt(int argc_, char *const *argv_, const char *shortopts_) {
91 return gnu_getopt_internal_(argc_, argv_, shortopts_,
92 reinterpret_cast<const struct option *>(0),
93 reinterpret_cast<int *>(0), 0);
96 inline int
97 gnu_getopt_long(int argc_, char *const *argv_, const char *shortopts_,
98 const struct option *longopts_, int *optind_) {
99 return gnu_getopt_internal_(argc_, argv_, shortopts_, longopts_, optind_, 0);
102 inline int
103 gnu_getopt_long_only(int argc_, char *const *argv_, const char *shortopts_,
104 const struct option *longopts_, int *optind_) {
105 return gnu_getopt_internal_(argc_, argv_, shortopts_, longopts_, optind_, 1);
107 #endif
109 #endif