diff options
author | FreeArtMan <dos21h@gmail.com> | 2019-05-12 15:05:57 +0100 |
---|---|---|
committer | FreeArtMan <dos21h@gmail.com> | 2019-05-12 15:05:57 +0100 |
commit | 082dee0658b67744ab3428eea964f375f7aab9dc (patch) | |
tree | 6dd58d5044513ec7bef3b445fefa7003e33ebeed /buf.c | |
parent | 654bb8b09dd90c12c3a51ffdce800b2255b753b5 (diff) | |
download | libbuf-082dee0658b67744ab3428eea964f375f7aab9dc.tar.gz libbuf-082dee0658b67744ab3428eea964f375f7aab9dc.zip |
Added more functionality to Buf, substring, search for characer, and shifting string.
Diffstat (limited to 'buf.c')
-rw-r--r-- | buf.c | 58 |
1 files changed, 58 insertions, 0 deletions
@@ -440,4 +440,62 @@ int Buf::getc(int idx, char *c) *c = buf[idx]; return 0; +} + +int Buf::findc(char c, int *idx) +{ + int i=0; + + for (i=0;i<this->cur_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;i<mvsz;i++) + { + char c = buf[n+i]; + printf("%c",c); + buf[i] = c; + } + this->cur_size -= n; + + //amount of shifted bytes + return n; }
\ No newline at end of file |