add basic journal file handling

This commit is contained in:
Luna 2019-04-29 00:42:44 -03:00
parent c0cfeaa333
commit 171e315d4e
6 changed files with 73 additions and 9 deletions

View file

@ -9,6 +9,10 @@ LDLIBS=
PREFIX=/usr/local PREFIX=/usr/local
SRC = src/journal/main.o \
src/journal/journal.o \
src/journal/emotion.o
all: ensure_bin journal all: ensure_bin journal
ensure_bin: bin ensure_bin: bin
@ -22,10 +26,10 @@ uninstall:
rm $(DESTDIR)$(PREFIX)/bin/journal rm $(DESTDIR)$(PREFIX)/bin/journal
# currently we only have one journal util, others may come later # currently we only have one journal util, others may come later
journal: src/journal/main.o src/journal/emotion.o journal: $(SRC)
$(CC) $(LDFLAGS) -o bin/journal \ $(CC) $(LDFLAGS) -o bin/journal $(SRC) $(LDLIBS)
src/journal/main.o src/journal/emotion.o $(LDLIBS)
src/journal/journal.o: src/journal/journal.c
src/journal/emotion.o: src/journal/emotion.c src/journal/emotion.o: src/journal/emotion.c
src/journal/main.o: src/journal/main.c src/journal/main.o: src/journal/main.c

View file

@ -1,7 +1,10 @@
#include <stdio.h> #include <stdio.h>
void handle_emotion() #include "journal.h"
void handle_emotion(FILE *journal, char* msg)
{ {
printf("test\n"); // TODO: add newline to msg
journal_write(journal, msg);
return; return;
} }

View file

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

38
src/journal/journal.c Normal file
View file

@ -0,0 +1,38 @@
// functions to deal with the journal file
#include <stdio.h>
#include <stdlib.h>
#include <sys/stat.h>
#include <string.h>
FILE *journal_open(char *topic)
{
char *home_path = getenv("HOME");
// combine w/ home path
char *journal_dir = (char*)malloc(1024);
snprintf(journal_dir, 1024, "%s/.lunabot", home_path);
// construct a 744
mkdir(journal_dir, S_IRWXU | S_IRGRP | S_IROTH);
char *journal_path = (char*)malloc(1024);
snprintf(journal_path, 1024, "%s/%s", journal_dir, topic);
FILE* res = fopen(journal_path, "a");
free(journal_path);
free(journal_dir);
return res;
}
void journal_write(FILE* journal_fd, char* message)
{
// TODO: strlen()?
fwrite(message, strlen(message), 1, journal_fd);
}
void journal_close(FILE* journal_fd)
{
fclose(journal_fd);
}

10
src/journal/journal.h Normal file
View file

@ -0,0 +1,10 @@
#ifndef __JOURNAL_H__
#define __JOURNAL_H__
#include <stdio.h>
FILE *journal_open(char *topic);
void journal_write(FILE*, char* message);
void journal_close(FILE*);
#endif

View file

@ -2,6 +2,7 @@
#include <string.h> #include <string.h>
#include "emotion.h" #include "emotion.h"
#include "journal.h"
#define TOPICS 10 #define TOPICS 10
@ -20,17 +21,23 @@ int main(int argc, char** argv)
"emotion" "emotion"
}; };
void (*handlers[])() = { void (*handlers[])(FILE*, char*) = {
handle_emotion, handle_emotion,
}; };
for(int i = 0; i < TOPICS; i++) for(int i = 0; i < TOPICS; i++)
{ {
const char* cur_topic = topics[i]; const char* cur_topic = topics[i];
if(strcmp(topic, cur_topic) == 0) if(strcmp(topic, cur_topic) == 0)
{ {
void (*fun_ptr)() = handlers[i]; void (*fun_ptr)(FILE*, char*) = handlers[i];
fun_ptr();
FILE* journal_file = journal_open(topic);
fun_ptr(journal_file, "hello world");
journal_close(journal_file);
printf("done\n");
return 0; return 0;
} }
} }