From f2580ca3a5ec951216fe7cb2c092b13e8ffbdbe4 Mon Sep 17 00:00:00 2001 From: =?utf8?q?Jos=C3=A9=20Bollo?= Date: Tue, 10 Oct 2017 10:52:12 +0200 Subject: [PATCH] afm-udb: remove comments and join lines of unit files MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit Change-Id: I9d901e074925a666190d5e3007a66aea81ef6253 Signed-off-by: José Bollo --- src/afm-udb.c | 61 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 60 insertions(+), 1 deletion(-) diff --git a/src/afm-udb.c b/src/afm-udb.c index e199ae1..e3d5d77 100644 --- a/src/afm-udb.c +++ b/src/afm-udb.c @@ -263,6 +263,65 @@ error: return -1; } +/* + * read a unit file + */ +static int read_unit_file(const char *path, char **content, size_t *length) +{ + int rc, st; + char c, *read, *write; + + /* read the file */ + rc = getfile(path, content, length); + if (rc >= 0) { + /* removes any comment and join lines */ + st = 0; + read = write = *content; + for (;;) { + do { c = *read++; } while (c == '\r'); + if (!c) + break; + switch (st) { + case 0: + if (c == ';' || c == '#') { + st = 3; /* removes lines starting with ; or # */ + break; + } + if (c == '\n') + break; /* removes empty lines */ +enter_state_1: + st = 1; + /*@fallthrough@*/ + case 1: + if (c == '\\') + st = 2; + else { + *write++ = c; + if (c == '\n') + st = 0; + } + break; + case 2: + if (c == '\n') + c = ' '; + else + *write++ = '\\'; + goto enter_state_1; + case 3: + if (c == '\n') + st = 0; + break; + } + } + if (st == 1) + *write++ = '\n'; + *write = 0; + *length = (size_t)(write - *content); + *content = realloc(*content, *length + 1); + } + return rc; +} + /* * called for each unit */ @@ -284,7 +343,7 @@ static int update_cb(void *closure, const char *name, const char *path, int isus return 0; /* reads the file */ - rc = getfile(path, &content, &length); + rc = read_unit_file(path, &content, &length); if (rc < 0) return rc; -- 2.16.6