summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorlouie <lshprung@yahoo.com>2020-12-29 17:09:17 -0800
committerlouie <lshprung@yahoo.com>2020-12-29 17:09:17 -0800
commit9875aaebf2d44c3f38be3c72b0c9c0af9422f8cc (patch)
tree189d89d948f7a7e7f3638928d483f3ecaf8d9142
parent3624ef3e18fa074ed7fadb11da68f61896dd67d3 (diff)
Added menu location caching
-rw-r--r--Makefile17
-rw-r--r--draw.c25
-rw-r--r--include/cache.h8
-rw-r--r--unix/cache.c87
-rw-r--r--windows/cache.c84
5 files changed, 205 insertions, 16 deletions
diff --git a/Makefile b/Makefile
index 119160d..6d331f1 100644
--- a/Makefile
+++ b/Makefile
@@ -6,31 +6,34 @@ PREFIX = /usr/local
ifeq ($(OS),Windows_NT)
-$(NAME): draw.o read_cfg.o group.o entry.o windows/draw.o windows/read_cfg.o
- $(CC) -o $(NAME) draw.o read_cfg.o group.o entry.o windows/draw.o windows/read_cfg.o $(LIBS)
+$(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)
windows/draw.o: windows/draw.c include/draw.h
windows/read_cfg.o: windows/read_cfg.c include/read_cfg.h
+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/draw.o unix/read_cfg.o
- $(CC) -o $(NAME) draw.o read_cfg.o group.o entry.o unix/draw.o unix/read_cfg.o $(LIBS)
+$(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)
unix/draw.o: unix/draw.c include/draw.h
unix/read_cfg.o: unix/read_cfg.c include/read_cfg.h
+unix/cache.o: unix/cache.c include/cache.h include/read_cfg.h
endif
-draw.o: draw.c 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
+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
entry.o: entry.c include/entry.h include/group.h include/read_cfg.h
-
.PHONY: clean
clean:
rm *.o $(NAME)
+ rm -f unix/*.o
+ rm -f windows/*.o
ifneq ($(OS),Windows_NT)
diff --git a/draw.c b/draw.c
index b2f5074..e3217d1 100644
--- a/draw.c
+++ b/draw.c
@@ -10,6 +10,7 @@
#include <ncurses.h>
#endif
+#include "include/cache.h"
#include "include/draw.h"
#include "include/entry.h"
#include "include/group.h"
@@ -32,9 +33,9 @@ int locateChar(char input);
WINDOW *group_win = NULL;
WINDOW *entry_win = NULL;
WINDOW *info_win = NULL;
-int g_hover = 0;
-int e_hover = 0;
-int true_hover = 0; //0 = hovering on groups, 1 = hovering on entries
+int g_hover;
+int e_hover;
+int true_hover; //0 = hovering on groups, 1 = hovering on entries
GROUP **g;
ENTRY **e;
int g_count;
@@ -68,6 +69,9 @@ int main(int argc, char **argv){
exit(0);
}
+ //load cached data
+ load_cache(&g_hover, &e_hover, &true_hover, cfg_path);
+
initscr();
cbreak();
keypad(stdscr, true);
@@ -85,8 +89,11 @@ int main(int argc, char **argv){
update_display(false);
- //drawing is done, now run a while loop to receive input
- while(1){
+ //update highlighting for loaded location
+ update_display(true);
+
+ //drawing is done, now run a while loop to receive input (ESC ends this loop)
+ while(input != 27){
input = getch();
switch(input){
@@ -124,10 +131,6 @@ int main(int argc, char **argv){
launch();
break;
- case 27: //escape key
- endwin();
- return 0;
-
default: //a search char was entered, locate where to jump to
trav_col(locateChar(input));
}
@@ -141,6 +144,10 @@ int main(int argc, char **argv){
}
endwin();
+
+ //save position data to cache
+ save_to_cache(g_hover, e_hover, true_hover, cfg_path);
+
return 0;
}
diff --git a/include/cache.h b/include/cache.h
new file mode 100644
index 0000000..b3f0adb
--- /dev/null
+++ b/include/cache.h
@@ -0,0 +1,8 @@
+#ifndef CACHE_H
+#define CACHE_H
+
+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);
+
+#endif
diff --git a/unix/cache.c b/unix/cache.c
new file mode 100644
index 0000000..af18077
--- /dev/null
+++ b/unix/cache.c
@@ -0,0 +1,87 @@
+#include <stdbool.h>
+#include <stdio.h>
+#include <stdlib.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 *home = getenv("HOME");
+
+ if(home == NULL){
+ printf("Failed to save cache data: HOME is not set\n");
+ return;
+ }
+
+ 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);
+
+ 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%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;
+}
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 <stdbool.h>
+#include <stdio.h>
+#include <stdlib.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 *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;
+}