1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
|
#include "globals.h"
/* NOTE: integers are represented internally by unsigned long long ints, but in the save they are always 64 bits */
void load_handler(struct player_t *player)
{
printf("Enter the file to load portfolio from: ");
char buf[128];
scanf("%127s", buf);
printf("Loading portfolio...\n");
if(player->need_to_free_portfolio)
free(player->portfolio);
player->portfolio_len = 0;
FILE *f = fopen(buf, "r");
char magic[6];
if(!f || fread(magic, 1, sizeof(magic), f) != 6 || memcmp(magic, "PORTv1", 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 = cash;
fflush(stdout);
do {
/* read portfolio data */
player->portfolio_len += 1;
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);
char *sym = malloc(symlen + 1);
if(fread(sym, symlen + 1, 1, f) != 1)
{
printf("FATAL: Save is corrupted (symbol too short).\n");
exit(EXIT_FAILURE);
}
player->portfolio[player->portfolio_len - 1].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);
player->portfolio[player->portfolio_len - 1].count = count;
int junk = fgetc(f);
ungetc(junk, f);
} while (!feof(f) && !ferror(f));
update_handler(player);
}
|