{
FILE *fd = NULL;
char line[4096];
- char *p, *a, *b;
- int ret;
+ char *p, *a;
+ int ret, index = 0, len, i, lineno = 0;
fd = fopen(file, "r");
if (fd == NULL)
/* XXX -1 */
while (fgets(line, sizeof(line), fd) != NULL) {
- p = strchr(line, '\n');
- if (p)
+ lineno++;
+ if ((p = strchr(line, '\n'))) // Removes trailing \n
*p = '\0';
- if(strlen(line) == strspn(line, " \t")) continue;
-
- p = line;
- while(p && *p == ' ')
- p++;
- a = p;
-
- p = strchr(line, ' ');
- if (!p) {
- ret = edg_wll_SetError(ctx, EINVAL, "Wrong format of mapping file");
- goto end;
- }
- *p++ = '\0';
-
- while(p && *p == ' ')
- p++;
- b = p;
+ index = strspn(line, " \t");
+ if(strlen(line) == index) continue; // Skips empty lines
+ if(line[index]=='#') continue; // Skips commented-out lines
mapping->rules = realloc(mapping->rules, (mapping->num+1) * sizeof(_edg_wll_mapping_rule));
if (mapping->rules == NULL) {
ret = edg_wll_SetError(ctx, ENOMEM, "Not enough memory");
goto end;
}
- if (!(mapping->rules[mapping->num].a = strdup(a)) ||
- !(mapping->rules[mapping->num].b = strdup(b))) {
- ret = edg_wll_SetError(ctx, ENOMEM, "Not enough memory");
- goto end;
+
+ for (i = 0; i < 2; i++) {
+
+ if (i) {
+ if((index + len) < strlen(line)) index = index + len + strspn(line + index + len, " \t"); // Move index to next
+ else {
+ char *errdesc;
+ asprintf(&errdesc, "Wrong format of mapping file, line %d", lineno);
+ ret = edg_wll_SetError(ctx, EINVAL, errdesc);
+ goto end;
+ }
+ }
+
+ len = (line[index] == '"' || line[index] == '\'') ? // Get token length
+ strcspn(line + (++index), "\"'") :
+ strcspn(line + index, " ");
+
+ if(!(a = calloc(sizeof(char*), len + 1))) { // Allocate and check
+ ret = edg_wll_SetError(ctx, ENOMEM, "Not enough memory");
+ goto end;
+ }
+
+ strncpy(a, line + index, len); // Copy token contents and assign
+ if (!i) mapping->rules[mapping->num].a = a;
+ else mapping->rules[mapping->num].b = a;
+
+ if(!strlen(a)) {
+ char *errdesc;
+ asprintf(&errdesc, "Orphaned ID in mapping file, line %d", lineno);
+ ret = edg_wll_SetError(ctx, EINVAL, errdesc);
+ goto end;
+ }
+
+ if(line[index+len] == '"' || line[index+len] == '\'') len++;
}
+
mapping->num++;
}
-
ret = 0;
end:
a = NULL;
for (i = 0; i < ctx->id_mapping.num; i++ ) {
- asprintf(&out_tmp, "%s%s%s%s%s%s%s",
+ asprintf(&out_tmp, "%s%s\"%s\"%s\"%s\"%s%s",
a ? a : "",
- text ? "\"" : "<code>",
+ text ? "" : "<code>",
ctx->id_mapping.rules[i].a,
- text ? "\"=\"" : " ",
+ text ? "=" : " ",
ctx->id_mapping.rules[i].b,
- text ? "\"" : "<code>",
+ text ? "" : "<code>",
i == ctx->id_mapping.num - 1 ? "" : (text ? "," : "<BR>"));
free(a);
a=out_tmp;