-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathloadConfig.c
119 lines (100 loc) · 2.56 KB
/
loadConfig.c
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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
#include <stdlib.h>
#include <stdio.h>
#include <stddef.h>
#include <string.h>
#include "loadConfig.h"
#include "portcfg.h"
#define SDPD_CONFIG_FILE "sdPromptDumper.cfg"
/* XXX: Not sure how well this will work on windows */
#ifdef _WIN32
#define SDPD_CONFIG_PATH "\\AppData\\Local\\sdPromptDumper\\" SDPD_CONFIG_FILE
const char *home_env = "%HOMEPATH%"; /* Are the percent escapes neccessary? */
#else /* POSIX */
#define SDPD_CONFIG_PATH "/.config/sdPromptDumper/" SDPD_CONFIG_FILE
const char *home_env = "HOME";
#endif /* Platform Check */
#define SDPD_PATH_MAX 2048
static char* lazyStrdup(const char *src)
{
char *dst = NULL;
if (src != NULL)
{
const size_t len = strlen(src) + 1;
if ((dst = malloc(sizeof(char) * (len))) != NULL)
{
dst = strncpy(dst, src, len);
}
}
return dst;
}
/* If loading a config file for a much larger project one would want to use
* a more sophisticated method to access these label/variable associations such
* as a hash-table or sorting the array so it can be binary searched */
static void cfgCallback(const char *key, const char *val, void *data)
{
struct cfgArguments *args = (struct cfgArguments *) data;
const struct
{
const char *label;
char **dest;
} lookup_table[] =
{
{"model-dir", args->model_path},
{"lora-dir", args->lora_path},
{"bin-dir", args->bin_path},
{"vae-path", args->vae_path},
{"exe-name", args->exe_name}
};
const size_t lookup_len
= sizeof(lookup_table) / sizeof(lookup_table[0]);
size_t i;
for (i = 0; i < lookup_len; i++)
{
if ((*lookup_table[i].dest == NULL)
&& (strcmp(key, lookup_table[i].label) == 0))
{
*lookup_table[i].dest = lazyStrdup(val);
return;
}
}
if ((args->abrv != NULL)
&& (strcmp(key, "abrv-bool") == 0))
{
*args->abrv = (strcmp(val, "TRUE") == 0)
? STI_TRUE : STI_FALSE;
}
}
int loadDefaultFile(struct cfgArguments *args, char *alt_path)
{
FILE *cfg_handle = NULL;
char config_path[SDPD_PATH_MAX] = {0};
if (alt_path == NULL)
{
const char *home_dir = getenv(home_env);
if (((args == NULL) || (home_dir == NULL))
|| (strlen(home_dir) + sizeof(SDPD_CONFIG_PATH) - 1
>= SDPD_PATH_MAX))
{
return 1;
}
strcat(config_path, home_dir);
strcat(config_path, SDPD_CONFIG_PATH);
}
else
{
if ((args == NULL)
|| (strlen(alt_path) + sizeof(SDPD_CONFIG_FILE) - 1
>= SDPD_PATH_MAX))
{
return 1;
}
strcat(config_path, alt_path);
strcat(config_path, SDPD_CONFIG_FILE);
}
if ((cfg_handle = fopen(config_path, "rb")) != NULL)
{
portcfgProcess(cfg_handle, cfgCallback, args);
fclose(cfg_handle);
}
return 0;
}