cleaned up myc library (M.Z.)
This commit is contained in:
@ -8,7 +8,6 @@
|
||||
#include "myc_err.h"
|
||||
#include "coc_util.h"
|
||||
#include "coc_client.h"
|
||||
#include "sys_util.h"
|
||||
#include "myc_str.h"
|
||||
|
||||
/* --- non ANSI signal --- */
|
||||
@ -390,8 +389,14 @@ int CocWatchLog(CocConn *conn, char *loglist) {
|
||||
StrBuf *buf;
|
||||
int fd, iret;
|
||||
int i, siz, n;
|
||||
static int init=1;
|
||||
|
||||
sys_ctrl_init();
|
||||
#if __VMS
|
||||
if (init) {
|
||||
init=0;
|
||||
DECC$CRTL_INIT();
|
||||
}
|
||||
#endif
|
||||
fd=conn->fd;
|
||||
CocReset(conn);
|
||||
ERR_I(CocPutStr(conn, "loglist", loglist));
|
||||
|
@ -1,3 +1,4 @@
|
||||
#include <sys/types.h>
|
||||
#include <sys/socket.h>
|
||||
#include <netdb.h>
|
||||
#include <stdio.h>
|
||||
@ -9,7 +10,6 @@
|
||||
#include <string.h>
|
||||
#include <assert.h>
|
||||
#include "myc_mem.h"
|
||||
#include "sys_util.h"
|
||||
#include "myc_err.h"
|
||||
#include "coc_logfile.h"
|
||||
#include "coc_util.h"
|
||||
@ -477,6 +477,12 @@ void CocShowHandlers(char *buf, int buf_len) {
|
||||
return;
|
||||
}
|
||||
|
||||
#ifdef __VMS
|
||||
typedef size_t socklen_type;
|
||||
#else
|
||||
typedef int socklen_type;
|
||||
#endif
|
||||
|
||||
int CocHandle1Request(int tmo_msec, int fd) {
|
||||
struct sockaddr_in cadr;
|
||||
struct hostent *h;
|
||||
@ -484,7 +490,7 @@ int CocHandle1Request(int tmo_msec, int fd) {
|
||||
CocClient *cl, *cl0;
|
||||
CocVar *var;
|
||||
int i, lmask, newfd, n, iret;
|
||||
sys_adr_len cadrlen; /* see sys_util.h */
|
||||
socklen_type cadrlen;
|
||||
char *err, *cmd, *arg, *varname;
|
||||
void *base;
|
||||
|
||||
|
@ -1,4 +1,5 @@
|
||||
#include <stdlib.h>
|
||||
#include <sys/types.h>
|
||||
#include <sys/socket.h>
|
||||
#include <sys/time.h>
|
||||
#include <arpa/inet.h>
|
||||
|
@ -3,7 +3,7 @@
|
||||
#include <unistd.h>
|
||||
#include <stdio.h>
|
||||
#include "myc_str.h"
|
||||
#include "sys_util.h"
|
||||
#include "myc_fortran.h"
|
||||
|
||||
typedef struct { char *instr; char *host; int port; char *user; int cod; } Instrument;
|
||||
static Instrument list[]={
|
||||
|
@ -10,7 +10,7 @@ SERV_OBJ =tecs.o coc_server.o tecs_lsc.o tecs_serial.o coc_logfile.o \
|
||||
tecs_data.o $(LIBR_OBJ)
|
||||
CLI_OBJ =tecs_cli.o coc_client.o $(LIBR_OBJ)
|
||||
TCLI_OBJ =$(SYS_FILE).o $(SYS_FILE)_c.o $(CLI_OBJ)
|
||||
TECLI_OBJ =tecs_client.o tecs_plot.o sys_util.o str.o instr_hosts.o \
|
||||
TECLI_OBJ =tecs_client.o tecs_plot.o str.o instr_hosts.o \
|
||||
$(TCLI_OBJ)
|
||||
|
||||
#.f.o:
|
||||
|
@ -4,7 +4,6 @@
|
||||
#include <string.h>
|
||||
#include <strings.h>
|
||||
#include <stdlib.h>
|
||||
#include "sys_util.h"
|
||||
#include "myc_err.h"
|
||||
#include "myc_str.h"
|
||||
#include "myc_buf.h"
|
||||
|
@ -2,7 +2,7 @@
|
||||
#include <string.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
#include "sys_util.h"
|
||||
#include "myc_fortran.h"
|
||||
#include "myc_str.h"
|
||||
#include "myc_err.h"
|
||||
|
||||
@ -15,7 +15,7 @@ static int stack_empty=1;
|
||||
|
||||
int ErrCode;
|
||||
char *ErrMessage=NULL;
|
||||
void (*outrtn)()=NULL;
|
||||
void (*outrtn)(void *, char *)=NULL;
|
||||
void *outarg;
|
||||
|
||||
void ErrTxt(char *text, int systemError)
|
||||
@ -85,7 +85,7 @@ void ErrShort(char *msg) {
|
||||
outrtn(outarg, msg);
|
||||
}
|
||||
|
||||
void ErrSetOutRtn(void (*rtn)(), void *arg) {
|
||||
void ErrSetOutRtn(void (*rtn)(void *, char *), void *arg) {
|
||||
outrtn=rtn;
|
||||
outarg=arg;
|
||||
}
|
||||
@ -125,7 +125,7 @@ void F_FUN(err_msg)(F_CHAR(text), int text_len) {
|
||||
ErrMsg(buf);
|
||||
}
|
||||
|
||||
void F_FUN(err_set_outrtn)(void (*rtn)(), void *arg) {
|
||||
void F_FUN(err_set_outrtn)(void (*rtn)(void *, char*), void *arg) {
|
||||
ErrSetOutRtn(rtn, arg);
|
||||
}
|
||||
|
||||
|
@ -76,7 +76,7 @@ void ErrCod(int code);
|
||||
void ErrShow(char *text); /* write out error message with stack info */
|
||||
void ErrShort(char *msg); /* write out short error message */
|
||||
void ERR_EXIT(char *text);
|
||||
void ErrSetOutRtn(void (*rtn)(), void *arg);
|
||||
void ErrSetOutRtn(void (*rtn)(void *,char *), void *arg);
|
||||
void ErrSetOutFile(FILE *file);
|
||||
|
||||
extern int ErrCode;
|
||||
|
33
tecs/sys_util.h → tecs/myc_fortran.h
Normal file → Executable file
33
tecs/sys_util.h → tecs/myc_fortran.h
Normal file → Executable file
@ -12,10 +12,6 @@
|
||||
Use macros STR_TO_C and STR_TO_F to convert from Fortran character strings
|
||||
to C character arrays and vice versa.
|
||||
|
||||
sys_adr_len (argument of accept and gethostbyadr, system dependent)
|
||||
|
||||
sys_ctrl_init() (needed in VMS only)
|
||||
|
||||
*/
|
||||
|
||||
|
||||
@ -23,36 +19,37 @@
|
||||
|
||||
typedef struct { short size, dummy; char *text; } SysVmsChar;
|
||||
|
||||
#define F_CHAR(VAR) SysVmsChar *VAR##_desc
|
||||
#define STR_TO_C(DST,SRC) str_ntrim(DST, SRC##_desc->text, sizeof(DST), SRC##_desc->size)
|
||||
#define STR_TO_F(DST,SRC) str_npad(DST##_desc->text, SRC, DST##_desc->size)
|
||||
#define F_CHAR(VAR) SysVmsChar *VAR
|
||||
#define F_DCHAR(VAR,LEN) static char VAR##_str[LEN]; SysVmsChar VAR##_desc={LEN,270,&VAR##_str[0]}; SysVmsChar *VAR=&VAR##_desc
|
||||
#define F_CLEN(VAR)
|
||||
#define F_ALEN(VAR)
|
||||
#define F_LEN(VAR) VAR->size
|
||||
#define STR_TO_C(DST,SRC) str_ntrim(DST, SRC->text, sizeof(DST), SRC->size)
|
||||
#define STR_TO_F(DST,SRC) str_npad(DST->text, SRC, DST->size)
|
||||
#define F_FUN(A) A
|
||||
|
||||
typedef size_t sys_adr_len; /* argument of accept and gethostbyadr */
|
||||
|
||||
void sys_ctrl_init(void);
|
||||
|
||||
|
||||
#elif defined __alpha || defined __unix
|
||||
#elif defined __alpha || defined __unix || defined __GNUC__
|
||||
|
||||
#define F_CHAR(VAR) char *VAR
|
||||
#define F_DCHAR(VAR,LEN) char VAR[LEN]; int VAR##_len=LEN
|
||||
#define F_CLEN(VAR) ,int VAR##_len
|
||||
#define F_ALEN(VAR) ,VAR##_len
|
||||
#define F_LEN(VAR) VAR##_len
|
||||
#define STR_TO_C(DST,SRC) str_ntrim(DST, SRC, sizeof(DST), SRC##_len)
|
||||
#define STR_TO_F(DST,SRC) str_npad(DST, SRC, DST##_len)
|
||||
|
||||
#ifdef __alpha
|
||||
#define F_FUN(A) A##_
|
||||
#elif defined __GNUC__
|
||||
#define F_FUN(A) A##__
|
||||
#else
|
||||
#define F_FUN(A) A##__
|
||||
#endif
|
||||
|
||||
typedef int sys_adr_len; /* argument of accept and gethostbyadr */
|
||||
|
||||
#define sys_ctrl_init() 0
|
||||
|
||||
|
||||
#else
|
||||
|
||||
/* other machines are not supported */
|
||||
#error "other machines are not supported"
|
||||
|
||||
#endif
|
||||
|
@ -5,7 +5,6 @@
|
||||
#include <assert.h>
|
||||
#include <string.h>
|
||||
#include <ctype.h>
|
||||
#include "sys_util.h"
|
||||
#include "myc_err.h"
|
||||
#include "myc_str.h"
|
||||
#include "myc_mem.h"
|
||||
@ -94,7 +93,6 @@ char *str_nsplit(char *dst, const char *src, char sep, int dstlen) {
|
||||
}
|
||||
|
||||
char *str_read_until(FILE *fil, char *term, char *buf, char *end) {
|
||||
char *s;
|
||||
char fmt[24];
|
||||
int i, l, siz;
|
||||
char ch;
|
||||
@ -122,8 +120,7 @@ char *str_read_until(FILE *fil, char *term, char *buf, char *end) {
|
||||
char *str_read_file(char *file) {
|
||||
FILE *fil;
|
||||
char *str, *s, *e, *p, *q;
|
||||
char ch;
|
||||
int i, l, size;
|
||||
int i, size;
|
||||
struct stat statbuf;
|
||||
|
||||
i=stat(file, &statbuf);
|
||||
@ -198,7 +195,7 @@ int str_nsubstitute(char *result, char *str, char *old, char *new, int reslen) {
|
||||
void str_nupcase(char *dst, const char *src, int dstlen) {
|
||||
dstlen--; /* space for trailing nul */
|
||||
while (*src!='\0' && dstlen>0) {
|
||||
*dst=toupper(*src);
|
||||
*dst=toupper((int)*src);
|
||||
dst++; src++;
|
||||
dstlen--;
|
||||
}
|
||||
@ -208,13 +205,14 @@ void str_nupcase(char *dst, const char *src, int dstlen) {
|
||||
void str_nlowcase(char *dst, const char *src, int dstlen) {
|
||||
dstlen--; /* space for trailing nul */
|
||||
while (*src!='\0' && dstlen>0) {
|
||||
*dst=tolower(*src);
|
||||
*dst=tolower((int)*src);
|
||||
dst++; src++;
|
||||
dstlen--;
|
||||
}
|
||||
*dst='\0';
|
||||
}
|
||||
|
||||
#ifndef __GNUC__
|
||||
int strcasecmp(const char *str1, const char *str2) {
|
||||
int i;
|
||||
char ch1, ch2;
|
||||
@ -230,6 +228,7 @@ int strcasecmp(const char *str1, const char *str2) {
|
||||
}
|
||||
return(0);
|
||||
}
|
||||
#endif
|
||||
|
||||
int str_ncpy(char *dst, const char *src, int maxdest) {
|
||||
strncpy(dst, src, maxdest);
|
||||
|
@ -1,10 +1,14 @@
|
||||
#include <assert.h>
|
||||
#include <sys/time.h>
|
||||
#include <time.h>
|
||||
/*
|
||||
#include <sys/types.h>
|
||||
#include <sys/timeb.h>
|
||||
#include "sys_util.h"
|
||||
int ftime (struct timeb *__timeptr);
|
||||
*/
|
||||
#include "myc_fortran.h"
|
||||
#include "myc_time.h"
|
||||
|
||||
int ftime (struct timeb *__timeptr); /* for some reason not defined in timeb.h with flag -std1 */
|
||||
|
||||
static time_t my_base=0;
|
||||
|
||||
@ -34,12 +38,19 @@ int mycNow(void) {
|
||||
}
|
||||
|
||||
int mycMsecSince(int since) {
|
||||
/*
|
||||
struct timeb now;
|
||||
*/
|
||||
struct timeval now;
|
||||
int msec;
|
||||
|
||||
if (my_base == 0) initBase();
|
||||
/*
|
||||
ftime(&now);
|
||||
msec = (now.time - my_base) % (24*3600) * 1000 + now.millitm - since;
|
||||
*/
|
||||
gettimeofday(&now, NULL);
|
||||
msec = (now.tv_sec - my_base) % (24*3600) * 1000 + now.tv_usec/1000 - since;
|
||||
if (msec < 0) msec+=24*3600000;
|
||||
return msec;
|
||||
}
|
||||
|
189
tecs/str_buf.c
189
tecs/str_buf.c
@ -1,189 +0,0 @@
|
||||
#include <stdlib.h>
|
||||
#include <stdio.h>
|
||||
#include <math.h>
|
||||
#include <string.h>
|
||||
#include <strings.h>
|
||||
#include <stdlib.h>
|
||||
#include "sys_util.h"
|
||||
#include "err_handling.h"
|
||||
#include "str_util.h"
|
||||
#include "str_buf.h"
|
||||
|
||||
char *str_nget_str(Str_Buf *buf, char *result, int reslen)
|
||||
{ char *b, *f, *e, quote;
|
||||
int res, l, ll;
|
||||
|
||||
if (buf->rdpos < 0 || buf->rdpos >= buf->dsize) ERR_MSG("buffer corrupt");
|
||||
b=buf->buf + buf->rdpos;
|
||||
if (*b=='"' || *b=='\'') { /* take string within quotes (single or double) */
|
||||
quote=*b; b++;
|
||||
f=strchr(b, quote);
|
||||
if (f==NULL) ERR_MSG("missing '""'");
|
||||
l=f-b;
|
||||
e=strchr(f+1, buf->sep);
|
||||
if (e==NULL) {
|
||||
buf->rdpos = f - buf->buf + 1 + strlen(f+1);
|
||||
} else {
|
||||
buf->rdpos = e - buf->buf + 1;
|
||||
}
|
||||
} else {
|
||||
f=strchr(b, buf->sep);
|
||||
if (f==NULL) {
|
||||
l=strlen(b);
|
||||
f=b+l;
|
||||
buf->rdpos+=l;
|
||||
} else {
|
||||
l=f-b;
|
||||
buf->rdpos+=l+1;
|
||||
}
|
||||
}
|
||||
if (result==NULL) {
|
||||
*f='\0';
|
||||
return(b);
|
||||
} else {
|
||||
if (l>=reslen) ERR_MSG("result too short");
|
||||
strncpy(result, b, l);
|
||||
result[l]='\0';
|
||||
return(result);
|
||||
}
|
||||
OnError:
|
||||
buf->rdpos=-1;
|
||||
return(NULL);
|
||||
}
|
||||
|
||||
int str_get_int(Str_Buf *buf, int *res)
|
||||
{ char num[32];
|
||||
int i;
|
||||
|
||||
ERR_P(str_get_str(buf, num));
|
||||
i=sscanf(num, "%d", res);
|
||||
if (i==0) ERR_MSG("illegal number");
|
||||
return(0);
|
||||
OnError:
|
||||
buf->rdpos=-1;
|
||||
return(0);
|
||||
}
|
||||
|
||||
int str_get_float(Str_Buf *buf, float *res)
|
||||
{ char num[32];
|
||||
int i;
|
||||
|
||||
ERR_P(str_get_str(buf, num));
|
||||
i=sscanf(num, "%f", res);
|
||||
if (i==0) ERR_MSG("illegal number");
|
||||
return(0);
|
||||
OnError:
|
||||
buf->rdpos=-1;
|
||||
return(-1);
|
||||
}
|
||||
|
||||
int str_get_end(Str_Buf *buf)
|
||||
{
|
||||
if (buf->rdpos < 0 || buf->rdpos >= buf->dsize) ERR_MSG("buffer corrupt");
|
||||
if (buf->rdpos != buf->wrpos) ERR_MSG("superflous content in buffer");
|
||||
return(0);
|
||||
OnError:
|
||||
return(-1);
|
||||
}
|
||||
|
||||
int str_put_str(Str_Buf *buf, const char *str)
|
||||
{ int l, pos;
|
||||
char quote;
|
||||
|
||||
pos=buf->wrpos;
|
||||
if (pos < 0 || pos >= buf->dsize) ERR_MSG("buffer corrupt");
|
||||
l=strlen(str);
|
||||
quote='\0';
|
||||
if (buf->sep>STR_NOSEPARATOR) {
|
||||
if (strchr(str, buf->sep)!=NULL) {
|
||||
if (strchr(str, '"')==NULL) {
|
||||
quote='"'; l+=2;
|
||||
} else if (strchr(str, '\'')==NULL) {
|
||||
quote='\''; l+=2;
|
||||
} else {
|
||||
ERR_MSG("str must not contain separator and both kind of quotes");
|
||||
}
|
||||
}
|
||||
}
|
||||
if (pos+l >= buf->dsize) ERR_MSG("buffer too short");
|
||||
if (quote!='\0') {
|
||||
buf->buf[pos]=quote; pos++;
|
||||
strcpy(buf->buf + pos, str);
|
||||
buf->buf[pos]=quote; pos++;
|
||||
} else {
|
||||
strcpy(buf->buf + pos, str);
|
||||
}
|
||||
pos+=l;
|
||||
if (buf->sep!=STR_NOSEPARATOR) {
|
||||
buf->buf[pos]=buf->sep;
|
||||
pos++;
|
||||
} else {
|
||||
buf->buf[pos]='\0';
|
||||
}
|
||||
buf->wrpos=pos;
|
||||
return(0);
|
||||
OnError:
|
||||
buf->wrpos=-1;
|
||||
return(-1);
|
||||
}
|
||||
|
||||
int str_put_int(Str_Buf *buf, int val)
|
||||
{ char num[32];
|
||||
|
||||
sprintf(num, "%d", val);
|
||||
ERR_I(str_put_str(buf, num));
|
||||
return(0);
|
||||
OnError:
|
||||
return(-1);
|
||||
}
|
||||
|
||||
int str_put_float(Str_Buf *buf, float val)
|
||||
{ char num[32];
|
||||
|
||||
sprintf(num, "%f", val);
|
||||
ERR_I(str_put_str(buf, num));
|
||||
return(0);
|
||||
OnError:
|
||||
return(-1);
|
||||
}
|
||||
|
||||
void str_get_start(Str_Buf *buf)
|
||||
{ buf->rdpos=0;
|
||||
}
|
||||
|
||||
void str_put_start(Str_Buf *buf)
|
||||
{ buf->rdpos=0;
|
||||
buf->wrpos=0;
|
||||
}
|
||||
|
||||
Str_Buf *str_create_buf(size_t size, char separator)
|
||||
{ Str_Buf *buf;
|
||||
|
||||
NEW(buf);
|
||||
ERR_P(buf->buf=MALLOC(size));
|
||||
buf->dsize=size;
|
||||
buf->sep=separator;
|
||||
buf->wrpos=0;
|
||||
buf->rdpos=0;
|
||||
return(buf);
|
||||
OnError:
|
||||
return(NULL);
|
||||
}
|
||||
|
||||
void str_link_buf(Str_Buf *buf, char *str, int size, char separator) {
|
||||
buf->buf=str;
|
||||
buf->rdpos=0;
|
||||
if (size==0) {
|
||||
buf->wrpos=strlen(str);
|
||||
buf->dsize=buf->wrpos+1;
|
||||
} else {
|
||||
buf->wrpos=0;
|
||||
buf->dsize=size;
|
||||
}
|
||||
buf->sep=separator;
|
||||
}
|
||||
|
||||
void str_free_buf(Str_Buf *buf)
|
||||
{ FREE(buf->buf);
|
||||
FREE(buf);
|
||||
}
|
@ -1,27 +0,0 @@
|
||||
#ifndef _STR_BUF_H_
|
||||
#define _STR_BUF_H_
|
||||
|
||||
typedef struct { char *buf; int dsize, rdpos, wrpos; char sep; } Str_Buf;
|
||||
|
||||
#define str_get_str(BUF,RES) str_nget_str(BUF,RES,sizeof(RES))
|
||||
|
||||
/* input */
|
||||
void str_get_start(Str_Buf *buf);
|
||||
char *str_nget_str(Str_Buf *buf, char *result, int reslen);
|
||||
int str_get_int(Str_Buf *buf, int *res);
|
||||
int str_get_float(Str_Buf *buf, float *res);
|
||||
int str_get_end(Str_Buf *buf);
|
||||
|
||||
/* output */
|
||||
void str_put_start(Str_Buf *buf);
|
||||
int str_put_str(Str_Buf *buf, const char *str);
|
||||
int str_put_int(Str_Buf *buf, int val);
|
||||
int str_put_float(Str_Buf *buf, float val);
|
||||
|
||||
/* common */
|
||||
#define STR_NOSEPARATOR ((char)1)
|
||||
Str_Buf *str_create_buf(size_t size, char separator);
|
||||
void str_link_buf(Str_Buf *buf, char *str, int size, char separator);
|
||||
void str_free_buf(Str_Buf *buf);
|
||||
|
||||
#endif /* _STR_BUF_H_ */
|
242
tecs/str_util.c
242
tecs/str_util.c
@ -1,242 +0,0 @@
|
||||
#include <stdlib.h>
|
||||
#include <stdio.h>
|
||||
#include <sys/types.h>
|
||||
#include <sys/stat.h>
|
||||
#include <sys/socket.h>
|
||||
#include <sys/time.h>
|
||||
#include <assert.h>
|
||||
#include <string.h>
|
||||
#include <ctype.h>
|
||||
#include "sys_util.h"
|
||||
#include "err_handling.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);
|
||||
}
|
||||
|
||||
int str_npad(char *dest, const char *src, int ldest) {
|
||||
int i, lsrc;
|
||||
|
||||
lsrc=strlen(src);
|
||||
if (lsrc>=ldest) {
|
||||
if (dest!=src) strncpy(dest, src, ldest);
|
||||
lsrc=ldest;
|
||||
} else {
|
||||
if (dest!=src) strcpy(dest, src);
|
||||
for (i=lsrc; i<ldest; i++) {
|
||||
dest[i]=' ';
|
||||
}
|
||||
}
|
||||
return(lsrc);
|
||||
}
|
||||
|
||||
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)<size);
|
||||
return(str);
|
||||
OnError: return(NULL);
|
||||
}
|
||||
|
||||
void str_replace_char(char *str, char ch, char rep) {
|
||||
char *s;
|
||||
|
||||
assert(ch!='\0' && ch!=rep);
|
||||
s=strchr(str, ch);
|
||||
while (s!=NULL) {
|
||||
*s=rep;
|
||||
s=strchr(s, ch);
|
||||
}
|
||||
}
|
||||
|
||||
int str_nsubstitute(char *result, char *str, char *old, char *new, int reslen) {
|
||||
char *s, *p, *r;
|
||||
int l, ln, lo;
|
||||
|
||||
p=str;
|
||||
r=result;
|
||||
ln=strlen(new);
|
||||
lo=strlen(old);
|
||||
s=strstr(p, old);
|
||||
reslen--;
|
||||
if (reslen<0) ERR_MSG("result buffer too short");
|
||||
while (s!=NULL) {
|
||||
l=s-p;
|
||||
if (l>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 (ch1<ch2) {
|
||||
return(-i);
|
||||
} else if (ch1>ch2) {
|
||||
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);
|
||||
}
|
114
tecs/str_util.h
114
tecs/str_util.h
@ -1,114 +0,0 @@
|
||||
#ifndef _UTIL_H_
|
||||
#define _UTIL_H_
|
||||
|
||||
/*
|
||||
use these macros if DST is a fixed length character array
|
||||
*/
|
||||
|
||||
#define str_trim(DST,SRC,L) str_ntrim(DST,SRC,sizeof(DST),L)
|
||||
#define str_pad(DST,SRC) str_npad(DST,SRC,sizeof(DST))
|
||||
#define str_split(DST,SRC,SEP) str_nsplit(DST,SRC,SEP,sizeof(DST))
|
||||
#define str_substitute(DST,SRC,OLD,NEW) str_nsubstitute(DST,SRC,OLD,NEW,sizeof(DST))
|
||||
#define str_upcase(DST,SRC) str_nupcase(DST,SRC,sizeof(DST))
|
||||
#define str_copy(DST,SRC) str_ncpy(DST,SRC,sizeof(DST))
|
||||
#define str_append(DST,SRC) str_ncat(DST,SRC,sizeof(DST))
|
||||
|
||||
|
||||
char *str_split1(char *str, char separator);
|
||||
/*
|
||||
trims text before separator in *str and returns
|
||||
a pointer to the first character after separator
|
||||
*/
|
||||
|
||||
char *str_splitx(char *str, char sep, char *list[], int *n);
|
||||
/*
|
||||
split string into *n strings using separator sep.
|
||||
spaces at the end of the elements are trimmed
|
||||
attention: *str is modified ('\0' placed at the end of the elements)
|
||||
|
||||
if *n separators are found, result points to string after *n-th separator
|
||||
else result is NULL
|
||||
*n contains number of elements stored in list
|
||||
*/
|
||||
|
||||
int str_ntrim(char *dest, const char *src, int ldest, int lsrc);
|
||||
/*
|
||||
copy characters 0 to lsrc-1 from src to dest (max ldest chars).
|
||||
*/
|
||||
|
||||
int str_npad(char *dest, const char *src, int ldest);
|
||||
/*
|
||||
copy src to dest and fill with spaces (fortran string format)
|
||||
*/
|
||||
|
||||
char *str_nsplit(char *dst, const char *src, char sep, int dstlen);
|
||||
/*
|
||||
returns a pointer to the text after the separator sep in *src
|
||||
and copies the text before the separator to *dst
|
||||
when *src does not contain the separator sep
|
||||
NULL is returned, and *dst is a copy of *src
|
||||
*/
|
||||
|
||||
char *str_read_file(char *file);
|
||||
/*
|
||||
return one string containing the contents of file *file
|
||||
comments separated by '!' are omitted. The caller must
|
||||
free the result after use.
|
||||
*/
|
||||
|
||||
void str_replace_char(char *str, char ch, char rep);
|
||||
/*
|
||||
replace all occurences of character ch by character rep in string *str
|
||||
*/
|
||||
|
||||
int str_nsubstitute(char *result, char *str, char *old, char *new, int reslen);
|
||||
/*
|
||||
replace every instance of old in str by new.
|
||||
the result must not overlap
|
||||
if the result would be longer than reslen, the result is en empty string
|
||||
and the return value is -1;
|
||||
else the return value is the length of the result.
|
||||
return one string containing the contents of file *file
|
||||
the contents are treated in the following way:
|
||||
- #0,#1,...#n is replaced by the corresponding argument *args[n] (n=0..nargs-1, nargs<10)
|
||||
- at the end of each line spaces and comments separated by ! are trimmed
|
||||
*/
|
||||
|
||||
void str_nupcase(char *dst, const char *src, int dstlen);
|
||||
/*
|
||||
convert *str to uppercase
|
||||
*/
|
||||
|
||||
#ifdef __VMS_VER
|
||||
#if __VMS_VER<70000000
|
||||
|
||||
int strcasecmp(const char *str1, const char *str2);
|
||||
/*
|
||||
compare *str1 with *str2
|
||||
the comparison is not case sensitive
|
||||
if result=0: strings are equal
|
||||
else
|
||||
result>0 <==> *str1>*str2
|
||||
first different character is at position abs(result)-1
|
||||
*/
|
||||
|
||||
#else
|
||||
#include <strings.h>
|
||||
#endif /* __VMS_VER<70000000 */
|
||||
#else
|
||||
#include <strings.h>
|
||||
#endif /* __VMS_VER */
|
||||
|
||||
int str_ncpy(char *dst, const char *src, int maxdest);
|
||||
/*
|
||||
copy *src to *dest, maximal maxdest characters,
|
||||
it is guaranteed, that dst contains '\0'
|
||||
*/
|
||||
|
||||
int str_ncat(char *dst, const char *src, int maxdest);
|
||||
/*
|
||||
append *src to *dest, maximal maxdest characters,
|
||||
it is guaranteed, that dst contains '\0'
|
||||
*/
|
||||
|
||||
#endif /* _UTIL_H_ */
|
@ -5,7 +5,7 @@
|
||||
#include "myc_err.h"
|
||||
#include "myc_str.h"
|
||||
#include "myc_time.h"
|
||||
#include "sys_util.h"
|
||||
#include "myc_fortran.h"
|
||||
#include "coc_util.h"
|
||||
#include "tecs_cli.h"
|
||||
#include "tecs_data.h"
|
||||
|
@ -55,7 +55,7 @@
|
||||
logical loop
|
||||
|
||||
! functions
|
||||
integer sys_gmt_off, myc_now, myc_time, myc_date, get_data
|
||||
integer myc_now, myc_time, myc_date, get_data
|
||||
integer tecs_get_mult, tecs_get_par
|
||||
|
||||
data window/0./
|
||||
|
@ -7,7 +7,6 @@
|
||||
#include "rs232c_def.h"
|
||||
#include "asynsrv_def.h"
|
||||
#include "sinq_prototypes.h"
|
||||
#include "sys_util.h"
|
||||
#include "myc_err.h"
|
||||
#include "tecs_serial.h"
|
||||
#include "coc_logfile.h"
|
||||
|
Reference in New Issue
Block a user