add a default handler for topics that want it

This commit is contained in:
Luna 2019-04-30 02:20:32 -03:00
parent 6d80249659
commit db27512143
4 changed files with 15 additions and 29 deletions

View file

@ -10,8 +10,7 @@ LDLIBS=
PREFIX=/usr/local
SRC = src/journal/main.o \
src/journal/journal.o \
src/journal/emotion.o
src/journal/journal.o
all: ensure_bin journal
@ -30,7 +29,6 @@ journal: $(SRC)
$(CC) $(LDFLAGS) -o bin/journal $(SRC) $(LDLIBS)
src/journal/journal.o: src/journal/journal.c
src/journal/emotion.o: src/journal/emotion.c
src/journal/main.o: src/journal/main.c
clean:

View file

@ -1,9 +0,0 @@
#include <stdio.h>
#include "journal.h"
void handle_emotion(FILE *journal, char* msg)
{
journal_write_topic(journal, "emotion", msg);
return;
}

View file

@ -1,8 +0,0 @@
#ifndef __EMOTION_H__
#define __EMOTION_H__
#include <stdio.h>
void handle_emotion(FILE*, char*);
#endif

View file

@ -2,7 +2,6 @@
#include <string.h>
#include <stdlib.h>
#include "emotion.h"
#include "journal.h"
#define TOPICS 10
@ -60,16 +59,19 @@ int main(int argc, char** argv)
"emotion"
};
// default handling by journal_write_topic is marked
// as the NULL values in this array.
void (*handlers[])(FILE*, char*) = {
handle_emotion,
NULL,
};
// list all topics when arg1 is list
if(strcmp(topic, "list") == 0)
{
// list all topics
for(int i = 0; topics[i] != NULL; i++)
printf("%s ", topics[i]);
printf("\n");
return 0;
}
@ -84,15 +86,18 @@ int main(int argc, char** argv)
FILE* journal_file = journal_open(topic);
char *handler_message = extract_handler_msg(argc, argv);
printf("'%s'\n", handler_message);
printf("[%s] said '%s'\n", topic, handler_message);
// the joined args[2] and beyond come as the
// second argument
if(fun_ptr == NULL) {
journal_write_topic(journal_file, topic, handler_message);
} else {
fun_ptr(journal_file, handler_message);
journal_close(journal_file);
}
journal_close(journal_file);
free(handler_message);
printf("done\n");
printf("done!\n");
return 0;
}
}