diff options
author | louie <lshprung@yahoo.com> | 2020-12-30 11:03:02 -0800 |
---|---|---|
committer | louie <lshprung@yahoo.com> | 2020-12-30 11:03:02 -0800 |
commit | e7c5382d71339d15f2b96e7ee06e5ef3639df837 (patch) | |
tree | f2de46d281532f4b505cbbea4ec3504c9aa98013 | |
parent | 9875aaebf2d44c3f38be3c72b0c9c0af9422f8cc (diff) |
Improved cache.c organization
-rw-r--r-- | Makefile | 9 | ||||
-rw-r--r-- | cache.c | 65 | ||||
-rw-r--r-- | include/cache.h | 2 | ||||
-rw-r--r-- | unix/cache.c | 81 | ||||
-rw-r--r-- | windows/cache.c | 73 |
5 files changed, 97 insertions, 133 deletions
@@ -6,8 +6,8 @@ PREFIX = /usr/local ifeq ($(OS),Windows_NT) -$(NAME): draw.o read_cfg.o group.o entry.o windows/cache.o windows/draw.o windows/read_cfg.o - $(CC) -o $(NAME) draw.o read_cfg.o group.o entry.o windows/cache.o windows/draw.o windows/read_cfg.o $(LIBS) +$(NAME): cache.o draw.o read_cfg.o group.o entry.o windows/cache.o windows/draw.o windows/read_cfg.o + $(CC) -o $(NAME) cache.o draw.o read_cfg.o group.o entry.o windows/cache.o windows/draw.o windows/read_cfg.o $(LIBS) windows/draw.o: windows/draw.c include/draw.h windows/read_cfg.o: windows/read_cfg.c include/read_cfg.h @@ -15,8 +15,8 @@ windows/cache.o: windows/cache.c include/cache.h include/read_cfg.h else -$(NAME): draw.o read_cfg.o group.o entry.o unix/cache.o unix/draw.o unix/read_cfg.o - $(CC) -o $(NAME) draw.o read_cfg.o group.o entry.o unix/cache.o unix/draw.o unix/read_cfg.o $(LIBS) +$(NAME): cache.o draw.o read_cfg.o group.o entry.o unix/cache.o unix/draw.o unix/read_cfg.o + $(CC) -o $(NAME) cache.o draw.o read_cfg.o group.o entry.o unix/cache.o unix/draw.o unix/read_cfg.o $(LIBS) unix/draw.o: unix/draw.c include/draw.h unix/read_cfg.o: unix/read_cfg.c include/read_cfg.h @@ -24,6 +24,7 @@ unix/cache.o: unix/cache.c include/cache.h include/read_cfg.h endif +cache.o: cache.c include/cache.h include/read_cfg.h draw.o: draw.c include/cache.h include/draw.h include/entry.h include/group.h include/read_cfg.h read_cfg.o: read_cfg.c include/entry.h include/group.h include/read_cfg.h group.o: group.c include/entry.h include/group.h include/read_cfg.h @@ -0,0 +1,65 @@ +#include <stdbool.h> +#include <stdio.h> +#include <stdlib.h> +#include <string.h> + +#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 = get_cache_path(true); + + //ensure get_cache_path() did not return NULL + if(path == NULL) return; + + //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(cfg_name, sizeof(char), BUF_LEN, fp); + fwrite(&g_hover, sizeof(int), 1, fp); + fwrite(&e_hover, sizeof(int), 1, fp); + fwrite(&true_hover, sizeof(int), 1, fp); + + fclose(fp); + return; +} + +void load_cache(int *g_hover, int *e_hover, int *true_hover, char *new_cfg_name){ + FILE *fp; + char *path = get_cache_path(false); + char saved_cfg_name[BUF_LEN]; + + //ensure get_cache_path() did not return NULL + if(path == NULL) return; + + //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 + fread(saved_cfg_name, sizeof(char), BUF_LEN, fp); + + if(!(strcmp(saved_cfg_name, new_cfg_name))){ + 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; +} diff --git a/include/cache.h b/include/cache.h index b3f0adb..0f5a6c4 100644 --- a/include/cache.h +++ b/include/cache.h @@ -5,4 +5,6 @@ void save_to_cache(int g_hover, int e_hover, int true_hover, char *cfg_name); void load_cache(int *g_hover, int *e_hover, int *true_hover, char *new_cfg_name); +char *get_cache_path(bool create); + #endif diff --git a/unix/cache.c b/unix/cache.c index af18077..5aa14bf 100644 --- a/unix/cache.c +++ b/unix/cache.c @@ -1,87 +1,36 @@ +#include <assert.h> #include <stdbool.h> #include <stdio.h> #include <stdlib.h> -#include <string.h> +//#include <string.h> #include <sys/stat.h> #include <sys/types.h> #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 *get_cache_path(bool create){ + char *path = malloc(sizeof(char) * BUF_LEN); char *home = getenv("HOME"); + assert(path != NULL); + if(home == NULL){ printf("Failed to save cache data: HOME is not set\n"); - return; + free(path); + return NULL; } - sprintf(path, "%s%c.cache%c", home, sep, sep); - mkdir(path, 0755); - - sprintf(path, "%s%c.cache%ctml%c", home, sep, sep, sep); - mkdir(path, 0755); + //if create is asserted, build the path to the file + if(create){ + sprintf(path, "%s%c.cache%c", home, sep, sep); + mkdir(path, 0755); - sprintf(path, "%s%c.cache%ctml%cdata.bin", home, sep, 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 *home = getenv("HOME"); - - if(home == NULL){ - printf("Failed to load cached data: HOME is not set\n"); - return; + sprintf(path, "%s%c.cache%ctml%c", home, sep, sep, sep); + mkdir(path, 0755); } sprintf(path, "%s%c.cache%ctml%cdata.bin", home, sep, 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; + return path; } diff --git a/windows/cache.c b/windows/cache.c index c6fa3ef..313b16a 100644 --- a/windows/cache.c +++ b/windows/cache.c @@ -1,3 +1,4 @@ +#include <assert.h> #include <stdbool.h> #include <stdio.h> #include <stdlib.h> @@ -8,77 +9,23 @@ #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 *get_cache_path(bool create){ + char *path = malloc(sizeof(char) * 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; + free(path); + return NULL; } - //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; + //if create is asserted, build the path to the file + if(create){ + sprintf(path, "%s%ctml%c", appdata, sep, sep); + mkdir(path); } 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; + return path; } |