Commit e28c63da authored by Michael Rouse's avatar Michael Rouse

Finishing up passing structure, parsing dates now.

parent eeb33610
......@@ -11,3 +11,6 @@ SOURCES=./src/*.c ./src/args/*.c
all:
$(CC) $(CFLAGS) $(LANGUAGE_FLAGS) $(SOURCES) -o $(OUT)
debug:
$(CC) $(CFLAGS) -D DEBUG $(LANGUAGE_FLAGS) $(SOURCES) -o $(OUT)
\ No newline at end of file
......@@ -40,4 +40,89 @@ timeslime_args parse_args(int argc, char **argv)
return result;
}
// Sections
#define YEAR 0
#define MONTH 1
#define DAY 2
/* Parse a date */
timeslime_date parse_date(char *dateStr)
{
log_debug("Parsing Date: %s", dateStr);
timeslime_date date;
date.error = False;
date.month = 0;
date.day = 0;
date.year = 0;
strcpy(date.str, dateStr);
char running[256] = "";
int tmp = 0;
int section = YEAR;
int i;
for (i = 0; i <= strlen(dateStr); i++)
{
// - and / are valid separators
if (dateStr[i] == '/' || dateStr[i] == '\0')
{
log_debug("Found Date Separator: %c, read: %s", dateStr[i], running);
tmp = atoi(running);
if (section == YEAR)
{
if (tmp < 1000)
{
log_error("Invalid year, %d", tmp);
date.error = True;
return date;
}
date.year = tmp;
log_debug("Year: %d", date.year);
}
else if (section == MONTH)
{
if (tmp < 1 || tmp > 12)
{
log_error("Invalid month, %d", tmp);
date.error = True;
return date;
}
date.month = tmp;
log_debug("Month: %d", date.month);
}
else if (section == DAY)
{
if (tmp < 1 || tmp > 31)
{
log_error("Invalid day, %d", tmp);
date.error = True;
return date;
}
date.day = tmp;
log_debug("Day: %d", date.day);
break;
}
section++;
running[0] = '\0';
}
else {
// Add to running string
int len = strlen(running);
running[len] = dateStr[i];
running[len + 1] = '\0';
}
}
#ifdef DEBUG
log_dull("%c", '\0');
#endif
return date;
}
\ No newline at end of file
......@@ -4,6 +4,7 @@
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "../logger.h"
/* Constants */
#define HELP_ARG "help"
......@@ -23,8 +24,18 @@ typedef struct _timeslime_args_t {
int error;
} timeslime_args;
typedef struct _timeslime_date_t {
bool error;
int month;
int day;
int year;
char str[256];
} timeslime_date;
/* Parse the command line arguments */
timeslime_args parse_args(int argc, char **argv);
/* Parse into a date */
timeslime_date parse_date(char *date);
#endif
\ No newline at end of file
......@@ -6,12 +6,15 @@
/* ACTIONS */
#define ADD_ACTION "add"
#define CLOCK_ACTION "clock"
#define ADD_ACTION "add"
#define CLOCK_ACTION "clock"
#define REPORT_ACTION "report"
/* Misc. */
#define TODAY "TODAY"
#define TODAY "Today"
#define CLOCK_IN "in"
#define CLOCK_OUT "out"
#endif
\ No newline at end of file
......@@ -8,24 +8,37 @@
#include <stdio.h>
#define log_dull(format, args...) \
printf(format, ## args); \
printf("\n");
#define log_info(format, args...) \
printf("INFO: "); \
printf(format, ## args); \
printf("\n")
#define log_warning(format, args...) \
printf("WARNING: "); \
printf(format, ## args); \
printf("\n");
#define log_error(format, args...) \
printf("ERROR: "); \
printf(format, ## args); \
printf("\n");
#define log_dull(format, args...) { \
printf(format, ## args); \
printf("\n"); \
}
#define log_info(format, args...) { \
printf("INFO: "); \
printf(format, ## args); \
printf("\n"); \
}
#define log_warning(format, args...) { \
printf("WARNING: "); \
printf(format, ## args); \
printf("\n"); \
}
#define log_error(format, args...) { \
printf("ERROR: "); \
printf(format, ## args); \
printf("\n"); \
}
#ifdef DEBUG
#define log_debug(format, args...) { \
printf("DEBUG: "); \
printf(format, ## args); \
printf("\n"); \
}
#else
#define log_debug(format, args...) { }
#endif
#endif
\ No newline at end of file
......@@ -12,7 +12,7 @@
*/
int main(int argc, char *argv[])
{
log_dull("==== Time Slime ====\n");
log_dull("==== Time Slime ====\n")
timeslime_args parsed_args;
parsed_args = parse_args(argc, argv);
......@@ -21,14 +21,17 @@ int main(int argc, char *argv[])
printf("Helping\n");
else
{
if (strcmp(ADD_ACTION, parsed_args.action) == 0)
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);
}
//double x = atof("-4");
//printf("%f\n", x);
printf("\n");
return 0;
}
......@@ -41,7 +44,7 @@ void perform_add_action(timeslime_args args)
{
if (args.modifier1 == NULL)
{
printf("ERROR: 'add' action needs at least a second parameter.\n");
log_error("'add' action needs at least a second parameter.")
return;
}
......@@ -49,14 +52,78 @@ void perform_add_action(timeslime_args args)
float toAdd;
toAdd = atof(args.modifier1);
// Verify range of time to add
if (toAdd == 0.0)
{
log_error("Invalid amount of time to add to timesheet. Must be > 0 or < 0");
return;
}
// Get the date to add onto the time sheet
char *date = TODAY;
if (args.modifier2 != NULL)
date = args.modifier2;
// Show message for what is about to happen
log_info("Adding %.2f hours to the time sheet for %s. \n", toAdd, date);
log_info("Adding %.2f hour(s) to the time sheet for %s", toAdd, date);
}
/* Clock in and clock out of the time sheet */
void perform_clock_action(timeslime_args args)
{
if (args.modifier1 == NULL)
{
log_error("'clock' action needs an additional parameter: 'in' or 'out'")
return;
}
char *direction = args.modifier1;
if (strcmp(direction, CLOCK_IN) != 0 && strcmp(direction, CLOCK_OUT) != 0)
{
log_error("Invalid parameter on 'clock' action, must be 'in' or 'out'");
return;
}
// Show message for what is about to happen
log_info("Clocking %s", direction);
}
/* Show all time worked */
void perform_report_action(timeslime_args args)
{
if (args.modifier1 == NULL)
{
log_error("'report' action needs another paramter, the start date");
return;
}
if (args.modifier2 == NULL)
{
log_error("'report' action needs another parameter, the end date");
return;
}
timeslime_date startDate = parse_date(args.modifier1);
if (startDate.error)
return;
timeslime_date endDate = parse_date(args.modifier2);
if (endDate.error)
return;
// Verify that dates are in the correct order
if (
(endDate.month < startDate.month && endDate.year == startDate.year) || // Same year, but earlier month
(endDate.day < startDate.day && endDate.year == startDate.year && endDate.month == startDate.month) || // Same month and year, but earlier day
(endDate.year < startDate.year) // Earlier year
)
{
log_error("Dates in wrong order");
return;
}
log_info("Running report between %s and %s", startDate.str, endDate.str);
}
\ No newline at end of file
......@@ -17,4 +17,10 @@
/* Add to the time sheet */
void perform_add_action(timeslime_args args);
/* Clock In or Out */
void perform_clock_action(timeslime_args args);
/* Show time worked for a period of time */
void perform_report_action(timeslime_args args);
#endif
\ No newline at end of file
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