diff options
Diffstat (limited to 'fbuf.c')
| -rw-r--r-- | fbuf.c | 121 |
1 files changed, 121 insertions, 0 deletions
@@ -0,0 +1,121 @@ +#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; +} |
