Compare commits
2 commits
72127b5059
...
db27512143
Author | SHA1 | Date | |
---|---|---|---|
db27512143 | |||
6d80249659 |
7 changed files with 36 additions and 32 deletions
4
Makefile
4
Makefile
|
@ -10,8 +10,7 @@ LDLIBS=
|
||||||
PREFIX=/usr/local
|
PREFIX=/usr/local
|
||||||
|
|
||||||
SRC = src/journal/main.o \
|
SRC = src/journal/main.o \
|
||||||
src/journal/journal.o \
|
src/journal/journal.o
|
||||||
src/journal/emotion.o
|
|
||||||
|
|
||||||
all: ensure_bin journal
|
all: ensure_bin journal
|
||||||
|
|
||||||
|
@ -30,7 +29,6 @@ journal: $(SRC)
|
||||||
$(CC) $(LDFLAGS) -o bin/journal $(SRC) $(LDLIBS)
|
$(CC) $(LDFLAGS) -o bin/journal $(SRC) $(LDLIBS)
|
||||||
|
|
||||||
src/journal/journal.o: src/journal/journal.c
|
src/journal/journal.o: src/journal/journal.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
|
||||||
|
|
||||||
clean:
|
clean:
|
||||||
|
|
|
@ -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;
|
|
||||||
}
|
|
|
@ -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
0
src/journal/format.h
Normal file
|
@ -1,8 +1,11 @@
|
||||||
// functions to deal with the journal file
|
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include <sys/stat.h>
|
#include <sys/stat.h>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
|
#include <time.h>
|
||||||
|
|
||||||
|
// functions to deal with the journal file
|
||||||
|
|
||||||
|
|
||||||
FILE *journal_open(char *topic)
|
FILE *journal_open(char *topic)
|
||||||
{
|
{
|
||||||
|
@ -36,3 +39,19 @@ void journal_close(FILE* journal_fd)
|
||||||
{
|
{
|
||||||
fclose(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);
|
||||||
|
}
|
||||||
|
|
|
@ -5,6 +5,7 @@
|
||||||
|
|
||||||
FILE *journal_open(char *topic);
|
FILE *journal_open(char *topic);
|
||||||
void journal_write(FILE*, char* message);
|
void journal_write(FILE*, char* message);
|
||||||
|
void journal_write_topic(FILE*, char *topic, char *message);
|
||||||
void journal_close(FILE*);
|
void journal_close(FILE*);
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|
|
@ -2,7 +2,6 @@
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
|
|
||||||
#include "emotion.h"
|
|
||||||
#include "journal.h"
|
#include "journal.h"
|
||||||
|
|
||||||
#define TOPICS 10
|
#define TOPICS 10
|
||||||
|
@ -60,16 +59,19 @@ int main(int argc, char** argv)
|
||||||
"emotion"
|
"emotion"
|
||||||
};
|
};
|
||||||
|
|
||||||
|
// default handling by journal_write_topic is marked
|
||||||
|
// as the NULL values in this array.
|
||||||
void (*handlers[])(FILE*, char*) = {
|
void (*handlers[])(FILE*, char*) = {
|
||||||
handle_emotion,
|
NULL,
|
||||||
};
|
};
|
||||||
|
|
||||||
|
// list all topics when arg1 is list
|
||||||
if(strcmp(topic, "list") == 0)
|
if(strcmp(topic, "list") == 0)
|
||||||
{
|
{
|
||||||
// list all topics
|
|
||||||
for(int i = 0; topics[i] != NULL; i++)
|
for(int i = 0; topics[i] != NULL; i++)
|
||||||
printf("%s ", topics[i]);
|
printf("%s ", topics[i]);
|
||||||
printf("\n");
|
printf("\n");
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -84,15 +86,18 @@ int main(int argc, char** argv)
|
||||||
FILE* journal_file = journal_open(topic);
|
FILE* journal_file = journal_open(topic);
|
||||||
|
|
||||||
char *handler_message = extract_handler_msg(argc, argv);
|
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);
|
journal_close(journal_file);
|
||||||
|
|
||||||
free(handler_message);
|
free(handler_message);
|
||||||
printf("done\n");
|
|
||||||
|
printf("done!\n");
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue