summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorFreeArtMan <dos21h@gmail.com>2019-05-12 12:46:02 +0100
committerFreeArtMan <dos21h@gmail.com>2019-05-12 12:46:02 +0100
commit654bb8b09dd90c12c3a51ffdce800b2255b753b5 (patch)
tree67591880f34d6220b9026883b0ceb54f022aa385
parent03bfeedc5f4c04c20764c8a9a58bd02604f27b2c (diff)
downloadlibbuf-654bb8b09dd90c12c3a51ffdce800b2255b753b5.tar.gz
libbuf-654bb8b09dd90c12c3a51ffdce800b2255b753b5.zip
Moved everything to C+. First test works
-rw-r--r--Makefile25
-rw-r--r--__cpp.h30
-rw-r--r--buf.c246
-rw-r--r--buf.h37
-rw-r--r--buf_misc.c47
-rw-r--r--buf_misc.h27
-rw-r--r--test.c71
7 files changed, 464 insertions, 19 deletions
diff --git a/Makefile b/Makefile
index 31ada08..5cc9d05 100644
--- a/Makefile
+++ b/Makefile
@@ -1,19 +1,26 @@
+CPP=g++
CC=gcc
-CFLAGS=-fPIC -g3
+CFLAGS=-fPIC -g3 -Wall -Wwrite-strings -fno-exceptions -fno-unwind-tables -fpermissive
+CPPFLAGS=-fPIC -g3 -Wall -Wwrite-strings -fpermissive -fno-rtti -fno-exceptions -fno-unwind-tables
make:
- $(CC) $(CFLAGS) -c buf.c
- $(CC) $(CFLAGS) -c buf_misc.c
- $(CC) $(CFLAGS) -fPIC -lc -ldl buf_misc.o buf.o -shared -o libbuf.so
+ $(CPP) $(CPPFLAGS) -c buf.c
+ $(CPP) $(CFLAGS) -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
-t: buf.c buf_misc.c
- $(CC) $(CFLAGS) buf.o buf_misc.o test.c -o test
- $(CC) $(CFLAGS) buf.o buf_misc.o test_circ.c -o test_circ
+t: buf.c buf_misc.c buf_sys.c
+ $(CPP) $(CPPFLAGS) test.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
leak:
- valgrind --leak-check=full --track-origins=yes --log-file=log.txt ./test_circ
+ valgrind --leak-check=full --track-origins=yes --log-file=log.txt ./test
clean:
- rm -rf *.so *.o
+ rm -rf *.so *.o test test_circ test_sys
diff --git a/__cpp.h b/__cpp.h
new file mode 100644
index 0000000..6a8e03e
--- /dev/null
+++ b/__cpp.h
@@ -0,0 +1,30 @@
+#include <unistd.h>
+
+extern "C" void* emulate_cc_new(unsigned long len) { \
+ void *p = malloc(len);
+ if (p == 0) {
+ /* Don't use stdio (e.g. fputs), because that may want to allocate more
+ * memory.
+ */
+ (void)!write(2, "out of memory\n", 14);
+ abort();
+ }
+ return p;
+}
+extern "C" void emulate_cc_delete(void* p) {
+ if (p != 0)
+ free(p);
+}
+
+extern "C" void emulate_cc_delete2(void* p, unsigned long len) {
+ if (p != 0)
+ free(p);
+}
+
+void* operator new (unsigned long) __attribute__((alias("emulate_cc_new")));
+void* operator new[](unsigned long) __attribute__((alias("emulate_cc_new")));
+void operator delete (void* p) __attribute__((alias("emulate_cc_delete")));
+void operator delete[](void* p) __attribute__((alias("emulate_cc_delete")));
+void operator delete (void* p, unsigned long len) __attribute__((alias("emulate_cc_delete2")));
+void operator delete[](void* p, unsigned long len) __attribute__((alias("emulate_cc_delete2")));
+void* __cxa_pure_virtual = 0; \ No newline at end of file
diff --git a/buf.c b/buf.c
index 04c4d70..c9cf59a 100644
--- a/buf.c
+++ b/buf.c
@@ -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
diff --git a/buf.h b/buf.h
index 4db4f62..212577d 100644
--- a/buf.h
+++ b/buf.h
@@ -5,16 +5,20 @@
#include <stdio.h>
#include <string.h>
#include <ctype.h>
+#include <unistd.h>
+/*
typedef struct bbuf {
int size; //total size
int buf_size; //used size
char *buf;
} bbuf;
+*/
//operations on plain buffers also operations on bufer to buffer should be
//supoorted
+/*
//create new buffer without content
bbuf* bbuf_new(int size);
@@ -33,7 +37,8 @@ int bbuf_inc(bbuf *a, char *b, int size);
//decrease buffer for size
int bbuf_dec(bbuf *a, char *b, int size);
//free buffer
-void bbuf_free(bbuf *buf);
+int bbuf_set_max(bbuf *buf);
+int bbuf_memset(bbuf *buf, char c);
int bbuf_concat(bbuf *a, bbuf *b);
@@ -42,5 +47,35 @@ int bbuf_size(bbuf *a);
int bbuf_print(bbuf *a);
void bbuf_free(bbuf *buf);
+*/
+
+class Buf
+{
+private:
+ int cur_size;
+ int buf_size;
+ char *buf;
+public:
+ Buf(int size);
+ ~Buf();
+
+ int set(char *val, int size);
+ int set(char *val);
+ Buf* copy();
+ int get(char **val, int *size);
+ int realloc(int size);
+ int reduce();
+ int inc(int size);
+ int dec(int size);
+ int set_max();
+ int memset(char c);
+ int concat(Buf *b);
+ int size();
+ int cursize();
+ int print();
+ int setc(int idx, char c);
+ int pushc(char c);
+ int getc(int idx, char *c);
+};
#endif \ No newline at end of file
diff --git a/buf_misc.c b/buf_misc.c
index 938e8a3..bcf7705 100644
--- a/buf_misc.c
+++ b/buf_misc.c
@@ -1,6 +1,7 @@
#include "buf_misc.h"
-int bbuf_line_new(bbuf_line *buf, char sep, int size)
+/*
+int bbuf_line_new(bbuf_line *buf, int size)
{
bbuf *newbuf=NULL;
bbuf_line *bline=NULL;
@@ -19,7 +20,6 @@ int bbuf_line_new(bbuf_line *buf, char sep, int size)
}
memset(bline, 0, sizeof(bbuf_line));
- bline->sep = 0x0;
bline->buf = newbuf;
@@ -28,6 +28,10 @@ int bbuf_line_new(bbuf_line *buf, char sep, int size)
return 0;
}
+int bbuf_line_pattern(bbuf_line *buf, char *patt, int size)
+{
+ return -1;
+}
int bbuf_line_add(bbuf_line *buf_line, bbuf *new_data)
{
@@ -49,6 +53,43 @@ int bbuf_line_free(bbuf_line *buf)
return -1;
}
+*/
+
+BufLine::BufLine(int size)
+{
+ *buf = Buf(size);
+ sep=0x0;
+ memset(pattern,0,BUF_PATTER_SIZE);
+}
+
+BufLine::~BufLine()
+{
+ delete buf;
+}
+
+int BufLine::set_pattern(char *pattern)
+{
+ return -1;
+}
+
+
+int BufLine::add(char *string, int size)
+{
+ return -1;
+}
+
+int BufLine::add(Buf *newdata)
+{
+ buf->concat(newdata);
+ return -1;
+}
+
+int BufLine::pop_line(char **val, int *size)
+{
+ return -1;
+}
+
+/*
int bbuf_circ_new(bbuf_circ **circ, int size)
{
bbuf *buf = NULL;
@@ -188,4 +229,4 @@ int bbuf_circ_free(bbuf_circ *circ)
free(circ);
circ = NULL;
}
-
+*/ \ No newline at end of file
diff --git a/buf_misc.h b/buf_misc.h
index c0d8aab..9eabd97 100644
--- a/buf_misc.h
+++ b/buf_misc.h
@@ -3,21 +3,44 @@
#include "buf.h"
+#define BUF_PATTER_SIZE 2
+
+/*
//line buffer, put data get line out
typedef struct bbuf_line
{
bbuf *buf;
+ char pattern[BUF_PATTER_SIZE];
char sep; //seperator character
} bbuf_line;
-int bbuf_line_new(bbuf_line *buf, char sep, int size);
+int bbuf_line_new(bbuf_line *buf, int size);
+int bbuf_line_pattern(bbuf_line *buf, char *patt, int size);
int bbuf_line_add(bbuf_line *buf_line, bbuf *new_data);
int bbuf_line_get_line(bbuf_line *buf_line, bbuf *line);
int bbuf_line_free(bbuf_line *buf);
+*/
+
+class BufLine
+{
+private:
+ Buf *buf;
+ char pattern[BUF_PATTER_SIZE];
+ char sep; //seperator character
+public:
+ BufLine(int size);
+ ~BufLine();
+ int set_pattern(char *pattern);
+ int add(char *string, int size);
+ int add(Buf *newdata);
+ int pop_line(char **val, int *size);
+};
+
//formating buf ops
//circular buffer
+/*
typedef struct bbuf_circ
{
bbuf *buf;
@@ -34,5 +57,5 @@ int bbuf_circ_put(bbuf_circ *circ, char data);
int bbuf_circ_empty(bbuf_circ *circ);
int bbuf_circ_full(bbuf_circ *circ);
int bbuf_circ_free(bbuf_circ *circ);
-
+*/
#endif
diff --git a/test.c b/test.c
index 9bb8f31..c4dc609 100644
--- a/test.c
+++ b/test.c
@@ -1,6 +1,8 @@
#include <stdio.h>
#include <stdlib.h>
+#include <stdint.h>
+#include "__cpp.h"
#include "buf.h"
#include "debug.h"
@@ -37,45 +39,108 @@ void print_s(char *str, int sz)
int main()
{
+ /*
bbuf *b1=NULL, *b2=NULL;
int ret;
+ */
+ Buf *b1=NULL, *b2=NULL;
+ int rc;
printf("Start test\n");
+ /*
bbuf_free(b1);
+ */
+ delete b1;
printf("Create new buffer\n");
+ /*
b1 = bbuf_new(BUF_SIZE_1);
bbuf_free(b1);
+ */
+
+ b1 = new Buf(BUF_SIZE_1);
+ delete b1;
printf("Copy buffer\n");
+ /*
b1 = bbuf_new(BUF_SIZE_1);
b2 = bbuf_copy(b1);
bbuf_free(b1);
bbuf_free(b2);
-
+ */
+ b1 = new Buf(BUF_SIZE_1);
+ b2 = b1->copy();
+ delete b1; b1=NULL;
+ delete b2; b2=NULL;
+
printf("Get buffer\n");
char name1[] = "Lets get buffer and print it out";
char *get_val = NULL;
int get_size;
+ /*
b1 = bbuf_new(BUF_SIZE_1);
B(bbuf_set(b1,name1,strlen(name1)));
B(bbuf_get(b1,&get_val,&get_size));
print_s(get_val,get_size);printf("\n");
+ */
+ b1 = new Buf(BUF_SIZE_1);
+ B(b1->set(name1, strlen(name1)));
+ B(b1->get(&get_val, &get_size));
+ print_s(get_val, get_size);printf("\n");
+
+ /*
b2 = bbuf_copy(b1);
B(bbuf_get(b2,&get_val,&get_size));
print_s(get_val,get_size);printf("\n");
+ */
+ b2 = b1->copy();
+ B(b2->get(&get_val,&get_size));
+ print_s(get_val,get_size);printf("\n");
+ /*
bbuf_free(b1);
bbuf_free(b2);
-
+ */
+ delete b1;
+ delete b2;
+
printf("Realloc buffer\n");
+ /*
b1 = bbuf_new(BUF_SIZE_1);
B(bbuf_realloc(b1,BUF_SIZE_2));
bbuf_free(b1);
-
+ */
+ b1 = new Buf(BUF_SIZE_1);
+ b1->realloc(BUF_SIZE_2);
+ delete b1;
+
+
+ printf("Concat 2 strings");
+ b1 = new Buf(8);
+ b2 = new Buf(8);
+ char n1[] = "Name";
+ char n2[] = "File";
+ char n3[] = "Nothing";
+
+ B(b1->set(n1));
+ B(b2->set(n2));
+
+ b1->print(); printf("\n");
+ b2->print(); printf("\n");
+
+ printf("%d:",b1->concat(b2));
+ b1->print(); printf("\n");
+
+ B(b2->set(n3));
+ printf("%d:",b1->concat(b2));
+ b1->print(); printf("\n");
+
+ delete b1;
+ delete b2;
+
printf("End test\n");
return 0;