annotate fft/fftw/fftw-3.3.4/libbench2/my-getopt.c @ 40:223f770b5341 kissfft-double tip

Try a double-precision kissfft
author Chris Cannam
date Wed, 07 Sep 2016 10:40:32 +0100
parents 26056e866c29
children
rev   line source
Chris@19 1 /*
Chris@19 2 * Copyright (c) 2003, 2007-14 Matteo Frigo
Chris@19 3 * Copyright (c) 2003, 2007-14 Massachusetts Institute of Technology
Chris@19 4 *
Chris@19 5 * This program is free software; you can redistribute it and/or modify
Chris@19 6 * it under the terms of the GNU General Public License as published by
Chris@19 7 * the Free Software Foundation; either version 2 of the License, or
Chris@19 8 * (at your option) any later version.
Chris@19 9 *
Chris@19 10 * This program is distributed in the hope that it will be useful,
Chris@19 11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
Chris@19 12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
Chris@19 13 * GNU General Public License for more details.
Chris@19 14 *
Chris@19 15 * You should have received a copy of the GNU General Public License
Chris@19 16 * along with this program; if not, write to the Free Software
Chris@19 17 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
Chris@19 18 *
Chris@19 19 */
Chris@19 20
Chris@19 21 #include <string.h>
Chris@19 22 #include <stdio.h>
Chris@19 23
Chris@19 24 #include "config.h"
Chris@19 25 #include "my-getopt.h"
Chris@19 26
Chris@19 27 int my_optind = 1;
Chris@19 28 const char *my_optarg = 0;
Chris@19 29 static const char *scan_pointer = 0;
Chris@19 30
Chris@19 31 void my_usage(const char *progname, const struct my_option *opt)
Chris@19 32 {
Chris@19 33 int i;
Chris@19 34 size_t col = 0;
Chris@19 35
Chris@19 36 fprintf(stdout, "Usage: %s", progname);
Chris@19 37 col += (strlen(progname) + 7);
Chris@19 38 for (i = 0; opt[i].long_name; i++) {
Chris@19 39 size_t option_len;
Chris@19 40
Chris@19 41 option_len = strlen(opt[i].long_name);
Chris@19 42 if (col >= 80 - (option_len + 16)) {
Chris@19 43 fputs("\n\t", stdout);
Chris@19 44 col = 8;
Chris@19 45 }
Chris@19 46 fprintf(stdout, " [--%s", opt[i].long_name);
Chris@19 47 col += (option_len + 4);
Chris@19 48 if (opt[i].short_name < 128) {
Chris@19 49 fprintf(stdout, " | -%c", opt[i].short_name);
Chris@19 50 col += 5;
Chris@19 51 }
Chris@19 52 switch (opt[i].argtype) {
Chris@19 53 case REQARG:
Chris@19 54 fputs(" arg]", stdout);
Chris@19 55 col += 5;
Chris@19 56 break;
Chris@19 57 case OPTARG:
Chris@19 58 fputs(" [arg]]", stdout);
Chris@19 59 col += 10;
Chris@19 60 break;
Chris@19 61 default:
Chris@19 62 fputs("]", stdout);
Chris@19 63 col++;
Chris@19 64 }
Chris@19 65 }
Chris@19 66
Chris@19 67 fputs ("\n", stdout);
Chris@19 68 }
Chris@19 69
Chris@19 70 int my_getopt(int argc, char *argv[], const struct my_option *optarray)
Chris@19 71 {
Chris@19 72 const char *p;
Chris@19 73 const struct my_option *l;
Chris@19 74
Chris@19 75 if (scan_pointer && *scan_pointer) {
Chris@19 76 /* continue a previously scanned argv[] element */
Chris@19 77 p = scan_pointer;
Chris@19 78 goto short_option;
Chris@19 79 } else {
Chris@19 80 /* new argv[] element */
Chris@19 81 if (my_optind >= argc)
Chris@19 82 return -1; /* no more options */
Chris@19 83
Chris@19 84 p = argv[my_optind];
Chris@19 85
Chris@19 86 if (*p++ != '-')
Chris@19 87 return (-1); /* not an option */
Chris@19 88
Chris@19 89 if (!*p)
Chris@19 90 return (-1); /* string is exactly '-' */
Chris@19 91
Chris@19 92 ++my_optind;
Chris@19 93 }
Chris@19 94
Chris@19 95 if (*p == '-') {
Chris@19 96 /* long option */
Chris@19 97 scan_pointer = 0;
Chris@19 98 my_optarg = 0;
Chris@19 99
Chris@19 100 ++p;
Chris@19 101
Chris@19 102 for (l = optarray; l->short_name; ++l) {
Chris@19 103 size_t len = strlen(l->long_name);
Chris@19 104 if (!strncmp(l->long_name, p, len) &&
Chris@19 105 (!p[len] || p[len] == '=')) {
Chris@19 106 switch (l->argtype) {
Chris@19 107 case NOARG:
Chris@19 108 goto ok;
Chris@19 109 case OPTARG:
Chris@19 110 if (p[len] == '=')
Chris@19 111 my_optarg = p + len + 1;
Chris@19 112 goto ok;
Chris@19 113 case REQARG:
Chris@19 114 if (p[len] == '=') {
Chris@19 115 my_optarg = p + len + 1;
Chris@19 116 goto ok;
Chris@19 117 }
Chris@19 118 if (my_optind >= argc) {
Chris@19 119 fprintf(stderr,
Chris@19 120 "option --%s requires an argument\n",
Chris@19 121 l->long_name);
Chris@19 122 return '?';
Chris@19 123 }
Chris@19 124 my_optarg = argv[my_optind];
Chris@19 125 ++my_optind;
Chris@19 126 goto ok;
Chris@19 127 }
Chris@19 128 }
Chris@19 129 }
Chris@19 130 } else {
Chris@19 131 short_option:
Chris@19 132 scan_pointer = 0;
Chris@19 133 my_optarg = 0;
Chris@19 134
Chris@19 135 for (l = optarray; l->short_name; ++l) {
Chris@19 136 if (l->short_name == (char)l->short_name &&
Chris@19 137 *p == l->short_name) {
Chris@19 138 ++p;
Chris@19 139 switch (l->argtype) {
Chris@19 140 case NOARG:
Chris@19 141 scan_pointer = p;
Chris@19 142 goto ok;
Chris@19 143 case OPTARG:
Chris@19 144 if (*p)
Chris@19 145 my_optarg = p;
Chris@19 146 goto ok;
Chris@19 147 case REQARG:
Chris@19 148 if (*p) {
Chris@19 149 my_optarg = p;
Chris@19 150 } else {
Chris@19 151 if (my_optind >= argc) {
Chris@19 152 fprintf(stderr,
Chris@19 153 "option -%c requires an argument\n",
Chris@19 154 l->short_name);
Chris@19 155 return '?';
Chris@19 156 }
Chris@19 157 my_optarg = argv[my_optind];
Chris@19 158 ++my_optind;
Chris@19 159 }
Chris@19 160 goto ok;
Chris@19 161 }
Chris@19 162 }
Chris@19 163 }
Chris@19 164 }
Chris@19 165
Chris@19 166 fprintf(stderr, "unrecognized option %s\n", argv[my_optind - 1]);
Chris@19 167 return '?';
Chris@19 168
Chris@19 169 ok:
Chris@19 170 return l->short_name;
Chris@19 171 }
Chris@19 172