*** empty log message ***
This commit is contained in:
184
tecs/buf.c
184
tecs/buf.c
@ -1,184 +0,0 @@
|
||||
#include <math.h>
|
||||
#include <string.h>
|
||||
#include <strings.h>
|
||||
#include <stdlib.h>
|
||||
#include "buf.h"
|
||||
|
||||
#define TWO28 268435456.0
|
||||
#define MAXINT 2147483647
|
||||
|
||||
int buf_encode_int(char b[4]) {
|
||||
int i;
|
||||
i=(unsigned char)b[0];
|
||||
if (i>=128) i=i-256;
|
||||
i=i*256+(unsigned char)b[1];
|
||||
i=i*256+(unsigned char)b[2];
|
||||
i=i*256+(unsigned char)b[3];
|
||||
return(i);
|
||||
}
|
||||
|
||||
void buf_decode_int(char b[4], int i) {
|
||||
if (i<0) {
|
||||
i+=0x10000000;
|
||||
b[0]=i/0x01000000+128;
|
||||
i=i % 0x01000000;
|
||||
} else {
|
||||
b[0]=i/0x01000000;
|
||||
i=i % 0x01000000;
|
||||
}
|
||||
b[1]=i/0x10000; i=i % 0x10000;
|
||||
b[2]=i/0x100;
|
||||
b[3]=i % 0x100;
|
||||
}
|
||||
|
||||
int buf_get_int(buf_type *buf)
|
||||
{ char *b;
|
||||
int res;
|
||||
|
||||
b=buf->buf;
|
||||
if (b==NULL) return(0);
|
||||
if (b[0]!=1 || buf->size<5) {buf->buf=NULL; return(0); }
|
||||
b++;
|
||||
res=buf_encode_int(b);
|
||||
b+=4;
|
||||
buf->size-=5;
|
||||
buf->buf=b;
|
||||
return(res);
|
||||
}
|
||||
|
||||
char *buf_get_str(buf_type *buf)
|
||||
{ char *b;
|
||||
int l;
|
||||
|
||||
b=buf->buf;
|
||||
if (b==NULL) return("");
|
||||
if (b[0]==1 || b[0]==2) return("");
|
||||
l=strlen(b);
|
||||
if (buf->size<=l) {buf->buf=NULL; return(""); }
|
||||
buf->buf+=l+1;
|
||||
buf->size-=l+1;
|
||||
return(b);
|
||||
}
|
||||
|
||||
float buf_get_float(buf_type *buf)
|
||||
{ char *b;
|
||||
float res;
|
||||
int mant, iexp;
|
||||
|
||||
b=buf->buf;
|
||||
if (b==NULL) return(0.0);
|
||||
if (b[0]!=2 || buf->size<6) { buf->buf=NULL; return(0.0); }
|
||||
iexp=b[1];
|
||||
b+=2;
|
||||
mant=buf_encode_int(b);
|
||||
b+=4;
|
||||
buf->size-=6;
|
||||
res=(float)ldexp(mant/TWO28, iexp);
|
||||
buf->buf=b;
|
||||
return res;
|
||||
}
|
||||
|
||||
int buf_size(buf_type *buf)
|
||||
{
|
||||
if (buf->buf==NULL) return(-1);
|
||||
return (buf->size);
|
||||
}
|
||||
|
||||
void buf_put_int(buf_type *buf, int val)
|
||||
{ char *b;
|
||||
|
||||
b=buf->buf;
|
||||
if (b==NULL) return;
|
||||
b[0]=1;
|
||||
b++;
|
||||
if (buf->size<=4) { buf->buf=NULL; return; }
|
||||
buf_decode_int(b, val);
|
||||
b+=4;
|
||||
buf->size-=5;
|
||||
buf->buf=b;
|
||||
return;
|
||||
}
|
||||
|
||||
void buf_put_str(buf_type *buf, const char *str)
|
||||
{ char *b;
|
||||
int l;
|
||||
|
||||
if (str[0]<=2 && str[0]!='\0') { buf->buf=NULL; return; };
|
||||
b=buf->buf;
|
||||
if (b==NULL) return;
|
||||
l=strlen(str);
|
||||
if (buf->size<=l) { buf->buf=NULL; return; }
|
||||
strcpy(b, str);
|
||||
buf->buf+=l+1;
|
||||
buf->size-=l+1;
|
||||
}
|
||||
|
||||
int buf_nint(float val)
|
||||
{ int res;
|
||||
|
||||
if (val<0) {
|
||||
if (val<-MAXINT) {
|
||||
return(-MAXINT);
|
||||
} else {
|
||||
return(val-0.5);
|
||||
}
|
||||
} else {
|
||||
if (val>MAXINT) {
|
||||
return(MAXINT);
|
||||
} else {
|
||||
return(val+0.5);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void buf_put_float(buf_type *buf, float val)
|
||||
{ char *b;
|
||||
double d;
|
||||
int iexp;
|
||||
int mant;
|
||||
|
||||
b=buf->buf;
|
||||
if (b==NULL) return;
|
||||
if (buf->size<=4) {buf->buf=NULL; return; }
|
||||
d=frexp((double)val, &iexp);
|
||||
mant=buf_nint(d*TWO28);
|
||||
if (iexp>127) iexp=127;
|
||||
if (iexp<-128) iexp=-128;
|
||||
b[0]=2;
|
||||
b[1]=iexp;
|
||||
b+=2;
|
||||
buf_decode_int(b, mant);
|
||||
b+=4;
|
||||
buf->size-=6;
|
||||
buf->buf=b;
|
||||
}
|
||||
|
||||
void buf_put_end(buf_type *buf)
|
||||
{ buf->buf=buf->start;
|
||||
buf->usize=buf->isize-buf->size;
|
||||
buf->size=buf->usize;
|
||||
}
|
||||
|
||||
void buf_put_start(buf_type *buf)
|
||||
{ buf->buf=buf->start;
|
||||
buf->size=buf->isize;
|
||||
}
|
||||
|
||||
void buf_reset(buf_type *buf)
|
||||
{ buf->buf=buf->start;
|
||||
buf->size=buf->usize;
|
||||
}
|
||||
|
||||
buf_type *buf_create(size_t size)
|
||||
{ buf_type *buf;
|
||||
|
||||
buf=malloc(sizeof(*buf));
|
||||
buf->start=malloc(size);
|
||||
buf->isize=size;
|
||||
return(buf);
|
||||
}
|
||||
|
||||
void buf_free(buf_type *buf)
|
||||
{ free(buf->buf);
|
||||
free(buf);
|
||||
}
|
22
tecs/buf.h
22
tecs/buf.h
@ -1,22 +0,0 @@
|
||||
#ifndef _buf_h_
|
||||
#define _buf_h_
|
||||
typedef struct { char *buf; char *start; int size; int usize; int isize, dummy; } buf_type;
|
||||
|
||||
/* input */
|
||||
void buf_reset(buf_type *buf);
|
||||
int buf_get_int(buf_type *buf);
|
||||
char *buf_get_str(buf_type *buf);
|
||||
float buf_get_float(buf_type *buf);
|
||||
int buf_size(buf_type *buf);
|
||||
|
||||
/* output */
|
||||
void buf_put_start(buf_type *buf);
|
||||
void buf_put_int(buf_type *buf, int val);
|
||||
void buf_put_str(buf_type *buf, const char *str);
|
||||
void buf_put_float(buf_type *buf, float val);
|
||||
void buf_put_end(buf_type *buf);
|
||||
|
||||
/* common */
|
||||
buf_type *buf_create(size_t size);
|
||||
void buf_free(buf_type *buf);
|
||||
#endif
|
@ -37,7 +37,7 @@ int CocGetVar(CocVar *varList, Str_Buf *buf, const char *name, int secure);
|
||||
#define CocDefStruct(V,S) CocDefVarS(#V,#S,&V,COC_STRUCT*(&V!=(S *)NULL));
|
||||
#define CocIntFld(S,V,F) CocDefVar(#S":"#V,&((S *)NULL)->V,COC_INT,&F);
|
||||
#define CocFltFld(S,V,F) CocDefVar(#S":"#V,&((S *)NULL)->V,COC_FLT,&F);
|
||||
#define CocStrFld(S,V,F) CocDefVar(#S":"#V,&((S *)NULL)->V,sizeof(((S *)NULL)->V),&F);
|
||||
#define CocStrFld(S,V,F) CocDefVar(#S":"#V,((S *)NULL)->V,sizeof(((S *)NULL)->V),&F);
|
||||
#define CocDefCmd(V) CocDefVar("$",V,sizeof(V),&CocWR)
|
||||
#define CocDefStrPtr(V,S,F) CocDefVar(#V,V,S,&F)
|
||||
#define CocAlias(A,V) CocDefVar(#A, #V, COC_ALIAS, &CocRD);
|
||||
|
192
tecs/util.c
192
tecs/util.c
@ -1,192 +0,0 @@
|
||||
#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 "errhdl.h"
|
||||
#include "util.h"
|
||||
|
||||
/* when changing FBUF_LEN, change also the constant in the fscanf call in subroutine LscExeCmd */
|
||||
#define FBUF_LEN 132
|
||||
|
||||
char *str_split(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);
|
||||
}
|
||||
|
||||
char *str_ntrim(char *dest, int ldest, const char *src, int lsrc) {
|
||||
char *s, *e;
|
||||
|
||||
if (lsrc<ldest) ldest=lsrc;
|
||||
if (dest!=src) strncpy(dest, src, ldest);
|
||||
e=dest+ldest-2;
|
||||
while (e>dest && *e==' ') e--; /* trim sequence */
|
||||
e[1]='\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_arg(char *file, char *args[], int nargs) {
|
||||
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;
|
||||
if (nargs>0) size+=size/2+100; /* max size */
|
||||
while (1) {
|
||||
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 if (*p=='#') {
|
||||
ch=fgetc(fil);
|
||||
i=ch-'0';
|
||||
if (i<0 || i>=nargs) {
|
||||
s=p+1; *s=ch; s++;
|
||||
} else {
|
||||
l=strlen(args[i]);
|
||||
if (s+l>=e) { p=NULL; break; }
|
||||
strcpy(p, args[i]);
|
||||
s=p+l;
|
||||
}
|
||||
} else {
|
||||
assert(*p=='\0');
|
||||
break;
|
||||
}
|
||||
}
|
||||
ERR_SI(fclose(fil));
|
||||
if (p!=NULL) break;
|
||||
size=size*3/2; /* allocation not sufficient -> try again */
|
||||
free(str);
|
||||
}
|
||||
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);
|
||||
}
|
||||
}
|
||||
|
||||
void str_upcase(char *str) {
|
||||
while (*str!='\0') {
|
||||
*str=toupper(*str); str++;
|
||||
}
|
||||
}
|
||||
|
||||
int str_cmp(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);
|
||||
}
|
||||
|
||||
void util_delay(int tmo_msec) {
|
||||
struct timeval tmo;
|
||||
|
||||
tmo.tv_sec=tmo_msec / 1000;
|
||||
tmo.tv_usec=(tmo_msec % 1000)*1000+1;
|
||||
select(1,NULL,NULL,NULL,&tmo);
|
||||
}
|
73
tecs/util.h
73
tecs/util.h
@ -1,73 +0,0 @@
|
||||
char *str_ntrim(char *dest, int ldest, const char *src, int lsrc);
|
||||
/*
|
||||
copy characters 0 to lsrc-1 from src to dest (max ldest chars).
|
||||
*/
|
||||
|
||||
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_split(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
|
||||
*/
|
||||
|
||||
char *str_read_arg(char *file, char *args[], int nargs);
|
||||
/*
|
||||
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_replace_char(char *str, char ch, char rep);
|
||||
/*
|
||||
replace all occurences of character ch by character rep in string *str
|
||||
*/
|
||||
|
||||
void str_upcase(char *str);
|
||||
/*
|
||||
convert *str to uppercase
|
||||
*/
|
||||
|
||||
int str_cmp(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
|
||||
*/
|
||||
|
||||
|
||||
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'
|
||||
*/
|
||||
|
||||
#define str_copy(DST,SRC) str_ncpy(DST,SRC,sizeof(DST))
|
||||
#define str_append(DST,SRC) str_ncat(DST,SRC,sizeof(DST))
|
||||
/*
|
||||
use these macros if DST is an fixed length character array
|
||||
*/
|
||||
|
||||
void util_delay(int tmo_msec);
|
||||
/*
|
||||
usleep is not available on VMS 6
|
||||
*/
|
Reference in New Issue
Block a user