common/gnu_getopt.h

Go to the documentation of this file.
00001 /* gnu_getopt.h: Wrappers to allow GNU getopt to be used cleanly from C++ code.
00002  *
00003  * ----START-LICENCE----
00004  * Copyright 2004 Olly Betts
00005  *
00006  * This program is free software; you can redistribute it and/or
00007  * modify it under the terms of the GNU General Public License as
00008  * published by the Free Software Foundation; either version 2 of the
00009  * License, or (at your option) any later version.
00010  *
00011  * This program is distributed in the hope that it will be useful,
00012  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00013  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00014  * GNU General Public License for more details.
00015  *
00016  * You should have received a copy of the GNU General Public License
00017  * along with this program; if not, write to the Free Software
00018  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
00019  * USA
00020  * -----END-LICENCE-----
00021  */
00022 
00023 #ifndef XAPIAN_INCLUDED_GNU_GETOPT_H
00024 #define XAPIAN_INCLUDED_GNU_GETOPT_H
00025 
00026 // We need to include a header to get __GLIBC__ defined.  Hopefully ctype.h
00027 // is a safe bet.
00028 #include <ctype.h>
00029 
00030 #define GNU_GETOPT_INTERFACE_VERSION 2
00031 #if defined __GLIBC__ && __GLIBC__ >= 2
00032 # include <gnu-versions.h>
00033 # if _GNU_GETOPT_INTERFACE_VERSION == GNU_GETOPT_INTERFACE_VERSION
00034 #  define USE_GLIBC_GNUGETOPT
00035 # endif
00036 #endif
00037 
00038 #ifdef USE_GLIBC_GNUGETOPT
00039 
00040 #include <getopt.h>
00041 
00042 inline int
00043 gnu_getopt(int argc_, char *const *argv_, const char *shortopts_) {
00044     return getopt(argc_, argv_, shortopts_);
00045 }
00046 
00047 inline int
00048 gnu_getopt_long(int argc_, char *const *argv_, const char *shortopts_,
00049                 const struct option *longopts_, int *optind_) {
00050     return getopt_long(argc_, argv_, shortopts_, longopts_, optind_);
00051 }
00052 
00053 inline int
00054 gnu_getopt_long_only(int argc_, char *const *argv_, const char *shortopts_,
00055                      const struct option *longopts_, int *optind_) {
00056     return getopt_long_only(argc_, argv_, shortopts_, longopts_, optind_);
00057 }
00058 
00059 #else
00060 
00061 extern char *optarg;
00062 extern int optind;
00063 extern int opterr;
00064 extern int optopt;
00065 
00066 struct option {
00067     const char *name;
00068     int has_arg;
00069     int * flag;
00070     int val;
00071 };
00072 
00073 #define no_argument             0
00074 #define required_argument       1
00075 #define optional_argument       2
00076 
00077 // For internal use only.
00078 int
00079 gnu_getopt_internal_(int, char *const *, const char *, const struct option *,
00080                      int *, int);
00081 
00082 inline int
00083 gnu_getopt(int argc_, char *const *argv_, const char *shortopts_) {
00084     return gnu_getopt_internal_(argc_, argv_, shortopts_,
00085                             reinterpret_cast<const struct option *>(0),
00086                             reinterpret_cast<int *>(0), 0);
00087 }
00088 
00089 inline int
00090 gnu_getopt_long(int argc_, char *const *argv_, const char *shortopts_,
00091                 const struct option *longopts_, int *optind_) {
00092     return gnu_getopt_internal_(argc_, argv_, shortopts_, longopts_, optind_, 0);
00093 }
00094 
00095 inline int
00096 gnu_getopt_long_only(int argc_, char *const *argv_, const char *shortopts_,
00097                      const struct option *longopts_, int *optind_) {
00098     return gnu_getopt_internal_(argc_, argv_, shortopts_, longopts_, optind_, 1);
00099 }
00100 #endif
00101 
00102 #endif

Documentation for Xapian (version 1.0.10).
Generated on 24 Dec 2008 by Doxygen 1.5.2.