005431b8 |
1 | /* .----------------------------------------------------------------------. |
2 | / C<<1 header file - C language style sheet - GNU C/ANSI C99 compliant |
3 | / Cumbersome Extraction and Report Language - http://cll1h.arachne.cz/ |
f5725936 |
4 | '---------------------------------------------------------------------- */ |
5 | /* |
6 | Copyright (c) 2004-2008 Michael xChaos Polak - x(at)n.cz |
30628092 |
7 | |
8 | The C<<1 header file is free software; you can redistribute it and/or |
9 | modify it under the terms of the GNU Lesser General Public |
10 | License as published by the Free Software Foundation; either |
11 | version 2.1 of the License, or (at your option) any later version. |
12 | |
13 | The C<<1 header file is distributed in the hope that it will be useful, |
14 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
15 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
16 | Lesser General Public License for more details. |
17 | |
ceb7f88d |
18 | You should have received a copy of the GNU Lesser General Public License |
19 | along with C<<1 header file; if not, write to the Free Software |
20 | Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA |
097bac9a |
21 | */ |
097bac9a |
22 | #ifndef _CLL1_H |
23 | #define _CLL1_H |
f5725936 |
24 | #define CLL1H_VERSION "0.7.1" |
005431b8 |
25 | /* .----------------------------------------------------------------------. |
f5725936 |
26 | / 0. Library definitions not parsed by @CC, updated 2008-01-26 xCh. |
27 | '---------------------------------------------------------------------- */ |
28 | /* |
29 | @define USE_LIBRARY_MYSQL -lmysqlclient |
30 | @define USE_LIBRARY_FASTCGI -lfcgi |
31 | @define USE_PREPROCESSOR cake *.html *.xml > _include.c |
32 | @define USE_POSTPROCESSOR strip @ |
33 | */ |
005431b8 |
34 | /* .----------------------------------------------------------------------. |
f5725936 |
35 | / 1. C<<1 header auto include, updated 2008-01-26 xCh. |
36 | '---------------------------------------------------------------------- */ |
bc44f2cc |
37 | #ifndef DISABLE_MODULE_AUTOINCLUDE |
30628092 |
38 | #include <string.h> |
25a3f430 |
39 | #include <strings.h> |
04a13a6f |
40 | #include <malloc.h> |
30628092 |
41 | #include <time.h> |
04a13a6f |
42 | #include <stdlib.h> |
420bb771 |
43 | #include <stdarg.h> |
04a13a6f |
44 | #include <unistd.h> |
4643aeaf |
45 | #include <locale.h> |
04a13a6f |
46 | #ifdef USE_LIBRARY_FASTCGI |
47 | #include <fcgi_stdio.h> |
48 | #else |
49 | #include <stdio.h> |
50 | #endif |
bce4b05d |
51 | #endif |
52 | #ifdef USE_LIBRARY_MYSQL |
53 | #include <mysql/mysql.h> |
54 | #endif |
005431b8 |
55 | /* .----------------------------------------------------------------------. |
4643aeaf |
56 | / 0. C<<1 typedefs and public gloabl variables, updated 2008-02-03 xCh. |
57 | '----------------------------------------------------------------------- */ |
bc44f2cc |
58 | typedef char * str; /* #ifdef USE_MODULE_UNICODE...typedef wchar_t * str; */ |
59 | typedef unsigned char * buf; /* this will be always 8-bit usigned char * */ |
4643aeaf |
60 | typedef FILE * file; |
61 | typedef int bool; |
62 | extern str NIL,EOL,OFS,IFS; |
63 | extern unsigned RANDOM_SEED; |
ce26b4aa |
64 | #define _EXPORT_GLOBALS str NIL="(null)", EOL="\n", OFS=" ", IFS=" \t\n"; unsigned RANDOM_SEED=0; |
4643aeaf |
65 | /* .----------------------------------------------------------------------. |
bc44f2cc |
66 | / 1. C<<1 then buddhist section, updated 2008-02-04 xCh. |
f5725936 |
67 | '----------------------------------------------------------------------- */ |
68 | #ifndef DISABLE_MODULE_THEN_BUDDHISM |
04a13a6f |
69 | #define then |
25a3f430 |
70 | #define MAYBE (int)((long)(RANDOM_SEED?random():((RANDOM_SEED=(unsigned)time(NULL)),srandom(RANDOM_SEED),random()))%2) |
71 | #define WHOKNOWS (int)(srandom((unsigned)time(NULL)),random()%2) |
bc44f2cc |
72 | #define for_samsara(MIND,SOUL) for(MIND=SOUL; MIND || ((MIND->__next=SOUL),0) ; MIND=MIND->__next) |
4643aeaf |
73 | #define samsara(MIND,SOUL) for(MIND=SOUL; MIND || ((MIND->__next=SOUL),0) ; MIND=MIND->__next) |
25a3f430 |
74 | //#define atob(EXPR) (word_in_stri(YES,(EXPR))?1:0) |
4643aeaf |
75 | //#define _EXPORT_YES str YES="TRUE\nYES\nJA\nOUI\nDA\nANO\nY\n1"; |
25a3f430 |
76 | //#else |
25a3f430 |
77 | #define atob(EXPR) (eqi((EXPR),YES)?1:0) |
4643aeaf |
78 | #define _EXPORT_YES str YES="TRUE"; |
f5725936 |
79 | #endif |
005431b8 |
80 | /* .----------------------------------------------------------------------. |
61ad8a2b |
81 | / 2. C<<1 almost then buddhist section, updated 2008-02-02 xCh. |
f5725936 |
82 | '----------------------------------------------------------------------- */ |
30628092 |
83 | #define not ! |
4a48705a |
84 | #define and && |
85 | #define or || |
30628092 |
86 | #define TRUE 1 |
87 | #define FALSE 0 |
9ae3b221 |
88 | #define WEIRDINT (1<<(sizeof(int)*8-1)) |
25a3f430 |
89 | #define btoa(EXPR) ((EXPR)?"TRUE":"FALSE") |
90 | #define coalesce(VAR,EXPR) (VAR?VAR:(EXPR)) |
005431b8 |
91 | /* .----------------------------------------------------------------------. |
547332b7 |
92 | / 4. C<<1 data declaration and allocation, updated 2008-01-31 xCh. |
f5725936 |
93 | '----------------------------------------------------------------------- */ |
4643aeaf |
94 | #define def_mem(TYPE) typedef struct _##TYPE##_t * TYPE; struct _##TYPE##_t |
bc44f2cc |
95 | #define construct_mem(TYPE) TYPE _init_##TYPE(void) { create_mem(this,TYPE); /* <-' */ _init_##TYPE(this); return this; } _init_##TYPE(TYPE this) |
4643aeaf |
96 | #define get_mem(...) _GET_MEM(__VA_ARGS__,NULL) |
bc44f2cc |
97 | #define init_mem(...) _init_##_ARG1(__VA_ARGS__,)( _ARG2(__VA_ARGS__,,) - 0 ) |
4643aeaf |
98 | #define create_mem(ID,...) _ARG1(__VA_ARGS__,) ID=_GET_MEM(__VA_ARGS__,NULL) |
99 | #define new_mem(ID,...) _ARG1(__VA_ARGS__,) ID=init_mem(__VA_ARGS__) |
818209bd |
100 | #define mem_len(TYPE) sizeof(struct _##TYPE##_t) |
101 | //#define mem_copy |
4643aeaf |
102 | #define list(TYPE) TYPE __next |
103 | #define tree(TYPE) TYPE __next; TYPE __seek |
818209bd |
104 | #define dict(TYPE) tree(TYPE); str __key |
105 | #define array(TYPE) tree(TYPE); int __key |
bc44f2cc |
106 | #define interface(TYPE) TYPE __interface |
4643aeaf |
107 | #define this_must_be(TYPE) TYPE this |
005431b8 |
108 | /* .----------------------------------------------------------------------. |
f5725936 |
109 | / 5. C<<1 string manipulation, updated 2008-01-26 xCh. |
110 | '----------------------------------------------------------------------- */ |
61ad8a2b |
111 | #define get_str(...) _cll1_get_str(__VA_ARGS__,NIL) |
bc44f2cc |
112 | #define eq(A,B) (!_cll1_str_cmp(A,B)) |
113 | #define eqi(A,B) (!_cll1_str_cmpi(A,B)) |
ce26b4aa |
114 | //#define str_eq(A,B) eq(A,B) ? |
115 | //#define str_eqi(A,B) eqi(A,B) |
bc44f2cc |
116 | #define str_order(A,B) _cll1_str_coll(A,B) |
117 | #define str_ascii(A,B) _cll1_str_cmp(A,B) |
118 | #define str_len(...) _cll1__strlen(__VA_ARGS__,NIL) |
119 | #define str_in(A,B) _cll1_str_str(A,B) |
ce26b4aa |
120 | #define word_in(A,B) _cll1_wordinstr(A,B,0) |
bce4b05d |
121 | #define print(...) _cll1_print(__VA_ARGS__,NIL) |
122 | #define fprint(...) _cll1_fprint(__VA_ARGS__,NIL) |
123 | #define file_print(...) _cll1_fprint(_F,__VA_ARGS__,NIL) |
124 | #define echo(...) _cll1_echo(__VA_ARGS__,NIL) |
125 | #define fecho(...) _cll1_fecho(__VA_ARGS__,NIL) |
126 | #define file_echo(...) _cll1_fecho(_F,__VA_ARGS__,NIL) |
005431b8 |
127 | #define suffix(A,B,C) (((A=strrchr(B,C))&&!(*(A++)=0))||(A=B)) |
128 | #define prefix(A,B,C) ((A=B),((B=strchr(B,C))&&!(*(B++)=0)||(B=A))) |
547332b7 |
129 | #define tr(STR,B,C) _NEW(S,STR) while(S && *_S) { if(*_S==B)*_S=C; /* <-' */ _S++; } |
005431b8 |
130 | /* .----------------------------------------------------------------------. |
f5725936 |
131 | / 6. C<<1 data container manipulation, updated 2008-01-26 xCh. |
132 | '----------------------------------------------------------------------- */ |
005431b8 |
133 | #define push(NODE,HEAD) ( NODE ? ((NODE->__next=HEAD),(HEAD=NODE)) : NULL ) |
134 | #define pop(NODE,HEAD) ( HEAD ? ((NODE=HEAD->__next),free(HEAD),(HEAD=NODE)) : (NODE=NULL) ) |
06ab91f0 |
135 | #define append(NODE,HEAD) { void *N=NODE; /* <-' */ NODE->__next=NULL; for_search(NODE,HEAD,!NODE->__next) { NODE->__next=N; break; } } |
136 | #define remove(NODE,HEAD,EXPR) { void **_D=NULL; /* <-' */ for_search(NODE,HEAD,EXPR) { if(_D)*_D=NODE->__next; else HEAD=NODE->__next; free(NODE); } else _D=(void *)&(NODE->__next); } |
f5725936 |
137 | #define drop(NODE,HEAD) for( NODE=HEAD; NODE || (HEAD=NULL); HEAD=NODE, NODE=NODE->__next, free(HEAD) ) |
005431b8 |
138 | //to do: seek |
139 | //to do: store |
4643aeaf |
140 | #define count(NODE,HEAD,VAR) { _NEW(I,0) for(NODE=HEAD; NODE || ((VAR=_I),0) ; (NODE=NODE->__next)?(_I++):0 ); } |
ce26b4aa |
141 | #define order_by_int(K1,K2) (K1>K2) |
142 | #define order_by_int_desc(K1,K2) (K1<K2) |
143 | #define order_by_str(K1,K2) (str_order(K1,K2)>0) |
144 | #define order_by_str_desc(K1,K2) (str_order(K1,K2)<0) |
145 | #define order_by_ascii(K1,K2) (str_ascii(K1,K2)>0) |
146 | #define order_by_ascii_desc(K1,K2) (str_ascii(K1,K2)<0) |
147 | #define insert(NODE,HEAD,ORDERBY,KEY) { if(HEAD) { void **_L=NULL, *H=HEAD; /* <-' */ for_search(HEAD,H,ORDERBY(HEAD->KEY,NODE->KEY)) { if(_L) {*_L=NODE; NODE->__next=HEAD; } else push(NODE,H); break; } else _L=(void *)&(HEAD->__next); if(!HEAD)*_L=NODE; HEAD=H; } else if (NODE) { push(NODE,HEAD); HEAD->__next=NULL; } } |
148 | #define sort(NODE,HEAD,ORDERBY,KEY) { void *_NEXT; /* <-' */ NODE=HEAD; HEAD=NULL; do { _NEXT=NODE->__next; NODE->__next=NULL; insert(NODE,HEAD,ORDERBY,KEY); NODE=_NEXT; } while(_NEXT); } |
005431b8 |
149 | /* .----------------------------------------------------------------------. |
f5725936 |
150 | / 7. C<<1 builtin iterators and conditionals, updated 2008-01-26 xCh. |
151 | '----------------------------------------------------------------------- */ |
04a13a6f |
152 | #define loop while(TRUE) |
bce4b05d |
153 | #define repeat(N) if(N>0) _NEW(I,0) while(_I++<N) |
086367cd |
154 | #define for_range(VAR,FROM,TO) for( VAR=FROM ; FROM<=TO?VAR<=TO:VAR>=TO ; FROM<=TO?VAR++:VAR--) |
f5725936 |
155 | #define for_each(NODE,HEAD) for(NODE=HEAD; NODE; NODE=NODE->__next) |
156 | #define for_search(NODE,HEAD,EXPR) for_each(NODE,HEAD) if(EXPR) |
bce4b05d |
157 | #define for_file_lines(STR,FNAME) /* _NEW(CTX,get_context()) file_read(FNAME) for(use_context(_CTX),STR=file_line(_F);release_context(_M),(*S && !feof(_F));STR=file_line(_F),end_context()) _CATCH */ |
158 | #define for_input_lines(S,L) /* _NEW(CTX,get_context()) for(use_context(_CTX),STR=file_get_line(stdin);release_context(_M),(*S && !feof(stdin));STR=file_get_line(stdin)) _CATCH */ |
9ae3b221 |
159 | #define for_ints(VAR,...) _NEW(I,0) while( (VAR=_cll1_for_ints(_I++,__VA_ARGS__,WEIRDINT))!=WEIRDINT ) |
25a3f430 |
160 | #define for_strs(VAR,...) _NEW(I,0) while( (VAR=(char *)_cll1_for_ptrs(_I++,__VA_ARGS__,NIL))!=NIL ) |
bce4b05d |
161 | #define for_args _NEW(I,1) while(_I++<argc) |
8a93c4ed |
162 | #define this_arg argv[_I] |
420bb771 |
163 | #define next_arg ((_I+1<argc)?argv[_I]:NULL) |
f5725936 |
164 | #define skip_next _I++ |
61ad8a2b |
165 | #define if_exists(NODE,HEAD,EXPR) for_search(NODE,HEAD,EXPR) break; if(NODE) |
bce4b05d |
166 | #define if_args if(argc>1) |
818209bd |
167 | #define switch_for_args for_args switch_str(this_arg) |
bce4b05d |
168 | #define switch_str(STR) _NEW(S,STR) |
169 | #define if_case(VAL) if(eq(argv[_I],VAL)) |
25a3f430 |
170 | #define for_fields(FIELD,STR) for_valid_split(FIELD,STR,*OFS) |
bce4b05d |
171 | #define for_split_str(A,B,C) for(prefix(A,B,C);A;(A!=B)&&prefix(A,B,C)||(A=NULL)) |
172 | #define for_valid_split(A,B,C) for_split_str(A,B,C) if(*A) |
173 | #define switch_for_columns(A,B,C) _FOR_COLUMNS(A,B,C) switch(__I) |
174 | #define switch_for_valid_columns(A,B,C) _FOR_VALID_COLUMNS(A,B,C) switch (_I) |
175 | #define if_column_number(A,B,C,V) _FOR_COLUMNS(A,B,C) if(_I==V) |
176 | #define if_valid_column_number(A,B,C,V) _FOR_VALID_COLUMNS(A,B,C) if(_I==V) |
005431b8 |
177 | /* .----------------------------------------------------------------------. |
bc44f2cc |
178 | / 8. C<<1 object oriented infrastructure, updated 2008-02-04 |
8db501a0 |
179 | '----------------------------------------------------------------------- */ |
4643aeaf |
180 | #define I_must_be(TYPE) TYPE self, void *_method |
181 | #define we_can_be(TYPE) TYPE _we, void *_method |
182 | #define we_can_be_whatever void *_we, void *_method |
183 | #define I_am(TYPE) TYPE self=(TYPE)_we |
184 | #define def_community(METATYPE,ITYPE) def_mem(METATYPE) { ITYPE __interface; }; |
bc44f2cc |
185 | #define def_method(RET,NAME,...) RET (*NAME)(__VA_ARGS__) |
186 | #define construct_interface(MTYPE,ITYPE) ITYPE _init,_##MTYPE##ITYPE(void *dummy, ...) { new_mem(_FACE,ITYPE); va_list(ap); _##MTYPE##ITYPE##_init(_FACE); return _FACE; } void _##MTYPE##ITYPE##_init(IFACE _FACE) |
8db501a0 |
187 | #define bind_method(METHOD,FUNCTION) _FACE->METHOD=FUNCTION |
4643aeaf |
188 | #define new_interface(ID,MTYPE,...) _ARG1(__VA_ARGS__,) ID=_init_##MTYPE##_ARG1(__VA_ARGS__,) ((void *)init_##MTYPE##__VA_ARGS__,) |
547332b7 |
189 | #define init_interface(TYPE,...) |
bc44f2cc |
190 | #define construct_object(MTYPE,...) (void *)_init_##TYPE##_() |
4643aeaf |
191 | #define init_object(TYPE,...) (TYPE)_init_##TYPE##_(__VA_ARGS__) |
bc44f2cc |
192 | #define init_polymorph(TYPE,COMMUNITY,...) (COMMUNITY)_init_##TYPE##_(__VA_ARGS__) |
547332b7 |
193 | #define _(OBJECT,...) ( _ASSERT(OBJECT), (*( OBJECT->interface->_ARG1(__VA_ARGS__,) )) ( OBJECT,OBJECT->interface->__VA_ARGS__ ) ) |
194 | #define __(OBJECT,...) ( _ASSERT(OBJECT), (*( __cll1_seekmethod(OBJECT->interface,_ARG1(__VA_ARGS__,)) )) ( OBJECT,OBJECT->interface->__VA_ARGS__ ) ) |
bc44f2cc |
195 | #define call(OBJTYPE,METHOD,...) { int _N=0; OBJTYPE _OBJ; /* <-' */ while( (_OBJ=(OBJTYPE)_cll1_for_ptrs(_N++,__VA_ARGS__,NIL))!=(struct OBJTYPE *)NIL ) _(_OBJ,METHOD); } |
8db501a0 |
196 | /* .----------------------------------------------------------------------. |
197 | / 9. implementation of C<<1 library functions, updated 2008-01-26 xCh. |
f5725936 |
198 | '----------------------------------------------------------------------- */ |
f5725936 |
199 | #define _MALLOC void *_cll1_malloc(size_t size,...) { /*temporary solution*/ return malloc(size); } |
bce4b05d |
200 | #define _CLL1_FPRINT(IOSTREAM) { int ofs=0; va_list ap; /* <-' */ for(va_start(ap,s);s!=NIL;s=va_arg(ap,char *)) { if(ofs)fputs(OFS,IOSTREAM);else ofs=1;/**/fputs(coalesce(s,NIL),IOSTREAM); } va_end(ap); fputs(EOL,IOSTREAM); } |
4643aeaf |
201 | #define _DECLARE_PRINT void _cll1_print(char *s,...) |
202 | #define _PRINT _DECLARE_PRINT _CLL1_FPRINT(stdout) |
203 | #define _DECLARE_FPRINT void _cll1_fprint(FILE *f,char *s,...) |
204 | #define _FPRINT _DECLARE_FPRINT _CLL1_FPRINT(f) |
bce4b05d |
205 | #define _CLL1_FECHO(IOSTREAM) { va_list ap; /* <-' */ for(va_start(ap,s);s!=NIL;s=va_arg(ap,char *)) fputs(coalesce(s,NIL),IOSTREAM); va_end(ap); } |
206 | #define _ECHO void _cll1_echo(char *s,...) _CLL1_FECHO(stdout) |
207 | #define _FECHO void _cll1_fecho(FILE *f,char *s,...) _CLL1_FECHO(f) |
9ae3b221 |
208 | #define _FOR_INTS int _cll1_for_ints(int i, int n, ...) { int j=0;va_list ap; /* <-' */ va_start(ap,n); while(n!=WEIRDINT && j++<i) n=va_arg(ap,int); return n; } |
bce4b05d |
209 | #define _FOR_PTRS void *_cll1_for_ptrs(int i, void *v, ...) { int j=0;va_list ap; /* <-' */ va_start(ap,v); while(v!=(void *)NIL && j++<i) v=va_arg(ap,void *); return v; } |
bc44f2cc |
210 | #define _DECLARE_STRCOLL int _cll1_str_coll(str a, str b) |
211 | #define _STRCOLL _DECLARE_STRCOLL { if(a && b) return strcoll(a,b); else return (int)(a-b); } |
212 | #define _DECLARE_STRCMP int _cll1_str_cmp(str a, str b) |
213 | #define _STRCMP _DECLARE_STRCMP { if(a && b) return strcmp(a,b); else return (int)(a-b); } |
214 | #define _DECLARE_STRCMPI int _cll1_str_cmpi(str a, str b) |
215 | #define _STRCMPI _DECLARE_STRCMPI { if(a && b) return strcasecmp(a,b); else return (int)(a-b); } |
216 | #define _DECLARE_STRSTR str _cll1_str_str(str h, str n) |
217 | #define _STRSTR _DECLARE_STRSTR { if(h && n) return strstr(h,n); else return NULL; } |
25a3f430 |
218 | //#define _WORDINSTR char *_cll1_wordinstr(char *a,char *b,int ign) { char *IFS=a; for(;a&&*a;a++) for(;*ifs||ifs=IFS;ifs++) if(*a=b);) for return strcasestr(a,b); else return NULL; } |
4643aeaf |
219 | extern _DECLARE_PRINT; |
220 | extern _DECLARE_FPRINT; |
bc44f2cc |
221 | extern _DECLARE_STRCOLL; |
222 | extern _DECLARE_STRCMP; |
223 | extern _DECLARE_STRCMPI; |
224 | extern _DECLARE_STRSTR; |
4643aeaf |
225 | #define _EXPORT_OUTS _PRINT _FPRINT _ECHO _FECHO |
bc44f2cc |
226 | #define _EXPORT_STRS _STRCOLL _STRCMP _STRCMPI _STRSTR //_WORDINSTR |
4643aeaf |
227 | #define _EXPORT_ARGS _FOR_INTS _FOR_PTRS |
005431b8 |
228 | /* .----------------------------------------------------------------------. |
4643aeaf |
229 | / 10. C<<1 private infrastructure, updated 2008-02-03 by xCh. |
f5725936 |
230 | '----------------------------------------------------------------------- */ |
bce4b05d |
231 | #define _I (_Iatom->I) |
232 | #define _S (_Satom->S) |
4643aeaf |
233 | #define _F (_Fatom->F) |
bce4b05d |
234 | #define _CTX (_CTXatom->CTX) |
4643aeaf |
235 | #define _ARG1(ARG1,...) ARG1 |
236 | #define _ARG2(ARG1,ARG2,...) ARG2 |
237 | #define _GET_MEM(TYPE,CTX) ((TYPE)_cll1_malloc(sizeof(struct _##TYPE##_t),CTX)) |
238 | #define _ANYATOM(TYPE,NAME) def_mem(_##NAME##Atom ) { TYPE NAME; int c; list( _##NAME##Atom ); } |
bce4b05d |
239 | #define _FOR_COLUMNS(A,B,C) _NEW(I,0) for( prefix(A,B,C) ; A ; ((A!=B)&&prefix(A,B,C)||(A=NULL) ),_I++ ) |
240 | #define _FOR_VALID_COLUMNS(A,B,C) _NEW(I,0) for( prefix(A,B,C) ; A ; ((A!=B)&&prefix(A,B,C))||(A=NULL) ) if(*A&&++_I) |
4643aeaf |
241 | #define _NEW(NAME,VALUE) for(_##NAME##atom=get_mem(_##NAME##Atom),_##NAME##atom->NAME=(VALUE),_##NAME##atom->c=1,push(_##NAME##atom,_##NAME##stack);_##NAME##atom->c||(pop(_##NAME##atom,_##NAME##stack),0);_##NAME##atom->c--) |
242 | #define _ANYSTACK(NAME) _##NAME##Atom _##NAME##atom,_##NAME##stack=NULL; |
243 | #define _EXPORT_STACKS _ANYSTACK(I) _ANYSTACK(S) _ANYSTACK(F) _ANYSTACK(CTX) |
244 | _ANYATOM(int,I); |
245 | _ANYATOM(str,S); |
246 | _ANYATOM(FILE *,F); |
247 | _ANYATOM(void *,CTX); |
248 | /* .----------------------------------------------------------------------. |
249 | / 11. C<<1 memory contexts - alternative to garbage collection |
250 | '----------------------------------------------------------------------- */ |
251 | typedef _CTXAtom * context; |
252 | #define create_context(ID) create_mem(ID,context) |
005431b8 |
253 | /* .----------------------------------------------------------------------. |
4643aeaf |
254 | / 12. C<<1 CGI and FASTCGI handling, updated 2008-01-16 by xCh. |
f5725936 |
255 | '----------------------------------------------------------------------- */ |
04a13a6f |
256 | #ifdef USE_LIBRARY_FASTCGI |
257 | #define while_fastcgi_accept while(FCGI_Accept()>=0) |
7f13bdad |
258 | #define USE_MODULE_CGI |
259 | #endif |
260 | #ifdef USE_MODULE_CGI |
bce4b05d |
261 | #define cgi(MIME) { char *PTR; /* <-' */ recycle_context(_CGI); _CGIdict=NULL; for_split_str(PTR,getenv("QUERY_STRING"),'&') /* _init_cgi */ if(MIME) printf("Content-type: %s\r\n\r\n",MIME); } |
547332b7 |
262 | #define _GETCGI char *getcgi(VAR) { } |
25a3f430 |
263 | #define _CGI_GLOBALS def_context(_CGI); struct _CGIVariable { char *V; dictionary(_CGIVariable); } *_CGIvariable,*_CGIdict; |
8db501a0 |
264 | #else |
547332b7 |
265 | #define _GETCGI |
bce4b05d |
266 | #define _CGI_GLOBALS |
04a13a6f |
267 | #endif |
005431b8 |
268 | /* .----------------------------------------------------------------------. |
4643aeaf |
269 | / 13. MySQL wrappers + SQL object interface, updated 2007-12-19 by xCh. |
f5725936 |
270 | '----------------------------------------------------------------------- */ |
7f13bdad |
271 | #ifdef USE_LIBRARY_MYSQL |
25a3f430 |
272 | #define def_sql(CONN) MYSQL *CONN |
bce4b05d |
273 | #define create_sql(CONN) MYSQL *ID=mysql_init(NULL) |
274 | #define sql_connect(CONN,S,U,P,D) mysql_real_connect(CONN,S,U,P,D,0,NULL,0) |
275 | //todo sql_query |
276 | //todo sql_queryf |
277 | //todo for_sql_query_results(ROW,CONN,QUERY) |
278 | //todo for_sql_queryf_results |
279 | #define if_db_connect(U,P,D) _NEW(SQLC,mysql_init(NULL)) for(;_SQLC && mysql_real_connect(_SQLC,"localhost",U,P,D,0,NULL,0);mysql_close(SQLC),SQLC=NULL) |
280 | #define for_db_query_results(ROW,QUERY) _NEW(MYSQLR,mysql_query(_MYSQLC,QUERY)) for( (_SQLR=(_MYSQLR?0:mysql_use_result(_MYSQLC))),(ROW=(_MYSQLR?mysql_fetch_row(_MYSQLR):NULL)) ; ROW ; ROW=mysql_fetch_row(_MYSQLR) ) |
281 | //todo db_query |
282 | //todo for_db_results |
283 | //todo for_db_queryf_results(ROW,...) |
284 | //todo db_queryf |
285 | #define _MYSQLC (_MYSQLCatom->MYSQLC) |
286 | #define _MYSQLR (_MYSQLRatom->MYSQLR) |
287 | #define _SQL_GLOBALS _ANYSTACK(MYSQL,MYSQLC) _ANYSTACK(MYSQL_RES,MYSQLR) |
7f13bdad |
288 | #else |
bce4b05d |
289 | #define _SQL_GLOBALS |
7f13bdad |
290 | #endif |
bce4b05d |
291 | //#ifdef USE_INTERFACE_SQL |
292 | //todo #define _SQL_OBJECT mem Sql { interface(SqlInterface); char *hostname; char *username; char *password; char *database; } |
25a3f430 |
293 | //todo #define _SQL_CONSTRUCT def_construct(Sql) { self->hostname="localhost"; } |
bce4b05d |
294 | //#endif |
4643aeaf |
295 | |
04a13a6f |
296 | /* ------------------------------------------------------------------- */ |
7f13bdad |
297 | /* old - from version 0.6 */ |
f5725936 |
298 | /* =================================================================== */ |
7f13bdad |
299 | /* |
300 | obsolete |
30628092 |
301 | #define strlwr(A) {char *_S=A; while(_&&*_S){*_S=tolower(*_S);_S++;}} |
302 | #define strupr(A) {char *_S=A; while(_&&*_S){*_S=toupper(*_S);_S++;}} |
303 | #define string(S,L) (S=(char *)malloc(L),*S=0) |
304 | #define duplicate(A,B) if(A) { string(B,strlen(A)+1); strcpy(B,A); } |
305 | #define concatenate(A,B,C) if (A && B) { string(C,strlen(A)+strlen(B)+1); strcpy(C,A); strcat(C,B); } |
30628092 |
306 | #define gotoalpha(CHAR) if(CHAR)while(*CHAR && !isalpha(*CHAR))CHAR++ |
307 | #define goto_alpha(CHAR) if(CHAR)while(*CHAR && !isalpha(*CHAR) && *CHAR!='_')CHAR++ |
308 | #define gotoalnum(CHAR) if(CHAR)while(*CHAR && !isalnum(*CHAR))CHAR++ |
309 | #define goto_alnum(CHAR) if(CHAR)while(*CHAR && !isalnum(*CHAR) && *CHAR!='_')CHAR++ |
310 | #define skipalpha(CHAR) if(CHAR)while(*CHAR && isalpha(*CHAR))CHAR++ |
311 | #define skip_alpha(CHAR) if(CHAR)while(*CHAR && (isalpha(*CHAR) || *CHAR=='_'))CHAR++ |
312 | #define skipalnum(CHAR) if(CHAR)while(*CHAR && isalnum(*CHAR))CHAR++ |
313 | #define skip_alnum(CHAR) if(CHAR)while(*CHAR && (isalnum(*CHAR) || *CHAR=='_'))CHAR++ |
314 | #define skipspaces(CHAR) if(CHAR)while(*CHAR==' ')CHAR++ |
315 | #define cutspaces(CHAR) if(CHAR){int _L=strlen(CHAR); while(--_L>0 && CHAR[_L]==' ')CHAR[_L]=0;} |
316 | #define gotochr(CHAR,C) if(CHAR)while(*CHAR && *CHAR!=C)CHAR++ |
30628092 |
317 | #define strswitch(CHAR) {char *_K=CHAR; FILE *_F=NULL; {{ |
318 | #define stroption(STR) if(eq(STR,_K)) |
319 | #define match(KEY,VAL) {char *_K=KEY, *_V=VAL; FILE *_F=NULL; {{ |
320 | #define assign(STR,SETVAR) stroption(STR) SETVAR=_V |
7f13bdad |
321 | */ |
30628092 |
322 | /* I/O iterations, updated 2004-04-19 by xCh. */ |
323 | |
bce4b05d |
324 | //#define fparse(S,L,F) for(fgets(S,L,F);*S && !feof(F);fgets(S,L,F)) |
325 | //#define input(S,L) fparse(S,L,stdin) |
326 | //#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);} |
327 | //#define parses(S,F) {FILE *_F=fopen(F,"r"); if(_F) { while(!feof(_F)) { fstring(S,_F); |
f5725936 |
328 | //obsolete |
329 | //#define parse(F) {char *_; FILE *_F=fopen(F,"r"); if(_F) { while(!feof(_F)) { fstring(_,_F); |
330 | //#define fail }} else {{ |
331 | //#define done }} if(_F)fclose(_F);} |
005431b8 |
332 | //to do #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);} |
333 | //to do #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);} |
334 | //to do #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);} |
30628092 |
335 | |
30628092 |
336 | /* I/O sequences, updated 2003-05-29 by xCh. */ |
337 | |
338 | #define nullreopen(F) F=freopen("/dev/null","r",F) |
339 | #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]);} |
340 | #define shell(CMD) stdinredir(system(CMD)) |
341 | #define paste(STR) stdinredir(fputs(STR,stdout)) |
342 | |
30628092 |
343 | /* Useful structures, updated 2003-05-29 by xCh. */ |
344 | |
f5725936 |
345 | //#define date(S) { time_t _T; _T=time(NULL); duplicate(ctime(&_T),S); } |
4643aeaf |
346 | |
097bac9a |
347 | /* some general ideas, unfinished */ |
f5725936 |
348 | //bind_exception, unbind_exception |
7f13bdad |
349 | //#define _CLL1_THROW(ERR) |
f5725936 |
350 | //#define _CLL1_TRY _NEW_E for(bind_exception(0,NULL);;) if () {.... if(_Eatom->SYS) { if(_Eatom->RET) return; else break;} else { } |
420bb771 |
351 | //#define _ASSERT(EXPR) assert(EXPR) |
420bb771 |
352 | |
005431b8 |
353 | /* .----------------------------------------------------------------------. |
ce26b4aa |
354 | / 98. We have have done our homework - now let's have some fun: |
f5725936 |
355 | '----------------------------------------------------------------------- */ |
4643aeaf |
356 | #define _INIT_VARIABLES _EXPORT_GLOBALS _EXPORT_YES _EXPORT_STACKS _SQL_GLOBALS _CGI_GLOBALS |
357 | #define _IMPL_FUNCTIONS _MALLOC _EXPORT_OUTS _EXPORT_ARGS _EXPORT_STRS |
358 | #define _IMPL_INTERFACE //_SQL_IFC _CAKE_IFC _STD_IFC _X_IFC |
359 | #define _IMPLEMENT_CLL1_LIBRARY _INIT_VARIABLES _IMPL_FUNCTIONS _IMPL_INTERFACE |
ce26b4aa |
360 | #define _PROGRAM _cll1_program |
361 | #define _DECLARE_PROGRAM void _PROGRAM(int argc, char **argv) |
362 | #define program _IMPLEMENT_CLL1_LIBRARY _DECLARE_PROGRAM; int main(int argc, char **argv) { setlocale(LC_ALL, ""); _PROGRAM(argc,argv); return 0; } _DECLARE_PROGRAM |
f5725936 |
363 | #ifdef USE_PREPROCESSOR |
364 | #include "_include.c" |
365 | #endif |
ce26b4aa |
366 | /* .----------------------------------------------------------------------. |
367 | / 99. C<<1 is up and running. It's your turn now to show something new. |
368 | '----------------------------------------------------------------------- */ |
420bb771 |
369 | #endif |