/* ******************************************************************* * * * Copyright (c) WHOI * * * * * * File : def_noheader.c * * * * Purpose : i/o routines for handling def style data * * but where field names are not needed after * * the first level (level 0) * * * * Version Number : 1.0 * * * * Revision History : * * * * Date Developer * * ---- --------- * * * * January 31, 1996 Bob Groman, from vbs method * * Wed. Janaury 25, 1995 Bob Groman (from original source * * nmfs2.c by Glenn Flierl) * * * * * ******************************************************************* */ #include #include double atof(); #define DIRSEP '/' /* #define DEBUG */ #define TOKEN 40 #define NVAR 150 #define SEPARATOR " \t,\n" char names[NVAR][TOKEN]; char values[NVAR][TOKEN]; int nvarlevel[6]; int fileopen[5] = {0,0,0,0,0}; FILE *fl[5]; int maxlev; char dirstring[80]; char comments[2000]=""; err(s,t) char *s,*t; { printf("%&x error - %s %s\n",s,t); exit(1); } int scanheader(lev) int lev; { int ncnt,i,j,k; char tmp[1258],*tok,attrsav[TOKEN],*attr; ncnt= 0; i=0;j=1; if(lev==0){ while(j){ if (fgets(tmp,1257,fl[lev]) == NULL)err("file structure",""); if (tmp[0] == '#'){ while (tmp[0] == '#') { strcat(comments,tmp+1); if (fgets(tmp,1257,fl[lev]) == NULL)err("file structure",""); }; }; tok=strtok(tmp,SEPARATOR); while (tok != NULL){ if(tok[0] == '"'){tok++,tok[strlen(tok)-1]=0;}; if(attr=strchr(tok,'[')){ strcpy(attrsav,attr+1); attrsav[strlen(attrsav)-1]='\0'; *attr = '\0'; } else attrsav[0]='\0'; strcpy(names[ncnt],tok); strcpy(values[ncnt],attrsav); ncnt++; tok=strtok(NULL,SEPARATOR); }; if(names[ncnt-1][0]=='>') ncnt--; else j=0; nvarlevel[i]=ncnt; if(j) i++; }; maxlev=i; return ncnt; } else if (lev=0;i--)if (*vn >= nvarlevel[i]) return i+1; return 0; } int ioattrout_(vn,str) int *vn; { char attr[TOKEN],*at; int ic; if (values[*vn][0]){ strcpy(attr,values[*vn]); at=strchr(attr,';'); if(at){ strcpy(values[*vn],at+1); *at = 0; } else values[*vn][0]=0; strcpy(str,attr); return 1; } else return 0; } iovalreal_(vn,f) int *vn; float *f; { int i; i= *vn; if(strspn(values[i],"0123456789.+-")) *f=atof(values[i]); else *f=-9999.0; #ifdef DEBUG printf("iovalreal %d %f\n",*vn,f); #endif } iovalstr_(vn,tmp) int *vn; char *tmp; { static char *s; int i; i= *vn; s=values[i]; s=s+strspn(s," "); strcpy(tmp,s); #ifdef DEBUG printf("iovalstr %d %s\n",*vn,tmp); #endif } static char subfilename[80]; int ioreadrec_(level) int *level; { char tmp[1258],*tok; int i,j,k; #ifdef DEBUG printf("ioreadrec %d\n",*level); #endif k= *level; if (! fileopen[k]) { #ifdef DEBUG printf("open %s\n",subfilename); #endif if((fl[k]=fopen(subfilename,"r")) == NULL)err("missing file ",subfilename); scanheader(k); fileopen[k]=1; }; j=1; while(j){ if(fgets(tmp,1257,fl[k]) == NULL){ #ifdef DEBUG printf("eof at level %d\n",k); #endif fclose(fl[k]); fileopen[k]=0; return 0; }; i=strspn(tmp,SEPARATOR); if(i