diff options
| author | Franklin Wei <git@fwei.tk> | 2015-05-15 18:37:00 -0400 |
|---|---|---|
| committer | Franklin Wei <git@fwei.tk> | 2015-05-15 18:37:00 -0400 |
| commit | 367c1ab04df3732d538a4761be78f66c289a78bd (patch) | |
| tree | 91bc86e06f9643fa1c3e73350e0ded0dd6dc4d99 /src/load.c | |
| parent | 42706e46a606766bb0a56d1dd727e556f4c17669 (diff) | |
| download | market-sim-367c1ab04df3732d538a4761be78f66c289a78bd.zip market-sim-367c1ab04df3732d538a4761be78f66c289a78bd.tar.gz market-sim-367c1ab04df3732d538a4761be78f66c289a78bd.tar.bz2 market-sim-367c1ab04df3732d538a4761be78f66c289a78bd.tar.xz | |
fix lots of stuff, implement history
Diffstat (limited to 'src/load.c')
| -rw-r--r-- | src/load.c | 103 |
1 files changed, 75 insertions, 28 deletions
@@ -2,6 +2,40 @@ /* NOTE: integers are represented internally by unsigned long long ints, but in the save they are always 64 bits */ +#define FAIL() exit(*(char*)NULL); + +uint64_t read_be64(FILE *f) +{ + uint64_t n; + if(fread(&n, sizeof(n), 1, f) != 1) + FAIL(); + return to_sys64(n); +} + +uint32_t read_be32(FILE *f) +{ + uint32_t n; + if(fread(&n, sizeof(n), 1, f) != 1) + FAIL(); + return to_sys32(n); +} + +uint16_t read_be16(FILE *f) +{ + uint16_t n; + if(fread(&n, sizeof(n), 1, f) != 1) + FAIL(); + return to_sys16(n); +} + +uint8_t read_int8(FILE *f) +{ + uint8_t n; + if(fread(&n, sizeof(n), 1, f) != 1) + FAIL(); + return n; +} + void load_handler(struct player_t *player) { printf("Enter the file to load portfolio from: "); @@ -14,25 +48,16 @@ void load_handler(struct player_t *player) free(player->portfolio); player->portfolio_len = 0; - FILE *f = fopen(buf, "r"); + FILE *f = fopen(buf, "rb"); char magic[6]; - if(!f || fread(magic, 1, sizeof(magic), f) != 6 || memcmp(magic, "PORTv1", sizeof(magic)) != 0) + if(!f || fread(magic, 1, sizeof(magic), f) != 6 || memcmp(magic, "PORTv2", sizeof(magic)) != 0) { printf("FATAL: Failed to load save."); exit(EXIT_FAILURE); } - uint64_t cash; - if(fread(&cash, sizeof(cash), 1, f) != 1) - { - printf("FATAL: Failed to load save."); - exit(EXIT_FAILURE); - } - cash = to_sys64(cash); + player->cash.cents = read_be64(f); - player->cash.cents = cash; - - fflush(stdout); do { /* read portfolio data */ @@ -40,13 +65,11 @@ void load_handler(struct player_t *player) player->portfolio = realloc(player->portfolio, player->portfolio_len * sizeof(struct stock_t)); player->need_to_free_portfolio = true; - uint64_t symlen; - if(fread(&symlen, sizeof(symlen), 1, f) != 1) - { - printf("FATAL: Save is corrupted (symbol length too short).\n"); - exit(EXIT_FAILURE); - } - symlen = to_sys64(symlen); + struct stock_t *stock = player->portfolio + player->portfolio_len - 1; + + memset(stock, 0, sizeof(struct stock_t)); + + uint64_t symlen = read_be64(f); char *sym = malloc(symlen + 1); if(fread(sym, symlen + 1, 1, f) != 1) { @@ -54,18 +77,42 @@ void load_handler(struct player_t *player) exit(EXIT_FAILURE); } - player->portfolio[player->portfolio_len - 1].symbol = sym; + stock->symbol = sym; - uint64_t count; - if(fread(&count, sizeof(count), 1, f) != 1) - { - printf("FATAL: Save is corrupted (count too short).\n"); - exit(EXIT_FAILURE); - } - count = to_sys64(count); + stock->count = read_be64(f); - player->portfolio[player->portfolio_len - 1].count = count; + uint32_t histlen = read_be32(f); + /* load history */ + + if(histlen) + { + stock->history = malloc(histlen * sizeof(struct history_item)); + + struct history_item *hist = stock->history; + + for(uint i = 0; i < histlen; ++i) + { + hist->action = read_be32(f); + hist->count = read_be64(f); + hist->price.cents = read_be64(f); + + hist->action_time.year = read_be16(f); + hist->action_time.month = read_int8(f); + hist->action_time.day = read_int8(f); + hist->action_time.hour = read_int8(f); + hist->action_time.minute = read_int8(f); + hist->action_time.second = read_int8(f); + + if(i + 1 < histlen) + hist->next = hist + 1; + else + hist->next = NULL; + + ++stock->history_len; + hist = hist->next; + } + } int junk = fgetc(f); ungetc(junk, f); |