This repository has been archived by the owner on Sep 2, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconfig.c
73 lines (62 loc) · 1.58 KB
/
config.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
#include <stdio.h>
#include <stdlib.h> // for getenv
#include <string.h> // for strncat and strncpy
#include "config.h"
#include "utils.h"
#include "main.h"
// Returns the root storage directory for jw plus an optional subdirectory
char *config_root_get(const char *subdir)
{
#ifdef _WIN32
char *home = getenv("USER");
#else
char *home = getenv("HOME");
#endif
static char path[512];
strncpy(path, home, 511);
strcat(path, SEPARATOR);
struct config config = config_get();
strncat(path, config.root, 511);
if (subdir != NULL && strncmp(subdir, SEPARATOR, 1) != 0) {
strcat(path, SEPARATOR);
strncat(path, subdir, 511);
}
strncat(path, SEPARATOR, 511);
return path;
}
// Returns the global configuration directory, making it if necessary
char *config_dir()
{
char *home = getenv("HOME");
static char path[512];
strncpy(path, home, 511);
strncat(path, SEPARATOR, 511);
strncat(path, CONFIG_DIR, 511);
char temp[512];
strcpy(temp, path);
utils_mkdir(temp);
return path;
}
struct config config_get()
{
char path[512];
strncpy(path, config_dir(), 511);
strncat(path, "config.yaml", 511);
// the default configuration
struct config config = {
.root = "jw",
.editor = "vi",
};
FILE *fp = fopen(path, "r");
if (fp == NULL) {
return config;
}
strncpy(config.editor, utils_default_editor(), 511); // for some reason this causes path to be blank. TODO debug and fix
char value[512];
if (utils_mini_yaml(fp, "root", value) != NULL)
strncpy(config.root, value, 511);
if (utils_mini_yaml(fp, "editor", value) != NULL) {
strncpy(config.editor, value, 511);
}
return config;
}