-
Notifications
You must be signed in to change notification settings - Fork 61
/
Copy pathgenf77tests.c
100 lines (98 loc) · 3.02 KB
/
genf77tests.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
/* generate F77 test sources for MINPACK from the documentation
author: Frederic.Devernay@m4x.org
*/
/* Original awk program was:
/DRIVER FOR [A-Z1]+ EXAMPLE/{
pgm=tolower($4);
oname="t" pgm ".f";
$0 = substr($0,3);
print >oname;
do {
getline; $0 = substr($0,3);
if (!/^ +Page$/) print >>oname;
}
while (!/LAST CARD OF SUBROUTINE FCN/);
getline; $0 = substr($0,3); print >>oname;
getline; $0 = substr($0,3); print >>oname;
}
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <assert.h>
int
main(int argc, char **argv)
{
FILE *f_doc;
char buf[256];
if (argc != 2) {
fprintf(stderr,"Usage: %s path/to/minpack-documentation.txt\n", argv[0]);
exit(2);
}
f_doc = fopen(argv[1], "r");
if (f_doc == NULL) {
perror("Error: cannot open input file");
exit(1);
}
while (fgets(buf, sizeof(buf), f_doc) != NULL)
{
const char *head = "DRIVER FOR";
char *pos;
pos = strstr(buf, head); /* fgets() always returns a NUL-terminated buffer, so there's no buffer over-read risk */
if (pos != NULL) {
FILE *f_src;
/* we found a test source */
char *name, *line;
int i;
int finished = 0;
pos = pos + strlen(head);
name = strdup(pos);
assert(name[0] == ' ');
name[0] = 't';
i = 1;
while(name[i] != 0 && name[i] != ' ') {
if (name[i] >= 'A' && name[i] <= 'Z')
name[i] = name[i] - 'A' + 'a';
i++;
}
assert(name[i] == ' ' && name[i+1] != 0);
name[i] = '.';
name[i+1] = 'f';
name[i+2] = 0;
f_src = fopen(name, "w");
if (f_src == NULL) {
perror("Error: cannot open output file");
exit(1);
}
fputs(buf+2, f_src);
while (!finished && fgets(buf, sizeof(buf), f_doc) != NULL)
{
/* test for page skip */
const char *buf1 = buf;
while(*buf1 != 0 && *buf1 == ' ')
buf1++;
if (*buf1 == 0) {
/* blank line (happens before page skip) */
}
else if(strcmp(buf1, "Page\n") == 0) {
/* page skip */
/* fputs("\n",f_src); */
}
else {
/* test for end of function */
finished = (strstr(buf, "LAST CARD OF SUBROUTINE FCN") != NULL);
fputs(buf+2, f_src);
}
}
assert(finished); /* two more lines */
line = fgets(buf, sizeof(buf), f_doc);
assert(line != NULL);
fputs(buf+2, f_src);
line = fgets(buf, sizeof(buf), f_doc);
assert(line != NULL);
fputs(buf+2, f_src);
fclose(f_src);
}
}
fclose(f_doc);
}