aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorFranklin Wei <git@fwei.tk>2015-05-21 16:59:07 -0400
committerFranklin Wei <git@fwei.tk>2015-05-21 16:59:07 -0400
commitf5808cddfa783b5f270dca922c84e22800f62038 (patch)
tree0c041e891e358a1ce1e1f7b715a09cd802419725 /src
parent28ff4aa610c6c98645973ed4fc9acb6cc7a0cf30 (diff)
downloadmarket-sim-f5808cddfa783b5f270dca922c84e22800f62038.zip
market-sim-f5808cddfa783b5f270dca922c84e22800f62038.tar.gz
market-sim-f5808cddfa783b5f270dca922c84e22800f62038.tar.bz2
market-sim-f5808cddfa783b5f270dca922c84e22800f62038.tar.xz
command-line arg support
Diffstat (limited to 'src')
-rw-r--r--src/csv.c3
-rw-r--r--src/globals.h19
-rw-r--r--src/help.c23
-rw-r--r--src/history.c4
-rw-r--r--src/load.c16
-rw-r--r--src/main.c10
-rw-r--r--src/util.c56
7 files changed, 115 insertions, 16 deletions
diff --git a/src/csv.c b/src/csv.c
index f1d7ed4..979e97a 100644
--- a/src/csv.c
+++ b/src/csv.c
@@ -1,5 +1,8 @@
#include "globals.h"
+/* takes a pointer to a pointer to a csv string */
+/* the pointer pointed to will be modified, but the data it points to will not */
+
char *csv_read(char **ptr)
{
if(!ptr)
diff --git a/src/globals.h b/src/globals.h
index 5791a69..e01b729 100644
--- a/src/globals.h
+++ b/src/globals.h
@@ -13,6 +13,16 @@
#define ARRAYLEN(x) (sizeof(x) / sizeof(x[0]))
+/* VERSION_INFO is supplied by the compiler */
+#define PROGRAM_VERSION "prerelease (" VERSION_INFO ")"
+
+#define ARG_LOADED (1<<0)
+#define ARG_FAILURE (1<<1)
+#define ARG_VERBOSE (1<<2)
+
+/* don't change this, it will corrupt existing saves */
+#define EPOCH_YEAR 2000
+
typedef unsigned long long ullong;
typedef unsigned long ulong;
typedef unsigned int uint;
@@ -27,7 +37,7 @@ struct money_t {
enum history_action { BUY = 0, SELL };
struct history_time {
- ushort year; /* since 2000 */
+ ushort year; /* since EPOCH_YEAR */
uchar month; /* 0 = jan, 11 = dec */
uchar day;
uchar hour; /* 0-23 */
@@ -87,8 +97,12 @@ void print_history(struct stock_t*);
char *read_ticker(void);
char *read_string(void);
ullong read_int(void);
-void parse_args(int argc, char *argv[]);
+void print_usage(int argc, char *argv[]);
+void print_version(void);
+
+uint parse_args(struct player_t*, int argc, char *argv[]);
char *csv_read(char**);
+void load_portfolio(struct player_t*, const char*);
void buy_handler(struct player_t*);
void sell_handler(struct player_t*);
@@ -98,7 +112,6 @@ void save_handler(struct player_t*);
void load_handler(struct player_t*);
void quit_handler(struct player_t*);
void print_handler(struct player_t*);
-void help_handler(struct player_t*);
#ifndef NDEBUG
diff --git a/src/help.c b/src/help.c
index d4ce8e4..58e46c9 100644
--- a/src/help.c
+++ b/src/help.c
@@ -1,6 +1,25 @@
#include "globals.h"
-void help_handler(struct player_t *player)
+void print_usage(int argc, char *argv[])
{
- printf("not implemented\n");
+ assert(argc > 1);
+
+ printf("Usage: %s [OPTION] [PORTFOLIO]\n", argv[0]);
+ printf("Runs a simulated trading session with PORTFOLIO (creating a new one by default).\n\n");
+
+ printf("Options:\n");
+ printf(" -h, --help\tShow this help and exit\n");
+ printf(" -v, --verbose\tEnable verbose operation\n");
+ printf(" --version\tOutput version information and exit\n");
+}
+
+void print_version(void)
+{
+ printf("market-sim " PROGRAM_VERSION "\n");
+ printf("Copyright (C) 2015 Franklin Wei.\n\n");
+ printf("License GPLv2: GNU GPL version 2 <http://www.gnu.org/licenses/gpl-2.0.html>\n");
+ printf("This program is distributed in the hope that it will be useful, but WITHOUT ANY\n");
+ printf("WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A\n");
+ printf("PARTICULAR PURPOSE.\n");
+ printf("See the GNU General Public License version 2 for more details.\n");
}
diff --git a/src/history.c b/src/history.c
index 324633b..bcb63ae 100644
--- a/src/history.c
+++ b/src/history.c
@@ -12,7 +12,7 @@ void add_hist(struct stock_t *stock, enum history_action action, ullong count)
time_t timer = time(0);
struct tm *tm = localtime(&timer);
- newhist->action_time.year = tm->tm_year - 100;
+ newhist->action_time.year = tm->tm_year - (EPOCH_YEAR - 1900);
newhist->action_time.month = tm->tm_mon;
newhist->action_time.day = tm->tm_mday;
newhist->action_time.hour = tm->tm_hour;
@@ -49,7 +49,7 @@ void print_history(struct stock_t *stock)
{
ullong total = hist->count * hist->price.cents;
- printf("[%d-%d-%d %d:%02d:%02d] ", hist->action_time.year + 2000, hist->action_time.month + 1, hist->action_time.day + 1,
+ printf("[%d-%d-%d %d:%02d:%02d] ", hist->action_time.year + EPOCH_YEAR, hist->action_time.month + 1, hist->action_time.day + 1,
hist->action_time.hour, hist->action_time.minute, hist->action_time.second);
switch(hist->action)
diff --git a/src/load.c b/src/load.c
index c2e5b3d..fcb2147 100644
--- a/src/load.c
+++ b/src/load.c
@@ -36,11 +36,8 @@ uint8_t read_int8(FILE *f)
return n;
}
-void load_handler(struct player_t *player)
+void load_portfolio(struct player_t *player, const char *filename)
{
- printf("Enter the file to load portfolio from: ");
- char *filename = read_string();
-
printf("Loading portfolio...\n");
if(player->need_to_free_portfolio)
@@ -50,7 +47,6 @@ void load_handler(struct player_t *player)
player->portfolio = NULL;
FILE *f = fopen(filename, "rb");
- free(filename);
char magic[6];
if(!f || fread(magic, 1, sizeof(magic), f) != 6 || memcmp(magic, "PORTv2", sizeof(magic)) != 0)
@@ -123,3 +119,13 @@ void load_handler(struct player_t *player)
update_handler(player);
}
+
+void load_handler(struct player_t *player)
+{
+ printf("Enter the file to load portfolio from: ");
+ char *filename = read_string();
+
+ load_portfolio(player, filename);
+
+ free(filename);
+}
diff --git a/src/main.c b/src/main.c
index 7850bcf..0f6ae16 100644
--- a/src/main.c
+++ b/src/main.c
@@ -10,7 +10,6 @@ void quit_handler(struct player_t *player)
int main(int argc, char *argv[])
{
- parse_args(argc, argv);
atexit(cleanup);
@@ -23,7 +22,13 @@ int main(int argc, char *argv[])
struct player_t *player = malloc(sizeof(struct player_t));
memset(player, 0, sizeof(struct player_t));
- player->cash.cents = 1000000 * 100;
+ uint args_status = parse_args(player, argc, argv);
+
+ if(args_status & ARG_FAILURE)
+ return EXIT_FAILURE;
+
+ if(args_status & ARG_LOADED)
+ player->cash.cents = 1000 * 100;
while(1)
{
@@ -33,7 +38,6 @@ int main(int argc, char *argv[])
{ "[P]rint portfolio", "print", print_handler },
{ "[U]pdate stock prices", "update", update_handler },
{ "Stock [i]nfo", "info", info_handler },
- { "[H]elp", "help", help_handler },
{ "[W]rite portfolio", "write", save_handler },
{ "[L]oad portfolio", "load", load_handler },
#ifndef NDEBUG
diff --git a/src/util.c b/src/util.c
index 8cd197d..585112b 100644
--- a/src/util.c
+++ b/src/util.c
@@ -258,10 +258,64 @@ void update_handler(struct player_t *player)
}
}
-void parse_args(int argc, char *argv[])
+uint parse_args(struct player_t *player, int argc, char *argv[])
{
+ uint ret = 0;
+ char *port_file = NULL;
+
for(int i = 1; i < argc; ++i)
{
+ char *arg = argv[i];
+ if(arg && arg[0] != '\0')
+ {
+ if(arg[0] == '-')
+ {
+ if(strcmp(arg, "--help") == 0 ||
+ strcmp(arg, "-h") == 0)
+ {
+ print_usage(argc, argv);
+ ret |= ARG_FAILURE;
+ break;
+ }
+ else if(strcmp(arg, "-v") == 0 ||
+ strcmp(arg, "--verbose") == 0)
+ {
+ ret |= ARG_VERBOSE;
+ }
+ else if(strcmp(arg, "--version") == 0)
+ {
+ print_version();
+ ret |= ARG_FAILURE;
+ break;
+ }
+ else if(strcmp(arg, "--") == 0)
+ {
+ break;
+ }
+ }
+ else
+ {
+ if(!(ret & ARG_LOADED))
+ {
+ port_file = arg;
+ ret |= ARG_LOADED;
+ }
+ else
+ {
+ printf("FATAL: multiple portfolio files specified.\n");
+ ret |= ARG_FAILURE;
+ break;
+ }
+ }
+ }
}
+
+ if(ret & ARG_FAILURE)
+ return ret;
+
+ if(port_file)
+ load_portfolio(player, port_file);
+
+ return ret;
}