summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorFreeArtMan <dos21h@gmail.com>2019-05-12 15:05:57 +0100
committerFreeArtMan <dos21h@gmail.com>2019-05-12 15:05:57 +0100
commit082dee0658b67744ab3428eea964f375f7aab9dc (patch)
tree6dd58d5044513ec7bef3b445fefa7003e33ebeed
parent654bb8b09dd90c12c3a51ffdce800b2255b753b5 (diff)
downloadlibbuf-082dee0658b67744ab3428eea964f375f7aab9dc.tar.gz
libbuf-082dee0658b67744ab3428eea964f375f7aab9dc.zip
Added more functionality to Buf, substring, search for characer, and shifting string.
-rw-r--r--Makefile7
-rw-r--r--buf.c58
-rw-r--r--buf.h3
-rw-r--r--buf_misc.c18
-rw-r--r--buf_misc.h4
-rw-r--r--test.c29
-rw-r--r--test_line.c65
7 files changed, 177 insertions, 7 deletions
diff --git a/Makefile b/Makefile
index 5cc9d05..ad3bde0 100644
--- a/Makefile
+++ b/Makefile
@@ -5,17 +5,18 @@ CPPFLAGS=-fPIC -g3 -Wall -Wwrite-strings -fpermissive -fno-rtti -fno-exceptions
make:
$(CPP) $(CPPFLAGS) -c buf.c
- $(CPP) $(CFLAGS) -c buf_misc.c
+ $(CPP) $(CPPFLAGS) -c buf_misc.c
#$(CPP) $(CFLAGS) -c buf_sys.c
#$(CC) $(CFLAGS) -fPIC -lc -ldl buf_sys.o buf_misc.o buf.o -shared -o libbuf.so
- $(CC) $(CFLAGS) -fPIC -lc -ldl buf.o -shared -o libbuf.so
+ $(CC) $(CFLAGS) -fPIC -lc -ldl buf.o buf_misc.o -shared -o libbuf.so
t: buf.c buf_misc.c buf_sys.c
$(CPP) $(CPPFLAGS) test.c -c
+ $(CPP) $(CPPFLAGS) test_line.c -c
$(CC) $(CFLAGS) buf.o test.o -o test
#$(CC) $(CFLAGS) buf.o buf_misc.o buf_sys.o test_circ.c -o test_circ
#$(CC) $(CFLAGS) buf.o buf_misc.o buf_sys.o test_circ.c -o test_sys
- #$(CC) $(CFLAGS) buf.o buf_misc.o test_line.c -o test_line
+ $(CC) $(CFLAGS) buf.o buf_misc.o test_line.o -o test_line
leak:
valgrind --leak-check=full --track-origins=yes --log-file=log.txt ./test
diff --git a/buf.c b/buf.c
index c9cf59a..2a64b1c 100644
--- a/buf.c
+++ b/buf.c
@@ -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
diff --git a/buf.h b/buf.h
index 212577d..2d92f33 100644
--- a/buf.h
+++ b/buf.h
@@ -76,6 +76,9 @@ public:
int setc(int idx, char c);
int pushc(char c);
int getc(int idx, char *c);
+ int findc(char c, int *idx);
+ int popsubstring(int sz, char **val, int *size);
+ int shiftleft(int n);
};
#endif \ No newline at end of file
diff --git a/buf_misc.c b/buf_misc.c
index bcf7705..dee1b04 100644
--- a/buf_misc.c
+++ b/buf_misc.c
@@ -57,21 +57,26 @@ int bbuf_line_free(bbuf_line *buf)
BufLine::BufLine(int size)
{
- *buf = Buf(size);
+ buf = new Buf(size);
sep=0x0;
memset(pattern,0,BUF_PATTER_SIZE);
}
BufLine::~BufLine()
{
- delete buf;
+
}
-int BufLine::set_pattern(char *pattern)
+int BufLine::setpattern(char *pattern)
{
return -1;
}
+int BufLine::setseperator(char s)
+{
+ this->sep = s;
+ return 0;
+}
int BufLine::add(char *string, int size)
{
@@ -86,6 +91,13 @@ int BufLine::add(Buf *newdata)
int BufLine::pop_line(char **val, int *size)
{
+
+ return -1;
+}
+
+int BufLine::print()
+{
+ buf->print();
return -1;
}
diff --git a/buf_misc.h b/buf_misc.h
index 9eabd97..ef86f42 100644
--- a/buf_misc.h
+++ b/buf_misc.h
@@ -30,10 +30,12 @@ private:
public:
BufLine(int size);
~BufLine();
- int set_pattern(char *pattern);
+ int setpattern(char *pattern);
+ int setseperator(char s);
int add(char *string, int size);
int add(Buf *newdata);
int pop_line(char **val, int *size);
+ int print();
};
diff --git a/test.c b/test.c
index c4dc609..326ff26 100644
--- a/test.c
+++ b/test.c
@@ -141,6 +141,35 @@ int main()
delete b1;
delete b2;
+ printf("Shift string\n");
+
+ char shs1[] = "TheDominionOfTheLife";
+ b1 = new Buf(64);
+
+ b1->set(shs1); b1->print(); printf("\n");
+ printf("%d:",b1->shiftleft(3));
+ b1->print(); printf("\n");
+ printf("%d:",b1->shiftleft(8));
+ b1->print(); printf("\n");
+
+ delete b1;
+
+ printf("Pop substring\n");
+
+ b1 = new Buf(64);
+ char *subs=NULL;
+ int sz;
+
+ b1->set(shs1); b1->print(); printf("\n");
+ printf("%d:",b1->popsubstring(3,&subs,&sz));
+ b1->print(); printf(" substring ");
+ print_s(subs,sz); printf("\n");
+ free(subs);sz=0;
+ printf("%d:",b1->popsubstring(8,&subs,&sz));
+ b1->print(); printf(" substring ");
+ print_s(subs,sz); printf("\n");
+
+ free(subs);sz=0;
printf("End test\n");
return 0;
diff --git a/test_line.c b/test_line.c
new file mode 100644
index 0000000..3c3ccab
--- /dev/null
+++ b/test_line.c
@@ -0,0 +1,65 @@
+#include <stdio.h>
+#include <stdlib.h>
+
+#include "__cpp.h"
+#include "buf.h"
+#include "buf_misc.h"
+#include "debug.h"
+
+void blow_if_error(int err)
+{
+ if (err < 0)
+ {
+ printf("Should be no error but there is\n");
+ exit(1);
+ }
+}
+
+//#define B(X) PNL();blow_if_error(X);
+#define B(X) blow_if_error(X);
+
+void error_is_good(int err)
+{
+ if (err >= 0)
+ {
+ printf("Should be error but there is no\n");
+ exit(1);
+ }
+}
+#define E(X) error_is_good(X);
+
+int main()
+{
+ Buf *b1=NULL, *b2=NULL, *b3=NULL;
+ BufLine *bline=NULL;
+
+ printf("Start test\n");
+ b1 = new Buf(8);
+ b2 = new Buf(8);
+ b3 = new Buf(1);
+ bline = new BufLine(32);
+
+ char n1[] = "Line";
+ char n2[] = "Home";
+ char n3[] = "\n";
+
+ b1->set(n1);
+ b2->set(n2);
+ b3->set(n3);
+ b1->print(); printf("\n");
+ b2->print(); printf("\n");
+ b1->print(); printf("\n");
+ bline->print(); printf("\n");
+
+ printf("Add data\n");
+
+ bline->add(b1); bline->print(); printf("\n");
+ bline->add(b3);
+ bline->add(b2); bline->print(); printf("\n");
+
+ printf("Get line\n");
+
+ printf("End test\n");
+
+ return 0;
+} \ No newline at end of file