blob: c69f9aa9a20beec6bd485d078f93664175c00181 (
plain)
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
|
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "counts.h"
counts_t * createCounts(void) {
counts_t * c = malloc(sizeof(* c));
c->countArray = NULL;
c->arraySize = 0;
c->count_of_unknowns = 0;
return c;
}
void addCount(counts_t * c, const char * name) {
if (name == NULL) {
c->count_of_unknowns++;
return;
}
for (int i = 0; i < c->arraySize; i++) {
if (strcmp(name, c->countArray[i].string) == 0) {
c->countArray[i].count++;
return;
}
}
one_count_t * newCount = malloc(sizeof(one_count_t));
newCount->string = malloc((strlen(name) + 1) * sizeof(char));
strcpy(newCount->string, name);
newCount->count = 1;
c->countArray = realloc(c->countArray, (c->arraySize + 1) * sizeof(one_count_t));
c->countArray[c->arraySize] = *newCount;
c->arraySize++;
free(newCount);
}
void printCounts(counts_t * c, FILE * outFile) {
for (int i = 0; i < c->arraySize; i++) {
fprintf(outFile, "%s: %d\n", c->countArray[i].string, c->countArray[i].count);
}
if (c->count_of_unknowns > 0) {
fprintf(outFile, "%s: %d\n", "<unknown> ", c->count_of_unknowns);
}
if(fclose(outFile) != 0) {
fprintf(stderr, "oh no, couldn't close the file!\n");
}
}
void freeCounts(counts_t * c) {
for (int i = 0; i < c->arraySize; i++) {
free(c->countArray[i].string);
}
free(c->countArray);
free(c);
}
|