-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmovie.c
More file actions
91 lines (76 loc) · 2.04 KB
/
movie.c
File metadata and controls
91 lines (76 loc) · 2.04 KB
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
#include <assert.h>
#include <stdlib.h>
#include <string.h>
#include "movie.h"
#if defined(WIN32) && !defined(STRNDUP_IMPL)
// god why oh god why god why oh god why why why god oh why god why god why why god
static char *strndup(const char *s, size_t len)
{
char *copy = malloc(len + 1);
if (NULL == copy)
return NULL;
size_t i = 0;
for (; i < len && s[i] != '\0'; i++)
copy[i] = s[i];
copy[i] = '\0';
return copy;
}
#endif
struct movie * movie_find_or_create(struct movie **match, char *symbol, int len)
{
struct movie **last = match;
while (*last != NULL && strncmp((*last)->symbol, symbol, len))
last = &(*last)->next;
if (NULL == *last)
{
*last = malloc(sizeof (**last));
if (NULL == *last)
return NULL;
if (NULL == ((*last)->symbol = strndup(symbol, len)))
{
free(*last);
*last = NULL;
return NULL;
}
(*last)->year = 0;
(*last)->aut = 0;
(*last)->name = NULL;
(*last)->next = NULL;
(*last)->defined = false;
}
return *last;
}
void movies_free(struct movies *em)
{
free(em->title);
em->title = NULL;
struct element *curr = em->elements;
while (curr != NULL)
{
switch (curr->type)
{
case TEXT: free(curr->text ); break;
case HEADING: free(curr->heading.text); break;
case TITLE: assert(!"Impossible"); break;
case PP: /* Do nothing. */ break;
case MOV: /* Do nothing. */ break;
default: assert(!"no no no n"); break;
}
struct element *next = curr->next;
free(curr);
curr = next;
}
// same drill
struct movie *curr2 = em->mov_first;
while (curr2 != NULL)
{
free(curr2->symbol);
free(curr2->name);
struct movie *next = curr2->next;
free(curr2);
curr2 = next;
}
em->mov_first = NULL;
em->elements = NULL;
em->last = &em->elements;
}