
/* C<<1 header file v0.3 - style sheet for ANSI C  */
/* Please pronounce as "cee-shift-left-by-one" :)  */
/* GNUpyright(G)2003 Michael xChaos Polak, x@n.cz  */

#ifndef __CLL1H__
#define __CLL1H__

#include <stdio.h>
#include <string.h>

/* Section For Dummies, updated 2003-05-29 by xCh. */

#define not !
#define TRUE 1
#define FALSE 0
#define loop while(1)
#define iterate(VAR,FROM,TO) for(VAR=FROM; VAR <= TO; VAR++)

/* Dynamic list macros & sequences, updated 2003-05-29 by xCh. */

#define list(T) struct T *_next 
#define create(A,T) (A=(struct T *)malloc(sizeof(struct T)),A->_next=NULL)
#define push(A,B) { if(A && A!=B) A->_next=B; B=A; }
#define append(A,B) { if(B) { void *N=A; A->_next=NULL; search(A,B,!A->_next) {A->_next=N; break;}} else push(A,B); }
#define remove(A,B,C) { void **_D=NULL; search(A,B,C) { if(_D)*_D=A->_next; else B=A->_next; free(A); } else _D=(void *)&(A->_next); }
#define drop(A,B) { for( A=B; A ; B=A, A=A->_next, free(B)); B=NULL; }

/* Dynamic list iterations and sequences, updated 2003-05-29 by xCh. */

#define every(A,B) for( A=B; A; A=A->_next)
#define search(A,B,C) every(A,B) if(C)
#define find(A,B,C) search(A,B,C) break; if(A)

/* EXP macros for Dummysort sequences, updated 2003-05-29 by xCh. */

#define order_by(K1,K2) (K1>K2)
#define desc_order_by(K1,K2) (K1<K2)
#define sort_by(K1,K2) (strcasecmp(K1, K2)>0)
#define desc_sort_by(K1,K2) (strcasecmp(K1, K2)<0)
#define ascii_by(K1,K2) (strcmp(K1, K2)>0)
#define desc_ascii_by(K1,K2) (strcmp(K1, K2)<0)

/* Dummysort sequences, updated 2003-05-29 by xCh. */

#define insert(A,B,EXP,K) { if(B) { void **_L=NULL, *H=B; 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); }

/* I/O iterations, updated 2003-05-30 by xCh. */

#define fparse(S,L,F) for(fgets(S,L,F);*S && !feof(F);fgets(S,L,F))
#define input(S,L) fparse(S,L,stdin)
#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);}
#define parse(S,F) {FILE *_F=fopen(F,"r"); if(_F) {while(!feof(_F)) { fstring(S,_F);  
#define fail }} else {{
#define done }} if(_F)fclose(_F);}

/* Dynamic list advanced I/O, updated 2003-05-30 by xCh. */

#define load(A,B,F,T,K) {char *_S; parse(_S,F) { create(A,T); A->K=_S; append(A,B);} done; }
#define save(A,B,F,K) {FILE *_F=fopen(F,"w"); if(_F) { every(A,B) fputs(A->K,_F); fclose(_F);}}
     
/* I/O sequences, updated 2003-05-29 by xCh. */

#define nullreopen(F) F=freopen("/dev/null","r",F)
#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]);}
#define shell(CMD) stdinredir(system(CMD))
#define paste(STR) stdinredir(fputs(STR,stdout))

/* String macros & sequences, updated 2003-05-29 by xCh. */

#define eq(A,B) !strcmp(A,B)
#define strcmpi(A,B) strcasecmp(A,B)
#define string(S,L) (S=(char *)malloc(L),*S=0)
#define duplicate(A,B) if(A) { B=string(strlen(A)); strcpy(B,A); }
#define suffix(A,B,C) (((A=strrchr(B,C))&&!(*(A++)=0))||(A=B))
#define prefix(A,B,C) ((A=B)&&((B=strchr(B,C))&&!(*(B++)=0)||(B=A)))
#define gotoalpha(A) if(A)while(*A && !isalpha(*A))A++
#define goto_alpha(A) if(A)while(*A && !isalpha(*A) && *A!='_')A++
#define gotoalnum(A) if(A)while(*A && !isalnum(*A))A++
#define goto_alnum(A) if(A)while(*A && !isalnum(*A) && *A!='_')A++
#define skipalpha(A) if(A)while(*A && isalpha(*A))A++
#define skip_alpha(A) if(A)while(*A && (isalpha(*A) || *A=='_'))A++
#define skipalnum(A) if(A)while(*A && isalnum(*A))A++
#define skip_alnum(A) if(A)while(*A && (isalnum(*A) || *A=='_'))A++
#define gotochr(A,C) if(A)while(*A && *A!=C)A++

/* String iterations, updated 2003-05-29 by xCh. */

#define split(A,B,C) for(prefix(A,B,C);A;(A!=B)&&prefix(A,B,C)||(A=NULL))

/* Useful structures, updated 2003-05-29 by xCh. */

#define hashtable(TYPE,NAME,VALUE) struct TYPE { char *NAME; char *VALUE; list(TYPE); }
#define textfile(TYPE,LINE) struct TYPE { char *LINE; list(TYPE); }

#endif

