From 420bb7716ee099d369582836ef4fc8ab25d81fde Mon Sep 17 00:00:00 2001 From: xchaos Date: Tue, 22 Jan 2008 02:37:07 +0000 Subject: [PATCH] C<<1 va_list based print and echo commands finaly work git-svn-id: https://dev.arachne.cz/repos/cll1h/trunk@20 4bb87942-c103-4e5a-b51c-0ebff58f8515 --- cll1.h | 57 ++++++++++++++++++++---------- demos/arguments1.c | 30 +++++++++------- demos/arguments2.c | 27 +++++++++----- demos/boolean.c | 10 +++--- demos/for_range.c | 17 +++++---- demos/hello.c | 3 +- demos/just-c99/print.c | 12 +++++++ demos/performance/cll1-print.c | 12 +++++++ demos/performance/cll1.h | 1 + demos/performance/fputs.c | 11 ++++++ demos/performance/print.pl | 8 +++++ demos/performance/print.py | 5 +++ demos/performance/printf.c | 11 ++++++ demos/performance/puts.c | 11 ++++++ demos/performance/test-performance | 24 +++++++++++++ demos/repeat.c | 4 +-- 16 files changed, 189 insertions(+), 54 deletions(-) create mode 100644 demos/just-c99/print.c create mode 100644 demos/performance/cll1-print.c create mode 120000 demos/performance/cll1.h create mode 100644 demos/performance/fputs.c create mode 100755 demos/performance/print.pl create mode 100755 demos/performance/print.py create mode 100644 demos/performance/printf.c create mode 100644 demos/performance/puts.c create mode 100755 demos/performance/test-performance diff --git a/cll1.h b/cll1.h index 8cd24a4..5305e3c 100644 --- a/cll1.h +++ b/cll1.h @@ -27,6 +27,7 @@ #include #include #include +#include #include #ifdef USE_LIBRARY_MYSQL #include @@ -59,7 +60,7 @@ #define Define_mem(ID,TYPE) struct TYPE *ID=NULL #define Define_mems(ID1,I2,TYPE) Define_mem(ID1,TYPE);Define_mem(ID2,TYPE) -/* .... well.. ehm.. not very useful... :-) */ +/* .... well....ehm...not very useful... :-) */ #define Create_var(ID,TYPE,INIT) TYPE ID=INIT #define Define_var(ID,TYPE) TYPE ID /* ------------------------------------------------------------------- */ @@ -92,12 +93,19 @@ /* ------------------------------------------------------------------- */ /* 5. C<<1 argument handling, updated 2008-01-16 by xCh. */ /* ------------------------------------------------------------------- */ -#define for_args _NEW_I if(argc>1) for(_I=1;_I1) #define if_arg(VAL) if(eq(argv[_I],VAL)) #define this_arg argv[_I] -#define next_arg ((_I+1=0) @@ -110,7 +118,7 @@ //todo #define _QSTACK struct _QStack { char *Q; int _23; list(_QStack); } *_Qatom,*_Qstack=NULL; #endif /* ------------------------------------------------------------------- */ -/* 7. MySQL infrastructure and wrappers - updated 2007-12-19 by xCh. */ +/* 9. MySQL infrastructure and wrappers - updated 2007-12-19 by xCh. */ /* ------------------------------------------------------------------- */ #ifdef USE_LIBRARY_MYSQL #define mysql_connect(S,U,P,D) (create(_Matom,_MStack) && push(_Matom,_Mstack) && (_Matom->M=mysql_init(NULL)),(_Matom->M && mysql_real_connect(_Matom->M,S,U,P,D,0,NULL,0)?_Matom->M:NULL)) @@ -139,10 +147,14 @@ /* ------------------------------------------------------------------- */ #define insert(A,B,EXP,K) { if(B) { void **_L=NULL, *H=B; for_search(B,H,EXP(B->K,A->K)) { if(_L) {*_L=A; A->_next=B; } else push(A,H); break; } else _L=(void *)&(B->_next); if(!B)*_L=A; B=H; } else push(A,B); } #define sort(A,B,EXP,K) { void *_C; A=B; B=NULL; do { _C=A->_next; A->_next=NULL; insert(A,B,EXP,K); A=_C; } while(_C); } -/* String macros & sequences, updated 2004-04-19 by xCh. */ - -#define eq(A,B) !strcmp(A,B) +/* ------------------------------------------------------------------- */ +/* String macros & sequences, updated 2008-01-22 by xCh. */ +/* ------------------------------------------------------------------- */ +#define eq(A,B) (A && B && !strcmp(A,B)) +#define eqi(A,B) (A && B && !strcasecmp(A,B)) +/* Borland C legacy */ #define strcmpi(A,B) strcasecmp(A,B) +/* ------------------------------------------------------------------- */ /* obsolete #define strlwr(A) {char *_S=A; while(_&&*_S){*_S=tolower(*_S);_S++;}} @@ -173,20 +185,19 @@ obsolete /* Infrastructure, updated 2008-01-08 by xCh. */ /* ------------------------------------------------------------------- */ /* old #define program int _I; int main(int argc, char **argv) */ -#define _ISTACK struct _IStack { int I; int _23; list(_IStack); } *_Iatom,*_Istack=NULL -#define _SSTACK struct _SStack { char *S; int _23; list(_SSTACK); } *_Satom,*_Sstack=NULL -#define _FSTACK struct _FStack { FILE *F; int _23; list(_FSTACK); } *_Fatom,*_Fstack=NULL +#define _ISTACK struct _IStack { int I; int _23; list(_IStack); } *_Iatom,*_Istack=NULL; +#define _SSTACK struct _SStack { char *S; int _23; list(_SSTACK); } *_Satom,*_Sstack=NULL; +#define _FSTACK struct _FStack { FILE *F; int _23; list(_FSTACK); } *_Fatom,*_Fstack=NULL; -#define define_ccl1_globals _ISTACK; _SSTACK; _FSTACK; _MSTACK; _RSTACK; _QSTACK -#define program define_cll1_globals; int main(int argc, char **argv) #define _NEW_(ATOM,STACK,TYPE) (create(ATOM,TYPE),ATOM->_23=1,push(ATOM,STACK);ATOM->_23||pop(ATOM,STACK)&&0;ATOM->_23--) #define _NEW_I for _NEW_(_Iatom,_Istack,_IStack) //#define _NEW_I for(create(_Iatom,_IStack),_Iatom->_23=1,push(_Iatom,_Istack);_Iatom->_23||pop(_Iatom,_Istack)&&0;_Iatom->_23--) #define _NEW_S for(create(_Satom,_SStack),_Satom->_23=1,push(_Satom,_Sstack);_Satom->_23||pop(_Satom,_Sstack)&&0;_Satom->_23--) -#define _I _Iatom->I -#define _S _Satom->I -#define _F _Satom->F +#define _I (_Iatom->I) +#define _S (_Satom->I) +#define _F (_Satom->F) +#define skip_one _I++ /* I/O iterations, updated 2004-04-19 by xCh. */ @@ -238,10 +249,20 @@ obsolete #define get_interface(IFACE) __get_##_##IFACE(void) // must be function #define get_object(TYPE) (_CLL1_MALLOC(TYPE),CLL1_ASSERT(),) //#define _(OBJECT,FUNCTION) OBJECT==NULL?NULL::(*(OBJECT->__class->METHOD))(OBJECT -#endif + /* ------------------------------------------------------------------- */ /* some general ideas, unfinished */ //#define _CLL1_THROW(ERR) //#define _CLL1_TRY _NEW_E for(;;) if () {.... if(_Eatom->SYS) { if(_Eatom->RET) return; else break;} else { } -//#define _CLL1_ASSERT(EXPR) assert(EXPR) +//#define _ASSERT(EXPR) assert(EXPR) //#define _CLL1_MALLOC(TYPE) malloc(sizeof(TYPE)) + +/* ------------------------------------------------------------------- */ +/* Voila - here we go ! */ +/* ------------------------------------------------------------------- */ +#define define_cll1_globals _ISTACK _SSTACK _FSTACK _MSTACK _RSTACK //_QSTACK +#define implement_cll1_functions _ECHO /* _GET_STR _GET_MEM */ +#define implement_cll1_library define_cll1_globals implement_cll1_functions +#define program implement_cll1_library int main(int argc, char **argv) +/* ------------------------------------------------------------------- */ +#endif diff --git a/demos/arguments1.c b/demos/arguments1.c index 6cd7bed..a892c3a 100644 --- a/demos/arguments1.c +++ b/demos/arguments1.c @@ -1,24 +1,30 @@ #include "cll1.h" -//try$ make arguments1 -//try$ ./arguments1 -x xxx yyyy -c cccc +//try to run: ./arguments1 -x xxx yyyy -c cccc program { - char *ptr; + Define_str(ptr); - arguments + if_args then { - thisargument(ptr); - if(*ptr=='-') then + for_args { - printf("Command line switch: -%c ",ptr[1]); - nextargument(ptr); - printf("followed by: %s\n",ptr); + ptr=this_arg; + if (ptr[0]=='-') then + { + printf("Command line switch: -%c ",ptr[1]); + if (next_arg) then + { + echo("followed by: ",next_arg); + skip_one; + } + print(""); + } + else + print("Standalone argument: ",ptr); } - else - printf("Simple argument: %s \n",ptr); } else - puts("Bleeeeh, no arguments supplied... :-("); + print("Bleeeeh, no arguments supplied... :-("); } diff --git a/demos/arguments2.c b/demos/arguments2.c index 3982e16..63276cf 100644 --- a/demos/arguments2.c +++ b/demos/arguments2.c @@ -1,18 +1,27 @@ #include "cll1.h" -//try$ make arguments2 -//try$ ./arguments2 -f xxx yyyy -? +//try to run: ./arguments2 -f reload yyyy -? program { - char *ptr; - - arguments + if_args then { - argument("-?") printf("This is just help!\n"); - else argument("-f") { nextargument(ptr) printf("Forced argument: %s\n",ptr); } - else { thisargument(ptr); printf("Simple argument: %s \n",ptr); } + for_args + { + if_arg ("-?") then + print("This is just help!"); + else if_arg ("-f") then + { + if (next_arg) then + { + print("Forced argument: ",next_arg); + skip_one; + } + } + else + print("Normal argument: ",this_arg); + } } else - puts("Bleeeeh, no arguments supplied... :-("); + print("Bleeeeh, no arguments supplied... :-("); } diff --git a/demos/boolean.c b/demos/boolean.c index 838e31c..1b5f1e2 100644 --- a/demos/boolean.c +++ b/demos/boolean.c @@ -3,9 +3,9 @@ program { boolean t=TRUE,f=FALSE; - - printf("TRUE and FALSE = %s", bool_str(t and f) ); - printf(",TRUE or FALSE = %s", bool_str(t or f) ); - printf(",not FALSE = %s", bool_str(not f) ); - printf(",not TRUE = %s\n", bool_str(not t) ); + + print("TRUE and FALSE = ", bool_str(t and f) ); + print("TRUE or FALSE = ", bool_str(t or f) ); + print("not FALSE = ", bool_str(not f) ); + print("not TRUE = ", bool_str(not t) ); } diff --git a/demos/for_range.c b/demos/for_range.c index 6aedae7..44a7b5b 100644 --- a/demos/for_range.c +++ b/demos/for_range.c @@ -4,20 +4,23 @@ program { int i; - printf("for_range(i,0,1)"); + echo("for_range(i,0,1)"); for_range(i,0,1) printf(" [%d]",i); + print(""); - printf("\nfor_range(i,1,0)"); + echo("for_range(i,1,0)"); for_range(i,1,0) printf(" [%d]",i); + print(""); - printf("\nfor_range(i,1,1)"); + echo("for_range(i,1,1)"); for_range(i,1,1) printf(" [%d]",i); + print(""); - printf("\nfor_range(i,1,5)"); + echo("for_range(i,1,5)"); for_range(i,1,5) printf(" [%d]",i); + print(""); - printf("\nfor_range(i,5,1)"); + echo("for_range(i,5,1)"); for_range(i,5,1) printf(" [%d]",i); - - printf("\n"); + print(""); } diff --git a/demos/hello.c b/demos/hello.c index 50a595d..b59bc77 100644 --- a/demos/hello.c +++ b/demos/hello.c @@ -2,5 +2,6 @@ program { - print("Hello ","world"," !\n"); + echo("Hello ","world","! "); + print("Hello ","world","!"); } diff --git a/demos/just-c99/print.c b/demos/just-c99/print.c new file mode 100644 index 0000000..7abbb16 --- /dev/null +++ b/demos/just-c99/print.c @@ -0,0 +1,12 @@ +#include +#include + +void _echo(char *s, ... ) { va_list ap; for(va_start(ap,s);s;s=va_arg(ap,char *)) fputs(s,stdout); va_end(ap); } +#define print(...) _echo(__VA_ARGS__,"\n",NULL) +#define echo(...) _echo(__VA_ARGS__,NULL) + +int main(void) +{ + echo("Hello ","world","! "); + print("Hello ","world","! "); +} diff --git a/demos/performance/cll1-print.c b/demos/performance/cll1-print.c new file mode 100644 index 0000000..eb20e07 --- /dev/null +++ b/demos/performance/cll1-print.c @@ -0,0 +1,12 @@ +#include "cll1.h" + +#define RUNS 10000000UL +program +{ + unsigned long i; + for_range(i,0,RUNS) + { + print("stuff"); + } + return 0; +} diff --git a/demos/performance/cll1.h b/demos/performance/cll1.h new file mode 120000 index 0000000..58b5d6f --- /dev/null +++ b/demos/performance/cll1.h @@ -0,0 +1 @@ +../../cll1.h \ No newline at end of file diff --git a/demos/performance/fputs.c b/demos/performance/fputs.c new file mode 100644 index 0000000..6c84320 --- /dev/null +++ b/demos/performance/fputs.c @@ -0,0 +1,11 @@ +#include + +#define RUNS 10000000UL +int main (void) +{ +unsigned long i; +for (i=0;i + +#define RUNS 10000000UL +int main (void) +{ +unsigned long i; +for (i=0;i + +#define RUNS 10000000UL +int main (void) +{ +unsigned long i; +for (i=0;i /dev/null +cat print.pl +echo "Running..." +time ./print.pl &> /dev/null +cat cll1-print.c +echo "Running..." +time ./cll1-print &> /dev/null +cat printf.c +echo "Running..." +time ./printf &> /dev/null +cat puts.c +echo "Running..." +time ./puts &> /dev/null +cat fputs.c +echo "Running..." +time ./fputs &> /dev/null diff --git a/demos/repeat.c b/demos/repeat.c index 7e8622e..70a5abe 100644 --- a/demos/repeat.c +++ b/demos/repeat.c @@ -4,7 +4,7 @@ program { repeat(2) { - printf("Tohle cele delam 2x:\n"); - repeat(3) printf("Tohle delam 3x...\n"); + print("Tohle cele delam 2x:"); + repeat(3) print("Tohle delam 3x..."); } } -- 2.30.2