Logo Search packages:      
Sourcecode: xapian-omega version File versions

gnu_getopt.h

/* gnu_getopt.h: Wrappers to allow GNU getopt to be used cleanly from C++ code.
 *
 * ----START-LICENCE----
 * Copyright 2004 Olly Betts
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License as
 * published by the Free Software Foundation; either version 2 of the
 * License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
 * USA
 * -----END-LICENCE-----
 */

#ifndef XAPIAN_INCLUDED_GNU_GETOPT_H
#define XAPIAN_INCLUDED_GNU_GETOPT_H

// We need to include a header to get __GLIBC__ defined.  Hopefully ctype.h
// is a safe bet.
#include <ctype.h>

#define GNU_GETOPT_INTERFACE_VERSION 2
#if defined __GLIBC__ && __GLIBC__ >= 2
# include <gnu-versions.h>
# if _GNU_GETOPT_INTERFACE_VERSION == GNU_GETOPT_INTERFACE_VERSION
#  define USE_GLIBC_GNUGETOPT
# endif
#endif

#ifdef USE_GLIBC_GNUGETOPT

#include <getopt.h>

inline int
gnu_getopt(int argc_, char *const *argv_, const char *shortopts_) {
    return getopt(argc_, argv_, shortopts_);
}

inline int
gnu_getopt_long(int argc_, char *const *argv_, const char *shortopts_,
            const struct option *longopts_, int *optind_) {
    return getopt_long(argc_, argv_, shortopts_, longopts_, optind_);
}

inline int
gnu_getopt_long_only(int argc_, char *const *argv_, const char *shortopts_,
                 const struct option *longopts_, int *optind_) {
    return getopt_long_only(argc_, argv_, shortopts_, longopts_, optind_);
}

#else

extern char *optarg;
extern int optind;
extern int opterr;
extern int optopt;

struct option {
    const char *name;
    int has_arg;
    int * flag;
    int val;
};

#define no_argument           0
#define required_argument     1
#define optional_argument     2

// For internal use only.
int
gnu_getopt_internal_(int, char *const *, const char *, const struct option *,
                 int *, int);

inline int
gnu_getopt(int argc_, char *const *argv_, const char *shortopts_) {
    return gnu_getopt_internal_(argc_, argv_, shortopts_,
                      reinterpret_cast<const struct option *>(0),
                      reinterpret_cast<int *>(0), 0);
}

inline int
gnu_getopt_long(int argc_, char *const *argv_, const char *shortopts_,
            const struct option *longopts_, int *optind_) {
    return gnu_getopt_internal_(argc_, argv_, shortopts_, longopts_, optind_, 0);
}

inline int
gnu_getopt_long_only(int argc_, char *const *argv_, const char *shortopts_,
                 const struct option *longopts_, int *optind_) {
    return gnu_getopt_internal_(argc_, argv_, shortopts_, longopts_, optind_, 1);
}
#endif

#endif

Generated by  Doxygen 1.6.0   Back to index