/* ******************************************************************* * * * Copyright (c) MIT/JGOFS * * * * * * File : nm.c * * * * Purpose : * * * * Version Number : 1.5 * * * * Revision History : * * * * Date Developer * * ---- --------- * * * * Sat Oct 17 1992 10.00 Glenn Flierl * * Oct 1993 attr[][] * * July 1995 Christine Hammond * * change to add field width determination based on width of * * the label in the data. Preparer must determine max width * * of column from inspection of data column width. * * Ex: event___ , sta__ ,bot * * will give lengths of 8, 5, and 3 for the respective cols * * * * January 1996 Christine Hammond * * 'type' the functions that outer method calls to avoid * * warnings on ANSI compilers - void() used for functions * * returning no value (e.g., return;) * * * * March 1996 Christine Hammond * * fix proclabel (width sensing) to ignore attributes with a * * substring 'width=', ex: 'inpwidth=N', and to properly deal * * with a 'width=N' attr when it occurs after a ';', as in * * a series of attributes, ex: 'meters;color=pink;width=12' * * * * March 1996 Warren Sass, Christine Hammond * * use a .h include file to set sizes of buffers used * * NOTE: 'minbufsize.h' also included in outer.c, to achieve * * some correspondence. User-specified include file allowed * * (see minbufsize.h) as a compile switch IOBUF_INCLUDE * * * ******************************************************************* */ #include #include #include "minbufsize.h" double atof(); /* #define DEBUG */ #define TOKEN 40 #define SEPARATOR " ,=\t\n" #define W_EXTEND '_' #ifndef NVAR #define NVAR 50 #endif #ifndef COMMENTSIZE #define COMMENTSIZE 30*MINCOMMENTLINE #endif #ifndef VARNAMESIZE #define VARNAMESIZE MINVARNAMESIZE #endif #ifndef ATTRSIZE #define ATTRSIZE MINATTRSIZE #endif #ifndef DATUMSIZE #define DATUMSIZE MINDATUMSIZE #endif #ifndef INBUFSIZE #define INBUFSIZE 1258 #endif char names[NVAR][VARNAMESIZE]; char values[NVAR][DATUMSIZE]; char attr[NVAR][ATTRSIZE]; int fldwidths[NVAR]; int map[NVAR]; int nheader, ndata; FILE *fl; int maxlev,newlev; int eofflag=0; char str[INBUFSIZE],*strp; char tabcomments[COMMENTSIZE]; void ioclose_(); /* * * * * * end of declarations * * * * * */ proclabel(n, ptr) int n; char *ptr; /* attr[] is the global array. routine to determine the length of the title string indexed at 'n' and pointed to by 'ptr', and based on the idea that the user preparing the data has placed W_EXTEND characters to pad the title to the desired width. An attribute is constructed and saved in attr[], either by appending to existing attr's or saving as the sole attribute, beginning with 'width='. Then, the W_EXTEND characters must be stripped from the names. Same name separators are used as before (space, tab, comma, newline). If an explicit 'width=' attribute is found, it is used. */ { #define YES 0 #define NO 1 int len, docalc, others; char attrib[ATTRSIZE], *attrptr; /* default value for flag indicating attributes other than 'width=' */ others = NO; /* calculate a field width, if no attributes are present or, if none of the attributes are exactly == 'width=N' */ docalc = YES; attrptr = attr[n]; while (*attrptr) { others = YES; if (strncmp(attrptr,"width=",6) == 0) docalc = NO; (attrptr = strchr(attrptr,';')) == NULL ? attrptr : attrptr++; } if (docalc == YES) { len = strlen(ptr); /* length of (possibly) extended label */ sprintf(attrib, "width=%d", len); if (others == YES) { /* add to end of other attributes? */ strcat(attr[n],";"); strcat(attr[n],attrib); } else strcpy(attr[n],attrib); }; /* end docalc loop */ /* strip off the column extenders from the label */ striptoken(ptr); /* and save label name without the extra W_EXTEND characters */ strcpy(names[n], ptr); return; } striptoken(p) char *p; { char *tp; int i; /* if there are extending characters at end of string, strip them */ i = strlen(p); while ( (tp = strrchr(p,W_EXTEND)) == p+i-1) { *tp = '\0'; /* shorten string by that one extend char */ i = strlen(p); }; return; } err(s,t) char *s,*t; { printf("&x error - %s%t\r\n",s,t); ioclose_(); exit(1); } int fg(str) char *str; { *str = 0; while(*str == 0){ if(fgets(str,INBUFSIZE-1,fl) == NULL) return 1; if(strspn(str,SEPARATOR)==strlen(str))*str = 0; }; return 0; } int readdata(flag) int flag; { int state,nn,i; char *pstr; state=1; nn=0; if(*str == 0) if(fg(str))return -1; if (str[0] == '#'){ *tabcomments=0; while (str[0] == '#') { strcat(tabcomments,str+1); if (fg(str)) return -1; }; }; if(flag<=1) while(strchr(str,'=')==NULL) if(fg(str))return -1; while(strchr(str,'=')){ state=2; strp=strtok(str,SEPARATOR); while(strp){ if(flag==0){ if(pstr=strchr(strp,'[')){ *(pstr+strlen(pstr)-1)=0; strcpy(attr[nheader],pstr+1); *pstr=0; } else attr[nheader][0]=NULL; /* routine to determine field width and assign field name */ proclabel(nheader, strp); nn=nheader++; } else { if(pstr=strchr(strp,'['))*pstr=0; for(nn=0;nn0){newlev=3;return 1;}; newlev=2;return 0; } void ioname_(vn,s) int *vn; char *s; { strcpy(s,names[*vn]); } int iovarlevel_(vn) int *vn; { if(*vn