diff --git a/tecs/coc.c b/tecs/coc.c index d804ade8..c0c3d5a4 100644 --- a/tecs/coc.c +++ b/tecs/coc.c @@ -6,7 +6,7 @@ #include #include #include "errhdl.h" -#include "util.h" +#include "str_util.h" #include "coc.h" /*-------------------------------------------------------------------------*/ /* CreateSocketAddress stolen from Tcl. Thanks to John Ousterhout */ diff --git a/tecs/dlog.c b/tecs/dlog.c index ae8b67e6..52bcfe11 100644 --- a/tecs/dlog.c +++ b/tecs/dlog.c @@ -14,7 +14,7 @@ #include #include #include "errhdl.h" -#include "util.h" +#include "str_util.h" #include "dlog.h" #define VERSION 1.1 diff --git a/tecs/logfile.c b/tecs/logfile.c index 2ac4521b..4f77ea84 100644 --- a/tecs/logfile.c +++ b/tecs/logfile.c @@ -12,7 +12,7 @@ #include #include "logfile.h" #include "errhdl.h" -#include "util.h" +#include "str_util.h" static FILE *fil=NULL; static char lnam[256]="", filnam[256]=""; diff --git a/tecs/lsc.c b/tecs/lsc.c index aa3d959f..9b32ec6a 100644 --- a/tecs/lsc.c +++ b/tecs/lsc.c @@ -6,7 +6,7 @@ #include "logfile.h" #include "coc.h" #include "lsc.h" -#include "util.h" +#include "str_util.h" #define MC LSC_MAX_CMDS #define MAX_PAR 16 diff --git a/tecs/serutil.c b/tecs/serutil.c index 195a7068..cab7b5f5 100644 --- a/tecs/serutil.c +++ b/tecs/serutil.c @@ -7,7 +7,7 @@ #include "errhdl.h" #include "serutil.h" #include "logfile.h" -#include "util.h" +#include "str_util.h" #define A_CHK(R) if (1!=(R)) { SerA_error(); ErrTxt(#R,0); goto OnError; } diff --git a/tecs/str_util.c b/tecs/str_util.c new file mode 100644 index 00000000..ede8724c --- /dev/null +++ b/tecs/str_util.c @@ -0,0 +1,223 @@ +#include +#include +#include +#include +#include +#include +#include +#include "errhdl.h" +#include "str_util.h" + +char *str_splitx(char *str, char sep, char *list[], int *n) { + int i; + char *s, *e; + + s=str; + for (i=0; i<*n; i++) { + list[i]=s; + e=strchr(s, sep); + if (e==NULL) { *n=i+1; return(NULL); } + s=e+1; + e--; + while (e>str && *e==' ') e--; /* trim sequence */ + e[1]='\0'; + } + return(s); +} + +char *str_split1(char *str, char sep) { + char *s, *e; + + e=strchr(str, sep); + if (e==NULL) { + s=NULL; + e=str+strlen(str); + } else { + s=e+1; + } + e--; + while (e>str && *e==' ') e--; /* trim sequence */ + e[1]='\0'; + return(s); +} + +int str_ntrim(char *dest, const char *src, int ldest, int lsrc) { + int i; + + if (lsrc>=ldest) lsrc=ldest-1; + if (dest!=src) strncpy(dest, src, lsrc); + dest[lsrc]='\0'; + i=strlen(dest)-1; + while (i>0 && dest[i]==' ') i--; /* trim sequence */ + i++; + dest[i]='\0'; + return(i); +} + +char *str_nsplit(char *dst, const char *src, char sep, int dstlen) { + char *s; + int i; + + s=strchr(src, sep); + if (s==NULL) { + s=NULL; + i=strlen(src); + } else { + i=s-src; + s++; /* skip separator */ + } + if (i>=dstlen) { + str_copy(dst, src); + } else { + strncpy(dst, src, i); + dst[i]='\0'; + } + return(s); +} + +char *str_read_until(FILE *fil, char *term, char *buf, char *end) { + char *s; + char fmt[24]; + int i, l, siz; + char ch; + + siz=end-buf-1; + if (siz<1) return(NULL); + sprintf(fmt, "%s%d[^%s%s", "%", siz, term, "]%n%c"); + i=fscanf(fil, fmt, buf, &l, &ch); + if (i<0) { /* eof */ + buf[0]='\0'; + return(&buf[0]); + } else if (i==0) { /* fscanf returns 0 if first char is terminator */ + buf[0]=fgetc(fil); + return(&buf[0]); + } else if (i==1) { /* terminator not found -> read until eof */ + buf[l]='\0'; + return(&buf[l]); + } else { + buf[l]=ch; + if (l==siz && NULL==strchr(term, ch)) return(NULL); + return(&buf[l]); + } +} + +char *str_read_file(char *file) { + FILE *fil; + char *str, *s, *e, *p, *q; + char ch; + int i, l, size; + struct stat statbuf; + + i=stat(file, &statbuf); + if (i<0) ERR_MSG("file not found"); + size=statbuf.st_size+4; + ERR_SP(str=malloc(size)); + e=&str[size-1]; + ERR_SP(fil=fopen(file, "r")); + s=str; + while (1) { + p=str_read_until(fil, "!", s, e); + if (p==NULL) break; + if (*p=='!') { + q=str_read_until(fil, "\n", p, e); + if (q==NULL) { p=NULL; break; } + s=p; *s='\n'; s++; + } else { + assert(*p=='\0'); + break; + } + } + ERR_SI(fclose(fil)); + assert(strlen(str)reslen) ERR_MSG("result buffer too short"); + strncpy(r, p, l); + r+=l; reslen-=l; + if (ln>reslen) ERR_MSG("result buffer too short"); + strncpy(r, new, reslen); + r+=ln; reslen-=ln; + p=s+lo; + s=strstr(p, old); + } + l=strlen(p); + if (l>reslen) ERR_MSG("result buffer too short"); + strncpy(r, p, l); + r+=l; + *r='\0'; + return(r-result); + OnError: + result[0]='\0'; + return(-1); +} + +void str_nupcase(char *dst, const char *src, int dstlen) { + dstlen--; /* space for trailing nul */ + while (*src!='\0' && dstlen>0) { + *dst=toupper(*src); + dst++; src++; + } + *dst='\0'; +} + +int strcasecmp(const char *str1, const char *str2) { + int i; + char ch1, ch2; + ch1=tolower(*(str1++)); ch2=tolower(*(str2++)); + i=1; + while (ch1!='\0' && ch2!='\0' && ch1==ch2) { + ch1=tolower(*(str1++)); ch2=tolower(*(str2++)); i++; + } + if (ch1ch2) { + return(i); + } + return(0); +} + +int str_ncpy(char *dst, const char *src, int maxdest) { + strncpy(dst, src, maxdest); + if (dst[maxdest-1]!='\0') { + dst[maxdest-1]='\0'; + ERR_MSG("destination string too short"); + } + return(0); + OnError: return(-1); +} + +int str_ncat(char *dst, const char *src, int maxdest) { + strncat(dst, src, maxdest-strlen(dst)-1); + if (dst[maxdest-1]!='\0') { + dst[maxdest-1]='\0'; + ERR_MSG("destination string too short"); + } + return(0); + OnError: return(-1); +} diff --git a/tecs/tecc.c b/tecs/tecc.c index 47690ddc..940cf0be 100644 --- a/tecs/tecc.c +++ b/tecs/tecc.c @@ -1,6 +1,6 @@ #include #include "errhdl.h" -#include "util.h" +#include "str_util.h" #include "tecc.h" static char device[80], command[80]; diff --git a/tecs/tecs.c b/tecs/tecs.c index ce899733..c2137f91 100644 --- a/tecs/tecs.c +++ b/tecs/tecs.c @@ -6,7 +6,7 @@ #include "errhdl.h" #include "server.h" #include "logfile.h" -#include "util.h" +#include "str_util.h" #include "lsc.h" #include "dlog.h"