-
Notifications
You must be signed in to change notification settings - Fork 1
/
confparser.c
146 lines (116 loc) · 3.4 KB
/
confparser.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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
// Copyright 2022 <Maros Varchola - mvarchdev>
#include "confparser.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
/// @brief Read config file
/// @param path Path to config file
/// @return Pointer to allocated config file data
config_option_t read_config_file(char *path) {
FILE *fp;
if ((fp = fopen(path, "r+")) == NULL) return NULL;
config_option_t last_co_addr = NULL;
while (1) {
config_option_t co = NULL;
if ((co = (config_option_t)calloc(1, sizeof(config_option))) == NULL)
continue;
memset(co, 0, sizeof(config_option));
co->prev = last_co_addr;
// char tmp = 0;
char lineout[CONFIG_ARG_MAX_BYTES * 2] = {0};
fgets(lineout, (CONFIG_ARG_MAX_BYTES * 2) - 1, fp);
char *newlinec = strstr(lineout, "\n");
if (newlinec) *newlinec = '\0';
char *endkey = strstr(lineout, "=");
char *startvalue = NULL;
if (endkey && endkey - 1 != NULL) {
startvalue = endkey + 1;
endkey--;
while (*(endkey) == ' ')
if (endkey <= lineout)
break;
else
endkey--;
*(endkey + 1) = '\0';
}
while (startvalue && *(startvalue) == ' ')
if (startvalue >= lineout + ((2 * CONFIG_ARG_MAX_BYTES) - 2))
break;
else
startvalue++;
if (!startvalue) startvalue = lineout;
strcpy(co->key, lineout);
strcpy(co->value, startvalue);
if (lineout[0] == '\0') {
if (feof(fp)) {
break;
}
if (co->key[0] == '#') {
while (fgetc(fp) != '\n') {
// Do nothing (to move the cursor to the end of the line).
}
free(co);
continue;
}
free(co);
continue;
}
// printf("Key: %s\nValue: %s\n", co->key, co->value);
last_co_addr = co;
}
return last_co_addr;
}
/// @brief Get config file integer value of key
/// @param filename Config name to use
/// @param key Key to find
/// @return Integer value of key, -1 if not found
int get_int_key_value(char filename[], char key[]) {
config_option_t sfile = read_config_file(filename);
if (!sfile) return -1;
if (sfile->key[0] == '\0') return -1;
while (sfile) {
if (strcmp(sfile->key, key) == 0) {
int outlvl = atoi(sfile->value);
free(sfile);
return outlvl;
}
config_option_t prev = sfile->prev;
free(sfile);
sfile = prev;
}
return -1;
}
/// @brief Set integer to config file based on key
/// @param tmpfilename Temporary filename
/// @param datafile Config file to use
/// @param key Key to set
/// @param value Vaule to assign to key
/// @return Error code
int set_int_key_value(char tmpfilename[], char datafile[], char key[],
int value) {
if (!tmpfilename || !datafile || !key) return -1;
if (strcmp("", key) == 0) return -1;
FILE *tmpfile = fopen(tmpfilename, "w");
FILE *dfile = fopen(datafile, "a+");
if (!tmpfile || !dfile) {
if (dfile) fclose(dfile);
if (tmpfile) fclose(tmpfile);
return -1;
}
rewind(dfile);
int bufferLength = 128;
char line[bufferLength];
memset(line, '\0', sizeof(char) * bufferLength);
while (fgets(line, bufferLength, dfile))
if (strstr(line, key) == NULL) {
fputs(line, tmpfile);
if (line[strlen(line) - 1] != '\n') fputc('\n', tmpfile);
}
fprintf(tmpfile, "%s = %d\n", key, value);
fclose(dfile);
fclose(tmpfile);
remove(datafile);
rename(tmpfilename, datafile);
return 0;
}