Commit 0c80cd80 authored by Michael Rouse's avatar Michael Rouse

More features from the command line

parent d7a09e07
#include "args.h"
#include "../string_literals.h"
#include "../logger.h"
timeslime_args parse_args(int argc, char **argv)
args_t args_parse(int argc, char **argv)
{
// Copy the arguments
char **args = NULL;
......@@ -13,8 +14,8 @@ timeslime_args parse_args(int argc, char **argv)
int num_args = argc - 1; // Adjust because the filename was dropped
// Create parsed args result and set defaults
timeslime_args result;
result.help = False;
args_t result;
result.help = ARGS_False;
result.error = 0;
result.action = NULL;
result.modifier1 = NULL;
......@@ -26,7 +27,7 @@ timeslime_args parse_args(int argc, char **argv)
result.action = args[0];
if (strcmp(HELP_ACTION, args[0]) == 0)
{
result.help = True;
result.help = ARGS_True;
return result; // Nothing else matters
}
......@@ -48,12 +49,12 @@ timeslime_args parse_args(int argc, char **argv)
#define DAY 2
/* Parse a date */
timeslime_date parse_date(char *dateStr)
date_t args_parse_date(char *dateStr)
{
log_debug("Parsing Date: %s", dateStr);
timeslime_date date;
date.error = False;
date_t date;
date.error = ARGS_False;
date.month = 0;
date.day = 0;
date.year = 0;
......@@ -76,7 +77,7 @@ timeslime_date parse_date(char *dateStr)
if (tmp < 1000)
{
log_error("Invalid year, %d; dates must be in the format YYYY/MM/DD", tmp);
date.error = True;
date.error = ARGS_True;
return date;
}
......@@ -88,7 +89,7 @@ timeslime_date parse_date(char *dateStr)
if (tmp < 1 || tmp > 12)
{
log_error("Invalid month, %d; dates must be in the format YYYY/MM/DD", tmp);
date.error = True;
date.error = ARGS_True;
return date;
}
......@@ -100,7 +101,7 @@ timeslime_date parse_date(char *dateStr)
if (tmp < 1 || tmp > 31)
{
log_error("Invalid day, %d; dates must be in the format YYYY/MM/DD", tmp);
date.error = True;
date.error = ARGS_True;
return date;
}
......
......@@ -4,36 +4,33 @@
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "../logger.h"
#include "../string_literals.h"
#define ARGS_False 0
#define ARGS_True 1
typedef char args_bool_t;
#define False 0
#define True 1
typedef char bool;
typedef struct _timeslime_args_t {
bool help;
typedef struct _args_t {
args_bool_t help;
char *action;
char *modifier1;
char *modifier2;
char *modifier3;
int error;
} timeslime_args;
} args_t;
typedef struct _timeslime_date_t {
bool error;
typedef struct _date_t {
args_bool_t error;
int month;
int day;
int year;
char str[256];
} timeslime_date;
} date_t;
/* Parse the command line arguments */
timeslime_args parse_args(int argc, char **argv);
args_t args_parse(int argc, char **argv);
/* Parse into a date */
timeslime_date parse_date(char *date);
date_t args_parse_date(char *date);
#endif
\ No newline at end of file
......@@ -4,66 +4,61 @@
#include "../timeslime.h"
#include "logger.h"
#include "args/args.h"
#include "string_literals.h"
static TIMESLIME_STATUS_t status;
static void perform_add_action(args_t args);
static void perform_clock_action(args_t args);
/**
* Entry point to the command line interface for Time Slime
*/
int main(int argc, char *argv[])
{
TimeSlime_Initialize("build");
return 0;
}
/**
* Entry point for program, parsed arguments and
* acts according to the arguments
*
int main2(int argc, char *argv[])
{
int i;
for (i = 0; i < argc; i++)
log_dull(argv[i]);
log_dull("==== Time Slime ====\n")
setup_database();
status = TimeSlime_Initialize("build");
if (status != TIMESLIME_OK)
{
printf("An error occured: %d\n", status);
return -1;
}
timeslime_args parsed_args;
parsed_args = parse_args(argc, argv);
// Parse command line arguments
args_t parsed_args;
parsed_args = args_parse(argc, argv);
if (parsed_args.help)
{
display_help();
// display_help();
}
else
{
else {
if (strcmp(parsed_args.action, ADD_ACTION) == 0)
perform_add_action(parsed_args);
else if (strcmp(parsed_args.action, CLOCK_ACTION) == 0)
perform_clock_action(parsed_args);
else if (strcmp(parsed_args.action, REPORT_ACTION) == 0)
perform_report_action(parsed_args);
//else if (strcmp(parsed_args.action, REPORT_ACTION) == 0)
// perform_report_action(parsed_args);
if (status != TIMESLIME_OK)
{
printf("Error: %d\n", status);
}
}
sqlite3_close(db);
printf("\n");
TimeSlime_Close();
return 0;
}
/**
* Add to the time sheet
*
void perform_add_action(timeslime_args args)
*/
static void perform_add_action(args_t args)
{
if (args.modifier1 == NULL)
{
......@@ -89,11 +84,23 @@ void perform_add_action(timeslime_args args)
// Show message for what is about to happen
log_info("Adding %.2f hour(s) to the time sheet for %s", toAdd, date);
}
if (strcmp(date, TODAY) == 0)
{
status = TimeSlime_AddHours(toAdd, TIMESLIME_DATE_NOW);
}
else {
date_t parsed = args_parse_date(date);
if (!parsed.error)
{
status = TimeSlime_AddHours(toAdd, parsed.year, parsed.month, parsed.day);
}
}
}
/* Clock in and clock out of the time sheet *
void perform_clock_action(timeslime_args args)
/* Clock in and clock out of the time sheet */
static void perform_clock_action(args_t args)
{
if (args.modifier1 == NULL)
{
......@@ -111,6 +118,14 @@ void perform_clock_action(timeslime_args args)
// Show message for what is about to happen
log_info("Clocking %s", direction);
if (strcmp(direction, CLOCK_IN) == 0)
{
status = TimeSlime_ClockIn(TIMESLIME_CLOCK_IN_NOW);
}
else {
status = TimeSlime_ClockOut(TIMESLIME_CLOCK_OUT_NOW);
}
}
/* Show all time worked *
......
......@@ -20,7 +20,7 @@
/* Misc. */
#define TODAY "Today"
#define TODAY "today"
#define CLOCK_IN "in"
#define CLOCK_OUT "out"
......
......@@ -53,14 +53,18 @@ TIMESLIME_STATUS_t TimeSlime_Initialize(char directory_for_database[])
sprintf(database_file_path, "%s\\%s", directory_for_database, "timeslime.db"); // Append the file name
// Create database if it doesn't exist
int rc = sqlite3_open(database_file_path, &db);
int rc;
rc = sqlite3_open(database_file_path, &db);
if (rc != SQLITE_OK)
{
return TIMESLIME_SQLITE_ERROR;
}
// Initialize the results array
database_results = malloc(TIMESLIME_DEFAULT_RESULT_LIMIT * sizeof(TIMESLIME_INTERNAL_ROW_t*));
if (database_results == NULL)
return TIMESLIME_UNKOWN_ERROR;
result_array_size = TIMESLIME_DEFAULT_RESULT_LIMIT;
int i;
for (i = 0; i < result_array_size; i++)
......@@ -75,8 +79,15 @@ TIMESLIME_STATUS_t TimeSlime_Initialize(char directory_for_database[])
*/
TIMESLIME_STATUS_t TimeSlime_Close(void)
{
int rc;
if (db != NULL)
sqlite3_close(db);
{
rc = sqlite3_close(db);
if (rc != SQLITE_OK)
{
printf("SQLITE CLOSING ERROR: %d\n", rc);
}
}
if (database_file_path != NULL)
{
......@@ -214,7 +225,7 @@ static TIMESLIME_STATUS_t _TimeSlime_CreateTables(void)
char *sql;
// Create time sheet table
sql = "CREATE TABLE TimeSheet(" \
sql = "CREATE TABLE IF NOT EXISTS TimeSheet(" \
"ID INTEGER PRIMARY KEY AUTOINCREMENT," \
"HoursAdded REAL NOT NULL DEFAULT 0," \
"HoursAddedDate DATE DEFAULT NULL," \
......@@ -223,9 +234,9 @@ static TIMESLIME_STATUS_t _TimeSlime_CreateTables(void)
"CreationTime DATETIME DEFAULT (DATETIME('now', 'localtime')), " \
"LastUpdateTime DATETIME DEFAULT (DATETIME('now', 'localtime'))" \
"); " \
"CREATE INDEX HoursAdded_Index ON TimeSheet (HoursAddedDate);" \
"CREATE INDEX ClockIn_Index ON TimeSheet (ClockInTime);" \
"CREATE INDEX ClockOut_Index ON TimeSheet (ClockOutTime);";
"CREATE INDEX IF NOT EXISTS HoursAdded_Index ON TimeSheet (HoursAddedDate);" \
"CREATE INDEX IF NOT EXISTS ClockIn_Index ON TimeSheet (ClockInTime);" \
"CREATE INDEX IF NOT EXISTS ClockOut_Index ON TimeSheet (ClockOutTime);";
rc = sqlite3_exec(db, sql, NULL, 0, &errMsg);
if (rc != SQLITE_OK)
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment