summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--arg.c145
-rw-r--r--debug.h69
-rw-r--r--test/test1.c5
3 files changed, 211 insertions, 8 deletions
diff --git a/arg.c b/arg.c
index cae18d7..7a0cd64 100644
--- a/arg.c
+++ b/arg.c
@@ -1,5 +1,7 @@
#include "arg.h"
+#include "debug.h"
+
//for local usage only?
int __search_arg( char *in, def_arg *arg );
/*from s_arg_[TYPE] create type arg_[TYPE]*/
@@ -65,7 +67,24 @@ arg_t* arg_load( int argc, char **argv, def_arg *argl )
}
case ARGT_RANGE:
{
- //printf("%d is range\n",i);
+ printf("%d is range\n",i);
+ /*
+ CHK_ARG(i,1);
+
+ if (argl[pos].used == 1)
+ {
+ printf("Allready used %s\n",argv[i]);
+ break;
+ }
+
+ arg_range *range = __create_arg(argl[pos].type, argl[pos].def);
+
+
+
+ i += 1;
+
+ */
+
break;
}
case ARGT_FLOAT:
@@ -75,8 +94,91 @@ arg_t* arg_load( int argc, char **argv, def_arg *argl )
}
case ARGT_LIST:
{
+ s_arg_list *parse_params = NULL;
+ char *iter=NULL,*st=NULL,*s=NULL;
+
//printf("%d is list\n",i);
+ CHK_ARG(i,1);
+
+
+ if (argl[pos].used == 1)
+ {
+ printf("Allready used %d\n", argv[i]);
+ break;
+ }
+
+ //super algo for parsing
+ parse_params = malloc(sizeof(arg_list));
+ memset(parse_params, 0, sizeof(arg_list));
+ iter = argv[i+1];
+ st = iter;
+ while ((*iter)!='\0')
+ {
+ if (((*iter)=='|'))
+ {
+ if ((iter)>st)
+ {
+ s = malloc(iter-st+1);
+ memcpy(s, st, iter-st);
+ s[iter-st]=0;
+ //realloc and add new pointer
+ parse_params->num += 1;
+ char *realoc_ptr = realloc(parse_params->vals, sizeof(void *)*parse_params->num);
+ if (realoc_ptr)
+ {
+ parse_params->vals = (void **)realoc_ptr;
+ parse_params->vals[parse_params->num-1] = s;
+ } else
+ {
+ parse_params->num -= 1;
+ }
+ st = iter+1;
+ } else
+ {
+ st = iter+1;
+ }
+ }
+ iter++;
+ if (*iter=='\0')
+ {
+ if ((iter)>st)
+ {
+ s = malloc(iter-st+1);
+ memcpy(s, st, iter-st);
+ s[iter-st]=0;
+ parse_params->num += 1;
+ char *realoc_ptr = realloc(parse_params->vals, sizeof(void *)*parse_params->num);
+ if (realoc_ptr)
+ {
+ parse_params->vals = (void **)realoc_ptr;
+ parse_params->vals[parse_params->num-1] = s;
+ } else
+ {
+ parse_params->num -= 1;
+ }
+ st = iter+1;
+ }
+ }
+ }
+
+ arg_list *list = __create_arg(argl[pos].type, parse_params);
+
+ //check param values
+
+ if (-1==__add_arg(ret, ARGT_LIST, list))
+ {
+ ENL();
+ }
+
+
+ argl[pos].used = 1;
+
+ used[i] = 1;
+ used[i+1] = 1;
+
+ i+=1;
break;
+
}
case ARGT_FILE:
{
@@ -115,7 +217,7 @@ arg_t* arg_load( int argc, char **argv, def_arg *argl )
break;
}
- // enouht that this is triggered
+ // enought that this is triggered
case ARGT_FLAG:
{
arg_flag *flg = NULL;
@@ -326,6 +428,18 @@ void arg_print( arg_t *arg )
printf("IP\n");
break;
}
+ case ARGT_LIST:
+ {
+ int k;
+ arg_list *lst = (arg_list *)v->val;
+ printf("Lst | ");
+ for (k=0;k<lst->num;k++)
+ {
+ printf("%s ", lst->vals[k]);
+ }
+ printf("\n");
+ break;
+ }
case ARGT_VAL:
{
arg_val *val = (void *)v->val;
@@ -418,9 +532,13 @@ void* __create_arg( int type, void *val )
case ARGT_LIST:
{
arg_list *list = NULL;
- s_arg_list *s_list = NULL;
+ s_arg_list *s_list = val;
- list = arg_c_list( s_list->num, s_list->vals );
+ //so NULL where given as we use *->* then be carefull
+ if (s_list != NULL)
+ {
+ list = arg_c_list( s_list->num, s_list->vals );
+ }
ret = list;
break;
@@ -467,20 +585,33 @@ void* __create_arg( int type, void *val )
int __add_arg( arg_t *arg, int type, void *val )
{
- int ret = 0;
argv_t *argv = NULL;
- arg->arg = realloc( arg->arg, sizeof(void*)*(arg->size+1) );
+ void *new_ptr = NULL;
+
+ new_ptr = realloc( arg->arg, sizeof(void*)*(arg->size+1) );
+ if (new_ptr == NULL)
+ {
+ ENL();
+ return -1;
+ }
+ arg->arg = new_ptr;
arg->size += 1;
argv = malloc(sizeof(argv_t));
+ if (argv == NULL)
+ {
+ //do resize back reallocated arg->arg
+ ENL();
+ return -1;
+ }
memset(argv, 0, sizeof(argv_t));
argv->type = type;
argv->val = val;
arg->arg[arg->size-1] = argv;
- return ret;
+ return 0;
}
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/test/test1.c b/test/test1.c
index eea5759..f6a7a70 100644
--- a/test/test1.c
+++ b/test/test1.c
@@ -3,12 +3,15 @@
#include "../arg.h"
+//define ip4 127.0.0.1:8080
s_arg_ip src_ip =
{
.port = 8080,
.ip = 0x7f000001
};
+//?
+//frequency range from 88:108
s_arg_range freq_range =
{
.start = 88,
@@ -41,7 +44,7 @@ def_arg cmd_arg[] =
ARG_ENTRY("-c",FLOAT,&ratio_float),
ARG_ENTRY("-d",LIST,&color_name_list),
ARG_ENTRY("-e",VAL,&number_val),
- ARG_ENTRY("-f",FLAG,NULL),
+ ARG_ENTRY("-f",FILE,NULL),
ARG_ENTRY("-g",FLAG,NULL),
{NULL,0,NULL}
};