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/save.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/save.c')
| -rw-r--r-- | src/save.c | 69 |
1 files changed, 59 insertions, 10 deletions
@@ -1,6 +1,37 @@ #include "globals.h" -/* NOTE: integers are represented internally by unsigned long long ints, but in the save they are always 64 bits */ +/* NOTE: integers are represented internally by long long ints, but in the save they are always 64 bits */ + +static bool write_be64(FILE *f, uint64_t n) +{ + n = to_be64(n); + if(fwrite(&n, sizeof(n), 1, f) != 1) + return false; + return true; +} + +static bool write_be32(FILE *f, uint32_t n) +{ + n = to_be32(n); + if(fwrite(&n, sizeof(n), 1, f) != 1) + return false; + return true; +} + +static bool write_be16(FILE *f, uint16_t n) +{ + n = to_be16(n); + if(fwrite(&n, sizeof(n), 1, f) != 1) + return false; + return true; +} + +static bool write_int8(FILE *f, uint8_t n) +{ + if(fwrite(&n, sizeof(n), 1, f) != 1) + return false; + return true; +} void save_handler(struct player_t *player) { @@ -10,25 +41,43 @@ void save_handler(struct player_t *player) scanf("%127s", buf); printf("Writing data...\n"); - FILE *f = fopen(buf, "w"); + FILE *f = fopen(buf, "wb"); - const char *magic = "PORTv1"; + const char *magic = "PORTv2"; fwrite(magic, strlen(magic), 1, f); - uint64_t be_cash = to_be64(player->cash.cents); - - fwrite(&be_cash, sizeof(be_cash), 1, f); + write_be64(f, player->cash.cents); for(uint i = 0; i < player->portfolio_len; ++i) { struct stock_t *stock = player->portfolio + i; - uint64_t be_symlen = to_be64(strlen(stock->symbol)); - fwrite(&be_symlen, sizeof(be_symlen), 1, f); + write_be64(f, strlen(stock->symbol)); + fwrite(stock->symbol, strlen(stock->symbol) + 1, 1, f); - uint64_t be_count = to_be64(stock->count); - fwrite(&be_count, sizeof(be_count), 1, f); + write_be64(f, stock->count); + + write_be32(f, stock->history_len); + + /* write history */ + struct history_item *hist = stock->history; + while(hist) + { + printf("WRITING HISTORY ITEM %d\n", hist->action); + write_be32(f, hist->action); + write_be64(f, hist->count); + write_be64(f, hist->price.cents); + + write_be16(f, hist->action_time.year); + write_int8(f, hist->action_time.month); + write_int8(f, hist->action_time.day); + write_int8(f, hist->action_time.hour); + write_int8(f, hist->action_time.minute); + write_int8(f, hist->action_time.second); + + hist = hist->next; + } } fclose(f); |