Compare commits

...

2 Commits

Author SHA1 Message Date
Luna db27512143 add a default handler for topics that want it 2019-04-30 02:20:32 -03:00
Luna 6d80249659 add nicer formatting to output files 2019-04-30 02:16:19 -03:00
7 changed files with 36 additions and 32 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,11 +0,0 @@
#include <stdio.h>
#include "journal.h"
void handle_emotion(FILE *journal, char* msg)
{
char formatted[512];
sprintf(formatted, "%s\n", msg);
journal_write(journal, formatted);
return;
}

View File

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

0
src/journal/format.h Normal file
View File

View File

@ -1,8 +1,11 @@
// functions to deal with the journal file
#include <stdio.h>
#include <stdlib.h>
#include <sys/stat.h>
#include <string.h>
#include <time.h>
// functions to deal with the journal file
FILE *journal_open(char *topic)
{
@ -36,3 +39,19 @@ void journal_close(FILE* journal_fd)
{
fclose(journal_fd);
}
void journal_write_topic(FILE *journal_fd, char *topic, char *message)
{
char *tstamp = malloc(128 * sizeof(char));
char fmt_msg[512];
time_t rawtime;
time(&rawtime);
const struct tm *cur_time = gmtime(&rawtime);
strftime(tstamp, 128, "%c", cur_time);
sprintf(fmt_msg, "[%s] [%s]: %s\n", tstamp, topic, message);
free(tstamp);
journal_write(journal_fd, fmt_msg);
}

View File

@ -5,6 +5,7 @@
FILE *journal_open(char *topic);
void journal_write(FILE*, char* message);
void journal_write_topic(FILE*, char *topic, char *message);
void journal_close(FILE*);
#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);
if(fun_ptr == NULL) {
journal_write_topic(journal_file, topic, handler_message);
} else {
fun_ptr(journal_file, handler_message);
}
// the joined args[2] and beyond come as the
// second argument
fun_ptr(journal_file, handler_message);
journal_close(journal_file);
free(handler_message);
printf("done\n");
printf("done!\n");
return 0;
}
}