main
saves.c
1#include <string.h>
2#include <stdlib.h>
3#include "saves.h"
4#include "util.h"
5
6//0123456789012345678901234678
7//Slot N - December 31st, XXXX
8#define MAX_DESC_SIZE 40
9
10char *get_slot_name(system_header *system, uint32_t slot_index, char *ext)
11{
12 if (!system->save_dir) {
13 return NULL;
14 }
15 char *fname;
16 if (slot_index < 10) {
17 size_t name_len = strlen("slot_N.") + strlen(ext) + 1;
18 fname = malloc(name_len);
19 snprintf(fname, name_len, "slot_%d.%s", slot_index, ext);
20 } else {
21 size_t name_len = strlen("quicksave.") + strlen(ext) + 1;
22 fname = malloc(name_len);
23 snprintf(fname, name_len, "quicksave.%s", ext);
24 }
25 char const *parts[] = {system->save_dir, PATH_SEP, fname};
26 char *ret = alloc_concat_m(3, parts);
27 free(fname);
28 return ret;
29}
30
31save_slot_info *get_slot_info(system_header *system, uint32_t *num_out)
32{
33 save_slot_info *dst = calloc(11, sizeof(save_slot_info));
34 time_t modtime;
35 struct tm ltime;
36 for (uint32_t i = 0; i <= QUICK_SAVE_SLOT; i++)
37 {
38 char * cur = dst[i].desc = malloc(MAX_DESC_SIZE);
39 char * fname = get_slot_name(system, i, "state");
40 modtime = get_modification_time(fname);
41 free(fname);
42 if (!modtime && system->type == SYSTEM_GENESIS) {
43 fname = get_slot_name(system, i, "gst");
44 modtime = get_modification_time(fname);
45 free(fname);
46 }
47 if (i == QUICK_SAVE_SLOT) {
48 cur += snprintf(cur, MAX_DESC_SIZE, "Quick - ");
49 } else {
50 cur += snprintf(cur, MAX_DESC_SIZE, "Slot %d - ", i);
51 }
52 if (modtime) {
53 strftime(cur, MAX_DESC_SIZE - (cur - dst->desc), "%c", localtime_r(&modtime, <ime));
54 } else {
55 strcpy(cur, "EMPTY");
56 }
57 dst[i].modification_time = modtime;
58 }
59 *num_out = QUICK_SAVE_SLOT + 1;
60 return dst;
61}
62
63void free_slot_info(save_slot_info *slots)
64{
65 if (!slots) {
66 return;
67 }
68 for (uint32_t i = 0; i <= QUICK_SAVE_SLOT; i++)
69 {
70 free(slots[i].desc);
71 }
72 free(slots);
73}