From 9875aaebf2d44c3f38be3c72b0c9c0af9422f8cc Mon Sep 17 00:00:00 2001 From: louie Date: Tue, 29 Dec 2020 17:09:17 -0800 Subject: Added menu location caching --- windows/cache.c | 84 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 84 insertions(+) create mode 100644 windows/cache.c (limited to 'windows') diff --git a/windows/cache.c b/windows/cache.c new file mode 100644 index 0000000..c6fa3ef --- /dev/null +++ b/windows/cache.c @@ -0,0 +1,84 @@ +#include +#include +#include +#include +#include +#include + +#include "../include/cache.h" +#include "../include/read_cfg.h" + +void save_to_cache(int g_hover, int e_hover, int true_hover, char *cfg_name){ + FILE *fp; + char path[BUF_LEN]; + + char *appdata = getenv("APPDATA"); + + if(appdata == NULL){ + printf("Failed to save cache data: APPDATA is not set\n"); + return; + } + + sprintf(path, "%s%ctml%c", appdata, sep, sep); + mkdir(path); + + sprintf(path, "%s%ctml%ccache.bin", appdata, sep, sep); + + //open cache file for writing + fp = fopen(path, "wb"); + if(fp == NULL){ + printf("Failed to save cache data: could not open \"%s\"\n", path); + return; + } + + //write to file + fwrite(&g_hover, sizeof(int), 1, fp); + fwrite(&e_hover, sizeof(int), 1, fp); + fwrite(&true_hover, sizeof(int), 1, fp); + fwrite(cfg_name, sizeof(char), BUF_LEN, fp); + + fclose(fp); + return; +} + +void load_cache(int *g_hover, int *e_hover, int *true_hover, char *new_cfg_name){ + FILE *fp; + char path[BUF_LEN]; + char saved_cfg_name[BUF_LEN]; + + char *appdata = getenv("APPDATA"); + + if(appdata == NULL){ + printf("Failed to load cached data: HOME is not set\n"); + return; + } + + sprintf(path, "%s%ctml%ccache.bin", appdata, sep, sep); + + //open cache file for reading + fp = fopen(path, "rb"); + if(fp == NULL){ + printf("Failed to load cached data: could not open \"%s\"\n", path); + return; + } + + //check if cfg_name matches; if not, do not load from cache + fseek(fp, sizeof(int) * 3, SEEK_SET); + fread(saved_cfg_name, sizeof(char), BUF_LEN, fp); + + if(!(strcmp(saved_cfg_name, new_cfg_name))){ + fseek(fp, 0, SEEK_SET); + fread(g_hover, sizeof(int), 1, fp); + fread(e_hover, sizeof(int), 1, fp); + fread(true_hover, sizeof(int), 1, fp); + } + + else{ + *g_hover = 0; + *e_hover = 0; + *true_hover = 0; + } + + fclose(fp); + return; +} -- cgit