From: Michael Göhler Date: Sat, 16 Aug 2014 16:20:28 +0000 (+0200) Subject: refactoring: only function prototypes belong in header files X-Git-Url: https://git.danieliu.xyz/?a=commitdiff_plain;h=76cedc9066d3fd65788f86ea5d0b71db98bb6e24;p=smdp.git refactoring: only function prototypes belong in header files --- diff --git a/cstring.c b/cstring.c new file mode 100644 index 0000000..e7308ef --- /dev/null +++ b/cstring.c @@ -0,0 +1,46 @@ +#include // strlen +#include // malloc, realloc + +#include "include/cstring.h" + +cstring_t *cstring_init() { + cstring_t *x = malloc(sizeof(cstring_t)); + x->text = (void*)0; + x->size = x->alloc = 0; + x->expand = cstring_expand; + x->expand_arr = cstring_expand_arr; + x->reset = cstring_reset; + x->delete = cstring_delete; + return x; +} + +void cstring_expand(cstring_t *self, char x) { + if(self->size + sizeof(x) + sizeof(char) > self->alloc) { + self->alloc += (REALLOC_ADD * sizeof(char)); + self->text = realloc(self->text, self->alloc); + } + self->text[self->size] = x; + self->text[self->size+1] = '\0'; + self->size = strlen(self->text); +} + +void cstring_expand_arr(cstring_t *self, char *x) { + if(self->size + strlen(x) + sizeof(char) > self->alloc) { + self->alloc += (REALLOC_ADD * sizeof(char)); + self->text = realloc(self->text, self->alloc); + } + self->text = strcat(self->text, x); + self->size = strlen(self->text); +} + +void cstring_reset(cstring_t *self) { + free(self->text); + self->text = (void*)0; + self->size = self->alloc = 0; +} + +void cstring_delete(cstring_t *self) { + free(self->text); + free(self); +} + diff --git a/include/cstring.h b/include/cstring.h index 175cc0b..5e6c3f0 100644 --- a/include/cstring.h +++ b/include/cstring.h @@ -17,9 +17,6 @@ * */ -#include // strlen -#include // malloc, realloc - // The amount of memory allocated from heap when string expansion hits the // allocated memory limit #define REALLOC_ADD 10 @@ -34,50 +31,10 @@ typedef struct _cstring_t { void (*delete)(struct _cstring_t *self); } cstring_t; +cstring_t *cstring_init(); void cstring_expand(cstring_t *self, char x); void cstring_expand_arr(cstring_t *self, char *x); void cstring_reset(cstring_t *self); void cstring_delete(cstring_t *self); -cstring_t *cstring_init() { - cstring_t *x = malloc(sizeof(cstring_t)); - x->text = (void*)0; - x->size = x->alloc = 0; - x->expand = cstring_expand; - x->expand_arr = cstring_expand_arr; - x->reset = cstring_reset; - x->delete = cstring_delete; - return x; -} - -void cstring_expand(cstring_t *self, char x) { - if(self->size + sizeof(x) + sizeof(char) > self->alloc) { - self->alloc += (REALLOC_ADD * sizeof(char)); - self->text = realloc(self->text, self->alloc); - } - self->text[self->size] = x; - self->text[self->size+1] = '\0'; - self->size = strlen(self->text); -} - -void cstring_expand_arr(cstring_t *self, char *x) { - if(self->size + strlen(x) + sizeof(char) > self->alloc) { - self->alloc += (REALLOC_ADD * sizeof(char)); - self->text = realloc(self->text, self->alloc); - } - self->text = strcat(self->text, x); - self->size = strlen(self->text); -} - -void cstring_reset(cstring_t *self) { - free(self->text); - self->text = (void*)0; - self->size = self->alloc = 0; -} - -void cstring_delete(cstring_t *self) { - free(self->text); - free(self); -} - #endif // !defined( CSTRING_H ) diff --git a/include/markdown.h b/include/markdown.h index 9ed9dbf..28b48e0 100644 --- a/include/markdown.h +++ b/include/markdown.h @@ -44,37 +44,13 @@ typedef struct _document_t { page_t *page; } document_t; -line_t *new_line() { - line_t *x = malloc(sizeof(line_t)); - return x; -} - -line_t *next_line(line_t *prev) { - line_t *x = new_line(); - x->prev = prev; - prev->next = x; - return x; -} - -page_t *new_page() { - page_t *x = malloc(sizeof(page_t)); - return x; -} - -page_t *next_page(page_t *prev) { - page_t *x = new_page(); - x->prev = prev; - prev->next = x; - return x; -} - -document_t *new_document() { - document_t *x = malloc(sizeof(document_t)); - return x; -} - +line_t *new_line(); +line_t *next_line(line_t *prev); +page_t *new_page(); +page_t *next_page(page_t *prev); +document_t *new_document(); int is_utf8(char ch); int next_nonblank(cstring_t *text, int i); -document_t * markdown_load(FILE *input); +document_t *markdown_load(FILE *input); #endif // !defined( MARKDOWN_H ) diff --git a/markdown.c b/markdown.c index 62aca6f..3cbdf0d 100644 --- a/markdown.c +++ b/markdown.c @@ -1,132 +1,35 @@ -// needed by tmp.c to process markdown - #include +#include +#include "include/cstring.h" #include "include/markdown.h" -document_t * markdown_load(FILE *input) { - - int c, i, bits; - - document_t *doc; - page_t *page; - line_t *line; - cstring_t *text; - - doc = new_document(); - page = new_page(); - doc->page = page; - text = cstring_init(); - - while ((c = fgetc(input)) != EOF) { - if(c == '\n') { - - // markdown analyse - bits = markdown_analyse(text); - - // if text is markdown hr - if(CHECK_BIT(bits, IS_HR)) { - - // clear text - (text->reset)(text); - // create next page - page = next_page(page); - - } else { - - // if page ! has line - if(!page->line) { - - // create new line - line = new_line(); - page->line = line; - - } else { - - // create next line - line = next_line(line); - - } - - // add text to line - line->text = text; - - // add bits to line - line->bits = bits; - - // calc offset - line->offset = next_nonblank(text, 0); - } - - } else if('\t') { - - // expand tab to spaces - for (i = 0; i <= 4; i++) - (text->expand)(text, ' '); - - } else if(isprint(c) || isspace(c) || is_utf8(c)) { - - // add char to line - (text->expand)(text, c); - } - } - - //TODO detect header - - return doc; +line_t *new_line() { + line_t *x = malloc(sizeof(line_t)); + return x; } -int markdown_analyse(cstring_t *text) { - int c, i, bits, - offset, eol, - equals, hashes, stars, minus, plus, - spaces, other; - - // count leading spaces - offset = next_nonblank(text, 0); - - // IS_CODE - if(offset >= 4) { - SET_BIT(bits, IS_CODE); - return bits; - } - - // strip trailing spaces - for(eol = text->size; eol > offset && isspace(text->text[eol - 1]); eol--); - - for(i = offset; i < eol; i++) { - - switch(text->text[i]) { - case '=': equals++; break; - case '#': hashes++; break; - case '*': stars++; break; - case '-': minus++; break; - case '+': plus++; break; - case ' ': spaces++; break; - default: other++; break; - } - } - - // IS_HR - if((minus >= 3 && equals + hashes + stars + plus == 0) || - (stars >= 3 && equals + hashes + minus + plus == 0)) { - - SET_BIT(bits, IS_HR); - return bits; - } - - //TODO all the other markdown tags +line_t *next_line(line_t *prev) { + line_t *x = new_line(); + x->prev = prev; + prev->next = x; + return x; +} - return bits; +page_t *new_page() { + page_t *x = malloc(sizeof(page_t)); + return x; } -int is_utf8(char ch) { - return (ch & 0x80); +page_t *next_page(page_t *prev) { + page_t *x = new_page(); + x->prev = prev; + prev->next = x; + return x; } -int next_nonblank(cstring_t *text, int i) { - while ((i < text->size) && isspace((text->text)[i])) - ++i; - return i; -}; +document_t *new_document() { + document_t *x = malloc(sizeof(document_t)); + return x; +} diff --git a/markdown_io.c b/markdown_io.c new file mode 100644 index 0000000..af04eed --- /dev/null +++ b/markdown_io.c @@ -0,0 +1,132 @@ +#include +#include + +#include "include/cstring.h" +#include "include/markdown.h" + +document_t *markdown_load(FILE *input) { + + int c, i, bits; + + document_t *doc; + page_t *page; + line_t *line; + cstring_t *text; + + doc = new_document(); + page = new_page(); + doc->page = page; + text = cstring_init(); + + while ((c = fgetc(input)) != EOF) { + if(c == '\n') { + + // markdown analyse + bits = markdown_analyse(text); + + // if text is markdown hr + if(CHECK_BIT(bits, IS_HR)) { + + // clear text + (text->reset)(text); + // create next page + page = next_page(page); + + } else { + + // if page ! has line + if(!page->line) { + + // create new line + line = new_line(); + page->line = line; + + } else { + + // create next line + line = next_line(line); + + } + + // add text to line + line->text = text; + + // add bits to line + line->bits = bits; + + // calc offset + line->offset = next_nonblank(text, 0); + } + + } else if('\t') { + + // expand tab to spaces + for (i = 0; i <= 4; i++) + (text->expand)(text, ' '); + + } else if(isprint(c) || isspace(c) || is_utf8(c)) { + + // add char to line + (text->expand)(text, c); + } + } + + //TODO detect header + + return doc; +} + +int markdown_analyse(cstring_t *text) { + int c, i, bits, + offset, eol, + equals, hashes, stars, minus, plus, + spaces, other; + + // count leading spaces + offset = next_nonblank(text, 0); + + // IS_CODE + if(offset >= 4) { + SET_BIT(bits, IS_CODE); + return bits; + } + + // strip trailing spaces + for(eol = text->size; eol > offset && isspace(text->text[eol - 1]); eol--); + + for(i = offset; i < eol; i++) { + + switch(text->text[i]) { + case '=': equals++; break; + case '#': hashes++; break; + case '*': stars++; break; + case '-': minus++; break; + case '+': plus++; break; + case ' ': spaces++; break; + default: other++; break; + } + } + + // IS_HR + if((minus >= 3 && equals + hashes + stars + plus == 0) || + (stars >= 3 && equals + hashes + minus + plus == 0)) { + + SET_BIT(bits, IS_HR); + return bits; + } + + //TODO all the other markdown tags + + return bits; +} + +int is_utf8(char ch) { + return (ch & 0x80); +} + +int next_nonblank(cstring_t *text, int i) { + while ((i < text->size) && isspace((text->text)[i])) + ++i; + return i; +}; +