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,2010 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 #ifdef __CYGWIN__
00062 // Cygwin has __declspec(dllimport) magic on optarg, etc, so just pull in the
00063 // header there rather than trying to duplicate that.
00064 # include <getopt.h>
00065 #else
00066 extern "C" {
00067 extern char *optarg;
00068 extern int optind;
00069 extern int opterr;
00070 extern int optopt;
00071 }
00072 
00073 struct option {
00074     const char *name;
00075     int has_arg;
00076     int * flag;
00077     int val;
00078 };
00079 
00080 # define no_argument            0
00081 # define required_argument      1
00082 # define optional_argument      2
00083 #endif
00084 
00085 // For internal use only.
00086 int
00087 gnu_getopt_internal_(int, char *const *, const char *, const struct option *,
00088                      int *, int);
00089 
00090 inline int
00091 gnu_getopt(int argc_, char *const *argv_, const char *shortopts_) {
00092     return gnu_getopt_internal_(argc_, argv_, shortopts_,
00093                             reinterpret_cast<const struct option *>(0),
00094                             reinterpret_cast<int *>(0), 0);
00095 }
00096 
00097 inline int
00098 gnu_getopt_long(int argc_, char *const *argv_, const char *shortopts_,
00099                 const struct option *longopts_, int *optind_) {
00100     return gnu_getopt_internal_(argc_, argv_, shortopts_, longopts_, optind_, 0);
00101 }
00102 
00103 inline int
00104 gnu_getopt_long_only(int argc_, char *const *argv_, const char *shortopts_,
00105                      const struct option *longopts_, int *optind_) {
00106     return gnu_getopt_internal_(argc_, argv_, shortopts_, longopts_, optind_, 1);
00107 }
00108 #endif
00109 
00110 #endif

Documentation for Xapian (version 1.0.20).
Generated on 28 Apr 2010 by Doxygen 1.5.2.