kore

Kore is a web application platform for writing scalable, concurrent web based processes in C or Python.
Commits | Files | Refs | README | LICENSE | git clone https://git.kore.io/kore.git

commit 40f69924bb612938951093541d73f52015526970
parent 17e311dba92131a527330fc0643f7ff7677e8e87
Author: Joris Vink <joris@coders.se>
Date:   Mon,  1 Feb 2016 22:10:10 +0100

Move shared file reading code to kore_read_line()

Diffstat:
includes/kore.h | 1+
src/cli.c | 14++------------
src/config.c | 14++------------
src/utils.c | 27+++++++++++++++++++++++++++
4 files changed, 32 insertions(+), 24 deletions(-)

diff --git a/includes/kore.h b/includes/kore.h @@ -516,6 +516,7 @@ int kore_base64_encode(u_int8_t *, u_int32_t, char **); int kore_base64_decode(char *, u_int8_t **, u_int32_t *); void *kore_mem_find(void *, size_t, void *, u_int32_t); char *kore_text_trim(char *, size_t); +char *kore_read_line(FILE *, char *, size_t); #if !defined(KORE_NO_HTTP) void kore_websocket_handshake(struct http_request *, diff --git a/src/cli.c b/src/cli.c @@ -1053,20 +1053,10 @@ cli_buildopt_parse(const char *path) bopt = NULL; - while (fgets(buf, sizeof(buf), fp) != NULL) { - p = buf; - buf[strcspn(buf, "\n")] = '\0'; - - while (isspace(*p)) - p++; - if (p[0] == '#' || p[0] == '\0') + while ((p = kore_read_line(fp, buf, sizeof(buf))) != NULL) { + if (strlen(p) == 0) continue; - for (t = p; *t != '\0'; t++) { - if (*t == '\t') - *t = ' '; - } - if (bopt != NULL && !strcmp(p, "}")) { bopt = NULL; continue; diff --git a/src/config.c b/src/config.c @@ -198,22 +198,12 @@ kore_parse_config_file(const char *fpath) kore_debug("parsing configuration file '%s'", fpath); lineno = 1; - while (fgets(buf, sizeof(buf), fp) != NULL) { - p = buf; - buf[strcspn(buf, "\n")] = '\0'; - - while (isspace(*p)) - p++; - if (p[0] == '#' || p[0] == '\0') { + while ((p = kore_read_line(fp, buf, sizeof(buf))) != NULL) { + if (strlen(p) == 0) { lineno++; continue; } - for (t = p; *t != '\0'; t++) { - if (*t == '\t') - *t = ' '; - } - #if !defined(KORE_NO_HTTP) if (!strcmp(p, "}") && current_handler != NULL) { lineno++; diff --git a/src/utils.c b/src/utils.c @@ -513,6 +513,33 @@ kore_text_trim(char *string, size_t len) return (string); } +char * +kore_read_line(FILE *fp, char *in, size_t len) +{ + char *p, *t; + + if (fgets(in, len, fp) == NULL) + return (NULL); + + p = in; + in[strcspn(in, "\n")] = '\0'; + + while (isspace(*p)) + p++; + + if (p[0] == '#' || p[0] == '\0') { + p[0] = '\0'; + return (p); + } + + for (t = p; *t != '\0'; t++) { + if (*t == '\t') + *t = ' '; + } + + return (p); +} + void fatal(const char *fmt, ...) {