-
Notifications
You must be signed in to change notification settings - Fork 0
/
principals.c
132 lines (100 loc) · 2.95 KB
/
principals.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
#include "principals.h"
int getNumValidPrincipals(char* fname)
{
FILE* infile = NULL;
int numFound = 0;
char line[MAX_LINE_LEN];
char* category = NULL;
int categoryLen = 0;
infile = fopen(fname, "r");
while(fgets(line, MAX_LINE_LEN, infile))
{
category = cpycol(category, line, &categoryLen, 4, '\t');
if (strstr(category, "actor") || strstr(category, "actress"))
{
numFound++;
}
}
fclose(infile);
if (category != NULL)
{
free(category);
}
return numFound;
}
struct title_principals* constructTitlePrincipalsArr(char* fname, int *arrLen)
{
struct title_principals* newArr;
FILE* infile;
char line[MAX_LINE_LEN];
int x = 0;
char* category = NULL;
int categoryLen= 0;
*arrLen = getNumValidPrincipals(fname);
newArr = malloc(sizeof(struct title_principals) * *arrLen);
infile = fopen(fname, "r");
while (fgets(line, MAX_LINE_LEN, infile))
{
category = cpycol(category, line, &categoryLen, 4, '\t');
if (strstr(category, "actor") || strstr(category, "actress"))
{
char* tconst = NULL;
int tconstLen = 0;
char* nconst = NULL;
int nconstLen = 0;
char* characters = NULL;
int charactersLen = 0;
tconst = cpycol(tconst, line, &tconstLen, 1, '\t');
nconst = cpycol(nconst, line, &nconstLen, 3, '\t');
characters = cpycol(characters, line, &charactersLen, 6, '\t');
newArr[x].nconst = nconst;
newArr[x].tconst = tconst;
newArr[x].characters = characters;
x++;
}
}
fclose(infile);
if (category != NULL)
{
free(category);
}
return newArr;
}
void freeTitlePrincipalsArr(struct title_principals* arr, int arrLen)
{
int x;
for (x = 0; x < arrLen; x++)
{
free(arr[x].characters);
free(arr[x].nconst);
free(arr[x].tconst);
}
free(arr);
}
void printTP(void* toPrint)
{
printf("%s, %s, %s\n", ((struct title_principals*)(toPrint))->tconst,
((struct title_principals*)(toPrint))->nconst,
((struct title_principals*)(toPrint))->characters);
return;
}
int comparePrincipalsTConst(const void* a, const void* b)
{
return strcmp (((struct title_principals*)(a))->tconst, ((struct title_principals*)(b))->tconst);
}
int comparePrincipalsNConst(const void* a, const void* b)
{
return strcmp (((struct title_principals*)(a))->nconst, ((struct title_principals*)(b))->nconst);
}
void* TPDeref(void* p)
{
return *((struct title_principals**)(p));
}
int comparePTCptr(const void* a, const void* b)
{
return comparePrincipalsTConst(*((struct title_principals**)(a)), *((struct title_principals**)(b)));
}
int comparePNCptr (const void* a, const void* b)
{
return comparePrincipalsNConst(*((struct title_principals**)(a)), *((struct title_principals**)(b)));
}