c-hdr-parser: handle enums

This commit is contained in:
icst 2024-07-01 23:20:55 -04:00
parent dbcbfeeab5
commit 0b7c154dc8

View file

@ -435,6 +435,7 @@ char ** parse_c_header(size_t *ndecl, FILE *f) {
// We need to search leading keywords to see if struct/union is found
int found_struct = 0;
int found_union = 0;
int found_enum = 0;
size_t word_len = 0;
char *p = s, *prev_p = s;
@ -450,6 +451,11 @@ char ** parse_c_header(size_t *ndecl, FILE *f) {
break;
}
if ( word_len == (sizeof("enum")-1) && 0 == strncmp("enum", prev_p, word_len) ) {
found_enum = 1;
break;
}
prev_p = p;
}
@ -468,6 +474,63 @@ char ** parse_c_header(size_t *ndecl, FILE *f) {
slen++;
}
if ( found_enum ) {
s = realloc(s, slen+1);
s[slen] = '{';
slen++;
// in C all enums store values as 'int'
static const char typestr[] = "int, ";
char * p = body;
char * end_ptr;
while ( (end_ptr = memchr(p, ',', body_len - (p - body))) != NULL ) {
char * tmpp = memchr(p, '=', body_len - (end_ptr - body));
if ( tmpp != NULL ) *tmpp = 0;
else *end_ptr = 0;
size_t lwlen;
char * lastword = parse_c_last_word(&lwlen, p);
s = realloc(s, slen + (sizeof(typestr)-1) + lwlen + 1);
memcpy(s + slen, typestr, sizeof(typestr)-1);
slen += (sizeof(typestr)-1);
memcpy(s + slen, lastword, lwlen);
slen += lwlen;
s[slen] = ';';
slen++;
p = end_ptr + 1;
}
size_t lwlen;
char * lastword = parse_c_last_word(&lwlen, p);
if ( lastword != NULL ) {
s = realloc(s, slen + (sizeof(typestr)-1) + lwlen + 2);
memcpy(s + slen, typestr, sizeof(typestr)-1);
slen += (sizeof(typestr)-1);
memcpy(s + slen, lastword, lwlen);
slen += lwlen;
s[slen] = ';';
slen++;
}
s = realloc(s, slen+1);
s[slen] = '}';
slen++;
}
if ( body != NULL ) free( body );
if ( c == EOF ) break;