diff options
| author | Alexander Pickering <alexandermpickering@gmail.com> | 2018-10-25 12:08:54 -0400 |
|---|---|---|
| committer | Alexander Pickering <alexandermpickering@gmail.com> | 2018-10-25 12:08:54 -0400 |
| commit | b9899c6cbe2f694c9db36e9d4e15c532d10b546f (patch) | |
| tree | 248564d6bd22dd6f11459a51f73a144ae91756fc /fbuf.c | |
| parent | d416a80324225d0c64c5021e74773a2e768de73a (diff) | |
| download | libctemplates-b9899c6cbe2f694c9db36e9d4e15c532d10b546f.tar.gz libctemplates-b9899c6cbe2f694c9db36e9d4e15c532d10b546f.tar.bz2 libctemplates-b9899c6cbe2f694c9db36e9d4e15c532d10b546f.zip | |
Started refactoring code
Added a src/ and build/ directory
Added a include/ directory
Included file is smaller
Diffstat (limited to 'fbuf.c')
| -rw-r--r-- | fbuf.c | 121 |
1 files changed, 0 insertions, 121 deletions
@@ -1,121 +0,0 @@ -#include "fbuf.h" - -void bputc(struct TMPL_buf* b,char s){ - if(b->head == NULL){ - struct LLNode* new = (struct LLNode*)malloc(sizeof(struct LLNode)); - new->length = 1; - new->data = (char*)malloc(sizeof(char)); - new->data[0] = s; - new->next = NULL; - b->total_len = 1; - b->head = new; - b->tail = new; - }else{ - struct LLNode* last = b->tail; - struct LLNode* new = (struct LLNode*)malloc(sizeof(struct LLNode)); - new->length = 1; - new->data = (char*)malloc(sizeof(char)); - new->data[0] = s; - new->next = NULL; - b->total_len += new->length; - last->next = new; - b->tail = new; - } -} - -void bputsn(struct TMPL_buf* b, char* s, size_t size){ - if(b->head == NULL){ - struct LLNode* new = (struct LLNode*)malloc(sizeof(struct LLNode)); - new->length = size; - new->data = (char*)malloc(sizeof(char)*size); - memcpy(new->data,s,size); - b->total_len = size; - b->head = new; - b->tail = new; - new->next = NULL; - }else{ - struct LLNode* last = b->tail; - struct LLNode* new = (struct LLNode*)malloc(sizeof(struct LLNode)); - new->length = size; - new->data = (char*)malloc(sizeof(char)*size); - memcpy(new->data,s,size); - last->next = new; - new->next = NULL; - b->tail = new; - b->total_len += size; - } -} - -void bputs(struct TMPL_buf* b, char* s){ - size_t len = strlen(s); - bputsn(b,s,len); -} - -struct TMPL_buf* alloc_tmpl_buf(){ - struct TMPL_buf* ret = (struct TMPL_buf*)malloc(sizeof(struct TMPL_buf)); - ret->total_len = 0; - ret->head = NULL; - ret->tail = NULL; - return ret; -} - -void free_llnodes(struct LLNode* l){ - struct LLNode* cursor = l; - struct LLNode* next; - while(cursor != NULL){ - next = cursor->next; - free(cursor->data); - free(cursor); - cursor = next; - } -} - -void free_tmpl_buf(struct TMPL_buf* b){ - free_llnodes(b->head); - free(b); -} - -void bprint(struct TMPL_buf* b){ - printf("-------------\n"); - struct LLNode* cursor = b->head; - while(cursor != NULL){ - printf("Cursor is %p\n",(void*)cursor); - printf("Length:%d\nData:",(int)cursor->length); - char* cb = cursor->data; - size_t cn; - for(cn = 0; cn < cursor->length; cn++){ - printf("%c",*cb); - cb++; - } - cursor = cursor->next; - printf("\n-------------\n"); - } - printf("Done printing\n"); - printf("-----------------------\n"); - -} - -char* bstringify(struct TMPL_buf* b, size_t* size){ - struct LLNode* cursora = b->head; - size_t cursorb = 0; - - struct LLNode* single = (struct LLNode*)malloc(sizeof(struct LLNode)); - single->length = b->total_len; - single->data = (char*)malloc(sizeof(char)*(b->total_len + 1)); - single->data[b->total_len] = '\0'; - single->next = NULL; - - while(cursora != NULL){ - memcpy(single->data + cursorb,cursora->data,cursora->length); - cursorb += cursora->length; - cursora = cursora->next; - } - - free_llnodes(b->head); - - b->head = single; - b->tail = single; - *size = b->total_len; - - return single->data; -} |
