Skip to content
Extraits de code Groupes Projets
Vérifiée Valider 8c0ac8a1 rédigé par Kubat's avatar Kubat
Parcourir les fichiers

WIP: Writing an ini parser

parent f0615d2f
Aucune branche associée trouvée
Aucune étiquette associée trouvée
1 requête de fusion!70Resolve "Drop inih"
......@@ -7,6 +7,7 @@
/* Defines */
#define INI_MAX_LINE_LEN 1024
#define URL_MAX_LEN 1024
#define DEFAULT_URL "https://kurisu.iiens.net"
#define GET_ID_JSON DEFAULT_URL "/api_karas.php?id=%ld"
......
#define _POSIX_C_SOURCE 200809L
#include <common/common.h>
#include <lektor/common.h>
#include <lektor/config.h>
#include <lektor/database.h>
#include <ini/ini.h>
#include <stdlib.h>
#include <errno.h>
#include <string.h>
......@@ -14,6 +14,71 @@
#include <sys/types.h>
#include <sys/stat.h>
#include <limits.h>
#include <ctype.h>
typedef int (*ini_handler)(volatile sqlite3 *db, const char *section, const char *name, const char *value);
static inline char *
strip(char *s)
{
char *p = s + strlen(s);
while (p > s && isspace(*(--p)))
*p = 0;
return s;
}
static inline char *
skip(char *s)
{
while (*s && isspace(*s))
s++;
return s;
}
static inline int
ini_parse(const char *path, ini_handler handle, volatile sqlite3 *db)
{
char line[INI_MAX_LINE_LEN], *start, *end, *name, *value;
int error = 1, linenum = 0;
FILE *file = fopen(path, "r");
if (!file) {
LOG_ERROR_SCT("PARSER", "Failed to open config file '%s'", path);
return 1;
}
/* Parse the file */
while (NULL != fgets(line, INI_MAX_LINE_LEN, file)) {
++linenum;
start = skip(strip(line));
/* Skip comments */
if (strspn(start, ";#"))
continue;
/* Handle sections */
else if (start[0] == '[') {
end = &start[1 + strcspn(start + 1, "]")];
if (end[0] == ']') {
}
else {
LOG_ERROR_SCT("PARSER", "Invalid section name at line '%d'", linenum);
}
}
/* Handle others */
else if (start[0]) {
}
}
/* End of the function */
error = 0;
error:
fclose(file);
if (error)
LOG_ERROR_SCT("PARSER", "An error occured while parsing the file '%s'", path);
return error;
}
int
load_so(const char *const mod_path, const char *const mod_init, void *mod)
......@@ -67,7 +132,6 @@ validate_conf(volatile sqlite3 *db)
}
CHK_OPTION("externals", "mkvpropedit");
CHK_OPTION("externals", "sqlite3");
CHK_OPTION("server", "host");
CHK_OPTION("server", "port");
......@@ -87,14 +151,8 @@ validate_conf(volatile sqlite3 *db)
}
static int
#if INI_HANDLER_LINENO
handler(void *user, const char *section, const char *name, const char *value, int lineno)
{
UNUSED(lineno);
#else
handler(void *user, const char *section, const char *name, const char *value)
handler(volatile sqlite3 *user, const char *section, const char *name, const char *value)
{
#endif
RETURN_UNLESS(section && name && value, "I can't complete the database with incomplete lines", 1);
RETURN_UNLESS(database_config_set(user, section, name, value), "Failed to update the database", 0);
return 1;
......@@ -166,7 +224,7 @@ found:
int
config_new(volatile sqlite3 *db, const char *conf)
{
if (ini_parse(conf, handler, (void *) db)) {
if (ini_parse(conf, handler, db)) {
LOG_ERROR("Failed to parse file %s", conf);
return 1;
}
......
......@@ -110,48 +110,14 @@ ini_parse_stream(ini_reader reader, void *stream, ini_handler handler,
/* Scan through stream line by line */
while (reader(line, (int)max_line, stream) != NULL) {
#if INI_ALLOW_REALLOC && !INI_USE_STACK
offset = strlen(line);
while (offset == max_line - 1 && line[offset - 1] != '\n') {
max_line *= 2;
if (max_line > INI_MAX_LINE)
max_line = INI_MAX_LINE;
new_line = realloc(line, max_line);
if (!new_line) {
free(line);
return -2;
}
line = new_line;
if (reader(line + offset, (int)(max_line - offset), stream) == NULL)
break;
if (max_line >= INI_MAX_LINE)
break;
offset += strlen(line + offset);
}
#endif
lineno++;
start = line;
#if INI_ALLOW_BOM
if (lineno == 1 && (unsigned char)start[0] == 0xEF &&
(unsigned char)start[1] == 0xBB &&
(unsigned char)start[2] == 0xBF)
start += 3;
#endif
start = lskip(rstrip(start));
if (strchr(INI_START_COMMENT_PREFIXES, *start)) {
/* Start-of-line comment */
}
#if INI_ALLOW_MULTILINE
else if (*prev_name && *start && start > line) {
/* Non-blank line with leading whitespace, treat as continuation
of previous name's value (as per Python configparser). */
if (!HANDLER(user, section, prev_name, start) && !error)
error = lineno;
}
#endif
else if (*start == '[') {
/* A "[section]" line */
end = find_chars_or_comment(start + 1, "]");
......@@ -159,10 +125,6 @@ ini_parse_stream(ini_reader reader, void *stream, ini_handler handler,
*end = '\0';
strncpy0(section, start + 1, sizeof(section));
*prev_name = '\0';
#if INI_CALL_HANDLER_ON_NEW_SECTION
if (!HANDLER(user, section, NULL, NULL) && !error)
error = lineno;
#endif
} else if (!error) {
/* No ']' found on section line */
error = lineno;
......@@ -188,14 +150,7 @@ ini_parse_stream(ini_reader reader, void *stream, ini_handler handler,
error = lineno;
} else if (!error) {
/* No '=' or ':' found on name[=:]value line */
#if INI_ALLOW_NO_VALUE
*end = '\0';
name = rstrip(start);
if (!HANDLER(user, section, name, NULL) && !error)
error = lineno;
#else
error = lineno;
#endif
}
}
......@@ -205,10 +160,6 @@ ini_parse_stream(ini_reader reader, void *stream, ini_handler handler,
#endif
}
#if !INI_USE_STACK
free(line);
#endif
return error;
}
......
0% Chargement en cours ou .
You are about to add 0 people to the discussion. Proceed with caution.
Veuillez vous inscrire ou vous pour commenter