comparison saves.c @ 1478:da1dce39e846 nuklear_ui

Refactored save slot related logic to reduce duplication and allow reuse in new UI. Get state loading/saving mostly working in new UI
author Michael Pavone <pavone@retrodev.com>
date Fri, 24 Nov 2017 12:04:02 -0800
parents
children a568dca999b2
comparison
equal deleted inserted replaced
1477:1cdd7f492af8 1478:da1dce39e846
1 #include <string.h>
2 #include <stdlib.h>
3 #include "saves.h"
4 #include "util.h"
5
6 #ifdef _WIN32
7 #define localtime_r(a,b) localtime(a)
8 #include <windows.h>
9 #endif
10 //0123456789012345678901234678
11 //Slot N - December 31st, XXXX
12 #define MAX_DESC_SIZE 40
13
14 char *get_slot_name(system_header *system, uint32_t slot_index, char *ext)
15 {
16 if (!system->save_dir) {
17 return NULL;
18 }
19 char *fname;
20 if (slot_index < 10) {
21 size_t name_len = strlen("slot_N.") + strlen(ext) + 1;
22 fname = malloc(name_len);
23 snprintf(fname, name_len, "slot_%d.%s", slot_index, ext);
24 } else {
25 size_t name_len = strlen("quicksave.") + strlen(ext) + 1;
26 fname = malloc(name_len);
27 snprintf(fname, name_len, "quicksave.%s", ext);
28 }
29 char const *parts[] = {system->save_dir, PATH_SEP, fname};
30 char *ret = alloc_concat_m(3, parts);
31 free(fname);
32 return ret;
33 }
34
35 save_slot_info *get_slot_info(system_header *system, uint32_t *num_out)
36 {
37 save_slot_info *dst = calloc(11, sizeof(save_slot_info));
38 time_t modtime;
39 struct tm ltime;
40 for (uint32_t i = 0; i <= QUICK_SAVE_SLOT; i++)
41 {
42 char * cur = dst[i].desc = malloc(MAX_DESC_SIZE);
43 char * fname = get_slot_name(system, i, "state");
44 modtime = get_modification_time(fname);
45 free(fname);
46 if (!modtime && system->type == SYSTEM_GENESIS) {
47 fname = get_slot_name(system, i, "gst");
48 modtime = get_modification_time(fname);
49 free(fname);
50 }
51 if (i == QUICK_SAVE_SLOT) {
52 cur += snprintf(cur, MAX_DESC_SIZE, "Quick - ");
53 } else {
54 cur += snprintf(cur, MAX_DESC_SIZE, "Slot %d - ", i);
55 }
56 if (modtime) {
57 strftime(cur, MAX_DESC_SIZE - (cur - dst->desc), "%c", localtime_r(&modtime, &ltime));
58 } else {
59 strcpy(cur, "EMPTY");
60 }
61 dst[i].modification_time;
62 }
63 *num_out = QUICK_SAVE_SLOT + 1;
64 return dst;
65 }
66
67 void free_slot_info(save_slot_info *slots)
68 {
69 if (!slots) {
70 return;
71 }
72 for (uint32_t i = 0; i <= QUICK_SAVE_SLOT; i++)
73 {
74 free(slots[i].desc);
75 }
76 free(slots);
77 }