diff --git a/cmarkdown.c b/cmarkdown.c index 95177cf..be7857c 100644 --- a/cmarkdown.c +++ b/cmarkdown.c @@ -9,8 +9,12 @@ #include #include -#define BUFFERSIZE 1024 +#define BUFFERSIZE 512 #define LENGTH(x) sizeof(x)/sizeof(x[0]) +#define ADDC(b,i) if((i + 1) % BUFFERSIZE == 0) \ + { b = realloc(b,((i + 1)+ BUFFERSIZE) * sizeof(b)); if(!b) eprint("Malloc failed."); } \ + b[i] + typedef unsigned int (*Parser)(const char *, const char *); struct Tag { @@ -19,6 +23,7 @@ struct Tag { char *tag; }; + void eprint(const char *format, ...); /* Prints error and exits */ void hprint(const char *begin, const char *end); /* escapes HTML and prints it to stdout*/ unsigned int doamp(const char *begin, const char *end); /* Parser for & */ @@ -138,19 +143,19 @@ dolineprefix(const char *begin, const char *end) { continue; if(strncmp(lineprefix[i].search,p+1,l)) continue; - if(!(buffer = malloc(end - p+1))) + if(!(buffer = malloc(BUFFERSIZE))) eprint("Malloc failed."); buffer[0] = '\0'; printf("\n<%s>",lineprefix[i].tag); for(j = 0; p != end; p++, j++) { - buffer[j] = *p; + ADDC(buffer,j) = *p; if(*p == '\n') { if(strncmp(lineprefix[i].search,p+1,l) != 0) break; p += l; } } - buffer[j] = '\0'; + ADDC(buffer,j) = '\0'; if(lineprefix[i].process) process(buffer,buffer+strlen(buffer)); else @@ -227,7 +232,7 @@ dolist(const char *begin, const char *end) { for(p++; *p && p != end && *p == ' '; p++); indent = p - q - 1; - if(!(buffer = malloc(end - q+1))) + if(!(buffer = malloc(BUFFERSIZE))) eprint("Malloc failed."); puts(ul ? "