#ifndef __STRUTIL_C #define __STRUTIL_C #include "elastro.h" /* Entfernt alle bytes ch aus string str bis zur laenge len; len sollte groesser, oder gleich als der tatsächliche stringinhalt sein. Ist strflag auf 1 gesetzt werden keine zeichen ch innerhalb eines durch die zeichen " " eingeschlossenen stringbereiches entfernt. Werden zeichen entfernt wird der string entsprechend verkürzst, weil alle nachfolgenden zeichen an die stelle zurückversetzt werden wo das, oder die zeichen entfernt wurden. ein wert < 0x20 bedeutet stringende, und wird auf jeden Fall auf 0 gesetzt. Bei erfolg zeigt der rueckgabewert auf genau diese stelle. Wird in der länge len kein endezeichen gefunden wird die funktion abgebrochen, und 0 wird zurückgegeben. */ char * strEatBytes(unsigned char ch,char *str,unsigned int len,int strflag) { unsigned int ct; int flag; char *a,*b,*e; if(str == NULL)return(NULL); a = str; ct = 0; do{ if(a[ct] < 0x20){ break; } ct++; }while(ct <= len); if(ct >= len)return(NULL); e = &a[ct]; e[0] = 0; ct = 0; flag = 0; do{ if(a[ct] < 0x20)break; if(strflag == 1){ if(a[ct] == 0x22){ if(flag == 0) { flag = 1; } else { flag = 0; } } } if(flag == 0){ if(a[ct] == ch){ b = &a[ct]; do{ if(b[0] < 0x20){ e = &b[0]; e[0] = 0; break; } b[0] = b[1]; b++; }while(b <= e); } } if(a[ct] < 0x20)break; if(flag == 0){ if(a[ct] != ch)ct++; } else { ct++; } }while(ct <= len); if(a[ct] < 0x20)a[ct] = 0; return(e); } /* Scans string s for 0x22. toggles every found 0x22 starting with state flag. returns state when string ends. */ int strScanStrActive(char *s,int flag) { char *p; int fl; if(s == NULL)return(0); p = s; fl = flag; while(p[0] > 0x1f) { if(p[0] == 0x22) { if(fl == 0) { fl = 1; } else { fl = 0; } } p++; } return(fl); } /* Sets all chars which are not 0,0x0d,0x0a to 0x20 but < 0x20 to 0x20. Ends when 0x0d,0x0a, or 0 reached.(Sets first reached char of it to 0 */ void strCleanStr(char *s,int max) { int ct; if(s == NULL)return; ct = 0; while(ct < max) { if(s[ct] == 0x0d)break; if(s[ct] == 0x0a)break; if(s[ct] == 0)break; if(s[ct] < 0x20)s[ct] = 0x20; ct++; } s[ct] = 0; } /* Returns pointer to last char of String which is not 0x20, or 0 */ char * strGetStrLastChar(char *s) { char *p; if(s == NULL)return(NULL); p = s; while(p[0] > 0x1f) { if(p[0] < 0x20)break; p++; } p[0] = 0; while(p > s) { if(p[-1] == 0x20){ p--; p[0] = 0; } else { p--; break; } } if(p < s)return(NULL); if(p == s){ if(p[0] < 0x20)return(NULL); } return(p); } /*Sets all chars in String to 0 till max*/ void strCleanStr0(char *s,int max) { int ct; if(s == 0)return; ct = 0; while(ct < max) { s[ct] = 0; ct++; } } /*moves string src r positions left, returns ptr of new start of src*/ char * strRollStrLeft(char *src,int r) { if(r == 0)return(src); if(src == NULL)return(NULL); strcpy(&src[-r],src); return(&src[-r]); } /*moves string src r positions right, returns ptr of new start of src*/ char * strRollStrRight(char *src,int r) { int len; if(r == 0)return(src); if(src == NULL)return(NULL); len = strlen(src); memmove(&src[r],src,len); return(&src[r]); } /* sets next char in s which is less 0x20 to 0, and decrements end of string to next char which is not 0x20. pos of this point is set to 0 and a ptr is returned, or 0 is nothing found which is not 0x20 */ char * strPrepStrEnd(char *s) { char *p; if(s == NULL)return(NULL); p = s; while(p[0] > 0x1f) { if(p[0] < 0x20)break; p++; } p[0] = 0; while(p > s) { if(p[-1] == 0x20){ p--; } else { break; } } if(p < s)return(NULL); p[0] = 0; if(p == s){ if(p[0] < 0x20)return(NULL); } return(p); } /* eats stringstarts spaces till char unequal spaces. then eats spaces where more than one. returns end of string (nullchar), or 0, if only spaces, or NULL as error. spaces in a string (like " ") are not eaten. */ char * strPrepStrSpace(char *s) { char *p; int flag; if(s == NULL)return(NULL); p = s; if( strPrepStrEnd(p) == NULL)return(NULL); while(p[0] == 0x20) { if(p[0] < 0x1f)return(NULL); strRollStrLeft(&p[1],1); } if(p[0] < 0x20)return(NULL); flag = 0; while(p[0] > 0x1f) { if(p[0] == 0x22) { if(flag == 0) { flag = 1; } else { flag = 0; } } if(flag == 1) { p++; continue; } if(p[0] == 0x20) { p++; while(p[0] == 0x20) { if(p[0] < 0x1f)break; strRollStrLeft(&p[1],1); } continue; } p++; } if(flag == 1)return(NULL); p[0] = 0; return(p); } /* If a string like "text" the second found " will be set to 0, and a pointer will return after the first ". the string must start with ". */ char * strGetPrepString(char *s) { char *e; if(s == NULL)return(NULL); if(s[0] == 0x22){ e = strchr(&s[1],0x22); if(e == 0) { return(NULL); } else { e[0] = 0; } return(&s[1]); } return(s); } /*tests wether char s is >= 0 and <= 9*/ int strTestNumeric(char s) { if(s >= '0') { if(s <= '9')return(1); } return(0); } /*tests wether char at ptr s is >= 0 and <= 9*/ int strTestPtrNumeric(char * s) { if(s == NULL)return(0); if(s[0] >= '0') { if(s[0] <= '9')return(1); } return(0); } /*tests wether s is a float string*/ int strTestPtrFloat(char * s) { if(s == NULL)return(0); if(s[0] >= '0') { if(s[0] <= '9')return(1); } if(s[0] == '+')return(1); if(s[0] == '-')return(1); return(0); } /*tests wether next chars at s is a hexvalue start with 0x,or 0X*/ int strTestPtrHex(char * s) { if(s == NULL)return(0); if(strncmp(s,"0x",2)==0)return(1); if(strncmp(s,"0X",2)==0)return(1); return(0); } #endif