-
Notifications
You must be signed in to change notification settings - Fork 0
/
drafts_countmmproofs.c
168 lines (138 loc) · 4.08 KB
/
drafts_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
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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
// drafts_countmmproofs.c
// created Saturday, 16 November 2024
// by BismaBRJ
// https://github.com/BismaBRJ/countmmproofs
// last updated Saturday, 16 November 2024
#include <stdio.h>
int counter_in_str(char *text, char *key) {
if ((text == NULL) || (key == NULL)) { // points to nowhere
return -1; // "error", I guess
}
if (*key == '\0') { // annoying edge case: key is empty string
return -1; // could be infinity in theory, perhaps
}
char *ptr_text = text;
char *ptr_key = key;
int key_count = 0;
char to_check, to_satisfy;
while (*ptr_text != '\0') {
to_check = *ptr_text; // dereference
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;
}
ptr_text += 1; // advance the pointer (to next char)
}
return key_count;
}
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;
}
void test_1() {
int thing = counter_in_str("abcabcabca", "bc");
printf("count: %d\n", thing);
}
void test_2() {
FILE *fp;
fp = fopen("./smalltest.txt", "r");
char cur_char;
cur_char = fgetc(fp);
while (cur_char != EOF) {
printf("%c", cur_char);
cur_char = fgetc(fp);
}
fclose(fp);
}
void test_3() {
FILE *fp;
fp = fopen("./smalltest.txt", "r");
puts("Result:");
int howmany = counter_in_file(fp, "$=");
fclose(fp);
printf("Count: %d\n", howmany);
puts("Done!");
}
void test_4() {
FILE *fp;
fp = fopen("./set.mm", "r");
puts("Result:");
int howmany = counter_in_file(fp, "$=");
fclose(fp);
printf("Count: %d\n", howmany);
puts("Done!");
}
/* drafts driver code:
int main() {
// test_1();
// test_2();
// test_3();
test_4();
// post running note: took four seconds. Result: 43565
// Using file set.mm per 16 November 2024
// of commit:
// github.com/metamath/set.mm/commit/2813f8eb3a62e829c4c7afea58513c2d77d4c98f
return 0;
}
*/
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;
}