| 1 | /* ------------------------------------------------------------------- */ |
| 2 | /* C<<1 header file v0.7.1 - style sheet for ANSI C */ |
| 3 | /* CERL - Cumbersome Extraction and Report Language */ |
| 4 | /* ------------------------------------------------------------------- */ |
| 5 | /* Copyright (c) 2004-2008 Michael xChaos Polak, x(at)n.cz |
| 6 | |
| 7 | The C<<1 header file is free software; you can redistribute it and/or |
| 8 | modify it under the terms of the GNU Lesser General Public |
| 9 | License as published by the Free Software Foundation; either |
| 10 | version 2.1 of the License, or (at your option) any later version. |
| 11 | |
| 12 | The C<<1 header file is distributed in the hope that it will be useful, |
| 13 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 15 | Lesser General Public License for more details. |
| 16 | |
| 17 | You should have received a copy of the GNU Lesser General Public License |
| 18 | along with C<<1 header file; if not, write to the Free Software |
| 19 | Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA |
| 20 | |
| 21 | */ |
| 22 | |
| 23 | #ifndef _CLL1_H |
| 24 | #define _CLL1_H |
| 25 | |
| 26 | #include <string.h> |
| 27 | #include <malloc.h> |
| 28 | #include <time.h> |
| 29 | #include <stdlib.h> |
| 30 | #include <stdarg.h> |
| 31 | #include <unistd.h> |
| 32 | #ifdef USE_LIBRARY_MYSQL |
| 33 | #include <mysql/mysql.h> |
| 34 | #endif |
| 35 | #ifdef USE_LIBRARY_FASTCGI |
| 36 | #include <fcgi_stdio.h> |
| 37 | #else |
| 38 | #include <stdio.h> |
| 39 | #endif |
| 40 | /* ------------------------------------------------------------------- */ |
| 41 | /* 1. C<<1 constants and optional keywords, updated 2008-01-07 by xCh. */ |
| 42 | /* ------------------------------------------------------------------- */ |
| 43 | #define then |
| 44 | #define not ! |
| 45 | #define and && |
| 46 | #define or || |
| 47 | #define TRUE 1 |
| 48 | #define FALSE 0 |
| 49 | #define bool_str(I) (I?"TRUE":"FALSE") |
| 50 | #define boolean int |
| 51 | #define mem struct |
| 52 | #define construct struct |
| 53 | /* ------------------------------------------------------------------- */ |
| 54 | /* 2. C<<1 basic data types - updated 2008-01-07 by xCh. */ |
| 55 | /* ------------------------------------------------------------------- */ |
| 56 | #define Create_str(ID,INIT) char *ID=INIT |
| 57 | #define Define_str(ID) char *ID=NULL |
| 58 | #define Create_mem(ID,TYPE) struct TYPE *ID=get_mem(TYPE) |
| 59 | #define Create_mem_in_context(ID,TYPE,CONTEXT) struct TYPE *ID=get_mem_in_context(TYPE,CONTEXT) |
| 60 | #define Define_mem(ID,TYPE) struct TYPE *ID=NULL |
| 61 | #define Define_mems(ID1,I2,TYPE) Define_mem(ID1,TYPE);Define_mem(ID2,TYPE) |
| 62 | |
| 63 | /* .... well....ehm...not very useful... :-) */ |
| 64 | #define Create_var(ID,TYPE,INIT) TYPE ID=INIT |
| 65 | #define Define_var(ID,TYPE) TYPE ID |
| 66 | /* ------------------------------------------------------------------- */ |
| 67 | #define list(T) struct T *_next |
| 68 | #define tree(T) struct T *_next;struct T *_seek |
| 69 | #define create(A,T) (A=(struct T *)malloc(sizeof(struct T)),A->_next=NULL,A) |
| 70 | #define push(A,B) ((A && A!=B)?A->_next=B:0,B=A) |
| 71 | #define pop(A,B) (B?((A?free(A):0),A=B=B->_next):(A=NULL)) |
| 72 | #define append(A,B) if(B) { void *N=A; A->_next=NULL; for_search(A,B,!A->_next) {A->_next=N; break;}} else push(A,B); |
| 73 | #define remove(A,B,C) { void **_D=NULL; for_search(A,B,C) { if(_D)*_D=A->_next; else B=A->_next; free(A); } else _D=(void *)&(A->_next); } |
| 74 | #define drop(A,B) for( A=B; A || (B=NULL); B=A, A=A->_next, free(B) ) |
| 75 | /* ------------------------------------------------------------------- */ |
| 76 | /* 3. C<<1 general iterators - updated 2008-01-07 by xCh. */ |
| 77 | /* ------------------------------------------------------------------- */ |
| 78 | #define loop while(TRUE) |
| 79 | #define repeat(N) if(N>0) _NEW_I for_range(_I,1,N) |
| 80 | #define for_range(VAR,FROM,TO) for( VAR=FROM ; FROM<=TO?VAR<=TO:VAR>=TO ; FROM<=TO?VAR++:VAR--) |
| 81 | #define for_each(A,B) for(A=B; A; A=A->_next) |
| 82 | #define for_search(A,B,C) for_each(A,B) if(C) |
| 83 | #define for_file_lines(STR,F) /* _NEW_F for() */ |
| 84 | #define for_ints(VAR,...) _NEW_I while(_iterate_ints(&VAR,_I,__VA_LIST) ) |
| 85 | #define for_strs(VAR,...) _NEW_S |
| 86 | /* obsolete #define every(A,B) for( A=B; A; A=A->_next) */ |
| 87 | /* obsolete #define search(A,B,C) every(A,B) if(C) */ |
| 88 | /* obsolete #define find(A,B,C) search(A,B,C) break; if(A) */ |
| 89 | /* ------------------------------------------------------------------- */ |
| 90 | /* 4.C<<1 conditionals - updated 2008-01-07 by xCh. */ |
| 91 | /* ------------------------------------------------------------------- */ |
| 92 | #define if_exists(A,B,C) for_search(A,B,C) break; if(A) |
| 93 | /* ------------------------------------------------------------------- */ |
| 94 | /* 5. C<<1 argument handling, updated 2008-01-16 by xCh. */ |
| 95 | /* ------------------------------------------------------------------- */ |
| 96 | #define for_args _NEW_I for(_I=1;_I<argc;_I++) |
| 97 | #define if_args if(argc>1) |
| 98 | #define if_arg(VAL) if(eq(argv[_I],VAL)) |
| 99 | #define this_arg argv[_I] |
| 100 | #define next_arg ((_I+1<argc)?argv[_I]:NULL) |
| 101 | /* ------------------------------------------------------------------- */ |
| 102 | /* 6. C<<1 va_list based library functions, updated 2008-01-22 by xCh. */ |
| 103 | /* ------------------------------------------------------------------- */ |
| 104 | #define _ECHO void _cll1_print(int nl,char *s, ... ) { va_list ap; for(va_start(ap,s);s!=(void *)&_cll1_print;s=va_arg(ap,char *)) fputs(s?s:"",stdout); va_end(ap); if(nl) putchar('\n');} |
| 105 | #define print(...) _cll1_print(1,__VA_ARGS__,(void *)&_cll1_print) |
| 106 | #define echo(...) _cll1_print(0,__VA_ARGS__,(void *)&_cll1_print) |
| 107 | /* ------------------------------------------------------------------- */ |
| 108 | /* 8. C<<1 CGI and FASTCGI handling, updated 2008-01-16 by xCh. */ |
| 109 | /* ------------------------------------------------------------------- */ |
| 110 | #ifdef USE_LIBRARY_FASTCGI |
| 111 | #define while_fastcgi_accept while(FCGI_Accept()>=0) |
| 112 | #define USE_MODULE_CGI |
| 113 | #endif |
| 114 | #ifdef USE_MODULE_CGI |
| 115 | #define cgi(MIME) /* _init_cgi */ if(MIME) printf("Content-type: %s\r\n\r\n",MIME); |
| 116 | //todo getcgi(VAR) |
| 117 | //todo define_init_cgi |
| 118 | //todo #define _QSTACK struct _QStack { char *Q; int _23; list(_QStack); } *_Qatom,*_Qstack=NULL; |
| 119 | #endif |
| 120 | /* ------------------------------------------------------------------- */ |
| 121 | /* 9. MySQL infrastructure and wrappers - updated 2007-12-19 by xCh. */ |
| 122 | /* ------------------------------------------------------------------- */ |
| 123 | #ifdef USE_LIBRARY_MYSQL |
| 124 | #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)) |
| 125 | #define for_mysql_query_results(ROW,QUERY) _NEW_(_Ratom,_Rstack,_RStack)for(_Ratom->R=(mysql_query(_Matom->M,QUERY)?NULL:mysql_use_result(_Matom->M)),ROW=(_Ratom->R?mysql_fetch_row(_Ratom->R):NULL);ROW;ROW=mysql_fetch_row(_Ratom->R)) |
| 126 | #define _MSTACK struct _MStack { MYSQL *M; int _23; list(_MStack); } *_Matom,*_Mstack=NULL; |
| 127 | #define _RSTACK struct _RStack { MYSQL_RES *R; int _23; list(_RStack); } *_Ratom,*_Rstack=NULL; |
| 128 | #else |
| 129 | #define _MSTACK |
| 130 | #define _RSTACK |
| 131 | #endif |
| 132 | /* ------------------------------------------------------------------- */ |
| 133 | /* old - from version 0.6 */ |
| 134 | /* ------------------------------------------------------------------- */ |
| 135 | |
| 136 | /* ------------------------------------------------------------------- */ |
| 137 | /* EXP macros for dummysort sequences, updated 2003-05-29 by xCh. */ |
| 138 | /* ------------------------------------------------------------------- */ |
| 139 | #define order_by(K1,K2) (K1>K2) |
| 140 | #define desc_order_by(K1,K2) (K1<K2) |
| 141 | #define sort_by(K1,K2) (strcasecmp(K1, K2)>0) |
| 142 | #define desc_sort_by(K1,K2) (strcasecmp(K1, K2)<0) |
| 143 | #define ascii_by(K1,K2) (strcmp(K1, K2)>0) |
| 144 | #define desc_ascii_by(K1,K2) (strcmp(K1, K2)<0) |
| 145 | /* ------------------------------------------------------------------- */ |
| 146 | /* Dummysort sequences, updated 2003-05-29 by xCh. */ |
| 147 | /* ------------------------------------------------------------------- */ |
| 148 | #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); } |
| 149 | #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); } |
| 150 | /* ------------------------------------------------------------------- */ |
| 151 | /* String macros & sequences, updated 2008-01-22 by xCh. */ |
| 152 | /* ------------------------------------------------------------------- */ |
| 153 | #define eq(A,B) (A && B && !strcmp(A,B)) |
| 154 | #define eqi(A,B) (A && B && !strcasecmp(A,B)) |
| 155 | /* Borland C legacy */ |
| 156 | #define strcmpi(A,B) strcasecmp(A,B) |
| 157 | /* ------------------------------------------------------------------- */ |
| 158 | /* |
| 159 | obsolete |
| 160 | #define strlwr(A) {char *_S=A; while(_&&*_S){*_S=tolower(*_S);_S++;}} |
| 161 | #define strupr(A) {char *_S=A; while(_&&*_S){*_S=toupper(*_S);_S++;}} |
| 162 | #define string(S,L) (S=(char *)malloc(L),*S=0) |
| 163 | #define duplicate(A,B) if(A) { string(B,strlen(A)+1); strcpy(B,A); } |
| 164 | #define concatenate(A,B,C) if (A && B) { string(C,strlen(A)+strlen(B)+1); strcpy(C,A); strcat(C,B); } |
| 165 | #define suffix(A,B,C) (((A=strrchr(B,C))&&!(*(A++)=0))||(A=B)) |
| 166 | #define prefix(A,B,C) ((A=B)&&((B=strchr(B,C))&&!(*(B++)=0)||(B=A))) |
| 167 | #define gotoalpha(CHAR) if(CHAR)while(*CHAR && !isalpha(*CHAR))CHAR++ |
| 168 | #define goto_alpha(CHAR) if(CHAR)while(*CHAR && !isalpha(*CHAR) && *CHAR!='_')CHAR++ |
| 169 | #define gotoalnum(CHAR) if(CHAR)while(*CHAR && !isalnum(*CHAR))CHAR++ |
| 170 | #define goto_alnum(CHAR) if(CHAR)while(*CHAR && !isalnum(*CHAR) && *CHAR!='_')CHAR++ |
| 171 | #define skipalpha(CHAR) if(CHAR)while(*CHAR && isalpha(*CHAR))CHAR++ |
| 172 | #define skip_alpha(CHAR) if(CHAR)while(*CHAR && (isalpha(*CHAR) || *CHAR=='_'))CHAR++ |
| 173 | #define skipalnum(CHAR) if(CHAR)while(*CHAR && isalnum(*CHAR))CHAR++ |
| 174 | #define skip_alnum(CHAR) if(CHAR)while(*CHAR && (isalnum(*CHAR) || *CHAR=='_'))CHAR++ |
| 175 | #define skipspaces(CHAR) if(CHAR)while(*CHAR==' ')CHAR++ |
| 176 | #define cutspaces(CHAR) if(CHAR){int _L=strlen(CHAR); while(--_L>0 && CHAR[_L]==' ')CHAR[_L]=0;} |
| 177 | #define gotochr(CHAR,C) if(CHAR)while(*CHAR && *CHAR!=C)CHAR++ |
| 178 | #define tr(CHAR,B,C) {char *_S=CHAR; while(*_S){ if(*_S==B)*_S=C; _S++; }} |
| 179 | #define strswitch(CHAR) {char *_K=CHAR; FILE *_F=NULL; {{ |
| 180 | #define stroption(STR) if(eq(STR,_K)) |
| 181 | #define match(KEY,VAL) {char *_K=KEY, *_V=VAL; FILE *_F=NULL; {{ |
| 182 | #define assign(STR,SETVAR) stroption(STR) SETVAR=_V |
| 183 | */ |
| 184 | /* ------------------------------------------------------------------- */ |
| 185 | /* Infrastructure, updated 2008-01-08 by xCh. */ |
| 186 | /* ------------------------------------------------------------------- */ |
| 187 | /* old #define program int _I; int main(int argc, char **argv) */ |
| 188 | #define _ISTACK struct _IStack { int I; int _23; list(_IStack); } *_Iatom,*_Istack=NULL; |
| 189 | #define _SSTACK struct _SStack { char *S; int _23; list(_SSTACK); } *_Satom,*_Sstack=NULL; |
| 190 | #define _FSTACK struct _FStack { FILE *F; int _23; list(_FSTACK); } *_Fatom,*_Fstack=NULL; |
| 191 | |
| 192 | |
| 193 | #define _NEW_(ATOM,STACK,TYPE) (create(ATOM,TYPE),ATOM->_23=1,push(ATOM,STACK);ATOM->_23||pop(ATOM,STACK)&&0;ATOM->_23--) |
| 194 | #define _NEW_I for _NEW_(_Iatom,_Istack,_IStack) |
| 195 | //#define _NEW_I for(create(_Iatom,_IStack),_Iatom->_23=1,push(_Iatom,_Istack);_Iatom->_23||pop(_Iatom,_Istack)&&0;_Iatom->_23--) |
| 196 | #define _NEW_S for(create(_Satom,_SStack),_Satom->_23=1,push(_Satom,_Sstack);_Satom->_23||pop(_Satom,_Sstack)&&0;_Satom->_23--) |
| 197 | #define _I (_Iatom->I) |
| 198 | #define _S (_Satom->I) |
| 199 | #define _F (_Satom->F) |
| 200 | #define skip_one _I++ |
| 201 | |
| 202 | /* I/O iterations, updated 2004-04-19 by xCh. */ |
| 203 | |
| 204 | #define fparse(S,L,F) for(fgets(S,L,F);*S && !feof(F);fgets(S,L,F)) |
| 205 | #define input(S,L) fparse(S,L,stdin) |
| 206 | #define fstring(S,F) { int _C=0,_L=0; fpos_t _P; fgetpos(F,&_P); while(_C!='\n' && !feof(F)){ _C=fgetc(F); _L++; } string(S,_L); fsetpos(F,&_P);fgets(S,_L,F);fgetc(F);} |
| 207 | #define parses(S,F) {FILE *_F=fopen(F,"r"); if(_F) { while(!feof(_F)) { fstring(S,_F); |
| 208 | #define parse(F) {char *_; FILE *_F=fopen(F,"r"); if(_F) { while(!feof(_F)) { fstring(_,_F); |
| 209 | #define fail }} else {{ |
| 210 | #define done }} if(_F)fclose(_F);} |
| 211 | #define option(STR,SETVAR) if(_){char *_K,*_V,*_O,*_Q; duplicate(_,_Q); _O=_Q; tr(_O,'\t',' '); prefix(_K,_O,' '); if(eq(STR,_K)) {skipspaces(_O); prefix(_V,_O,'#'); cutspaces(_V); SETVAR=_V; _=NULL;} else free(_Q);} |
| 212 | #define ioption(STR,SETVAR) if(_){char *_K,*_V,*_O,*_Q; duplicate(_,_Q); _O=_Q; tr(_O,'\t',' '); prefix(_K,_O,' '); if(eq(STR,_K)) {skipspaces(_O); prefix(_V,_O,'#'); cutspaces(_V); SETVAR=atoi(_V); _=NULL;} free(_Q);} |
| 213 | #define loption(STR,SETVAR) if(_){char *_K,*_V,*_O,*_Q; duplicate(_,_Q); _O=_Q; tr(_O,'\t',' '); prefix(_K,_O,' '); if(eq(STR,_K)) {skipspaces(_O); prefix(_V,_O,'#'); cutspaces(_V); SETVAR=atol(_V); _=NULL;} free(_Q);} |
| 214 | |
| 215 | /* Dynamic list advanced I/O, updated 2003-05-30 by xCh. */ |
| 216 | |
| 217 | /* obsolete #define load(A,B,F,T,K) {char *_S; parses(_S,F) { create(A,T); A->K=_S; A->_eoln=TRUE; append(A,B);} done; A->_eoln=FALSE;} */ |
| 218 | /* obsolete #define save(A,B,F,K) {FILE *_F=fopen(F,"w"); if(_F) { for_each(A,B) {fputs(A->K,_F); if(A->_eoln) fputc('\n',_F);} fclose(_F);}} */ |
| 219 | |
| 220 | /* I/O sequences, updated 2003-05-29 by xCh. */ |
| 221 | |
| 222 | #define nullreopen(F) F=freopen("/dev/null","r",F) |
| 223 | #define stdinredir(CMD) {int _r[2];pipe(_r);if(fork()==0){dup2(_r[1],1);close(_r[0]);CMD;exit(0);}nullreopen(stdin);dup2(_r[0],0);close(_r[1]);} |
| 224 | #define shell(CMD) stdinredir(system(CMD)) |
| 225 | #define paste(STR) stdinredir(fputs(STR,stdout)) |
| 226 | |
| 227 | /* String iterations, updated 2003-06-19 by xCh. */ |
| 228 | |
| 229 | #define split(A,B,C) for(prefix(A,B,C);A;(A!=B)&&prefix(A,B,C)||(A=NULL)) |
| 230 | #define valid_split(A,B,C) split(A,B,C) if(*A) |
| 231 | #define columns(A,B,C,V) for(V=0,prefix(A,B,C);A;((A!=B)&&prefix(A,B,C)||(A=NULL)),V++) |
| 232 | #define valid_columns(A,B,C,V) for(V=0,prefix(A,B,C);A;((A!=B)&&prefix(A,B,C))||(A=NULL)) if(*A&&++V) |
| 233 | #define column(A,B,C,V) { int _V; columns(A,B,C,_V) if(_V==V) break; } |
| 234 | #define valid_column(A,B,C,V) { int _V; valid_columns(A,B,C,_V) if(_V==V) break; } |
| 235 | |
| 236 | /* Useful structures, updated 2003-05-29 by xCh. */ |
| 237 | |
| 238 | #define hashtable(TYPE,NAME,VALUE) struct TYPE { char *NAME; char *VALUE; list(TYPE); } |
| 239 | #define textfile(TYPE,LINE) struct TYPE { char *LINE; char _eoln; list(TYPE); } |
| 240 | #define date(S) { time_t _T; _T=time(NULL); duplicate(ctime(&_T),S); } |
| 241 | |
| 242 | /* ------------------------------------------------------------------- */ |
| 243 | /* Object infrastructure and basic macros - updated 2008-01-13 by xCh. */ |
| 244 | /* unfinished */ |
| 245 | #define bind_interface(TYPE) struct TYPE *__class |
| 246 | #define method(NAME) |
| 247 | #define define_interface(IFACE,TYPE) struct TYPE *__get_##_##IFACE(void) { struct TYPE *_FACE=get_mem(TYPE); __bind_##_##IFACE(_FACE); return _IFC; } void __bind_##_##IFACE(struct TYPE *_FACE) |
| 248 | #define bind_method(METHOD,FUNCTION) _IFACE->METHOD=FUNCTION |
| 249 | #define get_interface(IFACE) __get_##_##IFACE(void) |
| 250 | // must be function #define get_object(TYPE) (_CLL1_MALLOC(TYPE),CLL1_ASSERT(),) |
| 251 | //#define _(OBJECT,FUNCTION) OBJECT==NULL?NULL::(*(OBJECT->__class->METHOD))(OBJECT |
| 252 | |
| 253 | /* ------------------------------------------------------------------- */ |
| 254 | /* some general ideas, unfinished */ |
| 255 | //#define _CLL1_THROW(ERR) |
| 256 | //#define _CLL1_TRY _NEW_E for(;;) if () {.... if(_Eatom->SYS) { if(_Eatom->RET) return; else break;} else { } |
| 257 | //#define _ASSERT(EXPR) assert(EXPR) |
| 258 | //#define _CLL1_MALLOC(TYPE) malloc(sizeof(TYPE)) |
| 259 | |
| 260 | /* ------------------------------------------------------------------- */ |
| 261 | /* Voila - here we go ! */ |
| 262 | /* ------------------------------------------------------------------- */ |
| 263 | #define define_cll1_globals _ISTACK _SSTACK _FSTACK _MSTACK _RSTACK //_QSTACK |
| 264 | #define implement_cll1_functions _ECHO /* _GET_STR _GET_MEM */ |
| 265 | #define implement_cll1_library define_cll1_globals implement_cll1_functions |
| 266 | #define program implement_cll1_library int main(int argc, char **argv) |
| 267 | /* ------------------------------------------------------------------- */ |
| 268 | #endif |