#include "buf.h" /* bbuf* bbuf_new(int size) { bbuf *ret = NULL; ret = malloc(sizeof(bbuf)); if (!ret) { return NULL; } ret->buf = malloc(size); if (!ret->buf) { free(ret); return NULL; } ret->buf_size = size; ret->size = 0; return ret; } //set buffer value int bbuf_set(bbuf *a, char *val, int size) { if (!a) return -1; if (!a->buf) return -1; if (size<0) return -1; if (size > a->buf_size) { a->size = a->buf_size; } else { a->size = size; } memcpy(a->buf, val, a->size); return a->size; } //get copy of buffer bbuf *bbuf_copy(bbuf *buf) { bbuf *ret = NULL; if (!buf) { return NULL; } ret = malloc(sizeof(bbuf)); memcpy(ret, buf, sizeof(bbuf)); ret->buf = malloc(buf->buf_size); if (!ret->buf) { free(ret); return NULL; } memcpy(ret->buf, buf->buf, buf->buf_size); ret->size = buf->size; ret->buf_size = buf->buf_size; return ret; } //get buffer from buffer, mapped pointer, plz dont modify int bbuf_get(bbuf *buf, char **val, int *size) { if (!buf) { return -1; } if (!val) { return -1; } *size = buf->size; *val = buf->buf; return 0; } //resize buffer int bbuf_realloc(bbuf *buf, int size) { int ret = -1; char *ptr=NULL; if (!buf) return -1; if (size < 0) return -1; if (buf->buf_size == size) return size; ret = buf->buf_size; ptr = realloc(buf->buf, size); if (ptr != NULL) { buf->buf = ptr; ret = size; } return ret; } int bbuf_reduce(bbuf *buf) { printf("Not implemented\n"); return -1; } //increase buffer for size int bbuf_inc(bbuf *a, char *b, int size) { printf("Not implemented\n"); return -1; } //decrease buffer for size int bbuf_dec(bbuf *a, char *b, int size) { printf("Not implemented\n"); return -1; } int bbuf_set_max(bbuf *buf) { if (buf == NULL) { return -1; } buf->buf_size = buf->size; return 0; } int bbuf_memset(bbuf *buf, char c) { if (buf == NULL) { return -1; } memset(buf->buf, c, buf->size); return 0; } int bbuf_concat(bbuf *a, bbuf *b) { printf("Not implemented\n"); return -1; } int bbuf_size(bbuf *a) { if (a == NULL) { return -1; } //return used size return a->buf_size; } int bbuf_print(bbuf *a) { int i; if (NULL == a) { return -1; } for (i=0;ibuf_size;i++) { char ch = a->buf[i]; if (isalpha(ch)) { printf("%c",ch); } else { printf("."); } } return 0; } //free buffer void bbuf_free(bbuf *buf) { if (buf == NULL) { return; } if (buf->buf != NULL) { memset(buf->buf, 0, buf->buf_size); free(buf->buf); buf->buf = NULL; memset(buf, 0, sizeof(bbuf)); free(buf); buf = NULL; } } */ Buf::Buf(int size) { this->buf = (char *)malloc(size); if (!this->buf) { //put error here } this->buf_size = size; this->cur_size = 0; } Buf::~Buf() { if (this->buf != NULL) { ::memset(this->buf, 0, this->buf_size); free(this->buf); this->buf = NULL; } } int Buf::set(char *val, int size) { if (!val) { return -1; } if (size<0) { return -1; } if (size > this->buf_size) { this->cur_size = this->buf_size; } else { this->cur_size = size; } memcpy(this->buf, val, this->cur_size); return this->cur_size; } int Buf::set(char *val) { int size = strlen(val); if (size > this->buf_size) { this->cur_size = this->buf_size; } else { this->cur_size = size; } memcpy(this->buf, val, this->cur_size); return this->cur_size; } Buf* Buf::copy() { Buf *ret = new Buf(this->buf_size); char *data=NULL; int data_size=0; this->get_ptr(&data, &data_size); ret->set(data, data_size); data=NULL; return ret; } int Buf::get_ptr(char **val, int *size) { if (!val) { return -1; } *val = this->buf; *size = this->cur_size; return 0; } int Buf::realloc(int size) { char *ptr=NULL; if (size < 0) { return -1; } if (this->buf_size == size) { return this->buf_size; } ptr = (char *)::realloc(this->buf, size); if (ptr != NULL) { free(this->buf); this->buf = ptr; this->buf_size = size; } return this->buf_size; } int Buf::reduce() { printf("Not implemented\n"); return -1; } int Buf::inc(int size) { printf("Not implemented\n"); return -1; } int Buf::dec(int size) { printf("Not implemented\n"); return -1; } int Buf::set_max() { this->cur_size = this->buf_size; return 0; } int Buf::memset(char c) { ::memset(this->buf, c, this->cur_size); return 0; } int Buf::concat(Buf *b) { int i=0,j=this->cur_size; int get_size=b->cursize(); for (i=0;igetc(i,&tc); if (r==0) { this->pushc(tc); if (j+1>=this->buf_size) { break; } } } return i; } int Buf::size() { return this->buf_size; } int Buf::cursize() { return this->cur_size; } int Buf::print() { int i; for (i=0;ibuf_size;i++) { char ch = this->buf[i]; if (isalpha(ch)) { printf("%c",ch); } else { printf("."); } } return 0; } int Buf::setc(int idx, char c) { if ((idx < 0) || (idx > this->cur_size)) { return -1; } buf[idx] = c; return 0; } int Buf::pushc(char c) { if (this->cur_sizebuf_size) { this->buf[this->cur_size] = c; this->cur_size+=1; return 0; } return -1; } int Buf::getc(int idx, char *c) { if ((idx < 0) || (idx > this->cur_size)) { return -1; } *c = buf[idx]; return 0; } int Buf::findc(char c, int *idx) { int i=0; for (i=0;icur_size;i++) { if (buf[i] == c) { //found *idx = i; return 1; } } //not found return 0; } int Buf::popsubstring(int sz, char **val, int *size) { int ret=-1; if (sz>this->cur_size) { sz = this->cur_size; } *val = (char *)malloc(sz); memcpy(*val,buf,sz); *size = sz; shiftleft(sz); ret = sz; return ret; } int Buf::shiftleft(int n) { int i=0; if ((n>this->cur_size) || (n<0)) { //cant shift return -1; } int mvsz = cur_size-n; for (i=0;icur_size -= n; //amount of shifted bytes return n; } int Buf::zero() { this->memset(0); return 0; } bool Buf::isempty() { if (this->buf == NULL) return true; if (this->cur_size <= 0) return true; if (this->buf_size <= 0) return true; return false; } int Buf::empty() { this->cur_size = 0; return 0; } int Buf::set_size(int size) { if (size < 0) return -1; if (size > this->buf_size) return -1; this->cur_size = size; return 0; }