/* specific stringoperators B. Dierickx 27sep2002 */ #ifndef STRINGINCLUDED #include #include #define StringSize 512 char DUMMY[StringSize]; char U(char a) /*lower case char*/ { if(a>='a' && a<='z')return(a+'A'-'a'); else return (a); } char L(char a) { if(a>='A' && a<='Z')return(a+'a'-'A'); else return (a); } char *Upr(char *a) /*upper case string*/ { int i; i=0; while(a[i]) { a[i]=U(a[i]); i++; } return(a); } char *Lower(char *a) { int i; i=0; while(a[i]) { a[i]=L(a[i]); i++; } return(a); } int Len(char *a) /*length of string a*/ { int i=0; if(!a)return(0); while(a[i])i++; return(i); } #define S_len(a) Len(a) char *Knabbel(char *a)/*points beyond beginning controlcharacters of a*/ { while(a && *a && *a<' ')a++; return(a); } char *S_stripcontrol(char *a)/*strips controlcharacters from string*/ { char *h; char *b; h=a; b=a; while(*a) { if(*a>=' '||*a<0){*b=*a;b++;} a++; } *b='\0'; return(h); } int S_cins(char c, char *s) {//return position+1 of char c in s. returns 0 if not in string //char '\0' is always in string (at last position) int i=0; if(!c)return(S_len(s)+1); for(i=0;i=m)break; } b[i]='\0'; return(b); } char *Woordn(char *a,int n, char *b) /*takes n-th word from a, puts it in b and returns b. b should be large enough to accomodate any word. words count from 0, words separated by [multiple]-white-spaces, tabs or control chars If b==NULL, a temporary DUMMY string is used and returned. */ { int i; char *h; for(i=0;i<=n;i++) { h=DUMMY; while(*a<=' ' && *a>0)a++; while(*a>' '){*h=*a;h++;a++;}; *h='\0'; } if(b!=NULL)return(Copy(DUMMY,b));else return(DUMMY); } double Getaln(char *a,int n) /*returns double value of nth word of string a*/ { double atof(); return(atof(Woordn(a,n,NULL))); } char *Voorbij(char *a, char c) //geeft pointer naar eerste karakter voorbij karakter c, indien bestaat, //zoniet NULL { while(1) { if(*a==0)return(NULL); if(*a==c){a++;return(a);} a++; } } char *S_charpos(char *a,char c) {//pointer to first occurrence of char c in string a while(1) { if(*a==0)return(NULL); if(*a==c){return(a);} a++; } } int S_ne(char *a,char *b) //stringcomparison: returns 1 if a>b, -1 if b>a, 0 is a==b { while(*a && *a == *b){a++;b++;} if(*a>*b)return(1); if(*a<*b)return(-1); return(0); } #define Neq(a,b) (S_ne(a,b)) #define Groter(a,b) (Neq(a,b)==1) #define Kleiner(a,b) (Neq(a,b)== -1) #define Gelijk(a,b) (!Neq(a,b)) #define S_gt(a,b) (S_ne(a,b)==1) #define S_lt(a,b) (S_ne(a,b)== -1) #define S_eq(a,b) (!S_ne(a,b)) #define S_le(a,b) (S_ne(a,b)!=1) #define S_ge(a,b) (S_ne(a,b)!= -1) int S_sins(char *a, int from, char *b) //returns position of first occurrence + 1 of string a in string b, starting from position from in b { int i,la, lb; char *aa, *bb; la=S_len(a); lb=S_len(b); for (i=from;i=len-2)return; a[i]=c;a[i+1]='\0'; } void S_sappend(char *a, char *c, int len) //adds string c after string a { int alen,clen; alen=S_len(a); clen=S_len(c); if(alen+clen>=len){clen=len-alen-1;} S_copy(c,clen,a+alen); } char *S_dup(char *a) //creates duplicate of a string, returns pointer to allocated space { int l,i; char *h; l=S_len(a)+1; h=(char *)malloc(l); for(i=0;i=*elements) { if(*elements>0) {*pointer=(void *)realloc((void *)*pointer, ssize*((*elements)*=2));} else {*pointer=(void *)malloc(ssize*(*elements=1));} } return(*pointer+((*used)++)*ssize); } //////tokenbuffer: infinitely expandable array of tokens (strings) struct S_tokenbufferstruct { char **token; //array of strings int number; int arraysize; }; void S_tokenbuffer_init(struct S_tokenbufferstruct *t) { // t=(struct S_tokenbufferstruct *)malloc(sizeof(struct S_tokenbufferstruct)); t->number=0; t->arraysize=0; } int S_addtoken(char *token,struct S_tokenbufferstruct *t) { //add string (token) to buffer t //returns position number in tokenbuffer char **actualstring; actualstring=(char **)arrayalloc(sizeof(char *), &(t->number), &(t->arraysize), (void **)( &(t->token))); *actualstring=S_dup(token); return(t->number); } void S_tokenbuffer_print(struct S_tokenbufferstruct *t) { int i; for(i=0;inumber;i++) { printf("%s ",t->token[i]); } } /////////////////fifo & stack of strings S_fifo/////////////////// struct S_fifo_rootstruct { int number; struct S_fifo_struct *first; struct S_fifo_struct *last; }; struct S_fifo_struct { char *value; struct S_fifo_struct *next; }; typedef struct S_fifo_rootstruct S_fifo; void S_fifo_init(S_fifo *f) {//initializes S_fifo - do before first use //must have been declared with space allocated as: S_fifo g; f->number=0; f->first=NULL; f->last=NULL; } void S_fifo_write(S_fifo *f, char *value, int last) { //pushes string to begin or end (if last) of fifo (last==1 is fifo, last==0 is stack struct S_fifo_struct *h; f->number++; h=(struct S_fifo_struct *)malloc(sizeof(struct S_fifo_struct)); h->value=S_dup(value);h->next=NULL; if(f->number==1) //add first entry in fifo or stack { f->first=f->last=h; return; } if(last) //real fifo: add at end { f->last->next=h; f->last=h; } else //lifo == stack { h->next=f->first; f->first=h; } } int S_fifo_read(S_fifo *f, char *name) { //takes (and frees) first item from fifo //returns number of entries in fifo including the one just returned struct S_fifo_struct *h; if(f->number<=0) { if(name)S_clear(name); return(0); } if(name)Copy(f->first->value,name); h=f->first; S_free(f->first->value); f->first=f->first->next; free(h); f->number--; return(f->number+1); } int S_fifo_cleanup(struct S_fifo_rootstruct *f) {//frees all allocated space for this fifo int i=0; while(S_fifo_read(f,NULL))i++; return(i); } #define STRINGINCLUDED #endif