annotate src/fftw-3.3.5/api/import-wisdom-from-file.c @ 127:7867fa7e1b6b

Current fftw source
author Chris Cannam <cannam@all-day-breakfast.com>
date Tue, 18 Oct 2016 13:40:26 +0100
parents
children
rev   line source
cannam@127 1 /*
cannam@127 2 * Copyright (c) 2003, 2007-14 Matteo Frigo
cannam@127 3 * Copyright (c) 2003, 2007-14 Massachusetts Institute of Technology
cannam@127 4 *
cannam@127 5 * This program is free software; you can redistribute it and/or modify
cannam@127 6 * it under the terms of the GNU General Public License as published by
cannam@127 7 * the Free Software Foundation; either version 2 of the License, or
cannam@127 8 * (at your option) any later version.
cannam@127 9 *
cannam@127 10 * This program is distributed in the hope that it will be useful,
cannam@127 11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
cannam@127 12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
cannam@127 13 * GNU General Public License for more details.
cannam@127 14 *
cannam@127 15 * You should have received a copy of the GNU General Public License
cannam@127 16 * along with this program; if not, write to the Free Software
cannam@127 17 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
cannam@127 18 *
cannam@127 19 */
cannam@127 20
cannam@127 21 #include "api.h"
cannam@127 22 #include <stdio.h>
cannam@127 23
cannam@127 24 /* getc()/putc() are *unbelievably* slow on linux. Looks like glibc
cannam@127 25 is grabbing a lock for each call to getc()/putc(), or something
cannam@127 26 like that. You pay the price for these idiotic posix threads
cannam@127 27 whether you use them or not.
cannam@127 28
cannam@127 29 So, we do our own buffering. This completely defeats the purpose
cannam@127 30 of having stdio in the first place, of course.
cannam@127 31 */
cannam@127 32
cannam@127 33 #define BUFSZ 256
cannam@127 34
cannam@127 35 typedef struct {
cannam@127 36 scanner super;
cannam@127 37 FILE *f;
cannam@127 38 char buf[BUFSZ];
cannam@127 39 char *bufr, *bufw;
cannam@127 40 } S;
cannam@127 41
cannam@127 42 static int getchr_file(scanner * sc_)
cannam@127 43 {
cannam@127 44 S *sc = (S *) sc_;
cannam@127 45
cannam@127 46 if (sc->bufr >= sc->bufw) {
cannam@127 47 sc->bufr = sc->buf;
cannam@127 48 sc->bufw = sc->buf + fread(sc->buf, 1, BUFSZ, sc->f);
cannam@127 49 if (sc->bufr >= sc->bufw)
cannam@127 50 return EOF;
cannam@127 51 }
cannam@127 52
cannam@127 53 return *(sc->bufr++);
cannam@127 54 }
cannam@127 55
cannam@127 56 static scanner *mkscanner_file(FILE *f)
cannam@127 57 {
cannam@127 58 S *sc = (S *) X(mkscanner)(sizeof(S), getchr_file);
cannam@127 59 sc->f = f;
cannam@127 60 sc->bufr = sc->bufw = sc->buf;
cannam@127 61 return &sc->super;
cannam@127 62 }
cannam@127 63
cannam@127 64 int X(import_wisdom_from_file)(FILE *input_file)
cannam@127 65 {
cannam@127 66 scanner *s = mkscanner_file(input_file);
cannam@127 67 planner *plnr = X(the_planner)();
cannam@127 68 int ret = plnr->adt->imprt(plnr, s);
cannam@127 69 X(scanner_destroy)(s);
cannam@127 70 return ret;
cannam@127 71 }
cannam@127 72
cannam@127 73 int X(import_wisdom_from_filename)(const char *filename)
cannam@127 74 {
cannam@127 75 FILE *f = fopen(filename, "r");
cannam@127 76 int ret;
cannam@127 77 if (!f) return 0; /* error opening file */
cannam@127 78 ret = X(import_wisdom_from_file)(f);
cannam@127 79 if (fclose(f)) ret = 0; /* error closing file */
cannam@127 80 return ret;
cannam@127 81 }