#include <stdio.h>
#include <time.h>
#include <stdlib.h>
#include <string.h>
#include <assert.h>
typedef struct video_t {
char *title;
char *director;
char *actors;
struct tm showdate;
} video_t;
char *__strdup(const char *s);
void show_movie(video_t **list, int (*condfun)(const void *t));
video_t *new_movie(char *title, char *director, char *actors, struct tm showdate);
int condfunc(const void *t) {
return (((const video_t *)(t))->showdate.tm_mon == 2);
}
int condfunc2(const void *t) {
return (((const video_t *)(t))->showdate.tm_mon == 2 || ((const video_t *)(t))->showdate.tm_mon == 8);
}
int main(void) {
video_t *movie = new_movie("Psycho", "Alfred Hitchcock", "Anthony Perkins", (struct tm){.tm_year = 2021, .tm_mon = 8, .tm_mday = 15});
video_t *movie_list[] = {
new_movie("Batman", "Tim Burton", "Michael Keaton", (struct tm){.tm_year = 2021, .tm_mon = 2, .tm_mday = 23}),
new_movie("Batman returns", "Tim Burton", "Michael Keaton; Denny De Vito", (struct tm){.tm_year = 2021, .tm_mon = 2, .tm_mday = 1}),
new_movie("RoboCop", "Poul Verhoeven", "Peter Weller", (struct tm){.tm_year = 2021, .tm_mon = 3, .tm_mday = 3}),
movie,
NULL,
};
puts("** Without condition **");
show_movie(movie_list, NULL);
puts("\n\n** With condition **");
show_movie(movie_list, condfunc);
puts("\n\n** With condition 2 **");
show_movie(movie_list, condfunc2);
return 0;
}
char * __strdup(const char *s) {
size_t len = strlen(s) + 1;
char *ns = malloc(len);
if (ns == NULL)
return NULL;
return memcpy(ns, s, len);
}
video_t *new_movie(char *title, char *director, char *actors, struct tm showdate) {
video_t *v = malloc(sizeof *v);
v->title = __strdup(title);
v->director = __strdup(director);
v->actors = __strdup(actors);
v->showdate = showdate;
return v;
}
void show_movie(video_t **list, int (*condfun)(const void *t)) {
for (size_t i = 0; list[i]; i++) {
if (condfun != NULL && !condfun(list[i])) {
continue;
}
printf("Title : %s\n", list[i]->title);
printf("Director : %s\n", list[i]->director);
printf("Actors : %s\n", list[i]->actors);
printf("Show Date: %d-%02d-%02d\n", list[i]->showdate.tm_year, list[i]->showdate.tm_mon, list[i]->showdate.tm_mday);
puts("-----");
}
}