annotate src/fftw-3.3.8/kernel/scan.c @ 84:08ae793730bd

Add null config files
author Chris Cannam
date Mon, 02 Mar 2020 14:03:47 +0000
parents d0c2a83c1364
children
rev   line source
Chris@82 1 /*
Chris@82 2 * Copyright (c) 2003, 2007-14 Matteo Frigo
Chris@82 3 * Copyright (c) 2003, 2007-14 Massachusetts Institute of Technology
Chris@82 4 *
Chris@82 5 * This program is free software; you can redistribute it and/or modify
Chris@82 6 * it under the terms of the GNU General Public License as published by
Chris@82 7 * the Free Software Foundation; either version 2 of the License, or
Chris@82 8 * (at your option) any later version.
Chris@82 9 *
Chris@82 10 * This program is distributed in the hope that it will be useful,
Chris@82 11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
Chris@82 12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
Chris@82 13 * GNU General Public License for more details.
Chris@82 14 *
Chris@82 15 * You should have received a copy of the GNU General Public License
Chris@82 16 * along with this program; if not, write to the Free Software
Chris@82 17 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
Chris@82 18 *
Chris@82 19 */
Chris@82 20
Chris@82 21
Chris@82 22 #include "kernel/ifftw.h"
Chris@82 23 #include <string.h>
Chris@82 24 #include <stddef.h>
Chris@82 25 #include <stdarg.h>
Chris@82 26 #include <stdio.h>
Chris@82 27
Chris@82 28 #ifdef USE_CTYPE
Chris@82 29 #include <ctype.h>
Chris@82 30 #else
Chris@82 31 /* Screw ctype. On linux, the is* functions call a routine that gets
Chris@82 32 the ctype map in the current locale. Because this operation is
Chris@82 33 expensive, the map is cached on a per-thread basis. I am not
Chris@82 34 willing to link this crap with FFTW. Not over my dead body.
Chris@82 35
Chris@82 36 Sic transit gloria mundi.
Chris@82 37 */
Chris@82 38 #undef isspace
Chris@82 39 #define isspace(x) ((x) >= 0 && (x) <= ' ')
Chris@82 40 #undef isdigit
Chris@82 41 #define isdigit(x) ((x) >= '0' && (x) <= '9')
Chris@82 42 #undef isupper
Chris@82 43 #define isupper(x) ((x) >= 'A' && (x) <= 'Z')
Chris@82 44 #undef islower
Chris@82 45 #define islower(x) ((x) >= 'a' && (x) <= 'z')
Chris@82 46 #endif
Chris@82 47
Chris@82 48 static int mygetc(scanner *sc)
Chris@82 49 {
Chris@82 50 if (sc->ungotc != EOF) {
Chris@82 51 int c = sc->ungotc;
Chris@82 52 sc->ungotc = EOF;
Chris@82 53 return c;
Chris@82 54 }
Chris@82 55 return(sc->getchr(sc));
Chris@82 56 }
Chris@82 57
Chris@82 58 #define GETCHR(sc) mygetc(sc)
Chris@82 59
Chris@82 60 static void myungetc(scanner *sc, int c)
Chris@82 61 {
Chris@82 62 sc->ungotc = c;
Chris@82 63 }
Chris@82 64
Chris@82 65 #define UNGETCHR(sc, c) myungetc(sc, c)
Chris@82 66
Chris@82 67 static void eat_blanks(scanner *sc)
Chris@82 68 {
Chris@82 69 int ch;
Chris@82 70 while (ch = GETCHR(sc), isspace(ch))
Chris@82 71 ;
Chris@82 72 UNGETCHR(sc, ch);
Chris@82 73 }
Chris@82 74
Chris@82 75 static void mygets(scanner *sc, char *s, int maxlen)
Chris@82 76 {
Chris@82 77 char *s0 = s;
Chris@82 78 int ch;
Chris@82 79
Chris@82 80 A(maxlen > 0);
Chris@82 81 while ((ch = GETCHR(sc)) != EOF && !isspace(ch)
Chris@82 82 && ch != ')' && ch != '(' && s < s0 + maxlen)
Chris@82 83 *s++ = (char)(ch & 0xFF);
Chris@82 84 *s = 0;
Chris@82 85 UNGETCHR(sc, ch);
Chris@82 86 }
Chris@82 87
Chris@82 88 static long getlong(scanner *sc, int base, int *ret)
Chris@82 89 {
Chris@82 90 int sign = 1, ch, count;
Chris@82 91 long x = 0;
Chris@82 92
Chris@82 93 ch = GETCHR(sc);
Chris@82 94 if (ch == '-' || ch == '+') {
Chris@82 95 sign = ch == '-' ? -1 : 1;
Chris@82 96 ch = GETCHR(sc);
Chris@82 97 }
Chris@82 98 for (count = 0; ; ++count) {
Chris@82 99 if (isdigit(ch))
Chris@82 100 ch -= '0';
Chris@82 101 else if (isupper(ch))
Chris@82 102 ch -= 'A' - 10;
Chris@82 103 else if (islower(ch))
Chris@82 104 ch -= 'a' - 10;
Chris@82 105 else
Chris@82 106 break;
Chris@82 107 x = x * base + ch;
Chris@82 108 ch = GETCHR(sc);
Chris@82 109 }
Chris@82 110 x *= sign;
Chris@82 111 UNGETCHR(sc, ch);
Chris@82 112 *ret = count > 0;
Chris@82 113 return x;
Chris@82 114 }
Chris@82 115
Chris@82 116 /* vscan is mostly scanf-like, with our additional format specifiers,
Chris@82 117 but with a few twists. It returns simply 0 or 1 indicating whether
Chris@82 118 the match was successful. '(' and ')' in the format string match
Chris@82 119 those characters preceded by any whitespace. Finally, if a
Chris@82 120 character match fails, it will ungetchr() the last character back
Chris@82 121 onto the stream. */
Chris@82 122 static int vscan(scanner *sc, const char *format, va_list ap)
Chris@82 123 {
Chris@82 124 const char *s = format;
Chris@82 125 char c;
Chris@82 126 int ch = 0;
Chris@82 127 int fmt_len;
Chris@82 128
Chris@82 129 while ((c = *s++)) {
Chris@82 130 fmt_len = 0;
Chris@82 131 switch (c) {
Chris@82 132 case '%':
Chris@82 133 getformat:
Chris@82 134 switch ((c = *s++)) {
Chris@82 135 case 's': {
Chris@82 136 char *x = va_arg(ap, char *);
Chris@82 137 mygets(sc, x, fmt_len);
Chris@82 138 break;
Chris@82 139 }
Chris@82 140 case 'd': {
Chris@82 141 int *x = va_arg(ap, int *);
Chris@82 142 *x = (int) getlong(sc, 10, &ch);
Chris@82 143 if (!ch) return 0;
Chris@82 144 break;
Chris@82 145 }
Chris@82 146 case 'x': {
Chris@82 147 int *x = va_arg(ap, int *);
Chris@82 148 *x = (int) getlong(sc, 16, &ch);
Chris@82 149 if (!ch) return 0;
Chris@82 150 break;
Chris@82 151 }
Chris@82 152 case 'M': {
Chris@82 153 md5uint *x = va_arg(ap, md5uint *);
Chris@82 154 *x = (md5uint)
Chris@82 155 (0xFFFFFFFF & getlong(sc, 16, &ch));
Chris@82 156 if (!ch) return 0;
Chris@82 157 break;
Chris@82 158 }
Chris@82 159 case '*': {
Chris@82 160 if ((fmt_len = va_arg(ap, int)) <= 0) return 0;
Chris@82 161 goto getformat;
Chris@82 162 }
Chris@82 163 default:
Chris@82 164 A(0 /* unknown format */);
Chris@82 165 break;
Chris@82 166 }
Chris@82 167 break;
Chris@82 168 default:
Chris@82 169 if (isspace(c) || c == '(' || c == ')')
Chris@82 170 eat_blanks(sc);
Chris@82 171 if (!isspace(c) && (ch = GETCHR(sc)) != c) {
Chris@82 172 UNGETCHR(sc, ch);
Chris@82 173 return 0;
Chris@82 174 }
Chris@82 175 break;
Chris@82 176 }
Chris@82 177 }
Chris@82 178 return 1;
Chris@82 179 }
Chris@82 180
Chris@82 181 static int scan(scanner *sc, const char *format, ...)
Chris@82 182 {
Chris@82 183 int ret;
Chris@82 184 va_list ap;
Chris@82 185 va_start(ap, format);
Chris@82 186 ret = vscan(sc, format, ap);
Chris@82 187 va_end(ap);
Chris@82 188 return ret;
Chris@82 189 }
Chris@82 190
Chris@82 191 scanner *X(mkscanner)(size_t size, int (*getchr)(scanner *sc))
Chris@82 192 {
Chris@82 193 scanner *s = (scanner *)MALLOC(size, OTHER);
Chris@82 194 s->scan = scan;
Chris@82 195 s->vscan = vscan;
Chris@82 196 s->getchr = getchr;
Chris@82 197 s->ungotc = EOF;
Chris@82 198 return s;
Chris@82 199 }
Chris@82 200
Chris@82 201 void X(scanner_destroy)(scanner *sc)
Chris@82 202 {
Chris@82 203 X(ifree)(sc);
Chris@82 204 }