From b9e2b1c0e39053dbedb27efdc10e13d86281c1c0 Mon Sep 17 00:00:00 2001 From: Wim Taymans Date: Wed, 16 Nov 2016 09:43:51 +0100 Subject: [PATCH] Remove glib from config loading --- pinos/client/transport.h | 5 ++++ pinos/client/utils.c | 20 ++++++++++++++ pinos/client/utils.h | 3 +++ pinos/daemon/daemon-config.c | 52 +++++++++++++++++++++++------------- pinos/daemon/daemon-config.h | 10 ++++--- 5 files changed, 67 insertions(+), 23 deletions(-) diff --git a/pinos/client/transport.h b/pinos/client/transport.h index 4ecb79d8e..369ba66a1 100644 --- a/pinos/client/transport.h +++ b/pinos/client/transport.h @@ -48,6 +48,11 @@ typedef struct { size_t size; } PinosTransportInfo; +/** + * PinosTransportArea: + * + * Shared structure between client and server + */ struct _PinosTransportArea { unsigned int max_inputs; unsigned int n_inputs; diff --git a/pinos/client/utils.c b/pinos/client/utils.c index c4475bed4..2824dd9a8 100644 --- a/pinos/client/utils.c +++ b/pinos/client/utils.c @@ -81,3 +81,23 @@ pinos_free_strv (char **str) free (str[i]); free (str); } + +char * +pinos_strip (char *str, + const char *whitespace) +{ + char *e, *l = NULL; + + str += strspn (str, whitespace); + + for (e = str; *e; e++) + if (!strchr (whitespace, *e)) + l = e; + + if (l) + *(l+1) = '\0'; + else + *str = '\0'; + + return str; +} diff --git a/pinos/client/utils.h b/pinos/client/utils.h index a14e07b72..ab5d75ec0 100644 --- a/pinos/client/utils.h +++ b/pinos/client/utils.h @@ -36,6 +36,9 @@ char ** pinos_split_strv (const char *str, int *n_tokens); void pinos_free_strv (char **str); +char * pinos_strip (char *str, + const char *whitespace); + #ifdef __cplusplus } /* extern "C" */ #endif diff --git a/pinos/daemon/daemon-config.c b/pinos/daemon/daemon-config.c index d9680c074..d9b3fa7b6 100644 --- a/pinos/daemon/daemon-config.c +++ b/pinos/daemon/daemon-config.c @@ -23,6 +23,7 @@ #endif #include +#include #include #include @@ -48,7 +49,7 @@ parse_line (PinosDaemonConfig *config, *p = '\0'; /* remove whitespaces */ - g_strstrip (line); + pinos_strip (line, "\n\r \t"); if (*line == '\0') /* empty line */ return true; @@ -112,29 +113,42 @@ pinos_daemon_config_load_file (PinosDaemonConfig *config, const char *filename, char **err) { - char *data; - char **lines; - bool ret = true; - unsigned int i; - int n_lines; + unsigned int line; + FILE *f; + char buf[4096]; - pinos_log_debug ("deamon-config %p loading file %s", config, filename); + pinos_log_debug ("deamon-config %p: loading configuration file '%s'", config, filename); - if (!g_file_get_contents (filename, &data, NULL, NULL)) - return false; - - lines = pinos_split_strv (data, "\n", 0, &n_lines); - for (i = 0; lines[i] != NULL; i++) { - if (!parse_line (config, filename, lines[i], i+1, err)) { - ret = false; - break; - } + if ((f = fopen (filename, "r")) == NULL) { + asprintf (err, "failed to open configuration file '%s': %s", filename, strerror (errno)); + goto open_error; } - pinos_free_strv (lines); - free (data); + line = 0; - return ret; + while (!feof(f)) { + if (!fgets(buf, sizeof (buf), f)) { + if (feof(f)) + break; + + asprintf (err, "failed to read configuration file '%s': %s", filename, strerror (errno)); + goto read_error; + } + + line++; + + if (!parse_line (config, filename, buf, line, err)) + goto parse_failed; + } + fclose (f); + + return true; + +parse_failed: +read_error: + fclose (f); +open_error: + return false; } /** diff --git a/pinos/daemon/daemon-config.h b/pinos/daemon/daemon-config.h index f7358c509..15ff004b6 100644 --- a/pinos/daemon/daemon-config.h +++ b/pinos/daemon/daemon-config.h @@ -21,9 +21,9 @@ #ifndef __PINOS_DAEMON_CONFIG_H__ #define __PINOS_DAEMON_CONFIG_H__ -#include - -G_BEGIN_DECLS +#ifdef __cplusplus +extern "C" { +#endif #include @@ -42,7 +42,9 @@ bool pinos_daemon_config_load (PinosDaemonConfig *confi char **err); bool pinos_daemon_config_run_commands (PinosDaemonConfig *config, PinosDaemon *daemon); +#ifdef __cplusplus +} +#endif -G_END_DECLS #endif /* __PINOS_DAEMON_CONFIG_H__ */