new tecs version M.Z.08.2001
This commit is contained in:
218
tecs/myc_buf.c
Normal file
218
tecs/myc_buf.c
Normal file
@@ -0,0 +1,218 @@
|
||||
#include <stdlib.h>
|
||||
#include <stdio.h>
|
||||
#include <math.h>
|
||||
#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"
|
||||
|
||||
char *StrNGet(StrBuf *buf, char *result, int reslen, int sep) {
|
||||
char *b, *f, *e, quote;
|
||||
int res, l, ll;
|
||||
|
||||
if (buf->rdpos < 0 || buf->rdpos >= buf->dsize || buf->buf==NULL)
|
||||
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, sep);
|
||||
if (e==NULL) {
|
||||
buf->rdpos = f - buf->buf + 1 + strlen(f+1);
|
||||
buf->seen=0;
|
||||
} else {
|
||||
buf->rdpos = e - buf->buf + 1;
|
||||
buf->seen=1;
|
||||
}
|
||||
} else {
|
||||
f=strchr(b, sep);
|
||||
if (f==NULL) {
|
||||
l=strlen(b);
|
||||
f=b+l;
|
||||
buf->rdpos+=l;
|
||||
buf->seen=0;
|
||||
} else {
|
||||
l=f-b;
|
||||
buf->rdpos+=l+1;
|
||||
buf->seen=1;
|
||||
}
|
||||
}
|
||||
if (result==NULL) {
|
||||
if (reslen==-1) { /* special case for StrGetBuf */
|
||||
return(f);
|
||||
}
|
||||
*f='\0';
|
||||
return(b);
|
||||
} else {
|
||||
if (l>=reslen) l=reslen-1; /* ERR_MSG("result too short"); */
|
||||
strncpy(result, b, l);
|
||||
result[l]='\0';
|
||||
return(result);
|
||||
}
|
||||
OnError:
|
||||
buf->rdpos=buf->dsize; /* illegal value */
|
||||
return(NULL);
|
||||
}
|
||||
|
||||
int StrGetBuf(StrBuf *buf, StrBuf *res, int sep) {
|
||||
char *b, *f;
|
||||
|
||||
b = buf->buf + buf->rdpos;
|
||||
ERR_P(f=StrNGet(buf, NULL, -1, sep));
|
||||
res->buf = b;
|
||||
res->wrpos = f-b;
|
||||
res->rdpos = 0;
|
||||
res->dsize = f-b;
|
||||
return(0);
|
||||
OnError:
|
||||
buf->rdpos=buf->dsize; /* illegal value */
|
||||
return(-1);
|
||||
}
|
||||
|
||||
int StrGetInt(StrBuf *buf, int *res, int sep) {
|
||||
char num[32];
|
||||
int i, l, p;
|
||||
|
||||
p=buf->rdpos;
|
||||
ERR_P(StrGet(buf, num, sep));
|
||||
i=sscanf(num, "%d%n", res, &l);
|
||||
if (i==0)
|
||||
ERR_MSG("illegal number");
|
||||
if (sep==StrNONE) {
|
||||
buf->rdpos=p+l;
|
||||
}
|
||||
return(0);
|
||||
OnError:
|
||||
buf->rdpos=buf->dsize; /* illegal value */
|
||||
return(0);
|
||||
}
|
||||
|
||||
int StrGetFloat(StrBuf *buf, float *res, int sep) {
|
||||
char num[32];
|
||||
int i, l, p;
|
||||
|
||||
p=buf->rdpos;
|
||||
ERR_P(StrGet(buf, num, sep));
|
||||
if (num[0]=='N' && num[1]=='a' && num[2]=='N') {
|
||||
l=3;
|
||||
*res = MYC_NAN;
|
||||
} else {
|
||||
i=sscanf(num, "%f%n", res, &l);
|
||||
if (i==0) {
|
||||
ERR_MSG("illegal number");
|
||||
}
|
||||
}
|
||||
if (sep==StrNONE) {
|
||||
buf->rdpos=p+l;
|
||||
}
|
||||
return(0);
|
||||
OnError:
|
||||
buf->rdpos=buf->dsize; /* illegal value */
|
||||
return(-1);
|
||||
}
|
||||
|
||||
int StrGetEnd(StrBuf *buf) {
|
||||
if (buf->buf!=NULL) {
|
||||
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 StrPut(StrBuf *buf, const char *str, int sep) {
|
||||
int l, pos;
|
||||
char quote;
|
||||
|
||||
pos=buf->wrpos;
|
||||
if (pos < 0 || pos >= buf->dsize || buf->buf==NULL)
|
||||
ERR_MSG("buffer corrupt");
|
||||
l=strlen(str);
|
||||
quote='\0';
|
||||
if (sep>0) {
|
||||
if (strchr(str, sep)!=NULL) {
|
||||
if (strchr(str, '"')!=NULL) {
|
||||
quote='"'; l+=2;
|
||||
}
|
||||
if (strchr(str, '\'')!=NULL) {
|
||||
if (quote!='\0')
|
||||
ERR_MSG("str must not contain separator and both kind of quotes");
|
||||
quote='\''; l+=2;
|
||||
}
|
||||
}
|
||||
}
|
||||
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 (sep!=StrNONE) {
|
||||
buf->buf[pos]=sep;
|
||||
pos++;
|
||||
} else {
|
||||
buf->buf[pos]='\0';
|
||||
}
|
||||
buf->wrpos=pos;
|
||||
return(0);
|
||||
OnError:
|
||||
buf->wrpos=-1;
|
||||
return(-1);
|
||||
}
|
||||
|
||||
int StrPutInt(StrBuf *buf, int val, int sep) {
|
||||
char num[32];
|
||||
|
||||
sprintf(num, "%d", val);
|
||||
ERR_I(StrPut(buf, num, sep));
|
||||
return(0);
|
||||
OnError:
|
||||
return(-1);
|
||||
}
|
||||
|
||||
int StrPutFloat(StrBuf *buf, float val, int sep) {
|
||||
char num[32];
|
||||
int l;
|
||||
|
||||
if (val == MYC_NAN) {
|
||||
ERR_I(StrPut(buf, "NaN", sep));
|
||||
} else {
|
||||
sprintf(num, "%g", val);
|
||||
ERR_I(StrPut(buf, num, sep));
|
||||
}
|
||||
return(0);
|
||||
OnError:
|
||||
return(-1);
|
||||
}
|
||||
|
||||
void StrReset(StrBuf *buf) {
|
||||
buf->rdpos=0;
|
||||
}
|
||||
|
||||
void StrClear(StrBuf *buf) {
|
||||
buf->rdpos=0;
|
||||
buf->wrpos=0;
|
||||
}
|
||||
|
||||
void StrNLink(StrBuf *buf, char *str, int length) {
|
||||
int l;
|
||||
buf->buf=str;
|
||||
buf->rdpos=0;
|
||||
l=strlen(str);
|
||||
if (l<length) l=length;
|
||||
buf->wrpos=l;
|
||||
buf->dsize=buf->wrpos;
|
||||
}
|
||||
Reference in New Issue
Block a user