diff options
Diffstat (limited to 'buf.c')
-rw-r--r-- | buf.c | 246 |
1 files changed, 245 insertions, 1 deletions
@@ -1,6 +1,6 @@ #include "buf.h" - +/* bbuf* bbuf_new(int size) { bbuf *ret = NULL; @@ -136,7 +136,27 @@ int bbuf_dec(bbuf *a, char *b, int size) 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) { @@ -196,4 +216,228 @@ void bbuf_free(bbuf *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(&data, &data_size); + ret->set(data, data_size); + data=NULL; + return ret; +} + +int Buf::get(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) + { + 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;i<get_size;i++) + { + char tc; + int r = b->getc(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;i<this->buf_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_size<this->buf_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; +}
\ No newline at end of file |