-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcountmmproofs.c
74 lines (60 loc) · 1.99 KB
/
countmmproofs.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
#include <stdio.h>
int counter_in_file(FILE *fp, char *key) {
if ((fp == NULL) || (key == NULL)) {
return -1; // error code I guess
}
if (*key == '\0') {
return -2; // another error code
}
char *ptr_key = key;
int key_count = 0;
char to_check, to_satisfy;
to_check = fgetc(fp);
while (to_check != EOF) {
to_satisfy = *ptr_key;
if (to_check == to_satisfy) {
ptr_key += 1; // advance the pointer (to next char)
} else {
ptr_key = key; // reset key scanning (match fail in middle)
}
if (*ptr_key == '\0') {
key_count += 1;
ptr_key = key;
}
to_check = fgetc(fp); // advance to next char
}
return key_count;
}
int main(int args, char **kwargs) {
// needs exactly one non-executable-name argument: the file name
// therefore args must be 2
// for now, anything other than the first file is ignored
// (in the future, multiple databases could be counted,
// where the arguments are treated as .mm file names,
// then really using a for loop to count for each file)
// <= one arg -> only executable name -> zero file names given
if (args <= 1) {
puts("Please specify file name");
puts("(pass it in while calling the executable)");
printf("Syntax: %s the_metamath_database.mm\n", kwargs[0]);
return 0;
}
// otherwise, at least one file -> read the first file
char *file_name = kwargs[1];
FILE *fp = fopen(file_name, "r");
// if file not found, error
if (fp == NULL) {
printf("Error: file \"");
printf("%s", file_name);
printf("\" not found\n");
return 0;
}
puts("Counting...");
int proofs_count = counter_in_file(fp, "$=");
printf("Found %d occurrences of $=\n", proofs_count);
printf("(Over)estimate of number of proofs in \"");
printf("%s", file_name);
printf("\": %d\n", proofs_count);
fclose(fp);
return 0;
}