-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.c
162 lines (139 loc) · 3.24 KB
/
main.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
/*
Author: Ilan Schnell
Example of how to use Python as a C library. We require Python 3.7
*/
#include "Python.h"
/* command line options */
int summary;
/* add `str` to `dict`, and increase its value by 1 (or set its value
to 1 initially) */
static void
add_word(PyObject *dict, char *str)
{
PyObject *value;
long cnt;
value = PyDict_GetItemString(dict, str);
cnt = (value == NULL) ? 0 : PyLong_AsLong(value);
cnt++;
PyDict_SetItemString(dict, str, PyLong_FromLong(cnt));
}
/* process the file pointer, and add all words (separated by whitespace)
to the dictionary */
static int
process_fp(PyObject *dict, FILE *fp)
{
#define BUFFER_SIZE 256
char s[BUFFER_SIZE];
int c, i = 0;
while (1) {
c = fgetc(fp);
if (c == ' ' || c == '\n' || c == '\r' ||
c == '\t' || c == '\v' || c == EOF) {
s[i] = '\0';
if (i)
add_word(dict, s);
if (c == EOF)
break;
i = 0;
} else {
s[i++] = c;
}
if (i >= BUFFER_SIZE) {
fprintf(stderr, "Error: buffer size %d too small.\n",
BUFFER_SIZE);
return -1;
}
}
#undef BUFFER_SIZE
return 0;
}
static int
process_file(PyObject *dict, char *filename)
{
FILE *fp;
fp = fopen(filename, "r");
if (fp == NULL) {
fprintf(stderr, "Error: Can't open `%s` for reading.\n", filename);
return -1;
}
if (process_fp(dict, fp) < 0) {
fclose(fp);
return -1;
}
fclose(fp);
return 0;
}
static void
display(PyObject *dict)
{
PyObject *key, *value;
Py_ssize_t pos = 0;
long i, total = 0;
while (PyDict_Next(dict, &pos, &key, &value)) {
i = PyLong_AsLong(value);
if (!summary)
printf("%s: %ld\n", PyUnicode_AsUTF8(key), i);
total += i;
}
if (summary) {
printf("Unique words: %ld\n", PyDict_Size(dict));
printf("Total words: %ld\n", total);
}
}
static void
help(void)
{
printf("\n\
Calculates the frequency of words in file(s) or stdin.\n\
Options:\n\
-s only display summary instead of word count table\n\
-h display this help and exit\n");
}
/* changes the global variables: table, names, full */
static void
process_options(int argc, char *argv[])
{
int op;
#define USAGE printf("usage: %s [-sh] [file] ...\n", argv[0])
summary = 0;
while ((op = getopt(argc, argv, "hs")) != -1)
switch(op) {
case 'h':
USAGE;
help();
exit(0);
case 's':
summary = 1;
break;
default:
USAGE;
exit(1);
}
#undef USAGE
}
int main(int argc, char *argv[])
{
PyObject *dict;
process_options(argc, argv);
Py_Initialize();
dict = PyDict_New();
if (dict == NULL)
goto error;
if (argc == optind) {
if (process_fp(dict, stdin) < 0)
goto error;
}
else {
while (optind < argc)
if (process_file(dict, argv[optind++]) < 0)
goto error;
}
display(dict);
Py_DECREF(dict);
Py_Finalize();
return 0;
error:
Py_XDECREF(dict);
Py_Finalize();
return 1;
}