📄 esdutil.c
字号:
/*+---------------------------------------------------------------- esdutil.c - ecu extended string descriptor manipulation wht@n4hgf.Mt-Park.GA.US Defined functions: end_of_cmd(tesd) esd_null_terminate(tesd) esd_strip_trail_break(ztext) esdalloc(maxcb) esdcat(dest,suffix,realloc_ok) esdfgets(tesd,fileptr) esdfputs(tesd,fileptr,index_flag,nl_flag) esdfree(tesd) esdinit(tesd,cptr,maxcb) esdrealloc(tesd,maxcb) esdshow(tesd,title) esdstrcat(tesd,zstr) esdstrindex(esd1,esd2,index1_flag,index2_flag) esdzero(tesd) get_alpha_zstr(tesd,strbuf,strbuf_maxcb) get_alphanum_zstr(tesd,strbuf,strbuf_maxcb) get_cmd_char(tesd,pchar) get_numeric_value(tesd,value) get_numeric_zstr(tesd,strbuf,strbuf_maxcb) get_switches(tesd,switches,switches_max) get_word_zstr(tesd,strbuf,strbuf_maxcb) keyword_lookup(ktable,tesd) skip_cmd_break(tesd) skip_cmd_char(tesd,skipchar) skip_colon(tesd) skip_comma(tesd) skip_paren(tesd,fLeft) strindex(str1,str2)This is old code; give me a break-----------------------------------------------------------------*//*+:EDITS:*//*:09-10-1992-13:59-wht@n4hgf-ECU release 3.20 *//*:08-22-1992-15:38-wht@n4hgf-ECU release 3.20 BETA *//*:03-20-1992-06:26-wht@n4hgf-esdstrcat will grow an esd *//*:08-25-1991-23:20-root@n4hgf2-get_switches could overflow result string *//*:07-25-1991-12:57-wht@n4hgf-ECU release 3.10 *//*:05-02-1991-04:12-wht@n4hgf-how did esdrealloc ever work? *//*:04-23-1991-04:33-wht@n4hgf-function name reorganization *//*:04-23-1991-04:33-wht@n4hgf-add esdcat *//*:01-31-1991-14:49-wht@n4hgf-rework esdrealloc for speed *//*:08-14-1990-20:40-wht@n4hgf-ecu3.00-flush old edit history */#include "ecu.h"#include "ecuerror.h"#include "esd.h"extern int errno;/*+------------------------------------------------------------------------- esd_null_terminate(&esd) puts null at 'cb' position of string (standard esd always has one more byte in buffer than maxcb says)--------------------------------------------------------------------------*/voidesd_null_terminate(tesd)register ESD *tesd;{ tesd->pb[tesd->cb] = 0;} /* end of esd_null_terminate *//*+----------------------------------------------------------------------- esdzero(tesd) zero an esd ------------------------------------------------------------------------*/voidesdzero(tesd)register ESD *tesd;{ tesd->cb = 0; /* current count == 0 */ tesd->index = 0; /* parse index to first position */ tesd->old_index = 0; /* parse index to first position */ *tesd->pb = 0; /* start with null terminated string */} /* end of esdzero *//*+----------------------------------------------------------------------- esdinit(tesd,cptr,maxcb) init an esd ------------------------------------------------------------------------*/voidesdinit(tesd,cptr,maxcb)register ESD *tesd;char *cptr;register maxcb;{ tesd->pb = cptr; /* pointer to string */ tesd->maxcb = maxcb; /* max characters in buffer */ esdzero(tesd);} /* end of esdinit *//*+----------------------------------------------------------------------- esdptr = esdalloc(maxcb) allocate an esd and buffer------------------------------------------------------------------------*/ESD *esdalloc(maxcb)register maxcb; /* desired maxcb */{register ESD *tesd;register actual_cb; /* we get an extra character to ensure room for null past maxcb */ actual_cb = maxcb + 1; if(actual_cb & 1) /* even allocation */ ++actual_cb; if(!(tesd = (ESD *)malloc(sizeof(ESD)))) return((ESD *)0); /* return NULL if failure */ if(!(tesd->pb = malloc(actual_cb))) { free((char *)tesd); return((ESD *)0); /* return NULL if failure */ } esdinit(tesd,tesd->pb,maxcb); return(tesd);} /* end of esdalloc *//*+----------------------------------------------------------------------- esdptr = esdrealloc(maxcb) - realloc an esd buffermay only be used to enlarge an esd bufferthis used to use realloc(), which did a lot of unnecessary copyingalso no more abnormal program termination on memory failure------------------------------------------------------------------------*/intesdrealloc(tesd,maxcb)ESD *tesd;register maxcb; /* desired maxcb */{register actual_cb;char *newpb; if(!tesd || (tesd->maxcb > maxcb)) return(eInternalLogicError); /* enforce our limit */ if(maxcb > ESD_MAXSIZE) return(eBufferTooSmall); /* we get an extra character to ensure room for null past maxcb */ actual_cb = maxcb + 1; if(actual_cb & 1) /* even allocation */ ++actual_cb; if(!(newpb = malloc(actual_cb))) return(eNoMemory); if(tesd->cb) memcpy(newpb,tesd->pb,tesd->cb); free(tesd->pb); tesd->pb = newpb; tesd->maxcb = actual_cb; esd_null_terminate(tesd); return(0);} /* end of esdrealloc *//*+----------------------------------------------------------------------- esdfree(esdptr)------------------------------------------------------------------------*/voidesdfree(tesd)register ESD *tesd;{ if(tesd && tesd->pb) { free(tesd->pb); free((char *)tesd); } else { errno = ENOMEM; ff(se,"\r\n\r\nFREE_ESD FAILED. FATAL ERROR. SORRY.\r\n"); termecu(TERMECU_XMTR_FATAL_ERROR); }}/*+------------------------------------------------------------------------- esdcat(dest,suffix,realloc_ok) - "strcat" for ESDs Append 'suffix' contents to 'dest' if realloc_ok true, expand 'dest' as necessary Returns: 0 - success eNoMemory eBufferTooSmall--------------------------------------------------------------------------*/intesdcat(dest,suffix,realloc_ok)ESD *dest;ESD *suffix;int realloc_ok;{ int erc = 0; int new_maxcb = dest->cb + suffix->cb; if(dest->maxcb < new_maxcb) { if(!realloc_ok) return(eBufferTooSmall); if(erc = esdrealloc(dest,new_maxcb)) return(erc); } memcpy(dest->pb + dest->cb,suffix->pb,suffix->cb + 1); /* catch null too */ dest->cb += suffix->cb; return(0);} /* end of esdcat *//*+------------------------------------------------------------------------- esdstrcat(tesd,zstr) - "strcat" for ESDssimilar to esdcat(), but with automatic esd growth--------------------------------------------------------------------------*/intesdstrcat(tesd,zstr)ESD *tesd;char *zstr;{ register zstrlen = strlen(zstr); register erc = 0; if(zstrlen > (tesd->maxcb - tesd->cb)) { if(erc = esdrealloc(tesd,tesd->cb + zstrlen)) return(erc); } if(zstrlen) { strncpy(tesd->pb + tesd->cb,zstr,zstrlen); tesd->cb += zstrlen; esd_null_terminate(tesd); } return(erc);} /* end of esdstrcat *//*+------------------------------------------------------------------------- esdshow(tesd,title)--------------------------------------------------------------------------*/voidesdshow(tesd,title)ESD *tesd;char *title;{register itmp; if(title && *title) { pputs(title); pputs("\n"); } esd_null_terminate(tesd); pputs(tesd->pb); pputs("\n"); for(itmp = 0; itmp <= tesd->cb; itmp++) { if(itmp == tesd->old_index) pputc('^'); else if(itmp == tesd->index) pputc('^'); else pputc(' '); if((itmp > tesd->old_index) && (itmp > tesd->index)) break; }#if 0 pprintf(" o%d i%d c%d\n",tesd->old_index,tesd->index,tesd->cb);#else pputs("\n");#endif} /* end of esdshow *//*+---------------------------------------------------------------- strindex: string index function Returns position of 'str2' in 'str1' if found If 'str2' is null, then 0 is returned (null matches anything) Returns -1 if not found-----------------------------------------------------------------*/intstrindex(str1,str2)char *str1; /* the (target) string to search */char *str2; /* the (comparand) string to search for */{register istr1 = 0;register lstr2 = strlen(str2);register char *mstr = str1; /* the (target) string to search */ if(*str2 == 0) /* null string matches anything */ return(0); while(*mstr) { if(*mstr == *str2) { /* we have a first char match... does rest of string match? */ if(!strncmp(mstr,str2,lstr2)) return(istr1); /* if so, return match position */ } mstr++; istr1++; } return(-1); /* if we exhaust target string, flunk */} /* end of strindex *//*+------------------------------------------------------------------------- esdstrindex(esd1,esd2,index1_flag,index2_flag) Call strindex with esd1->pb and esd2->pb. If index1_flag != 0, esd1->pb + esd1->index passed If index2_flag != 0, esd2->pb + esd2->index passed--------------------------------------------------------------------------*/esdstrindex(esd1,esd2,index1_flag,index2_flag)register ESD *esd1;register ESD *esd2;register index1_flag;register index2_flag;{ return(strindex((index1_flag) ? esd1->pb : esd1->pb + esd1->index, (index2_flag) ? esd2->pb : esd2->pb + esd2->index));} /* end of esdstrindex *//*+---------------------------------------------------------------- keyword_lookup(ktable,tesd) Lookup string in keyword_table struct array Returns table->key_token if 'tesd' found in 'table', else -1 Beware substrings. "type","typedef" will both match "type"-----------------------------------------------------------------*/keyword_lookup(ktable,tesd)register KEYTAB *ktable;register char *tesd;{/* register plen = strlen(tesd); */ while(ktable->key_word) {/* if(!strncmp(ktable->key_word,tesd,plen)) */ if(!strcmp(ktable->key_word,tesd)) return(ktable->key_token); ++ktable; } /* end of while */ return(-1); /* search failed */} /* end of keyword_lookup *//*+---------------------------------------------------------------- skip_cmd_break(tesd) Finds next non-break 'tesd' is an esd with valid 'index' field Returns 0 index field points to non-break character eNoParameter end of command found-----------------------------------------------------------------*/intskip_cmd_break(tesd)register ESD *tesd;{register cb = tesd->cb;register index = tesd->index;register char *pb = tesd->pb; while(index < cb) { if(!isspace(*(pb + index))) break; index++; } tesd->old_index = tesd->index = index; if(index >= cb) return(eNoParameter); return(0);} /* end of skip_cmd_break *//*+------------------------------------------------------------------------- end_of_cmd(tesd) - return 1 if at end of command--------------------------------------------------------------------------*/intend_of_cmd(tesd)register ESD *tesd;{
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -