Commit eeb33610 authored by Michael Rouse's avatar Michael Rouse

No library for parsing args, dead simple parsing. Logger to help with frequently typed things.

parent e088d8e1
{
"files.associations": {
"*.tests": "c",
"*.driver": "c",
"stdio.h": "c",
"args.h": "c"
}
}
\ No newline at end of file
# time-slime
# Time Slime
A command line implementation of a time sheet
```
// Add 5 hours to the time sheet today
timeslime add 5
// Takes off worked hours from timesheet
timeslime add -2
// Shows time sheet for a period of time
timeslime report 8/15/2018 8/31/2018
// Add hours to a specific date
timeslime add 5 8/20/2018
// Clock in and clock out
timeslime clock in
timeslime clock out
```
\ No newline at end of file
......@@ -5,7 +5,7 @@ LANGUAGE_FLAGS=-x c
OUT=timeslime
# Files to compile (for all targets)
SOURCES=./src/timeslime.c
SOURCES=./src/*.c ./src/args/*.c
......
#include "args.h"
timeslime_args parse_args(int argc, char **argv)
{
// Copy the arguments
char **args = NULL;
args = (char**)malloc(sizeof(char*) * (argc + 1));
int i;
for (i = 1; i < argc; i++)
args[i - 1] = argv[i];
int num_args = argc - 1; // Adjust because the filename was dropped
// Create parsed args result and set defaults
timeslime_args result;
result.help = False;
result.error = 0;
result.action = NULL;
result.modifier1 = NULL;
result.modifier2 = NULL;
result.modifier3 = NULL;
if (num_args > 0)
{
result.action = args[0];
if (strcmp(HELP_ARG, args[0]) == 0)
{
result.help = True;
return result; // Nothing else matters
}
if (num_args > 1)
result.modifier1 = args[1];
if (num_args > 2)
result.modifier2 = args[2];
if (num_args > 3)
result.modifier3 = args[3];
}
return result;
}
\ No newline at end of file
#ifndef __ARGS_H__
#define __ARGS_H__
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
/* Constants */
#define HELP_ARG "help"
#define False 0
#define True 1
typedef char bool;
typedef struct _timeslime_args_t {
bool help;
char *action;
char *modifier1;
char *modifier2;
char *modifier3;
int error;
} timeslime_args;
/* Parse the command line arguments */
timeslime_args parse_args(int argc, char **argv);
#endif
\ No newline at end of file
/**
* File with directives that represent commands and such
*/
#ifndef __COMMANDS_H__
#define __COMMANDS_H__
/* ACTIONS */
#define ADD_ACTION "add"
#define CLOCK_ACTION "clock"
/* Misc. */
#define TODAY "TODAY"
#endif
\ No newline at end of file
/**
* Wrapper for logging
*/
#ifndef __LOGGER_H__
#define __LOGGER_H__
#include <stdarg.h>
#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");
#endif
\ No newline at end of file
......@@ -3,13 +3,60 @@
*
* Authors: Michael Rouse
*/
#include <stdio.h>
#include "../include/timeslime.h"
#include "timeslime.h"
int main(void)
/**
* Entry point for program, parsed arguments and
* acts according to the arguments
*/
int main(int argc, char *argv[])
{
printf("Hello World!");
log_dull("==== Time Slime ====\n");
timeslime_args parsed_args;
parsed_args = parse_args(argc, argv);
if (parsed_args.help)
printf("Helping\n");
else
{
if (strcmp(ADD_ACTION, parsed_args.action) == 0)
perform_add_action(parsed_args);
}
//double x = atof("-4");
//printf("%f\n", x);
printf("\n");
return 0;
}
\ No newline at end of file
}
/**
* Add to the time sheet
*/
void perform_add_action(timeslime_args args)
{
if (args.modifier1 == NULL)
{
printf("ERROR: 'add' action needs at least a second parameter.\n");
return;
}
// Parse argument to determine how long to add
float toAdd;
toAdd = atof(args.modifier1);
// 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);
}
......@@ -6,6 +6,15 @@
#ifndef __TIME_SLIME_H__
#define __TIME_SLIME_H__
#include <stdlib.h>
#include <string.h>
#include "args/args.h"
#include "logger.h"
#include "commands.h"
/* Add to the time sheet */
void perform_add_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