📄 winp.c
字号:
/*---------------------------------------------------------------------------*/
/* this function finds the last defined field */
static struct _field_t *last_rec(struct _form_t *cform)
{
return(cform->field);
}
/*---------------------------------------------------------------------------*/
/* this function finds next non-text format character in format string */
static void next_fchar(struct _form_t *cform)
{
char ch;
int done=NO;
cform->decimal=NO;
/* do while no format character */
while(!done) {
/* test current character in format string */
switch(*cform->pformat) {
case '\"':
case '\'':
/* find matching quote */
ch=*cform->pformat++;
while(ch!=*cform->pformat) {
cform->cwcol++;
cform->pformat++;
}
cform->pformat++;
break;
default:
/* if currrently inside an angle or square */
/* bracket list, find end of list */
if(*(cform->pformat-1)=='<') {
while(*cform->pformat!='>') cform->pformat++;
cform->pformat++;
}
else {
if(*(cform->pformat-1)=='[') {
while(*cform->pformat!=']') cform->pformat++;
cform->pformat++;
}
else {
/* skip readability spaces */
if(*cform->pformat==' ') {
cform->pformat++;
}
else {
/* check for decimal point */
if(*cform->pformat=='.') {
cform->pformat++;
cform->cwcol++;
cform->decimal=YES;
}
else {
/* must be at next format character */
done=YES;
}
}
}
}
}
}
}
/*---------------------------------------------------------------------------*/
/* this function advances the cursor to the next position in field */
static void next_pos(struct _form_t *cform)
{
/* increment buffer pointer */
cform->pbuf++;
/* if buffer pointer is greater than last position in field */
/* and field is valid, then display field and go to next field, */
/* first position. otherwise, advance to next position */
if( cform->pbuf >= (cform->cfield->buf+cform->cfield->lenbuf) ) {
if(!goto_field(cform,FLD_NR)) beg_pos(cform);
}
else {
cform->cwcol++;
cform->pformat++;
next_fchar(cform);
}
}
/*---------------------------------------------------------------------------*/
/* this function sets the current field to the next field in the linked list */
static struct _field_t *next_rec(struct _form_t *cform)
{
struct _field_t *field;
field=cform->cfield->next;
if(field==NULL) field=first_rec(cform);
return(field);
}
/*---------------------------------------------------------------------------*/
/* this function advances the cursor to the next word */
static void next_word(struct _form_t *cform)
{
next_pos(cform);
while(*cform->pbuf!=' ' &&
cform->pbuf!=(cform->cfield->buf+cform->cfield->lenbuf-1))
next_pos(cform);
while(*cform->pbuf==' ' &&
cform->pbuf!=(cform->cfield->buf+cform->cfield->lenbuf-1))
next_pos(cform);
if(cform->pbuf==(cform->cfield->buf+cform->cfield->lenbuf-1))
next_pos(cform);
}
/*---------------------------------------------------------------------------*/
/* this function finds previous non-text format character in format string */
static void prev_fchar(struct _form_t *cform)
{
char ch;
int done=NO;
/* do while format character not found */
while(!done) {
/* test current format character */
switch(*cform->pformat) {
case '\"':
case '\'':
/* find matching quote */
ch=*cform->pformat--;
while(ch!=*cform->pformat) {
cform->cwcol--;
cform->pformat--;
}
cform->pformat--;
break;
case ' ':
/* skip readability spaces */
cform->pformat--;
break;
case '>':
/* find left angle bracket */
while(*cform->pformat!='<') cform->pformat--;
done=YES;
break;
case ']':
/* find left square bracket */
while(*cform->pformat!='[') cform->pformat--;
done=YES;
break;
case '.':
/* skip decimal point */
cform->pformat--;
cform->cwcol--;
break;
default:
/* must be previous format character */
done=YES;
}
}
}
/*---------------------------------------------------------------------------*/
/* this function positions the cursor to the previous position in field */
static void prev_pos(struct _form_t *cform)
{
/* decrement buffer pointer */
cform->pbuf--;
/* if buffer pointer is less than first position in field and */
/* field is valid, then display field and go to previous field, */
/* last position. otherwise, advance to next position */
if( cform->pbuf < cform->cfield->buf ) {
if(!goto_field(cform,FLD_PR)) end_pos(cform);
}
else {
cform->cwcol--;
cform->pformat--;
prev_fchar(cform);
}
}
/*---------------------------------------------------------------------------*/
/* this function sets the current field to the prev field in linked list */
static struct _field_t *prev_rec(struct _form_t *cform)
{
struct _field_t *field;
field=cform->cfield->prev;
if(field==NULL) field=last_rec(cform);
return(field);
}
/*---------------------------------------------------------------------------*/
/* this function moves the cursor to the beginning of the previous word */
static void prev_word(struct _form_t *cform)
{
prev_pos(cform);
while(*cform->pbuf==' ' && cform->pbuf!=cform->cfield->buf)
prev_pos(cform);
while(*cform->pbuf!=' ' && cform->pbuf!=cform->cfield->buf)
prev_pos(cform);
if(cform->pbuf!=cform->cfield->buf) next_pos(cform);
}
/*---------------------------------------------------------------------------*/
/* this function restores a field to its contents before editing */
static void restore_field(struct _form_t *cform,int high)
{
struct _field_t *cfield;
/* abbreviate pointer to current field */
cfield=cform->cfield;
strcpy(cfield->buf,cfield->str);
if(cfield->fconv=='9') deci_right(cform,cfield->buf+cfield->lenbuf);
disp_field(cform,NULL,high);
}
/*---------------------------------------------------------------------------*/
/* this function sets the termination key variable, if defined */
static void set_term_key(struct _form_t *cform,unsigned xch)
{
if(cform->termkey!=NULL) *(cform->termkey)=xch;
}
/*---------------------------------------------------------------------------*/
/* this function searches for the previous field up from current field */
static void up_field(struct _form_t *cform)
{
struct _field_t *best,*temp;
int brow,bcol,trow,tcol,tdist,bdist,crow,ccol;
/* initialize best record to NULL */
best = NULL;
brow = -1;
bcol = 32767;
/* find the closest field upwards */
crow=(int)cform->cwrow;
ccol=(int)cform->cwcol;
for(temp=cform->field;temp!=NULL;temp=temp->prev) {
trow=(int)temp->wrow;
tcol=closest_column(cform,temp);
if(trow<crow) {
tdist=abs(ccol-tcol);
bdist=abs(ccol-bcol);
if((trow>brow)||((trow==brow&&tdist<bdist))) {
best=temp;
brow=trow;
bcol=tcol;
}
}
}
/* if a field was found, then set current field to found */
/* field, and advance to the matching column position */
if(best!=NULL) {
cform->cfield=best;
adv_column(cform,bcol);
}
}
/*---------------------------------------------------------------------------*/
/* this function validates the field buffer using the format string */
static int validate_field(struct _form_t *cform)
{
char *pb,*pf,ch;
struct _field_t *cfield;
struct _form_t *temp;
int valid=YES,i,pos;
/* abbreviate pointer to current field */
cfield=cform->cfield;
/* if field is numeric, then format it so */
if(cfield->fconv=='9') deci_right(cform,cfield->buf+cfield->lenbuf);
pb=cfield->buf;
pf=cfield->format;
/* if string is blank, don't do the built-in validation. */
/* As far as we're concerned, a blank field is okay. */
if(!strblank(pb)) {
/* do while not end of format string and field still valid */
while(*pf && valid) {
/* test character in the field buffer with */
/* current format character for validity. */
switch(*pf) {
case ' ':
/* skip readability spaces */
pf++;
break;
case '\"':
case '\'':
/* skip quoted text in format string */
ch=*pf++;
while(ch!=*pf) pf++;
pf++;
break;
case '<':
/* check current character in field buffer for */
/* inclusion in the list of valid characters. */
pf++;
valid=NO;
while(*pf!='>') {
if(*pb==*pf) valid=YES;
pf++;
}
if(valid) {
pf++;
pb++;
}
break;
case '[':
/* check current character in field buffer for */
/* exclusion in the list of valid characters. */
pf++;
valid=YES;
while(*pf!=']') {
if(*pb==*pf) valid=NO;
pf++;
}
if(valid) {
pf++;
pb++;
}
break;
case '.':
/* skip decimal point */
pf++;
break;
default:
/* see if current field character is a valid */
/* type of the current format character */
valid=cvaltype(*pb,*pf);
if(*pb==' '&&cfield->fconv=='9') valid=YES;
if(valid) {
pf++;
pb++;
}
}
}
}
/* if the field passes validation so far and a user-defined */
/* function was specified, save current environment, call */
/* the user function, then restore the environment */
if(valid) {
if(cfield->validate!=NULL) {
temp=cform;
pos=( (*(cform->cfield->validate)) (cform->cfield->buf) );
cform=temp;
cfield=cform->cfield;
/* check to see if any of the fields' redisp flags have been set */
check_redisp(cform);
/* test return value from user-defined function. */
/* if not zero, then field is in error */
if(pos) {
beg_pos(cform);
goto validate_error;
}
}
}
else {
/* advance to error position in field */
beg_pos(cform);
pos=(int)( ((unsigned long) pb ) - ((unsigned long) cfield->buf ) + 1);
validate_error:
if(pos>cfield->lenbuf) pos=1;
for(i=1;i<pos;i++) next_pos(cform);
/* return error code to caller */
return(pos);
}
/* return with no error (field is valid) */
return(0);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -