summaryrefslogtreecommitdiff
path: root/windows/cache.c
blob: c6fa3ef6ceacd51708c0707c6bc92bfbe244e3af (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
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;
}