diff --git a/c-hdr-parser.c b/c-hdr-parser.c index 9fb5736..1a0ec4f 100644 --- a/c-hdr-parser.c +++ b/c-hdr-parser.c @@ -340,7 +340,7 @@ char * parse_c_header_sub(char *out, size_t *out_len, const char *sub, size_t su size_t type_len = (lastword - sub_decl[n]); size_t type_array_len = ((sub_decl[n] + strlen(sub_decl[n])) - (lastword + lwlen)); - out = realloc(out, (*out_len) + type_len + type_array_len); + out = realloc(out, (*out_len) + type_len + type_array_len + 1); // add type identifiers and pointers memcpy(out + (*out_len), sub_decl[n], type_len); @@ -349,16 +349,38 @@ char * parse_c_header_sub(char *out, size_t *out_len, const char *sub, size_t su // add array []'s memcpy(out + (*out_len), lastword + lwlen, type_array_len); (*out_len) += type_array_len; - + + // NOTE: temporarily null terminate for calling parse_c_last_word again + out[*out_len] = 0; + + size_t type_offset = *out_len - type_len - type_array_len; + + // check if there are any words in the type string. If not, lastword is part of type instead + size_t type_lwlen; + char * type_lastword = parse_c_last_word(&type_lwlen, out + type_offset); + + //fwrite(type_lastword, 1, type_lwlen, stdout); + //putchar('\n'); + out = realloc(out, (*out_len) + 2 + lwlen); - out[(*out_len)] = ','; - (*out_len)++; + + // if lastword is a variable identifier + if ( type_lastword != NULL ) { + out[(*out_len)] = ','; + (*out_len)++; + } if ( lwlen > 0 ) { // add identifier memcpy(out + (*out_len), lastword, lwlen); (*out_len) += lwlen; } + + // if lastword is part of the type + if ( type_lastword == NULL ) { + out[(*out_len)] = ','; + (*out_len)++; + } out[(*out_len)] = ';'; (*out_len)++; @@ -461,6 +483,9 @@ char ** parse_c_header(size_t *ndecl, FILE *f) { // remove any space between the function identifier and '(' if ( slen > 0 && s[slen-1] == ' ' ) slen--; + //fwrite(s, 1, slen, stdout); + //putchar('\n'); + s = realloc(s, slen + 1); s[slen] = '('; slen++;