X-Git-Url: https://gerrit.automotivelinux.org/gerrit/gitweb?a=blobdiff_plain;f=src%2Fafm-udb.c;h=7f4a16ea783372a2e0a6188be945f5c3d3a4b7ff;hb=1bb3f2601e862b72cbc6c6dabf515ad9f2f6ad1e;hp=be1a5d3179a03e90aecce3bcb9eab7a54221dc0d;hpb=3f53c6fff76d6200ebf2165f5d206684a142db35;p=src%2Fapp-framework-main.git diff --git a/src/afm-udb.c b/src/afm-udb.c index be1a5d3..7f4a16e 100644 --- a/src/afm-udb.c +++ b/src/afm-udb.c @@ -182,17 +182,25 @@ static int add_fields_of_content( { char *name, *value, *read, *write; - read = strstr(content, x_afm_prefix); - while (read) { + /* start at the beginning */ + read = content; + for (;;) { + /* search the next key */ + read = strstr(read, x_afm_prefix); + if (!read) + return 0; + + /* search to equal */ name = read + x_afm_prefix_length; value = strchr(name, '='); if (value == NULL) - read = strstr(name, x_afm_prefix); + read = name; /* not found */ else { + /* get the value (translate it) */ *value++ = 0; read = write = value; while(*read && *read != '\n') { - if (read[0] != '\\') + if (*read != '\\') *write++ = *read++; else { switch(*++read) { @@ -203,13 +211,14 @@ static int add_fields_of_content( read += !!*read; } } - read = strstr(read, x_afm_prefix); + read += !!*read; *write = 0; + + /* add the found field now */ if (add_field(priv, pub, name, value) < 0) return -1; } } - return 0; } /* @@ -275,64 +284,76 @@ error: } /* - * read a unit file + * Crop and trim unit 'content' of 'length'. Return the new length. */ -static int read_unit_file(const char *path, char **content, size_t *length) +static size_t crop_and_trim_unit_content(char *content, size_t length) { - int rc, st; + int st; char c, *read, *write; - /* read the file */ - rc = getfile(path, content, length); - if (rc >= 0) { - /* removes any comment and join continued lines */ - st = 0; - read = write = *content; - for (;;) { - do { c = *read++; } while (c == '\r'); - if (!c) + /* removes any comment and join continued lines */ + st = 0; + read = write = content; + for (;;) { + do { c = *read++; } while (c == '\r'); + if (!c) + break; + switch (st) { + case 0: + /* state 0: begin of a line */ + if (c == ';' || c == '#') { + st = 3; /* removes lines starting with ; or # */ break; - switch (st) { - case 0: - /* state 0: begin of a line */ - if (c == ';' || c == '#') { - st = 3; /* removes lines starting with ; or # */ - break; - } - if (c == '\n') - break; /* removes empty lines */ + } + if (c == '\n') + break; /* removes empty lines */ enter_state_1: - st = 1; - /*@fallthrough@*/ - case 1: - /* state 1: emitting a normal line */ - if (c == '\\') - st = 2; - else { - *write++ = c; - if (c == '\n') - st = 0; - } - break; - case 2: - /* state 2: character after '\' */ - if (c == '\n') - c = ' '; - else - *write++ = '\\'; - goto enter_state_1; - case 3: - /* state 3: inside a comment, wait its end */ + st = 1; + /*@fallthrough@*/ + case 1: + /* state 1: emitting a normal line */ + if (c == '\\') + st = 2; + else { + *write++ = c; if (c == '\n') st = 0; - break; } + break; + case 2: + /* state 2: character after '\' */ + if (c == '\n') + c = ' '; + else + *write++ = '\\'; + goto enter_state_1; + case 3: + /* state 3: inside a comment, wait its end */ + if (c == '\n') + st = 0; + break; } - if (st == 1) - *write++ = '\n'; - *write = 0; - *length = (size_t)(write - *content); - *content = realloc(*content, *length + 1); + } + if (st == 1) + *write++ = '\n'; + *write = 0; + return (size_t)(write - content); +} + +/* + * read a unit file + */ +static int read_unit_file(const char *path, char **content, size_t *length) +{ + int rc; + size_t nl; + + /* read the file */ + rc = getfile(path, content, length); + if (rc >= 0) { + /* crop and trim it */ + *length = nl = crop_and_trim_unit_content(*content, *length); + *content = realloc(*content, nl + 1); } return rc; } @@ -474,6 +495,9 @@ error: return -1; } +/* + * set the default language to 'lang' + */ void afm_udb_set_default_lang(const char *lang) { char *oldval = default_lang;