From 7213af916cbb0087a6b85c800311c3f6a60c13b3 Mon Sep 17 00:00:00 2001 From: ZoRo Date: Sat, 4 Aug 2018 09:36:29 +0100 Subject: Added dynamic array --- README.md | 2 + darray.c | 141 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ darray.h | 37 ++++++++++++++++ debug.h | 69 +++++++++++++++++++++++++++++ notifylist.c | 52 ++++++++++++++++++++++ 5 files changed, 301 insertions(+) create mode 100644 darray.c create mode 100644 darray.h create mode 100644 debug.h 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 +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 +#include +#include + + +#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 +#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