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