eccodes/tools/wingetopt.cc

78 lines
1.8 KiB
C++
Raw Permalink Normal View History

2013-03-25 14:23:07 +00:00
/*
2020-01-28 14:32:34 +00:00
* (C) Copyright 2005- ECMWF.
2013-03-25 14:23:07 +00:00
*
* This software is licensed under the terms of the Apache Licence Version 2.0
* which can be obtained at http://www.apache.org/licenses/LICENSE-2.0.
*
* In applying this licence, ECMWF does not waive the privileges and immunities granted to it by
* virtue of its status as an intergovernmental organisation nor does it submit to any jurisdiction.
*/
2013-04-09 10:46:01 +00:00
#include "wingetopt.h"
2015-02-18 17:42:02 +00:00
#ifdef ECCODES_ON_WINDOWS
2013-03-25 14:23:07 +00:00
#include "string.h"
2020-01-22 13:10:59 +00:00
char* optarg; /* global argument pointer */
int optind = 0; /* global argv index */
2013-03-25 14:23:07 +00:00
int opterr = 0;
2020-01-22 13:10:59 +00:00
int getopt(int argc, char* argv[], const char* optstring)
2013-03-25 14:23:07 +00:00
{
2020-01-22 13:10:59 +00:00
static char* next = 0;
2014-01-10 14:07:02 +00:00
char c;
2022-05-21 15:13:38 +00:00
const char* cp;
2014-01-10 14:07:02 +00:00
if (optind == 0)
next = 0;
2013-03-25 14:23:07 +00:00
2014-01-10 14:07:02 +00:00
optarg = 0;
2013-03-25 14:23:07 +00:00
2020-01-22 13:10:59 +00:00
if (next == 0 || *next == '\0') {
2014-01-10 14:07:02 +00:00
if (optind == 0)
optind++;
2013-03-25 14:23:07 +00:00
2020-01-22 13:10:59 +00:00
if (optind >= argc || argv[optind][0] != '-' || argv[optind][1] == '\0') {
2014-01-10 14:07:02 +00:00
optarg = 0;
if (optind < argc)
optarg = argv[optind];
return -1;
}
2013-03-25 14:23:07 +00:00
2020-01-22 13:10:59 +00:00
if (strcmp(argv[optind], "--") == 0) {
2014-01-10 14:07:02 +00:00
optind++;
optarg = NULL;
if (optind < argc)
optarg = argv[optind];
2020-01-22 13:10:59 +00:00
return -1;
2014-01-10 14:07:02 +00:00
}
2013-03-25 14:23:07 +00:00
2014-01-10 14:07:02 +00:00
next = argv[optind];
2020-01-22 13:10:59 +00:00
next++; /* skip past - */
2014-01-10 14:07:02 +00:00
optind++;
}
2013-03-25 14:23:07 +00:00
2020-01-22 13:10:59 +00:00
c = *next++;
2014-01-10 14:07:02 +00:00
cp = strrchr(optstring, c);
2013-03-25 14:23:07 +00:00
2014-01-10 14:07:02 +00:00
if (cp == 0 || c == ':')
return '?';
2013-03-25 14:23:07 +00:00
2014-01-10 14:07:02 +00:00
cp++;
2020-01-22 13:10:59 +00:00
if (*cp == ':') {
if (*next != '\0') {
2014-01-10 14:07:02 +00:00
optarg = next;
2020-01-22 13:10:59 +00:00
next = 0;
2014-01-10 14:07:02 +00:00
}
2020-01-22 13:10:59 +00:00
else if (optind < argc) {
2014-01-10 14:07:02 +00:00
optarg = argv[optind];
optind++;
}
2020-01-22 13:10:59 +00:00
else {
2014-01-10 14:07:02 +00:00
return '?';
}
}
2013-03-25 14:23:07 +00:00
2014-01-10 14:07:02 +00:00
return c;
2013-03-25 14:23:07 +00:00
}
#endif