Compare commits

..

No commits in common. "db2751214314515a5659b0fbc21b3d1e03b78e80" and "72127b505937410f9b00d3c649a85bde18593bfb" have entirely different histories.

7 changed files with 32 additions and 36 deletions

View file

@ -10,7 +10,8 @@ LDLIBS=
PREFIX=/usr/local
SRC = src/journal/main.o \
src/journal/journal.o
src/journal/journal.o \
src/journal/emotion.o
all: ensure_bin journal
@ -29,6 +30,7 @@ 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:

11
src/journal/emotion.c Normal file
View file

@ -0,0 +1,11 @@
#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;
}

8
src/journal/emotion.h Normal file
View file

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

View file

View file

@ -1,11 +1,8 @@
// 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)
{
@ -39,19 +36,3 @@ 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,7 +5,6 @@
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,6 +2,7 @@
#include <string.h>
#include <stdlib.h>
#include "emotion.h"
#include "journal.h"
#define TOPICS 10
@ -59,19 +60,16 @@ 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*) = {
NULL,
handle_emotion,
};
// 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;
}
@ -86,18 +84,15 @@ int main(int argc, char** argv)
FILE* journal_file = journal_open(topic);
char *handler_message = extract_handler_msg(argc, argv);
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);
}
printf("'%s'\n", 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");
free(handler_message);
printf("done\n");
return 0;
}
}