summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorZoRo <dos21h@gmail.com>2018-08-04 09:36:29 +0100
committerZoRo <dos21h@gmail.com>2018-08-04 09:36:29 +0100
commit7213af916cbb0087a6b85c800311c3f6a60c13b3 (patch)
tree50ead29d81824ce5734b95903e2f8e16607d9d69
parent5a112810cfc0849c170c89f32b9f2192c8b37a96 (diff)
downloadnotifylist-7213af916cbb0087a6b85c800311c3f6a60c13b3.tar.gz
notifylist-7213af916cbb0087a6b85c800311c3f6a60c13b3.zip
Added dynamic arrayHEADmaster
-rw-r--r--README.md2
-rw-r--r--darray.c141
-rw-r--r--darray.h37
-rw-r--r--debug.h69
-rw-r--r--notifylist.c52
5 files changed, 301 insertions, 0 deletions
diff --git a/README.md b/README.md
index a410739..30aff30 100644
--- a/README.md
+++ b/README.md
@@ -18,5 +18,7 @@ GOAL
Output result in way
<INFO> <PATH> <EVENT>
+Total size of final binary not larger then 64KB
+
diff --git a/darray.c b/darray.c
new file mode 100644
index 0000000..4df4064
--- /dev/null
+++ b/darray.c
@@ -0,0 +1,141 @@
+#include "darray.h"
+
+#include "debug.h"
+
+DArray::DArray(size_t data_size, size_t init_size)
+{
+ int i;
+
+ if (init_size <= 0)
+ {
+ goto error;
+ }
+
+ this->_max = init_size;
+ this->data = (void **)malloc(init_size*sizeof(void *));
+ this->end = 0;
+ //this->size = data_size;
+ this->_expand = DEFAULT_EXPAND_RATE;
+
+
+error:;
+}
+
+DArray::~DArray()
+{
+
+}
+
+int DArray::push(void *val)
+{
+ if (this->data[this->end] != NULL)
+ {
+ ENL();
+ }
+
+ this->data[this->end] = val;
+ this->end++;
+
+ if (this->end > this->_max)
+ {
+ return this->expand();
+ }
+
+ return 0;
+}
+
+int DArray::expand()
+{
+ size_t old_max = this->_max;
+
+ if (this->resize(this->_max + DEFAULT_EXPAND_RATE))
+ {
+ goto error;
+ }
+
+ memset(this->data + old_max, 0, this->_expand+1);
+ return 0;
+
+error:
+ ENL();
+ return -1;
+}
+
+int DArray::resize(size_t size)
+{
+ int i = 0;
+ void *data = NULL;
+
+ if (size < 1)
+ {
+ ENL();
+ goto error;
+ }
+
+ if (size < this->_max)
+ {
+ i = this->_max;
+ do
+ {
+ if (this->data[i-1] != NULL)
+ {
+ free(this->data[i-1]);//??? is there is some data then free it?
+ this->data[i-1] = NULL;
+ }
+ i -= 1;
+ } while (i-1 == size);
+ }
+ this->_max = size;
+
+ data = (void **)realloc(this->data, this->_max * sizeof(void*));
+ if (data == NULL)
+ {
+ ENL();
+ goto error;
+ }
+
+ this->data = (void **)data;
+ return 0;
+
+error:
+ ERROR("\n");
+ return -1;
+}
+
+int DArray::set(int idx, void *val)
+{
+ if (idx > this->_max)
+ {
+ ENL();
+ return -1;
+ }
+
+ if (idx>this->end)
+ {
+ this->end = idx;
+ }
+
+ this->data[idx] = val;
+
+ return 0;
+}
+
+void* DArray::get(int idx)
+{
+ if (idx<0)
+ {
+ return NULL;
+ }
+
+ if (idx>=this->end)
+ {
+ return NULL; //???
+ }
+
+ return this->data[idx];
+}
+
+int DArray::count()
+{
+ return this->end;
+}
diff --git a/darray.h b/darray.h
new file mode 100644
index 0000000..04cbc6a
--- /dev/null
+++ b/darray.h
@@ -0,0 +1,37 @@
+#ifndef __DARRAY_H
+#define __DARRAY_H
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+
+
+#define DEFAULT_EXPAND_RATE 10
+
+class DArray
+{
+private:
+ int end;
+ int _max;
+ //size_t size;
+ size_t _expand;
+ void **data;
+public:
+ DArray(size_t data_size, size_t init_size);
+ ~DArray();
+ void clear();
+ void clead_idx(int idx);
+ int expand();
+ int resize(size_t size);
+ int push(void *val);
+ void *pop();
+ int set(int idx, void *val);
+ void* get(int idx);
+ void *last();
+ void *first();
+ int count();
+ int max();
+};
+
+#endif
+
diff --git a/debug.h b/debug.h
new file mode 100644
index 0000000..a8bf211
--- /dev/null
+++ b/debug.h
@@ -0,0 +1,69 @@
+#ifndef __RB_DEBUG_UTILS_H
+#define __RB_DEBUG_UTILS_H
+
+//what about kprintf?
+
+//config options
+#define PRINTF printf
+#define COLORIZE
+#define PRINT_LINENUM
+#define PRINT_FILENAME
+#define PRINT_DEBUG
+
+
+//use color
+#ifdef COLORIZE
+ #define D_COLOR "1;32m"
+ #define D_COLOR_S "\033[" D_COLOR
+ #define D_COLOR_E "\033[0m"
+ #define E_COLOR "1;31m"
+ #define E_COLOR_S "\033[" E_COLOR
+ #define E_COLOR_E "\033[0m"
+#else
+ #define D_COLOR
+ #define D_COLOR_S
+ #define D_COLOR_E
+ #define E_COLOR
+ #define E_COLOR_S
+ #define E_COLOR_E
+#endif
+
+//print debug line
+#ifdef PRINT_LINENUM
+ #define PRINT_LINE_F "LINE:%d "
+ #define PRINT_LINE_D __LINE__
+#else
+ #define PRINT_LINE_F ""
+ #define PRINT_LINE_D ""
+#endif
+
+//print
+#ifdef PRINT_FILENAME
+ #define PRINT_FILE_F "FILE:%s "
+ #define PRINT_FILE_D __FILE__
+#else
+ #define PRINT_FILE_F ""
+ #define PRINT_FILE_D ""
+#endif
+
+//print debug string
+#ifdef PRINT_DEBUG
+ #define PRINT_DEBUG_F "Debug: "
+#else
+ #define PRINT_DEBUG_F ""
+#endif
+
+#define PRINT( format, args ... ) PRINTF( D_COLOR_S PRINT_DEBUG_F \
+ PRINT_FILE_F PRINT_LINE_F format D_COLOR_E, PRINT_FILE_D, \
+ PRINT_LINE_D, ##args);
+
+#define ERROR( format, args ... ) PRINTF( E_COLOR_S PRINT_DEBUG_F \
+ PRINT_FILE_F PRINT_LINE_F format E_COLOR_E, PRINT_FILE_D, \
+ PRINT_LINE_D, ##args);
+
+#define PNL() PRINT("\n");
+
+#define ENL() ERROR("\n");
+
+
+#endif
diff --git a/notifylist.c b/notifylist.c
index d19355b..e2df6e8 100644
--- a/notifylist.c
+++ b/notifylist.c
@@ -7,6 +7,8 @@
#include <arg.h>
+#include "darray.h"
+
class INotify {
private:
public:
@@ -49,7 +51,57 @@ int main(int argc, char **argv)
if (helpArg->isUsed())
{
printf("Help\n");
+ return 0;
+ }
+
+ if (versionArg->isUsed())
+ {
+ printf("Version\n");
+ return 0;
+ }
+
+ if (pathArg->isUsed() and eventArg->isUsed())
+ {
+
+ }
+
+ #define N 10
+ DArray a = DArray(0,N);
+ int i;
+ //init
+ for (i=0;i<N;i++)
+ {
+ int *v = (int *)malloc(sizeof(int));
+ *v = 10;
+ a.push(v);
+ }
+ //list
+ for (i=0;i<a.count();i++)
+ {
+ int *val = (int *)a.get(i);
+ if (val != NULL)
+ {
+ printf("%d,",*val);
+ }
+ }
+ printf("\n");
+ //set
+ for (i=0;i<a.count();i++)
+ {
+ int *val = (int *)a.get(i);
+ *val = i;
+ a.set(i,(void *)val);
+ }
+ //list
+ for (i=0;i<a.count();i++)
+ {
+ int *val = (int *)a.get(i);
+ if (val != NULL)
+ {
+ printf("%d,",*val);
+ }
}
+ printf("\n");
return 0;
} \ No newline at end of file