Commit fa9811f8 authored by Alejandro Mery's avatar Alejandro Mery
Browse files

fex2bin: parse null and string entries

parent fc0f0d7f
...@@ -23,28 +23,23 @@ ...@@ -23,28 +23,23 @@
#define MAX_LINE 255 #define MAX_LINE 255
/** /** find first not blank char */
*/ static inline char *skip_blank(char *p)
static inline char *alltrim(char *s, size_t *l)
{ {
char *p; while(isblank(*p))
while (isblank(*s)) p++;
s++; return p;
p = s; }
while (*++p)
; /* seek \0 */ /** trim out blank chars at the end of a string */
static inline char *rtrim(const char *s, char *p)
if (p>s+1 && p[-2] == '\r' && p[-1] == '\n') {
p -= 2; if (p>s) {
else if (p>s && p[-1] == '\n') while (p!=s && isblank(*--p))
p -= 1; ;
*p-- = '\0'; *++p='\0';
}
while (p>s && isblank(*p)) return p;
*p-- = '\0';
*l = p-s+1;
return s;
} }
/** /**
...@@ -57,11 +52,24 @@ static int parse_fex(FILE *in, const char *filename, ...@@ -57,11 +52,24 @@ static int parse_fex(FILE *in, const char *filename,
/* TODO: deal with longer lines correctly (specially in comments) */ /* TODO: deal with longer lines correctly (specially in comments) */
for(size_t line = 1; fgets(buffer, sizeof(buffer), in); line++) { for(size_t line = 1; fgets(buffer, sizeof(buffer), in); line++) {
size_t l; char *s = skip_blank(buffer); /* beginning */
char *s = alltrim(buffer, &l); char *pe = s; /* \0... to be found */
if (l == 0 || *s == ';' || *s == '#') if (*pe) while (*++pe)
; ;
if (pe>s && pe[-1] == '\n') {
if (pe>s+1 && pe[-2] == '\r')
pe -= 2;
else
pe -= 1;
*pe = '\0';
}
pe = rtrim(s, pe);
if (pe == s || *s == ';' || *s == '#')
; /* empty */
else if (*s == '[') { else if (*s == '[') {
/* section */ /* section */
char *p = ++s; char *p = ++s;
...@@ -70,7 +78,7 @@ static int parse_fex(FILE *in, const char *filename, ...@@ -70,7 +78,7 @@ static int parse_fex(FILE *in, const char *filename,
if (*p == ']' && *(p+1) == '\0') { if (*p == ']' && *(p+1) == '\0') {
*p = '\0'; *p = '\0';
errf("I: %s:%zu: [%s]\n", filename, line, s); fprintf(stdout, "[%s]\n", s);
} else if (*p) { } else if (*p) {
errf("E: %s:%zu: invalid character at %zu.\n", errf("E: %s:%zu: invalid character at %zu.\n",
filename, line, p-buffer+1); filename, line, p-buffer+1);
...@@ -78,9 +86,33 @@ static int parse_fex(FILE *in, const char *filename, ...@@ -78,9 +86,33 @@ static int parse_fex(FILE *in, const char *filename,
} }
} else { } else {
/* key = value */ /* key = value */
fprintf(stdout, "%s:%zu: ", filename, line); const char *key = s;
fputs(s, stdout); char *mark, *p = s;
fputc('\n', stdout);
while (isalnum(*p) || *p == '_')
p++;
mark = p;
p = skip_blank(p);
if (*p != '=') {
errf("E: %s:%zu: invalid character at %zu.\n",
filename, line, p-buffer+1);
goto parse_error;
}
*mark = '\0'; /* truncate key */
p = skip_blank(p+1);
if (*p == '\0') {
/* NULL */
fprintf(stdout, "%s = NULL\n", key);
} else if (pe > p+1 && *p == '"' && pe[-1] == '"') {
/* string */
p++; *--pe = '\0';
fprintf(stdout, "%s = \"%s\"\n", key, p);
} else {
errf("I: key:%s value: %s (%zu)\n",
key, p, pe-p);
}
} }
}; };
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment