aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorFreeArtMan <dos21h@gmail.com>2019-09-25 19:17:09 +0100
committerFreeArtMan <dos21h@gmail.com>2019-09-25 19:17:09 +0100
commit3e187c094f12ef41ea6de2f55d128c3e037b5c12 (patch)
treef0dbbc5df260532e284ca5264297d0a8044eaeb4
parentbbd73385a0db271b457c63e4d6bab54f581410f9 (diff)
downloadihe-3e187c094f12ef41ea6de2f55d128c3e037b5c12.tar.gz
ihe-3e187c094f12ef41ea6de2f55d128c3e037b5c12.zip
Untested version, recompiled with new buf library and with c+
-rw-r--r--Makefile14
-rw-r--r--__cpp.h30
-rw-r--r--buf.c122
-rw-r--r--buf.h46
-rw-r--r--cmd/cmd_blk.c2
-rw-r--r--cmd/cmd_cd.c2
-rw-r--r--cmd/cmd_close.c10
-rw-r--r--cmd/cmd_dump.c10
-rw-r--r--cmd/cmd_dumps.c10
-rw-r--r--cmd/cmd_dumpx.c21
-rw-r--r--cmd/cmd_flags.c2
-rw-r--r--cmd/cmd_info.c2
-rw-r--r--cmd/cmd_ls.c2
-rw-r--r--cmd/cmd_open.c2
-rw-r--r--cmd/cmd_pos.c2
-rw-r--r--cmd/cmd_pwd.c2
-rw-r--r--cmd/cmd_read.c17
-rw-r--r--cmd/cmd_resize.c2
-rw-r--r--cmd/cmd_seek.c2
-rw-r--r--cmd/cmd_size.c2
-rw-r--r--cmd/cmd_write.c24
-rw-r--r--cmd/cmd_writes.c18
-rw-r--r--core.c36
-rw-r--r--core.h4
-rw-r--r--h64e/Makefile6
-rw-r--r--ihe.c14
-rw-r--r--ihe.h2
-rw-r--r--libbuf/Makefile29
-rw-r--r--libbuf/buf.c534
-rw-r--r--libbuf/buf.h89
-rw-r--r--libbuf/buf_misc.c272
-rw-r--r--libbuf/buf_misc.h64
-rw-r--r--libcmd/Makefile4
-rw-r--r--libterm/Makefile4
34 files changed, 1149 insertions, 253 deletions
diff --git a/Makefile b/Makefile
index bef2096..fa76dcd 100644
--- a/Makefile
+++ b/Makefile
@@ -1,25 +1,27 @@
PROJECT=ihe
CC=gcc
-CFLAGS=-I./
-SOURCES=buf.c core.c
+CPP=g++
+CFLAGS=-I./ -I./libbuf -Wwrite-strings -fpermissive -fno-rtti -fno-exceptions -fno-unwind-tables
+SOURCES=core.c
SOURCES+=$(wildcard cmd/*.c)
OBJECTS=$(SOURCES:.c=.o)
-LIB_OBJECTS=libcmd/libcmd.o libterm/libterm.o h64e/core.o
+LIB_OBJECTS=libcmd/libcmd.o libterm/libterm.o h64e/core.o libbuf/libbuf.o
all: clean $(OBJECTS) $(PROJECT)
$(PROJECT):
- $(CC) -c $(PROJECT).c
+ $(CPP) -c $(PROJECT).c
ld -r $(OBJECTS) $(PROJECT).o -o $(PROJECT)_.o
- $(CC) $(CFLAGS) $(PROJECT)_.o $(LIB_OBJECTS) -o $(PROJECT)
+ $(CPP) $(CFLAGS) $(PROJECT)_.o $(LIB_OBJECTS) -o $(PROJECT)
%.o: %.c
- $(CC) $(CFLAGS) -c $< -o $@
+ $(CPP) $(CFLAGS) -c $< -o $@
clean:
rm -f $(PROJECT)
rm -f *.o
+ rm -f cmd/*.o
leak:
valgrind --leak-check=full --track-origins=yes --log-file=log.txt ./ihe
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
deleted file mode 100644
index d0519e5..0000000
--- a/buf.c
+++ /dev/null
@@ -1,122 +0,0 @@
-#include "buf.h"
-
-buf_t* buf_init()
-{
- buf_t *ret = NULL;
-
- ret = malloc( sizeof(buf_t) );
- memset(ret, 0, sizeof(buf_t));
-
- return ret;
-}
-
-
-int buf_not_null( buf_t *bf )
-{
-
- if ( bf == NULL )
- return 0;
-
- if ( bf->buf == NULL )
- return 0;
-
- if ( bf->buf_size < 0 )
- return 0;
-
- return 1;
-}
-
-int buf_size( buf_t *bf, int size )
-{
- if (bf->buf != NULL)
- {
- printf("Buffer should be empty\n");
- return -1;
- }
-
- bf->buf = malloc( size );
- bf->size = size;
- bf->buf_size = size;
-
- return 0;
-}
-
-
-int buf_used_size( buf_t *bf, int size )
-{
- if ( size > bf->buf_size )
- {
- printf("Cannot set buffer size more then buffer itself\n");
- return -1;
- }
-
- if ( size < 0)
- {
- printf("Cannot set buffer less then zero\n");
- return -1;
- }
-
- bf->size = size;
-
- return 0;
-}
-
-
-int buf_resize( buf_t *bf, int size )
-{
- uint8_t *n=NULL;
-
- if ( size < 1)
- {
- printf("Cannot set buffer size less then 1\n");
- return -1;
- }
-
- n = realloc( bf->buf, size ); //resized data is not nullified at the end?
- if ( n == NULL )
- {
- printf("Buffer realloc failed\n");
- return -1;
- }
-
- if ( size > bf->buf_size )
- {
- memset(bf->buf+bf->buf_size-1, 0, size-(bf->buf_size)); //hope its correct
- }
-
- bf->buf_size = size;
- bf->buf = n;
- if (bf->size > bf->buf_size)
- {
- bf->size = bf->buf_size;
- }
-
- return 0;
-}
-
-
-int buf_zero( buf_t *bf )
-{
- if ( bf->buf == NULL )
- return -1;
-
- memset( bf->buf, 0, bf->buf_size );
-
- return 0;
-}
-
-
-void buf_free( buf_t *bf )
-{
- if ( bf == NULL )
- return;
-
- if ( bf->buf != NULL )
- {
- free( bf->buf );
- bf->buf = NULL;
- }
-
- free( bf );
- bf = NULL;
-}
diff --git a/buf.h b/buf.h
deleted file mode 100644
index 72bbc45..0000000
--- a/buf.h
+++ /dev/null
@@ -1,46 +0,0 @@
-#ifndef __IHE_BUF_H
-#define __IHE_BUF_H
-
-#include <stdlib.h>
-#include <stdio.h>
-#include <stdint.h>
-#include <string.h>
-
-typedef struct buf_t
-{
- uint8_t *buf;
- int size;
- int buf_size;
-} buf_t;
-
-/*
-create empty buffer structure
-*/
-buf_t* buf_init();
-
-int buf_not_null( buf_t *bf );
-
-/*
-if empty setup new buffer
-if not empty resize?
-*/
-int buf_size( buf_t *bf, int size );
-/*
-set used buffer size
-*/
-int buf_used_size( buf_t *bf, int size );
-/*
-change buffer size
-*/
-int buf_resize( buf_t *bf, int size );
-/*
-make buffer full of zeros
-*/
-int buf_zero( buf_t *bf );
-/*
-clean all buffer
-*/
-void buf_free( buf_t *bf );
-
-
-#endif \ No newline at end of file
diff --git a/cmd/cmd_blk.c b/cmd/cmd_blk.c
index 07f27f8..a568f98 100644
--- a/cmd/cmd_blk.c
+++ b/cmd/cmd_blk.c
@@ -5,7 +5,7 @@
#include "libcmd/cmd_parse.h"
extern file_t *g_file;
-extern buf_t *g_buf;
+extern Buf *g_buf;
extern int g_flags;
/*
diff --git a/cmd/cmd_cd.c b/cmd/cmd_cd.c
index 92c22b2..a24768c 100644
--- a/cmd/cmd_cd.c
+++ b/cmd/cmd_cd.c
@@ -5,7 +5,7 @@
#include "libcmd/cmd_parse.h"
extern file_t *g_file;
-extern buf_t *g_buf;
+extern Buf *g_buf;
extern int g_flags;
int c_cd( cmd_arg_t *arg )
diff --git a/cmd/cmd_close.c b/cmd/cmd_close.c
index d5a03b3..3db6a70 100644
--- a/cmd/cmd_close.c
+++ b/cmd/cmd_close.c
@@ -5,7 +5,7 @@
#include "libcmd/cmd_parse.h"
extern file_t *g_file;
-extern buf_t *g_buf;
+extern Buf *g_buf;
extern int g_flags;
/*
@@ -23,10 +23,12 @@ int c_close( cmd_arg_t *arg )
}
// clean buf
- if ( g_buf->buf != NULL )
+ if ( !g_buf->isempty() )
{
- buf_zero( g_buf );
- buf_free( g_buf );
+ //buf_zero( g_buf );
+ g_buf->zero();
+ //buf_free( g_buf );
+ g_buf->empty();
}
return 0;
diff --git a/cmd/cmd_dump.c b/cmd/cmd_dump.c
index bf2e5d1..999822e 100644
--- a/cmd/cmd_dump.c
+++ b/cmd/cmd_dump.c
@@ -5,22 +5,24 @@
#include "libcmd/cmd_parse.h"
extern file_t *g_file;
-extern buf_t *g_buf;
+extern Buf *g_buf;
extern int g_flags;
int c_dump( cmd_arg_t *arg )
{
int i;
- if ( g_buf->buf == NULL)
+ if ( g_buf->isempty())
{
printf("Buffer to print empty\n");
return -1;
}
- for (i=0; i<g_buf->size; i++)
+ for (i=0; i<g_buf->size(); i++)
{
- printf("%02x",(unsigned char)g_buf->buf[i]);
+ char c;
+ g_buf->getc(i,&c);
+ printf("%02x",(unsigned char)c);
}
printf("\n");
diff --git a/cmd/cmd_dumps.c b/cmd/cmd_dumps.c
index ecc5143..61d4c84 100644
--- a/cmd/cmd_dumps.c
+++ b/cmd/cmd_dumps.c
@@ -5,7 +5,7 @@
#include "libcmd/cmd_parse.h"
extern file_t *g_file;
-extern buf_t *g_buf;
+extern Buf *g_buf;
extern int g_flags;
int c_dumps( cmd_arg_t *arg )
@@ -20,11 +20,13 @@ int c_dumps( cmd_arg_t *arg )
return -1;
}
- for (i=0; i<g_buf->size; i++)
+ for (i=0; i<g_buf->size(); i++)
{
- if (isprint(g_buf->buf[i]))
+ char c;
+ g_buf->getc(i,&c);
+ if (isprint(c))
{
- printf("%c", g_buf->buf[i]);
+ printf("%c", c);
} else
{
printf("\e[7m.\e[0m");
diff --git a/cmd/cmd_dumpx.c b/cmd/cmd_dumpx.c
index 3e6cdf1..ecca20c 100644
--- a/cmd/cmd_dumpx.c
+++ b/cmd/cmd_dumpx.c
@@ -8,26 +8,28 @@
#include "libcmd/cmd_parse.h"
extern file_t *g_file;
-extern buf_t *g_buf;
+extern Buf *g_buf;
extern int g_flags;
int c_dumpx( cmd_arg_t *arg )
{
int i,j;
- if ( g_buf->buf == NULL)
+ if ( g_buf->isempty() )
{
printf("Buffer to print empty\n");
return -1;
}
- for (i=0; i<g_buf->size; i+=16)
+ for (i=0; i<g_buf->size(); i+=16)
{
for (j=i; j<i+16; j++)
{
- if ( j<g_buf->size )
+ if ( j<g_buf->size() )
{
- printf("%02x ",(unsigned char)g_buf->buf[j]);
+ char c;
+ g_buf->getc(j,&c);
+ printf("%02x ",(unsigned char)c);
} else
{
printf(" ");
@@ -36,11 +38,14 @@ int c_dumpx( cmd_arg_t *arg )
for (j=i; j<i+16; j++)
{
- if ( j<g_buf->size ) //wrong place move to cycle?
+ if ( j<g_buf->size() ) //wrong place move to cycle?
{
- if ( isprint(g_buf->buf[j]) )
+ char c;
+ g_buf->getc(j,&c);
+ if ( isprint(c) )
{
- printf("%c",(unsigned char)g_buf->buf[j]);
+
+ printf("%c",(unsigned char)c);
} else
{
printf("\e[7m.\e[0m");
diff --git a/cmd/cmd_flags.c b/cmd/cmd_flags.c
index 03f6b81..5e96651 100644
--- a/cmd/cmd_flags.c
+++ b/cmd/cmd_flags.c
@@ -5,7 +5,7 @@
#include "libcmd/cmd_parse.h"
extern file_t *g_file;
-extern buf_t *g_buf;
+extern Buf *g_buf;
extern int g_flags;
int c_flags( cmd_arg_t *arg )
diff --git a/cmd/cmd_info.c b/cmd/cmd_info.c
index 74324f6..01f58f6 100644
--- a/cmd/cmd_info.c
+++ b/cmd/cmd_info.c
@@ -5,7 +5,7 @@
#include "libcmd/cmd_parse.h"
extern file_t *g_file;
-extern buf_t *g_buf;
+extern Buf *g_buf;
extern int g_flags;
/*
diff --git a/cmd/cmd_ls.c b/cmd/cmd_ls.c
index 7e92db6..bb7781c 100644
--- a/cmd/cmd_ls.c
+++ b/cmd/cmd_ls.c
@@ -5,7 +5,7 @@
#include "libcmd/cmd_parse.h"
extern file_t *g_file;
-extern buf_t *g_buf;
+extern Buf *g_buf;
extern int g_flags;
int c_ls( cmd_arg_t *arg )
diff --git a/cmd/cmd_open.c b/cmd/cmd_open.c
index a4740c6..b7ea4f7 100644
--- a/cmd/cmd_open.c
+++ b/cmd/cmd_open.c
@@ -5,7 +5,7 @@
#include "libcmd/cmd_parse.h"
extern file_t *g_file;
-extern buf_t *g_buf;
+extern Buf *g_buf;
extern int g_flags;
/*
diff --git a/cmd/cmd_pos.c b/cmd/cmd_pos.c
index 05264d4..b040511 100644
--- a/cmd/cmd_pos.c
+++ b/cmd/cmd_pos.c
@@ -5,7 +5,7 @@
#include "libcmd/cmd_parse.h"
extern file_t *g_file;
-extern buf_t *g_buf;
+extern Buf *g_buf;
extern int g_flags;
int c_pos( cmd_arg_t *arg )
diff --git a/cmd/cmd_pwd.c b/cmd/cmd_pwd.c
index 4e21721..d35478d 100644
--- a/cmd/cmd_pwd.c
+++ b/cmd/cmd_pwd.c
@@ -5,7 +5,7 @@
#include "libcmd/cmd_parse.h"
extern file_t *g_file;
-extern buf_t *g_buf;
+extern Buf *g_buf;
extern int g_flags;
int c_pwd( cmd_arg_t *arg )
diff --git a/cmd/cmd_read.c b/cmd/cmd_read.c
index 68dab14..da1988c 100644
--- a/cmd/cmd_read.c
+++ b/cmd/cmd_read.c
@@ -5,23 +5,28 @@
#include "libcmd/cmd_parse.h"
extern file_t *g_file;
-extern buf_t *g_buf;
+extern Buf *g_buf;
extern int g_flags;
int c_read( cmd_arg_t *arg )
{
int ret;
- if ( g_buf->buf == NULL )
+ //if ( g_buf->buf == NULL )
+ if (g_buf->isempty())
{
- buf_resize( g_buf, g_file->blk_size );
+ //buf_resize( g_buf, g_file->blk_size );
+ g_buf->realloc(g_file->blk_size);
}
- ret = file_read_blk( g_file, g_buf->buf );
+ char *buf_ptr;
+ int sz;
+ g_buf->get_ptr(&buf_ptr,&sz);
+ ret = file_read_blk( g_file, (uint8_t *)buf_ptr, sz );
printf("Readed %d bytes\n", ret);
- if ( (ret >= 0) && (ret <= g_buf->buf_size) )
+ if ( (ret >= 0) && (ret <= g_buf->size()) )
{
- g_buf->size = ret;
+ g_buf->set_size(ret);
}
return 0;
diff --git a/cmd/cmd_resize.c b/cmd/cmd_resize.c
index 1e723cf..2b7fbd5 100644
--- a/cmd/cmd_resize.c
+++ b/cmd/cmd_resize.c
@@ -5,7 +5,7 @@
#include "libcmd/cmd_parse.h"
extern file_t *g_file;
-extern buf_t *g_buf;
+extern Buf *g_buf;
extern int g_flags;
int c_resize( cmd_arg_t *arg )
diff --git a/cmd/cmd_seek.c b/cmd/cmd_seek.c
index 7516811..d8bb768 100644
--- a/cmd/cmd_seek.c
+++ b/cmd/cmd_seek.c
@@ -5,7 +5,7 @@
#include "libcmd/cmd_parse.h"
extern file_t *g_file;
-extern buf_t *g_buf;
+extern Buf *g_buf;
extern int g_flags;
int c_seek( cmd_arg_t *arg )
diff --git a/cmd/cmd_size.c b/cmd/cmd_size.c
index 12723ef..3823923 100644
--- a/cmd/cmd_size.c
+++ b/cmd/cmd_size.c
@@ -5,7 +5,7 @@
#include "libcmd/cmd_parse.h"
extern file_t *g_file;
-extern buf_t *g_buf;
+extern Buf *g_buf;
extern int g_flags;
int c_size( cmd_arg_t *arg )
diff --git a/cmd/cmd_write.c b/cmd/cmd_write.c
index d54c0b5..978e244 100644
--- a/cmd/cmd_write.c
+++ b/cmd/cmd_write.c
@@ -5,13 +5,10 @@
#include "libcmd/cmd_parse.h"
extern file_t *g_file;
-extern buf_t *g_buf;
+extern Buf *g_buf;
extern int g_flags;
-//support masks
-int c_write( cmd_arg_t *arg)
-{
- /*
+/*
anonymous function
*/
uint8_t hex2u8( uint8_t *buf )
@@ -30,6 +27,11 @@ int c_write( cmd_arg_t *arg)
return ret;
}
+//support masks
+int c_write( cmd_arg_t *arg)
+{
+
+
int argc = arg->argc;
char **argv = arg->argv;
int i;
@@ -57,7 +59,7 @@ int c_write( cmd_arg_t *arg)
}
}
- if (strlen(argv[0]) > g_buf->size*2)
+ if (strlen(argv[0]) > g_buf->size()*2)
{
printf("Input param bigger then buffer\n");
return -1;
@@ -70,14 +72,18 @@ int c_write( cmd_arg_t *arg)
printf("\n");
- buf = malloc(strlen(argv[0])/2);
+ buf = (uint8_t *)malloc(strlen(argv[0])/2);
for (i=0; i<(strlen(argv[0])/2); i++)
{
buf[i] = hex2u8((unsigned char*)&argv[0][i*2]);
}
- memcpy( g_buf->buf, buf, strlen(argv[0])/2 );
- fret = file_write_blk( g_file, g_buf->buf );
+ char *buf_ptr;
+ int sz;
+ //memcpy( buf_ptr, buf, strlen(argv[0])/2 );
+ g_buf->set((char *)buf,strlen(argv[0])/2);
+ g_buf->get_ptr(&buf_ptr,&sz);
+ fret = file_write_blk( g_file, (uint8_t *)buf_ptr, sz );
free( buf );
if ( fret < 0)
diff --git a/cmd/cmd_writes.c b/cmd/cmd_writes.c
index 7d7eeed..0b555be 100644
--- a/cmd/cmd_writes.c
+++ b/cmd/cmd_writes.c
@@ -7,7 +7,7 @@
#include "libcmd/cmd_parse.h"
extern file_t *g_file;
-extern buf_t *g_buf;
+extern Buf *g_buf;
extern int g_flags;
//white spaces should be supported
@@ -23,16 +23,22 @@ int c_writes( cmd_arg_t *arg )
return -1;
}
- if (((g_buf == NULL) || (g_file == NULL)) || (g_buf->buf == NULL))
+ if (((g_buf == NULL) || (g_file == NULL)))
{
printf("Buffer or file not initialised");
return -1;
}
- if ( strlen(argv[0]) <= g_buf->size )
+ if ( strlen(argv[0]) <= g_buf->size() )
{
- memcpy( g_buf->buf, argv[0], strlen(argv[0]) );
- fret = file_write_blk( g_file, g_buf->buf );
+ //memcpy( g_buf->buf, argv[0], strlen(argv[0]) );
+ //check if can save all string in dat buffer
+ g_buf->set(argv[0],strlen(argv[0]));
+
+ char *buf_ptr;
+ int sz;
+ g_buf->get_ptr(&buf_ptr,&sz);
+ fret = file_write_blk( g_file, (uint8_t *)buf_ptr, sz );
if ( fret < 0 )
{
printf("Couldnt write block to file\n");
@@ -40,7 +46,7 @@ int c_writes( cmd_arg_t *arg )
}
} else
{
- printf("Input bigger then buffer buf %d input %zu\n", g_buf->size, strlen(argv[0]));
+ printf("Input bigger then buffer buf %d input %zu\n", g_buf->size(), strlen(argv[0]));
return -1;
}
diff --git a/core.c b/core.c
index 7de3085..1fe2891 100644
--- a/core.c
+++ b/core.c
@@ -98,7 +98,7 @@ file_t *file_init()
{
file_t *ret = NULL;
- ret = malloc( sizeof(file_t) );
+ ret = (file_t *)malloc( sizeof(file_t) );
memset( ret, 0, sizeof(file_t) );
ret->flags = FD_RO; //di we really need that?
@@ -123,7 +123,7 @@ int file_open_fn( file_t *ft, const char *filename, int flags )
ft->flags = flags;
uint8_t fn_sz = strlen( filename );
- char *fn = malloc( fn_sz );
+ char *fn = (char *)malloc( fn_sz );
memcpy( fn, filename, fn_sz );
ft->filename = fn;
@@ -230,7 +230,7 @@ int file_resize( file_t *ft, size_t size )
{
return -1;
}
- buf = malloc( size );
+ buf = (uint8_t *)malloc( size );
memset( buf, 0, size );
fd_seek( ft->fd, cur_size, FD_SEEK_SET );
resize = fd_write( ft->fd, buf, size );
@@ -251,7 +251,7 @@ int file_open( file_t *ft, const char *filename, int flags, int mode )
}
-int file_read_blk( file_t *ft, uint8_t *buf )
+int file_read_blk( file_t *ft, uint8_t *buf, int sz )
{
int ret = 0;
@@ -259,7 +259,12 @@ int file_read_blk( file_t *ft, uint8_t *buf )
return -1;
file_pos( ft );
- ret = fd_read( ft->fd, buf, ft->blk_size );
+ int read_sz = sz;
+ if (read_sz > ft->blk_size)
+ {
+ read_sz = ft->blk_size;
+ }
+ ret = fd_read( ft->fd, buf, read_sz );
fd_set_pos( ft->fd, ft->position ); //chck err?
if ( ret < 0)
@@ -288,21 +293,26 @@ int file_read( file_t *ft, uint8_t *buf, size_t count )
}
-int file_write_blk( file_t *ft, uint8_t *buf )
+int file_write_blk( file_t *ft, uint8_t *buf, int sz )
{
int ret = 0;
- unsigned int sz;
+ unsigned int write_sz;
file_pos( ft );
if ( ft->position + ft->blk_size <= ft->size )
{
- sz = ft->blk_size;
+ write_sz = ft->blk_size;
} else
{
- sz = ft->size - ft->position; //when pos 0 ans size 1 then will write 1 byte
+ write_sz = ft->size - ft->position; //when pos 0 ans size 1 then will write 1 byte
+ }
+
+ if (write_sz > sz)
+ {
+ write_sz = sz;
}
- ret = fd_write( ft->fd, buf, sz );
+ ret = fd_write( ft->fd, buf, write_sz );
if ( ret < 0 )
{
printf("Error while writing block to file\n");
@@ -394,12 +404,12 @@ uint8_t **dir_list( char *path)
//count one more in da list
cnt += 1;
//lets alloc pointer on pointer where we put pointer
- new_ptr = realloc( ret, sizeof(uint8_t*)*(cnt) );
+ new_ptr = (uint8_t **)realloc( ret, sizeof(uint8_t*)*(cnt) );
if ( new_ptr == NULL )
goto failed_realloc;
ret = new_ptr;
str_sz = strlen(ep->d_name);
- ret[cnt-1] = malloc( str_sz+1 );
+ ret[cnt-1] = (uint8_t *)malloc( str_sz+1 );
memcpy( ret[cnt-1], ep->d_name, str_sz );
ret[cnt-1][str_sz] = 0;
}
@@ -412,7 +422,7 @@ uint8_t **dir_list( char *path)
}
//add NULL element at the end
- new_ptr = realloc( ret, sizeof(uint8_t*)*(cnt+1) );
+ new_ptr = (uint8_t **)realloc( ret, sizeof(uint8_t*)*(cnt+1) );
if ( new_ptr == NULL )
goto failed_add_entry;
ret = new_ptr;
diff --git a/core.h b/core.h
index 638cf3c..152d6ca 100644
--- a/core.h
+++ b/core.h
@@ -98,9 +98,9 @@ typedef struct file_t
file_t *file_init();
int file_open_fn( file_t *ft, const char *filename, int mode );
int file_open( file_t *ft, const char *filename, int flags, int mode );
-int file_read_blk( file_t *ft, uint8_t *buf );
+int file_read_blk( file_t *ft, uint8_t *buf, int sz );
int file_read( file_t *ft, uint8_t *buf, size_t count );
-int file_write_blk( file_t *ft, uint8_t *buf );
+int file_write_blk( file_t *ft, uint8_t *buf, int sz );
int file_write( file_t *ft, uint8_t *buf, size_t count );
int file_seek( file_t *ft, off_t offset ); //seek by offset
int file_seekp( file_t *ft, off_t offset );
diff --git a/h64e/Makefile b/h64e/Makefile
index 074800b..8bc4bab 100644
--- a/h64e/Makefile
+++ b/h64e/Makefile
@@ -1,4 +1,4 @@
-CC=gcc
-CFLAGS=
+CC=g++
+CFLAGS=-Wwrite-strings -fpermissive -fno-rtti -fno-exceptions -fno-unwind-tables
make:
- $(CC) -c core.c \ No newline at end of file
+ $(CC) $(CFLAGS) -c core.c \ No newline at end of file
diff --git a/ihe.c b/ihe.c
index 641b09b..4390586 100644
--- a/ihe.c
+++ b/ihe.c
@@ -1,6 +1,8 @@
#include "ihe.h"
+#include "__cpp.h"
#include "core.h"
+
/* Global variables */
cmd_mng_t cmd_mng;
static int cmd_loop = 1;
@@ -239,7 +241,8 @@ int cmd_buf_full( cmd_in_buf_t *buf )
GLOBAL VARIABLES
*/
file_t *g_file = NULL;
-buf_t *g_buf = NULL;
+//buf_t *g_buf = NULL;
+Buf *g_buf = NULL;
int g_flags = FD_RW;
@@ -454,12 +457,15 @@ int main( int argc, char **argv )
memset( &tl, 0, sizeof( cmd_tok_t ));
//preapre global stuff
+
g_file = file_init();
//init basic buffer
- g_buf = buf_init();
- buf_size( g_buf, DEFAULT_BLK_SIZE );
- buf_zero( g_buf );
+ //#warning "replace"
+ g_buf = new Buf(DEFAULT_BLK_SIZE);
+ //g_buf = buf_init();
+ //buf_size( g_buf, DEFAULT_BLK_SIZE );
+ //buf_zero( g_buf );
struct term_screen ts; memset( &ts, 0, sizeof(ts) );
diff --git a/ihe.h b/ihe.h
index 621702f..6f553c6 100644
--- a/ihe.h
+++ b/ihe.h
@@ -6,7 +6,7 @@
#include <stdlib.h>
#include <ctype.h>
-#include "buf.h"
+#include "libbuf/buf.h"
#include "core.h"
#include "libcmd/cmd.h"
diff --git a/libbuf/Makefile b/libbuf/Makefile
new file mode 100644
index 0000000..ed2f1de
--- /dev/null
+++ b/libbuf/Makefile
@@ -0,0 +1,29 @@
+CPP=g++
+CC=gcc
+LD=ld
+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:
+ $(CPP) $(CPPFLAGS) -c buf.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 buf_misc.o -shared -o libbuf.so
+ $(LD) -r buf.o buf_misc.o -o libbuf.o
+
+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.o -o test_line
+
+leak:
+ valgrind --leak-check=full --track-origins=yes --log-file=log.txt ./test
+
+clean:
+ rm -rf *.so *.o test test_circ test_sys
+
+
diff --git a/libbuf/buf.c b/libbuf/buf.c
new file mode 100644
index 0000000..83457e9
--- /dev/null
+++ b/libbuf/buf.c
@@ -0,0 +1,534 @@
+#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->bu