Newer
Older
xmpp-bot / src / expenses / types.h
/* additional types for expenses grammar
 */
#pragma once
#include <stdio.h>
#include <stdbool.h>
#include <unistd.h>
#include <time.h>
#include <glib.h>

typedef enum BotCommand {
    BOT_CMD_NONE,
    BOT_CMD_IN,
    BOT_CMD_OUT,
    BOT_CMD_DELETE,
    BOT_CMD_REPORT,
    BOT_CMD_REMINDER
} BotCommand;

typedef enum BotReportTimespan {
    BOT_TS_DAILY,
    BOT_TS_WEEKLY,
    BOT_TS_MONTHLY,
    BOT_TS_YEARLY
} BotReportTimespan;

typedef struct BotExpenseEntry {
    unsigned int id;
    struct tm time;                /* number of seconds since the epoch, needs `localtime()` + timezone to localize: https://stackoverflow.com/questions/1442116/how-can-i-get-the-date-and-time-values-in-a-c-program */
    double value;               /* positive or negative according to in or out */
    bool _serialized;            /* utility to check if entry has been serialized before freeing */
    GString *user;
    GString *notes;
} BotExpenseEntry;

typedef struct BotReminder {
    GString *from;
    unsigned int minutes;
} BotReminder;

/* Parser definitions */
int yylex();
int yyparse();
int yylex_destroy();
typedef struct yy_buffer_state * YY_BUFFER_STATE;
YY_BUFFER_STATE yy_scan_string(const char * str);

/* Create a BotExpenseEntry with data fed from the parser.  */
BotExpenseEntry *create_bot_expense_entry
(
 const BotExpenseEntry *previous,
 const unsigned int id,
 const BotCommand cmd,
 const double value,
 GString* user,
 GString* notes
);

/* FP should not be null */
GString *serialize_bot_expense_entry_csv
(
  BotExpenseEntry *data
);

/* To be called after it has been serialized */
void free_bot_expense_entry
(
 BotExpenseEntry *entry
);

void free_bot_reminder
(
 BotReminder *reminder
 );

/* use this to check the last received command when responding */
extern BotCommand g_LastCommand;
BotCommand bot_get_last_command();
void bot_set_last_command(BotCommand cmd);

/* store the last expense in memory */
extern BotExpenseEntry *g_LastExpenseEntry;
BotExpenseEntry *bot_get_last_expense();
void bot_set_last_expense(BotExpenseEntry *entry);

/* store the last reminder in memory */
extern BotReminder *g_LastReminder;
BotReminder *bot_get_last_reminder();
void bot_set_last_reminder(BotReminder *reminder);

/* store the filename in memory */
extern GString *g_filename;
GString *bot_get_filename();
void bot_set_filename(GString *fn);